[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());
}

View File

@@ -0,0 +1,29 @@
// 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/kinematics/ChassisSpeeds.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::ChassisSpeeds>;
const ChassisSpeeds kExpectedData =
ChassisSpeeds{2.29_mps, 2.2_mps, 0.3504_rad_per_s};
} // namespace
TEST(ChassisSpeedsProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
ChassisSpeeds unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.vx.value(), unpacked_data.vx.value());
EXPECT_EQ(kExpectedData.vy.value(), unpacked_data.vy.value());
EXPECT_EQ(kExpectedData.omega.value(), unpacked_data.omega.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/kinematics/DifferentialDriveKinematics.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::DifferentialDriveKinematics>;
const DifferentialDriveKinematics kExpectedData =
DifferentialDriveKinematics{1.74_m};
} // namespace
TEST(DifferentialDriveKinematicsProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
DifferentialDriveKinematics unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.trackWidth.value(), unpacked_data.trackWidth.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 "frc/kinematics/DifferentialDriveWheelSpeeds.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::DifferentialDriveWheelSpeeds>;
const DifferentialDriveWheelSpeeds kExpectedData =
DifferentialDriveWheelSpeeds{1.74_mps, 35.04_mps};
} // namespace
TEST(DifferentialDriveWheelSpeedsProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
DifferentialDriveWheelSpeeds 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 "frc/kinematics/MecanumDriveKinematics.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::MecanumDriveKinematics>;
const MecanumDriveKinematics kExpectedData = MecanumDriveKinematics{
Translation2d{19.1_m, 2.2_m}, Translation2d{35.04_m, 1.91_m},
Translation2d{1.74_m, 3.504_m}, Translation2d{3.504_m, 1.91_m}};
} // namespace
TEST(MecanumDriveKinematicsProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
MecanumDriveKinematics unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.GetFrontLeftWheel(),
unpacked_data.GetFrontLeftWheel());
EXPECT_EQ(kExpectedData.GetFrontRightWheel(),
unpacked_data.GetFrontRightWheel());
EXPECT_EQ(kExpectedData.GetRearLeftWheel(), unpacked_data.GetRearLeftWheel());
EXPECT_EQ(kExpectedData.GetRearRightWheel(),
unpacked_data.GetRearRightWheel());
}

View File

@@ -0,0 +1,30 @@
// 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/kinematics/MecanumDriveWheelPositions.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::MecanumDriveWheelPositions>;
const MecanumDriveWheelPositions kExpectedData =
MecanumDriveWheelPositions{17.4_m, 2.29_m, 22.9_m, 1.74_m};
} // namespace
TEST(MecanumDriveWheelPositionsProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
MecanumDriveWheelPositions unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.frontLeft.value(), unpacked_data.frontLeft.value());
EXPECT_EQ(kExpectedData.frontRight.value(), unpacked_data.frontRight.value());
EXPECT_EQ(kExpectedData.rearLeft.value(), unpacked_data.rearLeft.value());
EXPECT_EQ(kExpectedData.rearRight.value(), unpacked_data.rearRight.value());
}

View File

