mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +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:
@@ -4,33 +4,34 @@
|
||||
|
||||
#include "frc/system/plant/proto/DCMotorProto.h"
|
||||
|
||||
#include <wpi/ProtoHelper.h>
|
||||
#include <optional>
|
||||
|
||||
#include "plant.pb.h"
|
||||
#include "wpimath/protobuf/plant.npb.h"
|
||||
|
||||
google::protobuf::Message* wpi::Protobuf<frc::DCMotor>::New(
|
||||
google::protobuf::Arena* arena) {
|
||||
return wpi::CreateMessage<wpi::proto::ProtobufDCMotor>(arena);
|
||||
}
|
||||
std::optional<frc::DCMotor> wpi::Protobuf<frc::DCMotor>::Unpack(
|
||||
InputStream& stream) {
|
||||
wpi_proto_ProtobufDCMotor msg;
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
|
||||
frc::DCMotor wpi::Protobuf<frc::DCMotor>::Unpack(
|
||||
const google::protobuf::Message& msg) {
|
||||
auto m = static_cast<const wpi::proto::ProtobufDCMotor*>(&msg);
|
||||
return frc::DCMotor{
|
||||
units::volt_t{m->nominal_voltage()},
|
||||
units::newton_meter_t{m->stall_torque()},
|
||||
units::ampere_t{m->stall_current()},
|
||||
units::ampere_t{m->free_current()},
|
||||
units::radians_per_second_t{m->free_speed()},
|
||||
units::volt_t{msg.nominal_voltage},
|
||||
units::newton_meter_t{msg.stall_torque},
|
||||
units::ampere_t{msg.stall_current},
|
||||
units::ampere_t{msg.free_current},
|
||||
units::radians_per_second_t{msg.free_speed},
|
||||
};
|
||||
}
|
||||
|
||||
void wpi::Protobuf<frc::DCMotor>::Pack(google::protobuf::Message* msg,
|
||||
bool wpi::Protobuf<frc::DCMotor>::Pack(OutputStream& stream,
|
||||
const frc::DCMotor& value) {
|
||||
auto m = static_cast<wpi::proto::ProtobufDCMotor*>(msg);
|
||||
m->set_nominal_voltage(value.nominalVoltage.value());
|
||||
m->set_stall_torque(value.stallTorque.value());
|
||||
m->set_stall_current(value.stallCurrent.value());
|
||||
m->set_free_current(value.freeCurrent.value());
|
||||
m->set_free_speed(value.freeSpeed.value());
|
||||
wpi_proto_ProtobufDCMotor msg{
|
||||
.nominal_voltage = value.nominalVoltage.value(),
|
||||
.stall_torque = value.stallTorque.value(),
|
||||
.stall_current = value.stallCurrent.value(),
|
||||
.free_current = value.freeCurrent.value(),
|
||||
.free_speed = value.freeSpeed.value(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user