[wpimath] Add protobuf/struct for trivial types (#5935)

This implements de/serialization for the types that aren't templated (SwerveDriveKinematics) in C++ or where there is no trivial way to go round-trip (Splines) for the messages.
This commit is contained in:
PJ Reiniger
2023-11-21 13:14:06 -05:00
committed by GitHub
parent 35744a036e
commit bb05e20247
158 changed files with 4266 additions and 4 deletions

View File

@@ -0,0 +1,33 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/controller/proto/ArmFeedforwardProto.h"
#include "controller.pb.h"
google::protobuf::Message* wpi::Protobuf<frc::ArmFeedforward>::New(
google::protobuf::Arena* arena) {
return google::protobuf::Arena::CreateMessage<
wpi::proto::ProtobufArmFeedforward>(arena);
}
frc::ArmFeedforward wpi::Protobuf<frc::ArmFeedforward>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufArmFeedforward*>(&msg);
return frc::ArmFeedforward{
units::volt_t{m->ks()},
units::volt_t{m->kg()},
units::unit_t<frc::ArmFeedforward::kv_unit>{m->kv()},
units::unit_t<frc::ArmFeedforward::ka_unit>{m->ka()},
};
}
void wpi::Protobuf<frc::ArmFeedforward>::Pack(
google::protobuf::Message* msg, const frc::ArmFeedforward& value) {
auto m = static_cast<wpi::proto::ProtobufArmFeedforward*>(msg);
m->set_ks(value.kS.value());
m->set_kg(value.kG.value());
m->set_kv(value.kV.value());
m->set_ka(value.kA.value());
}

View File

@@ -0,0 +1,34 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/controller/proto/DifferentialDriveWheelVoltagesProto.h"
#include "controller.pb.h"
google::protobuf::Message* wpi::Protobuf<
frc::DifferentialDriveWheelVoltages>::New(google::protobuf::Arena* arena) {
return google::protobuf::Arena::CreateMessage<
wpi::proto::ProtobufDifferentialDriveWheelVoltages>(arena);
}
frc::DifferentialDriveWheelVoltages
wpi::Protobuf<frc::DifferentialDriveWheelVoltages>::Unpack(
const google::protobuf::Message& msg) {
auto m =
static_cast<const wpi::proto::ProtobufDifferentialDriveWheelVoltages*>(
&msg);
return frc::DifferentialDriveWheelVoltages{
units::volt_t{m->left()},
units::volt_t{m->right()},
};
}
void wpi::Protobuf<frc::DifferentialDriveWheelVoltages>::Pack(
google::protobuf::Message* msg,
const frc::DifferentialDriveWheelVoltages& value) {
auto m =
static_cast<wpi::proto::ProtobufDifferentialDriveWheelVoltages*>(msg);
m->set_left(value.left.value());
m->set_right(value.right.value());
}

View File

@@ -0,0 +1,33 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/controller/proto/ElevatorFeedforwardProto.h"
#include "controller.pb.h"
google::protobuf::Message* wpi::Protobuf<frc::ElevatorFeedforward>::New(
google::protobuf::Arena* arena) {
return google::protobuf::Arena::CreateMessage<
wpi::proto::ProtobufElevatorFeedforward>(arena);
}
frc::ElevatorFeedforward wpi::Protobuf<frc::ElevatorFeedforward>::Unpack(
const google::protobuf::Message& msg) {
auto m = static_cast<const wpi::proto::ProtobufElevatorFeedforward*>(&msg);
return frc::ElevatorFeedforward{
units::volt_t{m->ks()},
units::volt_t{m->kg()},
units::unit_t<frc::ElevatorFeedforward::kv_unit>{m->kv()},
units::unit_t<frc::ElevatorFeedforward::ka_unit>{m->ka()},
};
}
void wpi::Protobuf<frc::ElevatorFeedforward>::Pack(
google::protobuf::Message* msg, const frc::ElevatorFeedforward& value) {
auto m = static_cast<wpi::proto::ProtobufElevatorFeedforward*>(msg);
m->set_ks(value.kS());
m->set_kg(value.kG());
m->set_kv(value.kV());
m->set_ka(value.kA());
}

