mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01: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:
@@ -24,6 +24,8 @@ template <typename WheelSpeeds, typename WheelPositions>
|
||||
std::assignable_from<WheelPositions&, const WheelPositions&>
|
||||
class WPILIB_DLLEXPORT Kinematics {
|
||||
public:
|
||||
virtual ~Kinematics() noexcept = default;
|
||||
|
||||
/**
|
||||
* Performs forward kinematics to return the resulting chassis speed from the
|
||||
* wheel speeds. This method is often used for odometry -- determining the
|
||||
|
||||
@@ -429,7 +429,7 @@ class SwerveDriveKinematics
|
||||
return {result};
|
||||
}
|
||||
|
||||
const wpi::array<Translation2d, NumModules> GetModules() const {
|
||||
const wpi::array<Translation2d, NumModules>& GetModules() const {
|
||||
return m_modules;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,13 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/ChassisSpeeds.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::ChassisSpeeds> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::ChassisSpeeds Unpack(const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
const frc::ChassisSpeeds& value);
|
||||
using MessageStruct = wpi_proto_ProtobufChassisSpeeds;
|
||||
using InputStream = wpi::ProtoInputStream<frc::ChassisSpeeds>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::ChassisSpeeds>;
|
||||
static std::optional<frc::ChassisSpeeds> Unpack(InputStream& stream);
|
||||
static bool Pack(OutputStream& stream, const frc::ChassisSpeeds& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,15 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/DifferentialDriveKinematics.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::DifferentialDriveKinematics> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::DifferentialDriveKinematics Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufDifferentialDriveKinematics;
|
||||
using InputStream = wpi::ProtoInputStream<frc::DifferentialDriveKinematics>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::DifferentialDriveKinematics>;
|
||||
static std::optional<frc::DifferentialDriveKinematics> Unpack(
|
||||
InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::DifferentialDriveKinematics& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,17 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/DifferentialDriveWheelPositions.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::DifferentialDriveWheelPositions> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::DifferentialDriveWheelPositions Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufDifferentialDriveWheelPositions;
|
||||
using InputStream =
|
||||
wpi::ProtoInputStream<frc::DifferentialDriveWheelPositions>;
|
||||
using OutputStream =
|
||||
wpi::ProtoOutputStream<frc::DifferentialDriveWheelPositions>;
|
||||
static std::optional<frc::DifferentialDriveWheelPositions> Unpack(
|
||||
InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::DifferentialDriveWheelPositions& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,16 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/DifferentialDriveWheelSpeeds.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::DifferentialDriveWheelSpeeds> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::DifferentialDriveWheelSpeeds Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufDifferentialDriveWheelSpeeds;
|
||||
using InputStream = wpi::ProtoInputStream<frc::DifferentialDriveWheelSpeeds>;
|
||||
using OutputStream =
|
||||
wpi::ProtoOutputStream<frc::DifferentialDriveWheelSpeeds>;
|
||||
static std::optional<frc::DifferentialDriveWheelSpeeds> Unpack(
|
||||
InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::DifferentialDriveWheelSpeeds& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,14 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/MecanumDriveKinematics.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::MecanumDriveKinematics> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::MecanumDriveKinematics Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufMecanumDriveKinematics;
|
||||
using InputStream = wpi::ProtoInputStream<frc::MecanumDriveKinematics>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::MecanumDriveKinematics>;
|
||||
static std::optional<frc::MecanumDriveKinematics> Unpack(InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::MecanumDriveKinematics& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,15 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/MecanumDriveWheelPositions.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::MecanumDriveWheelPositions> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::MecanumDriveWheelPositions Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufMecanumDriveWheelPositions;
|
||||
using InputStream = wpi::ProtoInputStream<frc::MecanumDriveWheelPositions>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::MecanumDriveWheelPositions>;
|
||||
static std::optional<frc::MecanumDriveWheelPositions> Unpack(
|
||||
InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::MecanumDriveWheelPositions& value);
|
||||
};
|
||||
|
||||
@@ -8,12 +8,15 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/MecanumDriveWheelSpeeds.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::MecanumDriveWheelSpeeds> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::MecanumDriveWheelSpeeds Unpack(
|
||||
const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufMecanumDriveWheelSpeeds;
|
||||
using InputStream = wpi::ProtoInputStream<frc::MecanumDriveWheelSpeeds>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::MecanumDriveWheelSpeeds>;
|
||||
static std::optional<frc::MecanumDriveWheelSpeeds> Unpack(
|
||||
InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::MecanumDriveWheelSpeeds& value);
|
||||
};
|
||||
|
||||
@@ -7,36 +7,40 @@
|
||||
#include <stdexcept>
|
||||
|
||||
#include <fmt/format.h>
|
||||
#include <wpi/ProtoHelper.h>
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
#include <wpi/protobuf/ProtobufCallbacks.h>
|
||||
|
||||
#include "frc/kinematics/SwerveDriveKinematics.h"
|
||||
#include "kinematics.pb.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <size_t NumModules>
|
||||
struct wpi::Protobuf<frc::SwerveDriveKinematics<NumModules>> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena) {
|
||||
return wpi::CreateMessage<wpi::proto::ProtobufSwerveDriveKinematics>(arena);
|
||||
}
|
||||
using MessageStruct = wpi_proto_ProtobufSwerveDriveKinematics;
|
||||
using InputStream =
|
||||
wpi::ProtoInputStream<frc::SwerveDriveKinematics<NumModules>>;
|
||||
using OutputStream =
|
||||
wpi::ProtoOutputStream<frc::SwerveDriveKinematics<NumModules>>;
|
||||
|
||||
static frc::SwerveDriveKinematics<NumModules> Unpack(
|
||||
const google::protobuf::Message& msg) {
|
||||
auto m =
|
||||
static_cast<const wpi::proto::ProtobufSwerveDriveKinematics*>(&msg);
|
||||
if (m->modules_size() != NumModules) {
|
||||
throw std::invalid_argument(fmt::format(
|
||||
"Tried to unpack message with {} elements in modules into "
|
||||
"SwerveDriveKinematics with {} modules",
|
||||
m->modules_size(), NumModules));
|
||||
static std::optional<frc::SwerveDriveKinematics<NumModules>> Unpack(
|
||||
InputStream& stream) {
|
||||
wpi::WpiArrayUnpackCallback<frc::Translation2d, NumModules> modules;
|
||||
wpi_proto_ProtobufSwerveDriveKinematics msg{
|
||||
.modules = modules.Callback(),
|
||||
};
|
||||
modules.SetLimits(wpi::DecodeLimits::Fail);
|
||||
if (!stream.Decode(msg)) {
|
||||
return {};
|
||||
}
|
||||
return frc::SwerveDriveKinematics<NumModules>{
|
||||
wpi::UnpackProtobufArray<wpi::proto::ProtobufTranslation2d,
|
||||
frc::Translation2d, NumModules>(m->modules())};
|
||||
|
||||
return frc::SwerveDriveKinematics<NumModules>{modules.Array()};
|
||||
}
|
||||
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::SwerveDriveKinematics<NumModules>& value) {
|
||||
auto m = static_cast<wpi::proto::ProtobufSwerveDriveKinematics*>(msg);
|
||||
wpi::PackProtobufArray(m->mutable_modules(), value.GetModules());
|
||||
wpi::PackCallback<frc::Translation2d> modules{value.GetModules()};
|
||||
wpi_proto_ProtobufSwerveDriveKinematics msg{
|
||||
.modules = modules.Callback(),
|
||||
};
|
||||
return stream.Encode(msg);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -8,11 +8,14 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/SwerveModulePosition.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::SwerveModulePosition> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::SwerveModulePosition Unpack(const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
using MessageStruct = wpi_proto_ProtobufSwerveModulePosition;
|
||||
using InputStream = wpi::ProtoInputStream<frc::SwerveModulePosition>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::SwerveModulePosition>;
|
||||
static std::optional<frc::SwerveModulePosition> Unpack(InputStream& stream);
|
||||
static bool Pack(OutputStream& stream,
|
||||
const frc::SwerveModulePosition& value);
|
||||
};
|
||||
|
||||
@@ -8,11 +8,13 @@
|
||||
#include <wpi/protobuf/Protobuf.h>
|
||||
|
||||
#include "frc/kinematics/SwerveModuleState.h"
|
||||
#include "wpimath/protobuf/kinematics.npb.h"
|
||||
|
||||
template <>
|
||||
struct WPILIB_DLLEXPORT wpi::Protobuf<frc::SwerveModuleState> {
|
||||
static google::protobuf::Message* New(google::protobuf::Arena* arena);
|
||||
static frc::SwerveModuleState Unpack(const google::protobuf::Message& msg);
|
||||
static void Pack(google::protobuf::Message* msg,
|
||||
const frc::SwerveModuleState& value);
|
||||
using MessageStruct = wpi_proto_ProtobufSwerveModuleState;
|
||||
using InputStream = wpi::ProtoInputStream<frc::SwerveModuleState>;
|
||||
using OutputStream = wpi::ProtoOutputStream<frc::SwerveModuleState>;
|
||||
static std::optional<frc::SwerveModuleState> Unpack(InputStream& stream);
|
||||
static bool Pack(OutputStream& stream, const frc::SwerveModuleState& value);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user