[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 <gtest/gtest.h>
#include "controller.pb.h"
#include "frc/controller/ArmFeedforward.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::ArmFeedforward>;
static constexpr auto Ks = 1.91_V;
static constexpr auto Kg = 2.29_V;
static constexpr auto Kv = 35.04_V * 1_s / 1_rad;
static constexpr auto Ka = 1.74_V * 1_s * 1_s / 1_rad;
const ArmFeedforward kExpectedData{Ks, Kg, Kv, Ka};
} // namespace
TEST(ArmFeedforwardProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
ArmFeedforward unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.kS.value(), unpacked_data.kS.value());
EXPECT_EQ(kExpectedData.kG.value(), unpacked_data.kG.value());
EXPECT_EQ(kExpectedData.kV.value(), unpacked_data.kV.value());
EXPECT_EQ(kExpectedData.kA.value(), unpacked_data.kA.value());
}

View File

@@ -0,0 +1,28 @@
// 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 <gtest/gtest.h>
#include "controller.pb.h"
#include "frc/controller/DifferentialDriveWheelVoltages.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::DifferentialDriveWheelVoltages>;
const DifferentialDriveWheelVoltages kExpectedData =
DifferentialDriveWheelVoltages{0.174_V, 0.191_V};
} // namespace
TEST(DifferentialDriveWheelVoltagesProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
DifferentialDriveWheelVoltages unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.left.value(), unpacked_data.left.value());
EXPECT_EQ(kExpectedData.right.value(), unpacked_data.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 <gtest/gtest.h>
#include "controller.pb.h"
#include "frc/controller/ElevatorFeedforward.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::ElevatorFeedforward>;
static constexpr auto Ks = 1.91_V;
static constexpr auto Kg = 2.29_V;
static constexpr auto Kv = 35.04_V * 1_s / 1_m;
static constexpr auto Ka = 1.74_V * 1_s * 1_s / 1_m;
constexpr ElevatorFeedforward kExpectedData{Ks, Kg, Kv, Ka};
} // namespace
TEST(ElevatorFeedforwardProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
ElevatorFeedforward unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.kS.value(), unpacked_data.kS.value());
EXPECT_EQ(kExpectedData.kG.value(), unpacked_data.kG.value());
EXPECT_EQ(kExpectedData.kV.value(), unpacked_data.kV.value());
EXPECT_EQ(kExpectedData.kA.value(), unpacked_data.kA.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 <gtest/gtest.h>
#include "frc/controller/ArmFeedforward.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::ArmFeedforward>;
static constexpr auto Ks = 1.91_V;
static constexpr auto Kg = 2.29_V;
static constexpr auto Kv = 35.04_V * 1_s / 1_rad;
static constexpr auto Ka = 1.74_V * 1_s * 1_s / 1_rad;
const ArmFeedforward kExpectedData{Ks, Kg, Kv, Ka};
} // namespace
TEST(ArmFeedforwardStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
ArmFeedforward unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.kS.value(), unpacked_data.kS.value());
EXPECT_EQ(kExpectedData.kG.value(), unpacked_data.kG.value());
EXPECT_EQ(kExpectedData.kV.value(), unpacked_data.kV.value());
EXPECT_EQ(kExpectedData.kA.value(), unpacked_data.kA.value());
}

View File

@@ -0,0 +1,27 @@
// 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 <gtest/gtest.h>
#include "frc/controller/DifferentialDriveWheelVoltages.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::DifferentialDriveWheelVoltages>;
const DifferentialDriveWheelVoltages kExpectedData{
DifferentialDriveWheelVoltages{0.174_V, 0.191_V}};
} // namespace
TEST(DifferentialDriveWheelVoltagesStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
DifferentialDriveWheelVoltages unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.left.value(), unpacked_data.left.value());
EXPECT_EQ(kExpectedData.right.value(), unpacked_data.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 <gtest/gtest.h>
#include "frc/controller/ElevatorFeedforward.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::ElevatorFeedforward>;
static constexpr auto Ks = 1.91_V;
static constexpr auto Kg = 2.29_V;
static constexpr auto Kv = 35.04_V * 1_s / 1_m;
static constexpr auto Ka = 1.74_V * 1_s * 1_s / 1_m;
constexpr ElevatorFeedforward kExpectedData{Ks, Kg, Kv, Ka};
} // namespace
TEST(ElevatorFeedforwardStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
ElevatorFeedforward unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.kS.value(), unpacked_data.kS.value());
EXPECT_EQ(kExpectedData.kG.value(), unpacked_data.kG.value());
EXPECT_EQ(kExpectedData.kV.value(), unpacked_data.kV.value());
EXPECT_EQ(kExpectedData.kA.value(), unpacked_data.kA.value());
}