@@ -0,0 +1,30 @@
// 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/kinematics/MecanumDriveWheelSpeeds.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::MecanumDriveWheelSpeeds>;
const MecanumDriveWheelSpeeds kExpectedData =
MecanumDriveWheelSpeeds{2.29_mps, 17.4_mps, 4.4_mps, 0.229_mps};
} // namespace
TEST(MecanumDriveWheelSpeedsProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
MecanumDriveWheelSpeeds unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.frontLeft.value(), unpacked_data.frontLeft.value());
EXPECT_EQ(kExpectedData.frontRight.value(), unpacked_data.frontRight.value());
EXPECT_EQ(kExpectedData.rearLeft.value(), unpacked_data.rearLeft.value());
EXPECT_EQ(kExpectedData.rearRight.value(), unpacked_data.rearRight.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 "frc/kinematics/SwerveModulePosition.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::SwerveModulePosition>;
const SwerveModulePosition kExpectedData =
SwerveModulePosition{3.504_m, Rotation2d{17.4_rad}};
} // namespace
TEST(SwerveModulePositionProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
SwerveModulePosition unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.distance.value(), unpacked_data.distance.value());
EXPECT_EQ(kExpectedData.angle, unpacked_data.angle);
}

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 "frc/kinematics/SwerveModuleState.h"
#include "kinematics.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::SwerveModuleState>;
const SwerveModuleState kExpectedData =
SwerveModuleState{22.9_mps, Rotation2d{3.3_rad}};
} // namespace
TEST(SwerveModuleStateProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
SwerveModuleState unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.speed.value(), unpacked_data.speed.value());
EXPECT_EQ(kExpectedData.angle, unpacked_data.angle);
}

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 "frc/kinematics/ChassisSpeeds.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::ChassisSpeeds>;
const ChassisSpeeds kExpectedData{
ChassisSpeeds{2.29_mps, 2.2_mps, 0.3504_rad_per_s}};
} // namespace
TEST(ChassisSpeedsStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
ChassisSpeeds unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.vx.value(), unpacked_data.vx.value());
EXPECT_EQ(kExpectedData.vy.value(), unpacked_data.vy.value());
EXPECT_EQ(kExpectedData.omega.value(), unpacked_data.omega.value());
}

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 <gtest/gtest.h>
#include "frc/kinematics/DifferentialDriveKinematics.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::DifferentialDriveKinematics>;
const DifferentialDriveKinematics kExpectedData{
DifferentialDriveKinematics{1.74_m}};
} // namespace
TEST(DifferentialDriveKinematicsStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
DifferentialDriveKinematics unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.trackWidth.value(), unpacked_data.trackWidth.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/kinematics/DifferentialDriveWheelSpeeds.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::DifferentialDriveWheelSpeeds>;
const DifferentialDriveWheelSpeeds kExpectedData{
DifferentialDriveWheelSpeeds{1.74_mps, 35.04_mps}};
} // namespace
TEST(DifferentialDriveWheelSpeedsStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
DifferentialDriveWheelSpeeds 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,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/kinematics/MecanumDriveKinematics.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::MecanumDriveKinematics>;
const MecanumDriveKinematics kExpectedData{MecanumDriveKinematics{
Translation2d{19.1_m, 2.2_m}, Translation2d{35.04_m, 1.91_m},
Translation2d{1.74_m, 3.504_m}, Translation2d{3.504_m, 1.91_m}}};
} // namespace
TEST(MecanumDriveKinematicsStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
MecanumDriveKinematics unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.GetFrontLeftWheel(),
unpacked_data.GetFrontLeftWheel());
EXPECT_EQ(kExpectedData.GetFrontRightWheel(),
unpacked_data.GetFrontRightWheel());
EXPECT_EQ(kExpectedData.GetRearLeftWheel(), unpacked_data.GetRearLeftWheel());
EXPECT_EQ(kExpectedData.GetRearRightWheel(),
unpacked_data.GetRearRightWheel());
}

View File

@@ -0,0 +1,29 @@
// 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/kinematics/MecanumDriveWheelPositions.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::MecanumDriveWheelPositions>;
const MecanumDriveWheelPositions kExpectedData{
MecanumDriveWheelPositions{17.4_m, 2.29_m, 22.9_m, 1.74_m}};
} // namespace
TEST(MecanumDriveWheelPositionsStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
MecanumDriveWheelPositions unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.frontLeft.value(), unpacked_data.frontLeft.value());
EXPECT_EQ(kExpectedData.frontRight.value(), unpacked_data.frontRight.value());
EXPECT_EQ(kExpectedData.rearLeft.value(), unpacked_data.rearLeft.value());
EXPECT_EQ(kExpectedData.rearRight.value(), unpacked_data.rearRight.value());
}

View File

@@ -0,0 +1,29 @@
// 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/kinematics/MecanumDriveWheelSpeeds.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::MecanumDriveWheelSpeeds>;
const MecanumDriveWheelSpeeds kExpectedData{
MecanumDriveWheelSpeeds{2.29_mps, 17.4_mps, 4.4_mps, 0.229_mps}};
} // namespace
TEST(MecanumDriveWheelSpeedsStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
MecanumDriveWheelSpeeds unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.frontLeft.value(), unpacked_data.frontLeft.value());
EXPECT_EQ(kExpectedData.frontRight.value(), unpacked_data.frontRight.value());
EXPECT_EQ(kExpectedData.rearLeft.value(), unpacked_data.rearLeft.value());
EXPECT_EQ(kExpectedData.rearRight.value(), unpacked_data.rearRight.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/kinematics/SwerveModulePosition.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::SwerveModulePosition>;
const SwerveModulePosition kExpectedData{
SwerveModulePosition{3.504_m, Rotation2d{17.4_rad}}};
} // namespace
TEST(SwerveModulePositionStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
SwerveModulePosition unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.distance.value(), unpacked_data.distance.value());
EXPECT_EQ(kExpectedData.angle, unpacked_data.angle);
}

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/kinematics/SwerveModuleState.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::SwerveModuleState>;
const SwerveModuleState kExpectedData{
SwerveModuleState{22.9_mps, Rotation2d{3.3_rad}}};
} // namespace
TEST(SwerveModuleStateStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
SwerveModuleState unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.speed.value(), unpacked_data.speed.value());
EXPECT_EQ(kExpectedData.angle, unpacked_data.angle);
}

View File

