[wpimath] Add remaining struct and protobuf implementations (#5953)

This commit is contained in:
Joseph Eng
2024-07-29 07:55:44 -07:00
committed by GitHub
parent 3e1e3fb4ca
commit 073192d513
112 changed files with 3989 additions and 45 deletions

View File

@@ -0,0 +1,59 @@
// 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.
#pragma once
#include <gtest/gtest.h>
#include <wpi/protobuf/Protobuf.h>
#include "controller.pb.h"
#include "kinematics.pb.h"
#include "spline.pb.h"
#include "system.pb.h"
#include "wpimath.pb.h"
template <typename T>
class ProtoTest : public testing::Test {};
TYPED_TEST_SUITE_P(ProtoTest);
TYPED_TEST_P(ProtoTest, RoundTrip) {
using Type = typename TypeParam::Type;
google::protobuf::Arena arena;
google::protobuf::Message* proto = wpi::Protobuf<Type>::New(&arena);
wpi::PackProtobuf(proto, TypeParam::kTestData);
Type unpacked_data = wpi::UnpackProtobuf<Type>(*proto);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
TYPED_TEST_P(ProtoTest, DoublePack) {
using Type = typename TypeParam::Type;
google::protobuf::Arena arena;
google::protobuf::Message* proto = wpi::Protobuf<Type>::New(&arena);
wpi::PackProtobuf(proto, TypeParam::kTestData);
wpi::PackProtobuf(proto, TypeParam::kTestData);
Type unpacked_data = wpi::UnpackProtobuf<Type>(*proto);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
TYPED_TEST_P(ProtoTest, DoubleUnpack) {
using Type = typename TypeParam::Type;
google::protobuf::Arena arena;
google::protobuf::Message* proto = wpi::Protobuf<Type>::New(&arena);
wpi::PackProtobuf(proto, TypeParam::kTestData);
{
Type unpacked_data = wpi::UnpackProtobuf<Type>(*proto);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
{
Type unpacked_data = wpi::UnpackProtobuf<Type>(*proto);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
}
REGISTER_TYPED_TEST_SUITE_P(ProtoTest, RoundTrip, DoublePack, DoubleUnpack);

View File

@@ -0,0 +1,61 @@
// 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.
#pragma once
#include <gtest/gtest.h>
#include <wpi/struct/Struct.h>
template <typename T>
class StructTest : public testing::Test {};
TYPED_TEST_SUITE_P(StructTest);
// For these tests:
// TypeParam defines Type, kTestData, and CheckEq
// Type is the data type
// StructType is the instantiation of wpi::Struct<>
TYPED_TEST_P(StructTest, RoundTrip) {
using Type = typename TypeParam::Type;
using StructType = wpi::Struct<Type>;
uint8_t buffer[StructType::GetSize()];
std::memset(buffer, 0, StructType::GetSize());
wpi::PackStruct(buffer, TypeParam::kTestData);
Type unpacked_data = wpi::UnpackStruct<Type>(buffer);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
TYPED_TEST_P(StructTest, DoublePack) {
using Type = typename TypeParam::Type;
using StructType = wpi::Struct<Type>;
uint8_t buffer[StructType::GetSize()];
std::memset(buffer, 0, StructType::GetSize());
wpi::PackStruct(buffer, TypeParam::kTestData);
wpi::PackStruct(buffer, TypeParam::kTestData);
Type unpacked_data = wpi::UnpackStruct<Type>(buffer);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
TYPED_TEST_P(StructTest, DoubleUnpack) {
using Type = typename TypeParam::Type;
using StructType = wpi::Struct<Type>;
uint8_t buffer[StructType::GetSize()];
std::memset(buffer, 0, StructType::GetSize());
wpi::PackStruct(buffer, TypeParam::kTestData);
{
Type unpacked_data = wpi::UnpackStruct<Type>(buffer);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
{
Type unpacked_data = wpi::UnpackStruct<Type>(buffer);
TypeParam::CheckEq(TypeParam::kTestData, unpacked_data);
}
}
REGISTER_TYPED_TEST_SUITE_P(StructTest, RoundTrip, DoublePack, DoubleUnpack);

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 "../../ProtoTestBase.h"
#include "frc/controller/DifferentialDriveFeedforward.h"
using namespace frc;
struct DifferentialDriveFeedforwardProtoTestData {
using Type = DifferentialDriveFeedforward;
inline static const Type kTestData{
decltype(1_V / 1_mps){0.174}, decltype(1_V / 1_mps_sq){0.229},
decltype(1_V / 1_mps){4.4}, decltype(1_V / 1_mps_sq){4.5}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.m_kVLinear.value(), data.m_kVLinear.value());
EXPECT_EQ(testData.m_kALinear.value(), data.m_kALinear.value());
EXPECT_EQ(testData.m_kVAngular.value(), data.m_kVAngular.value());
EXPECT_EQ(testData.m_kAAngular.value(), data.m_kAAngular.value());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(DifferentialDriveFeedforward, ProtoTest,
DifferentialDriveFeedforwardProtoTestData);

View File

@@ -0,0 +1,31 @@
// 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 "../../ProtoTestBase.h"
#include "frc/controller/SimpleMotorFeedforward.h"
#include "frc/controller/proto/SimpleMotorFeedforwardProto.h"
#include "units/acceleration.h"
#include "units/velocity.h"
using namespace frc;
struct SimpleMotorFeedforwardProtoTestData {
using Type = SimpleMotorFeedforward<units::meters>;
inline static const Type kTestData = {units::volt_t{0.4},
units::volt_t{4.0} / 1_mps,
units::volt_t{0.7} / 1_mps_sq, 25_ms};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetKs().value(), data.GetKs().value());
EXPECT_EQ(testData.GetKv().value(), data.GetKv().value());
EXPECT_EQ(testData.GetKa().value(), data.GetKa().value());
EXPECT_EQ(testData.GetDt().value(), data.GetDt().value());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(SimpleMotorFeedforwardMeters, ProtoTest,
SimpleMotorFeedforwardProtoTestData);

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 "../../StructTestBase.h"
#include "frc/controller/DifferentialDriveFeedforward.h"
using namespace frc;
struct DifferentialDriveFeedforwardStructTestData {
using Type = DifferentialDriveFeedforward;
inline static const Type kTestData{
decltype(1_V / 1_mps){0.174}, decltype(1_V / 1_mps_sq){0.229},
decltype(1_V / 1_mps){4.4}, decltype(1_V / 1_mps_sq){4.5}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.m_kVLinear.value(), data.m_kVLinear.value());
EXPECT_EQ(testData.m_kALinear.value(), data.m_kALinear.value());
EXPECT_EQ(testData.m_kVAngular.value(), data.m_kVAngular.value());
EXPECT_EQ(testData.m_kAAngular.value(), data.m_kAAngular.value());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(DifferentialDriveFeedforward, StructTest,
DifferentialDriveFeedforwardStructTestData);

View File

@@ -0,0 +1,31 @@
// 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 "../../StructTestBase.h"
#include "frc/controller/SimpleMotorFeedforward.h"
#include "frc/controller/struct/SimpleMotorFeedforwardStruct.h"
#include "units/acceleration.h"
#include "units/velocity.h"
using namespace frc;
struct SimpleMotorFeedforwardStructTestData {
using Type = SimpleMotorFeedforward<units::meters>;
inline static const Type kTestData = {units::volt_t{0.4},
units::volt_t{4.0} / 1_mps,
units::volt_t{0.7} / 1_mps_sq, 25_ms};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetKs().value(), data.GetKs().value());
EXPECT_EQ(testData.GetKv().value(), data.GetKv().value());
EXPECT_EQ(testData.GetKa().value(), data.GetKa().value());
EXPECT_EQ(testData.GetDt().value(), data.GetDt().value());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(SimpleMotorFeedforwardMeters, StructTest,
SimpleMotorFeedforwardStructTestData);

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 "../../ProtoTestBase.h"
#include "frc/kinematics/SwerveDriveKinematics.h"
#include "frc/kinematics/proto/SwerveDriveKinematicsProto.h"
using namespace frc;
struct SwerveDriveKinematicsProtoTestData {
using Type = SwerveDriveKinematics<4>;
inline static const Type kTestData{
frc::Translation2d{1.0_m, 0.9_m}, frc::Translation2d{1.1_m, -0.8_m},
frc::Translation2d{-1.2_m, 0.7_m}, frc::Translation2d{-1.3_m, -0.6_m}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetModules(), data.GetModules());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(SwerveDriveKinematics, ProtoTest,
SwerveDriveKinematicsProtoTestData);

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 "../../StructTestBase.h"
#include "frc/kinematics/SwerveDriveKinematics.h"
#include "frc/kinematics/struct/SwerveDriveKinematicsStruct.h"
using namespace frc;
struct SwerveDriveKinematicsStructTestData {
using Type = SwerveDriveKinematics<4>;
inline static const Type kTestData{
frc::Translation2d{1.0_m, 0.9_m}, frc::Translation2d{1.1_m, -0.8_m},
frc::Translation2d{-1.2_m, 0.7_m}, frc::Translation2d{-1.3_m, -0.6_m}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetModules(), data.GetModules());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(SwerveDriveKinematics, StructTest,
SwerveDriveKinematicsStructTestData);

View File

@@ -0,0 +1,23 @@
// 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 "../ProtoTestBase.h"
#include "frc/EigenCore.h"
#include "frc/proto/MatrixProto.h"
using namespace frc;
struct MatrixProtoTestData {
using Type = Matrixd<2, 3>;
inline static const Type kTestData{{1.1, 1.2, 1.3}, {1.4, 1.5, 1.6}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData, data);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(Matrix, ProtoTest, MatrixProtoTestData);

View File

@@ -0,0 +1,23 @@
// 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 "../ProtoTestBase.h"
#include "frc/EigenCore.h"
#include "frc/proto/VectorProto.h"
using namespace frc;
struct VectorProtoTestData {
using Type = Vectord<2>;
inline static const Type kTestData{1.1, 1.2};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData, data);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(Vector, ProtoTest, VectorProtoTestData);

View File

@@ -0,0 +1,32 @@
// 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 "../../ProtoTestBase.h"
#include "frc/spline/CubicHermiteSpline.h"
using namespace frc;
struct CubicHermiteSplineProtoTestData {
using Type = CubicHermiteSpline;
inline static const Type kTestData{
wpi::array<double, 2>{{0.1, 0.2}}, wpi::array<double, 2>{{0.3, 0.4}},
wpi::array<double, 2>{{0.5, 0.6}}, wpi::array<double, 2>{{0.7, 0.8}}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetInitialControlVector().x,
data.GetInitialControlVector().x);
EXPECT_EQ(testData.GetFinalControlVector().x,
data.GetFinalControlVector().x);
EXPECT_EQ(testData.GetInitialControlVector().y,
data.GetInitialControlVector().y);
EXPECT_EQ(testData.GetFinalControlVector().y,
data.GetFinalControlVector().y);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(CubicHermiteSpline, ProtoTest,
CubicHermiteSplineProtoTestData);

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 "../../ProtoTestBase.h"
#include "frc/spline/QuinticHermiteSpline.h"
using namespace frc;
struct QuinticHermiteSplineProtoTestData {
using Type = QuinticHermiteSpline;
inline static const Type kTestData{wpi::array<double, 3>{{0.01, 0.02, 0.03}},
wpi::array<double, 3>{{0.04, 0.05, 0.06}},
wpi::array<double, 3>{{0.07, 0.08, 0.09}},
wpi::array<double, 3>{{0.10, 0.11, 0.11}}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetInitialControlVector().x,
data.GetInitialControlVector().x);
EXPECT_EQ(testData.GetFinalControlVector().x,
data.GetFinalControlVector().x);
EXPECT_EQ(testData.GetInitialControlVector().y,
data.GetInitialControlVector().y);
EXPECT_EQ(testData.GetFinalControlVector().y,
data.GetFinalControlVector().y);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(QuinticHermiteSpline, ProtoTest,
QuinticHermiteSplineProtoTestData);

View File

@@ -0,0 +1,32 @@
// 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 "../../StructTestBase.h"
#include "frc/spline/CubicHermiteSpline.h"
using namespace frc;
struct CubicHermiteSplineStructTestData {
using Type = CubicHermiteSpline;
inline static const Type kTestData{
wpi::array<double, 2>{{0.1, 0.2}}, wpi::array<double, 2>{{0.3, 0.4}},
wpi::array<double, 2>{{0.5, 0.6}}, wpi::array<double, 2>{{0.7, 0.8}}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetInitialControlVector().x,
data.GetInitialControlVector().x);
EXPECT_EQ(testData.GetFinalControlVector().x,
data.GetFinalControlVector().x);
EXPECT_EQ(testData.GetInitialControlVector().y,
data.GetInitialControlVector().y);
EXPECT_EQ(testData.GetFinalControlVector().y,
data.GetFinalControlVector().y);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(CubicHermiteSpline, StructTest,
CubicHermiteSplineStructTestData);

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 "../../StructTestBase.h"
#include "frc/spline/QuinticHermiteSpline.h"
using namespace frc;
struct QuinticHermiteSplineStructTestData {
using Type = QuinticHermiteSpline;
inline static const Type kTestData{wpi::array<double, 3>{{0.01, 0.02, 0.03}},
wpi::array<double, 3>{{0.04, 0.05, 0.06}},
wpi::array<double, 3>{{0.07, 0.08, 0.09}},
wpi::array<double, 3>{{0.10, 0.11, 0.11}}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.GetInitialControlVector().x,
data.GetInitialControlVector().x);
EXPECT_EQ(testData.GetFinalControlVector().x,
data.GetFinalControlVector().x);
EXPECT_EQ(testData.GetInitialControlVector().y,
data.GetInitialControlVector().y);
EXPECT_EQ(testData.GetFinalControlVector().y,
data.GetFinalControlVector().y);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(QuinticHermiteSpline, StructTest,
QuinticHermiteSplineStructTestData);

View File

@@ -0,0 +1,23 @@
// 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 "../StructTestBase.h"
#include "frc/EigenCore.h"
#include "frc/struct/MatrixStruct.h"
using namespace frc;
struct MatrixStructTestData {
using Type = Matrixd<2, 3>;
inline static const Type kTestData{{1.1, 1.2, 1.3}, {1.4, 1.5, 1.6}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData, data);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(Matrix, StructTest, MatrixStructTestData);

View File

@@ -0,0 +1,23 @@
// 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 "../StructTestBase.h"
#include "frc/EigenCore.h"
#include "frc/struct/VectorStruct.h"
using namespace frc;
struct VectorStructTestData {
using Type = Vectord<2>;
inline static const Type kTestData{1.1, 1.2};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData, data);
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(Vector, StructTest, VectorStructTestData);

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 "../../ProtoTestBase.h"
#include "frc/system/LinearSystem.h"
#include "frc/system/proto/LinearSystemProto.h"
using namespace frc;
struct LinearSystemProtoTestData {
using Type = LinearSystem<2, 3, 4>;
inline static const Type kTestData{
Matrixd<2, 2>{{1.1, 1.2}, {1.3, 1.4}},
Matrixd<2, 3>{{2.1, 2.2, 2.3}, {2.4, 2.5, 2.6}},
Matrixd<4, 2>{{3.1, 3.2}, {3.3, 3.4}, {3.5, 3.6}, {3.7, 3.8}},
Matrixd<4, 3>{{4.01, 4.02, 4.03},
{4.04, 4.05, 4.06},
{4.07, 4.08, 4.09},
{4.10, 4.11, 4.12}}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.A(), data.A());
EXPECT_EQ(testData.B(), data.B());
EXPECT_EQ(testData.C(), data.C());
EXPECT_EQ(testData.D(), data.D());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(LinearSystem, ProtoTest,
LinearSystemProtoTestData);

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 "../../StructTestBase.h"
#include "frc/system/LinearSystem.h"
#include "frc/system/struct/LinearSystemStruct.h"
using namespace frc;
struct LinearSystemStructTestData {
using Type = LinearSystem<2, 3, 4>;
inline static const Type kTestData{
Matrixd<2, 2>{{1.1, 1.2}, {1.3, 1.4}},
Matrixd<2, 3>{{2.1, 2.2, 2.3}, {2.4, 2.5, 2.6}},
Matrixd<4, 2>{{3.1, 3.2}, {3.3, 3.4}, {3.5, 3.6}, {3.7, 3.8}},
Matrixd<4, 3>{{4.01, 4.02, 4.03},
{4.04, 4.05, 4.06},
{4.07, 4.08, 4.09},
{4.10, 4.11, 4.12}}};
static void CheckEq(const Type& testData, const Type& data) {
EXPECT_EQ(testData.A(), data.A());
EXPECT_EQ(testData.B(), data.B());
EXPECT_EQ(testData.C(), data.C());
EXPECT_EQ(testData.D(), data.D());
}
};
INSTANTIATE_TYPED_TEST_SUITE_P(LinearSystem, StructTest,
LinearSystemStructTestData);