[wpimath] Add structured data support for DifferentialDriveWheelPositions (#6412)

This commit is contained in:
DeltaDizzy
2024-03-09 12:09:02 -06:00
committed by GitHub
parent 18e57f7872
commit 7bd8c44570
12 changed files with 668 additions and 58 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.
package edu.wpi.first.math.kinematics.struct;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.math.kinematics.DifferentialDriveWheelPositions;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.junit.jupiter.api.Test;
class DifferentialDriveWheelPositionsStructTest {
private static final DifferentialDriveWheelPositions DATA =
new DifferentialDriveWheelPositions(1.74, 35.04);
@Test
void testRoundtrip() {
ByteBuffer buffer = ByteBuffer.allocate(DifferentialDriveWheelPositions.struct.getSize());
buffer.order(ByteOrder.LITTLE_ENDIAN);
DifferentialDriveWheelPositions.struct.pack(buffer, DATA);
buffer.rewind();
DifferentialDriveWheelPositions data = DifferentialDriveWheelPositions.struct.unpack(buffer);
assertEquals(DATA.leftMeters, data.leftMeters);
assertEquals(DATA.rightMeters, data.rightMeters);
}
}

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/DifferentialDriveWheelPositions.h"
using namespace frc;
namespace {
using StructType = wpi::Struct<frc::DifferentialDriveWheelPositions>;
const DifferentialDriveWheelPositions kExpectedData{
DifferentialDriveWheelPositions{1.74_m, 35.04_m}};
} // namespace
TEST(DifferentialDriveWheelPositionsStructTest, Roundtrip) {
uint8_t buffer[StructType::GetSize()];
std::memset(buffer, 0, StructType::GetSize());
StructType::Pack(buffer, kExpectedData);
DifferentialDriveWheelPositions unpacked_data = StructType::Unpack(buffer);
EXPECT_EQ(kExpectedData.left.value(), unpacked_data.left.value());
EXPECT_EQ(kExpectedData.right.value(), unpacked_data.right.value());
}