@@ -0,0 +1,42 @@
// 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/system/plant/DCMotor.h"
#include "plant.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::DCMotor>;
const DCMotor kExpectedData = DCMotor{units::volt_t{1.91},
units::newton_meter_t{19.1},
units::ampere_t{1.74},
units::ampere_t{2.29},
units::radians_per_second_t{2.2},
2};
} // namespace
TEST(DCMotorProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
DCMotor unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.nominalVoltage.value(),
unpacked_data.nominalVoltage.value());
EXPECT_EQ(kExpectedData.stallTorque.value(),
unpacked_data.stallTorque.value());
EXPECT_EQ(kExpectedData.stallCurrent.value(),
unpacked_data.stallCurrent.value());
EXPECT_EQ(kExpectedData.freeCurrent.value(),
unpacked_data.freeCurrent.value());
EXPECT_EQ(kExpectedData.freeSpeed.value(), unpacked_data.freeSpeed.value());
EXPECT_EQ(kExpectedData.R.value(), unpacked_data.R.value());
EXPECT_EQ(kExpectedData.Kv.value(), unpacked_data.Kv.value());
EXPECT_EQ(kExpectedData.Kt.value(), unpacked_data.Kt.value());
}

View File

@@ -0,0 +1,41 @@
// 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/system/plant/DCMotor.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::DCMotor>;
const DCMotor kExpectedData = DCMotor{units::volt_t{1.91},
units::newton_meter_t{19.1},
units::ampere_t{1.74},
units::ampere_t{2.29},
units::radians_per_second_t{2.2},
2};
} // namespace
TEST(DCMotorStructTest, Roundtrip) {
uint8_t buffer[StructType::kSize];
std::memset(buffer, 0, StructType::kSize);
StructType::Pack(buffer, kExpectedData);
DCMotor unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.nominalVoltage.value(),
unpacked_data.nominalVoltage.value());
EXPECT_EQ(kExpectedData.stallTorque.value(),
unpacked_data.stallTorque.value());
EXPECT_EQ(kExpectedData.stallCurrent.value(),
unpacked_data.stallCurrent.value());
EXPECT_EQ(kExpectedData.freeCurrent.value(),
unpacked_data.freeCurrent.value());
EXPECT_EQ(kExpectedData.freeSpeed.value(), unpacked_data.freeSpeed.value());
EXPECT_EQ(kExpectedData.R.value(), unpacked_data.R.value());
EXPECT_EQ(kExpectedData.Kv.value(), unpacked_data.Kv.value());
EXPECT_EQ(kExpectedData.Kt.value(), unpacked_data.Kt.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 <gtest/gtest.h>
#include "frc/trajectory/Trajectory.h"
#include "trajectory.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::Trajectory>;
const Trajectory kExpectedData = Trajectory{std::vector<frc::Trajectory::State>{
Trajectory::State{1.1_s, 2.2_mps, 3.3_mps_sq,
Pose2d(Translation2d(1.1_m, 2.2_m), Rotation2d(2.2_rad)),
units::curvature_t{6.6}},
Trajectory::State{2.1_s, 2.2_mps, 3.3_mps_sq,
Pose2d(Translation2d(2.1_m, 2.2_m), Rotation2d(2.2_rad)),
units::curvature_t{6.6}},
Trajectory::State{3.1_s, 2.2_mps, 3.3_mps_sq,
Pose2d(Translation2d(3.1_m, 2.2_m), Rotation2d(2.2_rad)),
units::curvature_t{6.6}}}};
} // namespace
TEST(TrajectoryProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
Trajectory unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.States(), unpacked_data.States());
}

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/trajectory/Trajectory.h"
#include "trajectory.pb.h"
using namespace frc;
namespace {
using ProtoType = wpi::Protobuf<frc::Trajectory::State>;
const Trajectory::State kExpectedData = Trajectory::State{
1.91_s, 4.4_mps, 17.4_mps_sq,
Pose2d{Translation2d{1.74_m, 19.1_m}, Rotation2d{22.9_rad}},
units::curvature_t{0.174}};
} // namespace
TEST(TrajectoryStateProtoTest, Roundtrip) {
google::protobuf::Arena arena;
google::protobuf::Message* proto = ProtoType::New(&arena);
ProtoType::Pack(proto, kExpectedData);
Trajectory::State unpacked_data = ProtoType::Unpack(*proto);
EXPECT_EQ(kExpectedData.t.value(), unpacked_data.t.value());
EXPECT_EQ(kExpectedData.velocity.value(), unpacked_data.velocity.value());
EXPECT_EQ(kExpectedData.acceleration.value(),
unpacked_data.acceleration.value());
EXPECT_EQ(kExpectedData.pose, unpacked_data.pose);
EXPECT_EQ(kExpectedData.curvature.value(), unpacked_data.curvature.value());
}