mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-03 03:01:44 +00:00
[wpiutil] Change C++ protobuf to nanopb (#7309)
The Google C++ protobuf implementation has issues with dynamic linkage across DLL boundaries because it uses global variables. It also has a compile-time dependency because the protoc version must exactly match the libprotobuf version. Using nanopb with a customized generator fixes both of these issues. Co-authored-by: Gold856 <117957790+Gold856@users.noreply.github.com>
This commit is contained in:
@@ -8,11 +8,14 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/controller/ArmFeedforward.h"
|
||||
#include "pb.h"
|
||||
#include "wpimath/protobuf/controller.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::ArmFeedforward> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::ArmFeedforward Unpack(const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
const frc::ArmFeedforward& value);
|
||||
using MessageStruct = wpi_proto_ProtobufArmFeedforward;
|
||||
using InputStream = wpi::ProtoInputStream<frc::ArmFeedforward>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::ArmFeedforward>;
|
||||
static std::optional<frc::ArmFeedforward> Unpack(InputStream& stream);
|
||||
static bool Pack(OutputStream& stream, const frc::ArmFeedforward& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,17 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/controller/DifferentialDriveFeedforward.h"
|
||||
#include "pb.h"
|
||||
#include "wpimath/protobuf/controller.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::DifferentialDriveFeedforward> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::DifferentialDriveFeedforward Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufDifferentialDriveFeedforward;
|
||||
using InputStream = wpi::ProtoInputStream<frc::DifferentialDriveFeedforward>;
|
||||
using OutputStream =
|
||||
wpi::ProtoOutputStream<frc::DifferentialDriveFeedforward>;
|
||||
static std::optional<frc::DifferentialDriveFeedforward> Unpack(
|
||||
InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::DifferentialDriveFeedforward& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,18 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/controller/DifferentialDriveWheelVoltages.h"
|
||||
#include "pb.h"
|
||||
#include "wpimath/protobuf/controller.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::DifferentialDriveWheelVoltages> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::DifferentialDriveWheelVoltages Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufDifferentialDriveWheelVoltages;
|
||||
using InputStream =
|
||||
wpi::ProtoInputStream<frc::DifferentialDriveWheelVoltages>;
|
||||
using OutputStream =
|
||||
wpi::ProtoOutputStream<frc::DifferentialDriveWheelVoltages>;
|
||||
static std::optional<frc::DifferentialDriveWheelVoltages> Unpack(
|
||||
InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::DifferentialDriveWheelVoltages& value);
|
||||
};
|
||||
|
||||
@@ -8,11 +8,14 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/controller/ElevatorFeedforward.h"
|
||||
#include "pb.h"
|
||||
#include "wpimath/protobuf/controller.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::ElevatorFeedforward> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::ElevatorFeedforward Unpack(const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
const frc::ElevatorFeedforward& value);
|
||||
using MessageStruct = wpi_proto_ProtobufElevatorFeedforward;
|
||||
using InputStream = wpi::ProtoInputStream<frc::ElevatorFeedforward>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::ElevatorFeedforward>;
|
||||
static std::optional<frc::ElevatorFeedforward> Unpack(InputStream& stream);
|
||||
static bool Pack(OutputStream& stream, const frc::ElevatorFeedforward& value);
|
||||
};
|
||||
|
||||
@@ -4,47 +4,55 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <wpi/ProtoHelper.h>
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "controller.pb.h"
|
||||
#include "frc/controller/SimpleMotorFeedforward.h"
|
||||
#include "pb.h"
|
||||
#include "units/length.h"
|
||||
#include "wpimath/protobuf/controller.npb.h"
|
||||
|
||||
// Everything is converted into units for
|
||||
// frc::SimpleMotorFeedforward<units::meters>
|
||||
|
||||
template <class Distance>
|
||||
struct wpi::Protobuf<frc::SimpleMotorFeedforward<Distance>> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena) {
|
||||
return wpi::CreateMessage<wpi::proto::ProtobufSimpleMotorFeedforward>(
|
||||
arena);
|
||||
}
|
||||
struct wpi::Protobuf<frc::SimpleMotorFeedforward<Distance>> { // NOLINT
|
||||
using MessageStruct = wpi_proto_ProtobufSimpleMotorFeedforward;
|
||||
using InputStream =
|
||||
wpi::ProtoInputStream<frc::SimpleMotorFeedforward<Distance>>;
|
||||
using OutputStream =
|
||||
wpi::ProtoOutputStream<frc::SimpleMotorFeedforward<Distance>>;
|
||||
|
||||
static frc::SimpleMotorFeedforward<Distance> Unpack(
|
||||
const google::protobuf::Message& msg) {
|
||||
auto m =
|
||||
static_cast<const wpi::proto::ProtobufSimpleMotorFeedforward*>(&msg);
|
||||
return {units::volt_t{m->ks()},
|
||||
units::unit_t<frc::SimpleMotorFeedforward<units::meters>::kv_unit>{
|
||||
m->kv()},
|
||||
units::unit_t<frc::SimpleMotorFeedforward<units::meters>::ka_unit>{
|
||||
m->ka()},
|
||||
units::second_t{m->dt()}};
|
||||
}
|
||||
static std::optional<frc::SimpleMotorFeedforward<Distance>> Unpack(
|
||||
InputStream& stream) {
|
||||
wpi_proto_ProtobufSimpleMotorFeedforward msg;
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
const frc::SimpleMotorFeedforward<Distance>& value) {
|
||||
auto m = static_cast<wpi::proto::ProtobufSimpleMotorFeedforward*>(msg);
|
||||
m->set_ks(value.GetKs().value());
|
||||
m->set_kv(
|
||||
return frc::SimpleMotorFeedforward<Distance>{
|
||||
units::volt_t{msg.ks},
|
||||
units::unit_t<frc::SimpleMotorFeedforward<units::meters>::kv_unit>{
|
||||
value.GetKv()}
|
||||
.value());
|
||||
m->set_ka(
|
||||
msg.kv},
|
||||
units::unit_t<frc::SimpleMotorFeedforward<units::meters>::ka_unit>{
|
||||
value.GetKa()}
|
||||
.value());
|
||||
m->set_dt(units::second_t{value.GetDt()}.value());
|
||||
msg.ka},
|
||||
units::second_t{msg.dt},
|
||||
};
|
||||
}
|
||||
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::SimpleMotorFeedforward<Distance>& value) {
|
||||
wpi_proto_ProtobufSimpleMotorFeedforward msg{
|
||||
.ks = value.GetKs().value(),
|
||||
.kv =
|
||||
units::unit_t<frc::SimpleMotorFeedforward<units::meters>::kv_unit>{
|
||||
value.GetKv()}
|
||||
.value(),
|
||||
.ka =
|
||||
units::unit_t<frc::SimpleMotorFeedforward<units::meters>::ka_unit>{
|
||||
value.GetKa()}
|
||||
.value(),
|
||||
.dt = units::second_t{value.GetDt()}.value(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user