[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

@@ -4,6 +4,9 @@
package edu.wpi.first.math.controller;
import edu.wpi.first.math.controller.proto.ArmFeedforwardProto;
import edu.wpi.first.math.controller.struct.ArmFeedforwardStruct;
/**
* A helper class that computes feedforward outputs for a simple arm (modeled as a motor acting
* against the force of gravity on a beam suspended at an angle).
@@ -14,6 +17,9 @@ public class ArmFeedforward {
public final double kv;
public final double ka;
public static final ArmFeedforwardProto proto = new ArmFeedforwardProto();
public static final ArmFeedforwardStruct struct = new ArmFeedforwardStruct();
/**
* Creates a new ArmFeedforward with the specified gains. Units of the gain values will dictate
* units of the computed feedforward.

View File

@@ -4,11 +4,19 @@
package edu.wpi.first.math.controller;
import edu.wpi.first.math.controller.proto.DifferentialDriveWheelVoltagesProto;
import edu.wpi.first.math.controller.struct.DifferentialDriveWheelVoltagesStruct;
/** Motor voltages for a differential drive. */
public class DifferentialDriveWheelVoltages {
public double left;
public double right;
public static final DifferentialDriveWheelVoltagesProto proto =
new DifferentialDriveWheelVoltagesProto();
public static final DifferentialDriveWheelVoltagesStruct struct =
new DifferentialDriveWheelVoltagesStruct();
public DifferentialDriveWheelVoltages() {}
public DifferentialDriveWheelVoltages(double left, double right) {

View File

@@ -6,6 +6,8 @@ package edu.wpi.first.math.controller;
import edu.wpi.first.math.MatBuilder;
import edu.wpi.first.math.Nat;
import edu.wpi.first.math.controller.proto.ElevatorFeedforwardProto;
import edu.wpi.first.math.controller.struct.ElevatorFeedforwardStruct;
import edu.wpi.first.math.system.plant.LinearSystemId;
/**
@@ -18,6 +20,9 @@ public class ElevatorFeedforward {
public final double kv;
public final double ka;
public static final ElevatorFeedforwardProto proto = new ElevatorFeedforwardProto();
public static final ElevatorFeedforwardStruct struct = new ElevatorFeedforwardStruct();
/**
* Creates a new ElevatorFeedforward with the specified gains. Units of the gain values will
* dictate units of the computed feedforward.

View File

@@ -0,0 +1,40 @@
// 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.controller.proto;
import edu.wpi.first.math.controller.ArmFeedforward;
import edu.wpi.first.math.proto.Controller.ProtobufArmFeedforward;
import edu.wpi.first.util.protobuf.Protobuf;
import us.hebi.quickbuf.Descriptors.Descriptor;
public class ArmFeedforwardProto implements Protobuf<ArmFeedforward, ProtobufArmFeedforward> {
@Override
public Class<ArmFeedforward> getTypeClass() {
return ArmFeedforward.class;
}
@Override
public Descriptor getDescriptor() {
return ProtobufArmFeedforward.getDescriptor();
}
@Override
public ProtobufArmFeedforward createMessage() {
return ProtobufArmFeedforward.newInstance();
}
@Override
public ArmFeedforward unpack(ProtobufArmFeedforward msg) {
return new ArmFeedforward(msg.getKs(), msg.getKg(), msg.getKv(), msg.getKa());
}
@Override
public void pack(ProtobufArmFeedforward msg, ArmFeedforward value) {
msg.setKs(value.ks);
msg.setKg(value.kg);
msg.setKv(value.kv);
msg.setKa(value.ka);
}
}

View File

@@ -0,0 +1,40 @@
// 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.controller.proto;
import edu.wpi.first.math.controller.DifferentialDriveWheelVoltages;
import edu.wpi.first.math.proto.Controller.ProtobufDifferentialDriveWheelVoltages;
import edu.wpi.first.util.protobuf.Protobuf;
import us.hebi.quickbuf.Descriptors.Descriptor;
public class DifferentialDriveWheelVoltagesProto
implements Protobuf<DifferentialDriveWheelVoltages, ProtobufDifferentialDriveWheelVoltages> {
@Override
public Class<DifferentialDriveWheelVoltages> getTypeClass() {
return DifferentialDriveWheelVoltages.class;
}
@Override
public Descriptor getDescriptor() {
return ProtobufDifferentialDriveWheelVoltages.getDescriptor();
}
@Override
public ProtobufDifferentialDriveWheelVoltages createMessage() {
return ProtobufDifferentialDriveWheelVoltages.newInstance();
}
@Override
public DifferentialDriveWheelVoltages unpack(ProtobufDifferentialDriveWheelVoltages msg) {
return new DifferentialDriveWheelVoltages(msg.getLeft(), msg.getRight());
}
@Override
public void pack(
ProtobufDifferentialDriveWheelVoltages msg, DifferentialDriveWheelVoltages value) {
msg.setLeft(value.left);
msg.setRight(value.right);
}
}

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.
package edu.wpi.first.math.controller.proto;
import edu.wpi.first.math.controller.ElevatorFeedforward;
import edu.wpi.first.math.proto.Controller.ProtobufElevatorFeedforward;
import edu.wpi.first.util.protobuf.Protobuf;
import us.hebi.quickbuf.Descriptors.Descriptor;
public class ElevatorFeedforwardProto
implements Protobuf<ElevatorFeedforward, ProtobufElevatorFeedforward> {
@Override
public Class<ElevatorFeedforward> getTypeClass() {
return ElevatorFeedforward.class;
}
@Override
public Descriptor getDescriptor() {
return ProtobufElevatorFeedforward.getDescriptor();
}
@Override
public ProtobufElevatorFeedforward createMessage() {
return ProtobufElevatorFeedforward.newInstance();
}
@Override
public ElevatorFeedforward unpack(ProtobufElevatorFeedforward msg) {
return new ElevatorFeedforward(msg.getKs(), msg.getKg(), msg.getKv(), msg.getKa());
}
@Override
public void pack(ProtobufElevatorFeedforward msg, ElevatorFeedforward value) {
msg.setKs(value.ks);
msg.setKg(value.kg);
msg.setKv(value.kv);
msg.setKa(value.ka);
}
}

View File

@@ -0,0 +1,48 @@
// 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.controller.struct;
import edu.wpi.first.math.controller.ArmFeedforward;
import edu.wpi.first.util.struct.Struct;
import java.nio.ByteBuffer;
public class ArmFeedforwardStruct implements Struct<ArmFeedforward> {
@Override
public Class<ArmFeedforward> getTypeClass() {
return ArmFeedforward.class;
}
@Override
public String getTypeString() {
return "struct:ArmFeedforward";
}
@Override
public int getSize() {
return kSizeDouble * 4;
}
@Override
public String getSchema() {
return "double ks;double kg;double kv;double ka";
}
@Override
public ArmFeedforward unpack(ByteBuffer bb) {
double ks = bb.getDouble();
double kg = bb.getDouble();
double kv = bb.getDouble();
double ka = bb.getDouble();
return new ArmFeedforward(ks, kg, kv, ka);
}
@Override
public void pack(ByteBuffer bb, ArmFeedforward value) {
bb.putDouble(value.ks);
bb.putDouble(value.kg);
bb.putDouble(value.kv);
bb.putDouble(value.ka);
}
}

View File

@@ -0,0 +1,45 @@
// 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.controller.struct;
import edu.wpi.first.math.controller.DifferentialDriveWheelVoltages;
import edu.wpi.first.util.struct.Struct;
import java.nio.ByteBuffer;
public class DifferentialDriveWheelVoltagesStruct
implements Struct<DifferentialDriveWheelVoltages> {
@Override
public Class<DifferentialDriveWheelVoltages> getTypeClass() {
return DifferentialDriveWheelVoltages.class;
}
@Override
public String getTypeString() {
return "struct:DifferentialDriveWheelVoltages";
}
@Override
public int getSize() {
return kSizeDouble * 2;
}
@Override
public String getSchema() {
return "double left;double right";
}
@Override
public DifferentialDriveWheelVoltages unpack(ByteBuffer bb) {
double left = bb.getDouble();
double right = bb.getDouble();
return new DifferentialDriveWheelVoltages(left, right);
}
@Override
public void pack(ByteBuffer bb, DifferentialDriveWheelVoltages value) {
bb.putDouble(value.left);
bb.putDouble(value.right);
}
}

View File

@@ -0,0 +1,48 @@
// 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.controller.struct;
import edu.wpi.first.math.controller.ElevatorFeedforward;
import edu.wpi.first.util.struct.Struct;
import java.nio.ByteBuffer;
public class ElevatorFeedforwardStruct implements Struct<ElevatorFeedforward> {
@Override
public Class<ElevatorFeedforward> getTypeClass() {
return ElevatorFeedforward.class;
}
@Override
public String getTypeString() {
return "struct:ElevatorFeedforward";
}
@Override
public int getSize() {
return kSizeDouble * 4;
}
@Override
public String getSchema() {
return "double ks;double kg;double kv;double ka";
}
@Override
public ElevatorFeedforward unpack(ByteBuffer bb) {
double ks = bb.getDouble();
double kg = bb.getDouble();
double kv = bb.getDouble();
double ka = bb.getDouble();
return new ElevatorFeedforward(ks, kg, kv, ka);
}
@Override
public void pack(ByteBuffer bb, ElevatorFeedforward value) {
bb.putDouble(value.ks);
bb.putDouble(value.kg);
bb.putDouble(value.kv);
bb.putDouble(value.ka);
}
}