View File

@@ -0,0 +1,33 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/controller/struct/ArmFeedforwardStruct.h"
namespace {
constexpr size_t kKsOff = 0;
constexpr size_t kKgOff = kKsOff + 8;
constexpr size_t kKvOff = kKgOff + 8;
constexpr size_t kKaOff = kKvOff + 8;
} // namespace
using StructType = wpi::Struct<frc::ArmFeedforward>;
frc::ArmFeedforward StructType::Unpack(std::span<const uint8_t, kSize> data) {
return frc::ArmFeedforward{
units::volt_t{wpi::UnpackStruct<double, kKsOff>(data)},
units::volt_t{wpi::UnpackStruct<double, kKgOff>(data)},
units::unit_t<frc::ArmFeedforward::kv_unit>{
wpi::UnpackStruct<double, kKvOff>(data)},
units::unit_t<frc::ArmFeedforward::ka_unit>{
wpi::UnpackStruct<double, kKaOff>(data)},
};
}
void StructType::Pack(std::span<uint8_t, kSize> data,
const frc::ArmFeedforward& value) {
wpi::PackStruct<kKsOff>(data, value.kS());
wpi::PackStruct<kKgOff>(data, value.kG());
wpi::PackStruct<kKvOff>(data, value.kV());
wpi::PackStruct<kKaOff>(data, value.kA());
}

View File

@@ -0,0 +1,26 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/controller/struct/DifferentialDriveWheelVoltagesStruct.h"
namespace {
constexpr size_t kLeftOff = 0;
constexpr size_t kRightOff = kLeftOff + 8;
} // namespace
using StructType = wpi::Struct<frc::DifferentialDriveWheelVoltages>;
frc::DifferentialDriveWheelVoltages StructType::Unpack(
std::span<const uint8_t, kSize> data) {
return frc::DifferentialDriveWheelVoltages{
units::volt_t{wpi::UnpackStruct<double, kLeftOff>(data)},
units::volt_t{wpi::UnpackStruct<double, kRightOff>(data)},
};
}
void StructType::Pack(std::span<uint8_t, kSize> data,
const frc::DifferentialDriveWheelVoltages& value) {
wpi::PackStruct<kLeftOff>(data, value.left.value());
wpi::PackStruct<kRightOff>(data, value.right.value());
}

View File

@@ -0,0 +1,34 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/controller/struct/ElevatorFeedforwardStruct.h"
namespace {
constexpr size_t kKsOff = 0;
constexpr size_t kKgOff = kKsOff + 8;
constexpr size_t kKvOff = kKgOff + 8;
constexpr size_t kKaOff = kKvOff + 8;
} // namespace
using StructType = wpi::Struct<frc::ElevatorFeedforward>;
frc::ElevatorFeedforward StructType::Unpack(
std::span<const uint8_t, kSize> data) {
return frc::ElevatorFeedforward{
units::volt_t{wpi::UnpackStruct<double, kKsOff>(data)},
units::volt_t{wpi::UnpackStruct<double, kKgOff>(data)},
units::unit_t<frc::ElevatorFeedforward::kv_unit>{
wpi::UnpackStruct<double, kKvOff>(data)},
units::unit_t<frc::ElevatorFeedforward::ka_unit>{
wpi::UnpackStruct<double, kKaOff>(data)},
};
}
void StructType::Pack(std::span<uint8_t, kSize> data,
const frc::ElevatorFeedforward& value) {
wpi::PackStruct<kKsOff>(data, value.kS());
wpi::PackStruct<kKgOff>(data, value.kG());
wpi::PackStruct<kKvOff>(data, value.kV());
wpi::PackStruct<kKaOff>(data, value.kA());
}