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