[wpimath] Add structs for TrapezoidProfile.State and ExponentialProfile.State (#8163)

This commit is contained in:
Daniel Chen
2025-08-10 14:45:36 -04:00
committed by GitHub
parent 78fa67099e
commit f209ecb0cb
7 changed files with 145 additions and 1 deletions

View File

@@ -206,6 +206,7 @@ task generateJavaDocs(type: Javadoc) {
"-edu.wpi.first.math.system.plant.proto," +
"-edu.wpi.first.math.system.plant.struct," +
"-edu.wpi.first.math.trajectory.proto," +
"-edu.wpi.first.math.trajectory.struct," +
// The .measure package contains generated source files for which automatic javadoc
// generation is very difficult to do meaningfully.
"-edu.wpi.first.units.measure", true)

View File

@@ -4,6 +4,8 @@
package edu.wpi.first.math.trajectory;
import edu.wpi.first.math.trajectory.struct.ExponentialProfileStateStruct;
import edu.wpi.first.util.struct.StructSerializable;
import java.util.Objects;
/**
@@ -128,7 +130,10 @@ public class ExponentialProfile {
}
/** Profile state. */
public static class State {
public static class State implements StructSerializable {
/** The struct that serializes this class. */
public static final ExponentialProfileStateStruct struct = new ExponentialProfileStateStruct();
/** The position at this state. */
public double position;

View File

@@ -6,6 +6,7 @@ package edu.wpi.first.math.trajectory;
import edu.wpi.first.math.MathSharedStore;
import edu.wpi.first.math.MathUsageId;
import edu.wpi.first.math.trajectory.struct.TrapezoidProfileStateStruct;
import java.util.Objects;
/**
@@ -76,6 +77,9 @@ public class TrapezoidProfile {
/** Profile state. */
public static class State {
/** The struct used to serialize this class. */
public static final TrapezoidProfileStateStruct struct = new TrapezoidProfileStateStruct();
/** The position at this state. */
public double position;

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.
package edu.wpi.first.math.trajectory.struct;
import edu.wpi.first.math.trajectory.ExponentialProfile;
import edu.wpi.first.util.struct.Struct;
import java.nio.ByteBuffer;
public class ExponentialProfileStateStruct implements Struct<ExponentialProfile.State> {
@Override
public Class<ExponentialProfile.State> getTypeClass() {
return ExponentialProfile.State.class;
}
@Override
public String getTypeName() {
return "ExponentialProfileState";
}
@Override
public int getSize() {
return kSizeDouble * 2;
}
@Override
public String getSchema() {
return "double position;double velocity";
}
@Override
public ExponentialProfile.State unpack(ByteBuffer bb) {
return new ExponentialProfile.State(bb.getDouble(), bb.getDouble());
}
@Override
public void pack(ByteBuffer bb, ExponentialProfile.State value) {
bb.putDouble(value.position);
bb.putDouble(value.velocity);
}
}

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.
package edu.wpi.first.math.trajectory.struct;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import edu.wpi.first.util.struct.Struct;
import java.nio.ByteBuffer;
public class TrapezoidProfileStateStruct implements Struct<TrapezoidProfile.State> {
@Override
public Class<TrapezoidProfile.State> getTypeClass() {
return TrapezoidProfile.State.class;
}
@Override
public String getTypeName() {
return "TrapezoidProfileState";
}
@Override
public int getSize() {
return kSizeDouble * 2;
}
@Override
public String getSchema() {
return "double position;double velocity";
}
@Override
public TrapezoidProfile.State unpack(ByteBuffer bb) {
return new TrapezoidProfile.State(bb.getDouble(), bb.getDouble());
}
@Override
public void pack(ByteBuffer bb, TrapezoidProfile.State value) {
bb.putDouble(value.position);
bb.putDouble(value.velocity);
}
}

View File

@@ -0,0 +1,25 @@
// 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.trajectory.struct;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.math.trajectory.ExponentialProfile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.junit.jupiter.api.Test;
class ExponentialProfileStateStructTest {
private static final ExponentialProfile.State STATE = new ExponentialProfile.State(4.0, 5.0);
@Test
void testRoundtrip() {
ByteBuffer buffer = ByteBuffer.allocate(ExponentialProfile.State.struct.getSize());
buffer.order(ByteOrder.LITTLE_ENDIAN);
ExponentialProfile.State.struct.pack(buffer, STATE);
buffer.rewind();
assertEquals(STATE, ExponentialProfile.State.struct.unpack(buffer));
}
}

View File

@@ -0,0 +1,25 @@
// 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.trajectory.struct;
import static org.junit.jupiter.api.Assertions.assertEquals;
import edu.wpi.first.math.trajectory.TrapezoidProfile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.junit.jupiter.api.Test;
class TrapezoidProfileStateStructTest {
private static final TrapezoidProfile.State STATE = new TrapezoidProfile.State(4.0, 5.0);
@Test
void testRoundtrip() {
ByteBuffer buffer = ByteBuffer.allocate(TrapezoidProfile.State.struct.getSize());
buffer.order(ByteOrder.LITTLE_ENDIAN);
TrapezoidProfile.State.struct.pack(buffer, STATE);
buffer.rewind();
assertEquals(STATE, TrapezoidProfile.State.struct.unpack(buffer));
}
}