[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,35 @@
// 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/system/plant/proto/DCMotorProto.h"
#include "plant.pb.h"
google::protobuf::Message* wpi::Protobuf<frc::DCMotor>::New(
google::protobuf::Arena* arena) {
return google::protobuf::Arena::CreateMessage<wpi::proto::ProtobufDCMotor>(
arena);
}
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()},
};
}
void wpi::Protobuf<frc::DCMotor>::Pack(google::protobuf::Message* msg,
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());
}

View File

@@ -0,0 +1,35 @@
// 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/system/plant/struct/DCMotorStruct.h"
namespace {
constexpr size_t kNominalVoltageOff = 0;
constexpr size_t kStallTorqueOff = kNominalVoltageOff + 8;
constexpr size_t kStallCurrentOff = kStallTorqueOff + 8;
constexpr size_t kFreeCurrentOff = kStallCurrentOff + 8;
constexpr size_t kFreeSpeedOff = kFreeCurrentOff + 8;
} // namespace
using StructType = wpi::Struct<frc::DCMotor>;
frc::DCMotor StructType::Unpack(std::span<const uint8_t, kSize> data) {
return frc::DCMotor{
units::volt_t{wpi::UnpackStruct<double, kNominalVoltageOff>(data)},
units::newton_meter_t{wpi::UnpackStruct<double, kStallTorqueOff>(data)},
units::ampere_t{wpi::UnpackStruct<double, kStallCurrentOff>(data)},
units::ampere_t{wpi::UnpackStruct<double, kFreeCurrentOff>(data)},
units::radians_per_second_t{
wpi::UnpackStruct<double, kFreeSpeedOff>(data)},
};
}
void StructType::Pack(std::span<uint8_t, kSize> data,
const frc::DCMotor& value) {
wpi::PackStruct<kNominalVoltageOff>(data, value.nominalVoltage.value());
wpi::PackStruct<kStallTorqueOff>(data, value.stallTorque.value());
wpi::PackStruct<kStallCurrentOff>(data, value.stallCurrent.value());
wpi::PackStruct<kFreeCurrentOff>(data, value.freeCurrent.value());
wpi::PackStruct<kFreeSpeedOff>(data, value.freeSpeed.value());
}