[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:
Thad House
2024-11-07 22:42:50 -08:00
committed by GitHub
parent fd2e0c0427
commit 8b8b634f65
166 changed files with 17522 additions and 1571 deletions

View File

@@ -4,29 +4,28 @@
#include "frc/kinematics/proto/ChassisSpeedsProto.h"
#include <wpi/ProtoHelper.h>
#include "wpimath/protobuf/kinematics.npb.h"
#include "kinematics.pb.h"
std::optional<frc::ChassisSpeeds> wpi::Protobuf<frc::ChassisSpeeds>::Unpack(
InputStream& stream) {
wpi_proto_ProtobufChassisSpeeds msg;
if (!stream.Decode(msg)) {
return {};
}
google::protobuf::Message* wpi::Protobuf<frc::ChassisSpeeds>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufChassisSpeeds>(arena);
}
frc::ChassisSpeeds wpi::Protobuf<frc::ChassisSpeeds>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufChassisSpeeds*>(&msg);
return frc::ChassisSpeeds{
units::meters_per_second_t{m->vx()},
units::meters_per_second_t{m->vy()},
units::radians_per_second_t{m->omega()},
units::meters_per_second_t{msg.vx},
units::meters_per_second_t{msg.vy},
units::radians_per_second_t{msg.omega},
};
}
void wpi::Protobuf<frc::ChassisSpeeds>::Pack(google::protobuf::Message* msg,
bool wpi::Protobuf<frc::ChassisSpeeds>::Pack(OutputStream& stream,
const frc::ChassisSpeeds& value) {
auto m = static_cast<wpi::proto::ProtobufChassisSpeeds*>(msg);
m->set_vx(value.vx.value());
m->set_vy(value.vy.value());
m->set_omega(value.omega.value());
wpi_proto_ProtobufChassisSpeeds msg{
.vx = value.vx.value(),
.vy = value.vy.value(),
.omega = value.omega.value(),
};
return stream.Encode(msg);
}

View File

@@ -4,29 +4,24 @@
#include "frc/kinematics/proto/DifferentialDriveKinematicsProto.h"
#include <wpi/ProtoHelper.h>
#include "wpimath/protobuf/kinematics.npb.h"
#include "kinematics.pb.h"
std::optional<frc::DifferentialDriveKinematics>
wpi::Protobuf<frc::DifferentialDriveKinematics>::Unpack(InputStream& stream) {
wpi_proto_ProtobufDifferentialDriveKinematics msg;
if (!stream.Decode(msg)) {
return {};
}
google::protobuf::Message* wpi::Protobuf<frc::DifferentialDriveKinematics>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufDifferentialDriveKinematics>(
arena);
}
frc::DifferentialDriveKinematics
wpi::Protobuf<frc::DifferentialDriveKinematics>::Unpack(
const google::protobuf::Message& msg) {
auto m =
static_cast<const wpi::proto::ProtobufDifferentialDriveKinematics*>(&msg);
return frc::DifferentialDriveKinematics{
units::meter_t{m->track_width()},
units::meter_t{msg.track_width},
};
}
void wpi::Protobuf<frc::DifferentialDriveKinematics>::Pack(
google::protobuf::Message* msg,
const frc::DifferentialDriveKinematics& value) {
auto m = static_cast<wpi::proto::ProtobufDifferentialDriveKinematics*>(msg);
m->set_track_width(value.trackWidth.value());
bool wpi::Protobuf<frc::DifferentialDriveKinematics>::Pack(
OutputStream& stream, const frc::DifferentialDriveKinematics& value) {
wpi_proto_ProtobufDifferentialDriveKinematics msg{
.track_width = value.trackWidth.value(),
};
return stream.Encode(msg);
}

View File

@@ -4,33 +4,26 @@
#include "frc/kinematics/proto/DifferentialDriveWheelPositionsProto.h"
#include <wpi/ProtoHelper.h>
#include "wpimath/protobuf/kinematics.npb.h"
#include "kinematics.pb.h"
std::optional<frc::DifferentialDriveWheelPositions> wpi::Protobuf<
frc::DifferentialDriveWheelPositions>::Unpack(InputStream& stream) {
wpi_proto_ProtobufDifferentialDriveWheelPositions msg;
if (!stream.Decode(msg)) {
return {};
}
google::protobuf::Message* wpi::Protobuf<
frc::DifferentialDriveWheelPositions>::New(google::protobuf::Arena* arena) {
return wpi::CreateMessage<
wpi::proto::ProtobufDifferentialDriveWheelPositions>(arena);
}
frc::DifferentialDriveWheelPositions
wpi::Protobuf<frc::DifferentialDriveWheelPositions>::Unpack(
const google::protobuf::Message& msg) {
auto m =
static_cast<const wpi::proto::ProtobufDifferentialDriveWheelPositions*>(
&msg);
return frc::DifferentialDriveWheelPositions{
units::meter_t{m->left()},
units::meter_t{m->right()},
units::meter_t{msg.left},
units::meter_t{msg.right},
};
}
void wpi::Protobuf<frc::DifferentialDriveWheelPositions>::Pack(
google::protobuf::Message* msg,
const frc::DifferentialDriveWheelPositions& value) {
auto m =
static_cast<wpi::proto::ProtobufDifferentialDriveWheelPositions*>(msg);
m->set_left(value.left.value());
m->set_right(value.right.value());
bool wpi::Protobuf<frc::DifferentialDriveWheelPositions>::Pack(
OutputStream& stream, const frc::DifferentialDriveWheelPositions& value) {
wpi_proto_ProtobufDifferentialDriveWheelPositions msg{
.left = value.left.value(),
.right = value.right.value(),
};
return stream.Encode(msg);
}

View File

@@ -4,31 +4,26 @@
#include "frc/kinematics/proto/DifferentialDriveWheelSpeedsProto.h"
#include <wpi/ProtoHelper.h>
#include "wpimath/protobuf/kinematics.npb.h"
#include "kinematics.pb.h"
std::optional<frc::DifferentialDriveWheelSpeeds>
wpi::Protobuf<frc::DifferentialDriveWheelSpeeds>::Unpack(InputStream& stream) {
wpi_proto_ProtobufDifferentialDriveWheelSpeeds msg;
if (!stream.Decode(msg)) {
return {};
}
google::protobuf::Message* wpi::Protobuf<
frc::DifferentialDriveWheelSpeeds>::New(google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufDifferentialDriveWheelSpeeds>(
arena);
}
frc::DifferentialDriveWheelSpeeds
wpi::Protobuf<frc::DifferentialDriveWheelSpeeds>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufDifferentialDriveWheelSpeeds*>(
&msg);
return frc::DifferentialDriveWheelSpeeds{
units::meters_per_second_t{m->left()},
units::meters_per_second_t{m->right()},
units::meters_per_second_t{msg.left},
units::meters_per_second_t{msg.right},
};
}
void wpi::Protobuf<frc::DifferentialDriveWheelSpeeds>::Pack(
google::protobuf::Message* msg,
const frc::DifferentialDriveWheelSpeeds& value) {
auto m = static_cast<wpi::proto::ProtobufDifferentialDriveWheelSpeeds*>(msg);
m->set_left(value.left.value());
m->set_right(value.right.value());
bool wpi::Protobuf<frc::DifferentialDriveWheelSpeeds>::Pack(
OutputStream& stream, const frc::DifferentialDriveWheelSpeeds& value) {
wpi_proto_ProtobufDifferentialDriveWheelSpeeds msg{
.left = value.left.value(),
.right = value.right.value(),
};
return stream.Encode(msg);
}

View File

@@ -4,31 +4,55 @@
#include "frc/kinematics/proto/MecanumDriveKinematicsProto.h"
#include <wpi/ProtoHelper.h>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "kinematics.pb.h"
#include "wpimath/protobuf/kinematics.npb.h"
google::protobuf::Message* wpi::Protobuf<frc::MecanumDriveKinematics>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufMecanumDriveKinematics>(arena);
}
std::optional<frc::MecanumDriveKinematics>
wpi::Protobuf<frc::MecanumDriveKinematics>::Unpack(InputStream& stream) {
wpi::UnpackCallback<frc::Translation2d> frontLeft;
wpi::UnpackCallback<frc::Translation2d> frontRight;
wpi::UnpackCallback<frc::Translation2d> rearLeft;
wpi::UnpackCallback<frc::Translation2d> rearRight;
wpi_proto_ProtobufMecanumDriveKinematics msg{
.front_left = frontLeft.Callback(),
.front_right = frontRight.Callback(),
.rear_left = rearLeft.Callback(),
.rear_right = rearRight.Callback(),
};
if (!stream.Decode(msg)) {
return {};
}
auto ifrontLeft = frontLeft.Items();
auto ifrontRight = frontRight.Items();
auto irearLeft = rearLeft.Items();
auto irearRight = rearRight.Items();
if (ifrontLeft.empty() || ifrontRight.empty() || irearLeft.empty() ||
irearRight.empty()) {
return {};
}
frc::MecanumDriveKinematics wpi::Protobuf<frc::MecanumDriveKinematics>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufMecanumDriveKinematics*>(&msg);
return frc::MecanumDriveKinematics{
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_front_left()),
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_front_right()),
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_rear_left()),
wpi::UnpackProtobuf<frc::Translation2d>(m->wpi_rear_right()),
ifrontLeft[0],
ifrontRight[0],
irearLeft[0],
irearRight[0],
};
}
void wpi::Protobuf<frc::MecanumDriveKinematics>::Pack(
google::protobuf::Message* msg, const frc::MecanumDriveKinematics& value) {
auto m = static_cast<wpi::proto::ProtobufMecanumDriveKinematics*>(msg);
wpi::PackProtobuf(m->mutable_front_left(), value.GetFrontLeft());
wpi::PackProtobuf(m->mutable_front_right(), value.GetFrontRight());
wpi::PackProtobuf(m->mutable_rear_left(), value.GetRearLeft());
wpi::PackProtobuf(m->mutable_rear_right(), value.GetRearRight());
bool wpi::Protobuf<frc::MecanumDriveKinematics>::Pack(
OutputStream& stream, const frc::MecanumDriveKinematics& value) {
wpi::PackCallback frontLeft{&value.GetFrontLeft()};
wpi::PackCallback frontRight{&value.GetFrontRight()};
wpi::PackCallback rearLeft{&value.GetRearLeft()};
wpi::PackCallback rearRight{&value.GetRearRight()};
wpi_proto_ProtobufMecanumDriveKinematics msg{
.front_left = frontLeft.Callback(),
.front_right = frontRight.Callback(),
.rear_left = rearLeft.Callback(),
.rear_right = rearRight.Callback(),
};
return stream.Encode(msg);
}

View File

@@ -4,35 +4,30 @@
#include "frc/kinematics/proto/MecanumDriveWheelPositionsProto.h"
#include <wpi/ProtoHelper.h>
#include "wpimath/protobuf/kinematics.npb.h"
#include "kinematics.pb.h"
std::optional<frc::MecanumDriveWheelPositions>
wpi::Protobuf<frc::MecanumDriveWheelPositions>::Unpack(InputStream& stream) {
wpi_proto_ProtobufMecanumDriveWheelPositions msg;
if (!stream.Decode(msg)) {
return {};
}
google::protobuf::Message* wpi::Protobuf<frc::MecanumDriveWheelPositions>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufMecanumDriveWheelPositions>(
arena);
}
frc::MecanumDriveWheelPositions
wpi::Protobuf<frc::MecanumDriveWheelPositions>::Unpack(
const google::protobuf::Message& msg) {
auto m =
static_cast<const wpi::proto::ProtobufMecanumDriveWheelPositions*>(&msg);
return frc::MecanumDriveWheelPositions{
units::meter_t{m->front_left()},
units::meter_t{m->front_right()},
units::meter_t{m->rear_left()},
units::meter_t{m->rear_right()},
units::meter_t{msg.front_left},
units::meter_t{msg.front_right},
units::meter_t{msg.rear_left},
units::meter_t{msg.rear_right},
};
}
void wpi::Protobuf<frc::MecanumDriveWheelPositions>::Pack(
google::protobuf::Message* msg,
const frc::MecanumDriveWheelPositions& value) {
auto m = static_cast<wpi::proto::ProtobufMecanumDriveWheelPositions*>(msg);
m->set_front_left(value.frontLeft.value());
m->set_front_right(value.frontRight.value());
m->set_rear_left(value.rearLeft.value());
m->set_rear_right(value.rearRight.value());
bool wpi::Protobuf<frc::MecanumDriveWheelPositions>::Pack(
OutputStream& stream, const frc::MecanumDriveWheelPositions& value) {
wpi_proto_ProtobufMecanumDriveWheelPositions msg{
.front_left = value.frontLeft.value(),
.front_right = value.frontRight.value(),
.rear_left = value.rearLeft.value(),
.rear_right = value.rearRight.value(),
};
return stream.Encode(msg);
}

View File

@@ -4,33 +4,30 @@
#include "frc/kinematics/proto/MecanumDriveWheelSpeedsProto.h"
#include <wpi/ProtoHelper.h>
#include "wpimath/protobuf/kinematics.npb.h"
#include "kinematics.pb.h"
std::optional<frc::MecanumDriveWheelSpeeds>
wpi::Protobuf<frc::MecanumDriveWheelSpeeds>::Unpack(InputStream& stream) {
wpi_proto_ProtobufMecanumDriveWheelSpeeds msg;
if (!stream.Decode(msg)) {
return {};
}
google::protobuf::Message* wpi::Protobuf<frc::MecanumDriveWheelSpeeds>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufMecanumDriveWheelSpeeds>(arena);
}
frc::MecanumDriveWheelSpeeds
wpi::Protobuf<frc::MecanumDriveWheelSpeeds>::Unpack(
const google::protobuf::Message& msg) {
auto m =
static_cast<const wpi::proto::ProtobufMecanumDriveWheelSpeeds*>(&msg);
return frc::MecanumDriveWheelSpeeds{
units::meters_per_second_t{m->front_left()},
units::meters_per_second_t{m->front_right()},
units::meters_per_second_t{m->rear_left()},
units::meters_per_second_t{m->rear_right()},
units::meters_per_second_t{msg.front_left},
units::meters_per_second_t{msg.front_right},
units::meters_per_second_t{msg.rear_left},
units::meters_per_second_t{msg.rear_right},
};
}
void wpi::Protobuf<frc::MecanumDriveWheelSpeeds>::Pack(
google::protobuf::Message* msg, const frc::MecanumDriveWheelSpeeds& value) {
auto m = static_cast<wpi::proto::ProtobufMecanumDriveWheelSpeeds*>(msg);
m->set_front_left(value.frontLeft.value());
m->set_front_right(value.frontRight.value());
m->set_rear_left(value.rearLeft.value());
m->set_rear_right(value.rearRight.value());
bool wpi::Protobuf<frc::MecanumDriveWheelSpeeds>::Pack(
OutputStream& stream, const frc::MecanumDriveWheelSpeeds& value) {
wpi_proto_ProtobufMecanumDriveWheelSpeeds msg{
.front_left = value.frontLeft.value(),
.front_right = value.frontRight.value(),
.rear_left = value.rearLeft.value(),
.rear_right = value.rearRight.value(),
};
return stream.Encode(msg);
}

View File

@@ -4,27 +4,39 @@
#include "frc/kinematics/proto/SwerveModulePositionProto.h"
#include <wpi/ProtoHelper.h>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "kinematics.pb.h"
#include "wpimath/protobuf/kinematics.npb.h"
google::protobuf::Message* wpi::Protobuf<frc::SwerveModulePosition>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufSwerveModulePosition>(arena);
}
std::optional<frc::SwerveModulePosition>
wpi::Protobuf<frc::SwerveModulePosition>::Unpack(InputStream& stream) {
wpi::UnpackCallback<frc::Rotation2d> angle;
wpi_proto_ProtobufSwerveModulePosition msg{
.distance = 0,
.angle = angle.Callback(),
};
if (!stream.Decode(msg)) {
return {};
}
auto iangle = angle.Items();
if (iangle.empty()) {
return {};
}
frc::SwerveModulePosition wpi::Protobuf<frc::SwerveModulePosition>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufSwerveModulePosition*>(&msg);
return frc::SwerveModulePosition{
units::meter_t{m->distance()},
wpi::UnpackProtobuf<frc::Rotation2d>(m->wpi_angle()),
units::meter_t{msg.distance},
iangle[0],
};
}
void wpi::Protobuf<frc::SwerveModulePosition>::Pack(
google::protobuf::Message* msg, const frc::SwerveModulePosition& value) {
auto m = static_cast<wpi::proto::ProtobufSwerveModulePosition*>(msg);
m->set_distance(value.distance.value());
wpi::PackProtobuf(m->mutable_angle(), value.angle);
bool wpi::Protobuf<frc::SwerveModulePosition>::Pack(
OutputStream& stream, const frc::SwerveModulePosition& value) {
wpi::PackCallback angle{&value.angle};
wpi_proto_ProtobufSwerveModulePosition msg{
.distance = value.distance.value(),
.angle = angle.Callback(),
};
return stream.Encode(msg);
}

View File

@@ -4,27 +4,39 @@
#include "frc/kinematics/proto/SwerveModuleStateProto.h"
#include <wpi/ProtoHelper.h>
#include <wpi/protobuf/ProtobufCallbacks.h>
#include "kinematics.pb.h"
#include "wpimath/protobuf/kinematics.npb.h"
google::protobuf::Message* wpi::Protobuf<frc::SwerveModuleState>::New(
google::protobuf::Arena* arena) {
return wpi::CreateMessage<wpi::proto::ProtobufSwerveModuleState>(arena);
}
std::optional<frc::SwerveModuleState>
wpi::Protobuf<frc::SwerveModuleState>::Unpack(InputStream& stream) {
wpi::UnpackCallback<frc::Rotation2d> angle;
wpi_proto_ProtobufSwerveModuleState msg{
.speed = 0,
.angle = angle.Callback(),
};
if (!stream.Decode(msg)) {
return {};
}
auto iangle = angle.Items();
if (iangle.empty()) {
return {};
}
frc::SwerveModuleState wpi::Protobuf<frc::SwerveModuleState>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufSwerveModuleState*>(&msg);
return frc::SwerveModuleState{
units::meters_per_second_t{m->speed()},
wpi::UnpackProtobuf<frc::Rotation2d>(m->wpi_angle()),
units::meters_per_second_t{msg.speed},
iangle[0],
};
}
void wpi::Protobuf<frc::SwerveModuleState>::Pack(
google::protobuf::Message* msg, const frc::SwerveModuleState& value) {
auto m = static_cast<wpi::proto::ProtobufSwerveModuleState*>(msg);
m->set_speed(value.speed.value());
wpi::PackProtobuf(m->mutable_angle(), value.angle);
bool wpi::Protobuf<frc::SwerveModuleState>::Pack(
OutputStream& stream, const frc::SwerveModuleState& value) {
wpi::PackCallback angle{&value.angle};
wpi_proto_ProtobufSwerveModuleState msg{
.speed = value.speed.value(),
.angle = angle.Callback(),
};
return stream.Encode(msg);
}