mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-23 01:21:42 +00:00
[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:
@@ -4,6 +4,8 @@
|
||||
|
||||
package edu.wpi.first.math.system.plant;
|
||||
|
||||
import edu.wpi.first.math.system.plant.proto.DCMotorProto;
|
||||
import edu.wpi.first.math.system.plant.struct.DCMotorStruct;
|
||||
import edu.wpi.first.math.util.Units;
|
||||
|
||||
/** Holds the constants for a DC motor. */
|
||||
@@ -17,6 +19,9 @@ public class DCMotor {
|
||||
public final double KvRadPerSecPerVolt;
|
||||
public final double KtNMPerAmp;
|
||||
|
||||
public static final DCMotorProto proto = new DCMotorProto();
|
||||
public static final DCMotorStruct struct = new DCMotorStruct();
|
||||
|
||||
/**
|
||||
* Constructs a DC motor.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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.system.plant.proto;
|
||||
|
||||
import edu.wpi.first.math.proto.Plant.ProtobufDCMotor;
|
||||
import edu.wpi.first.math.system.plant.DCMotor;
|
||||
import edu.wpi.first.util.protobuf.Protobuf;
|
||||
import us.hebi.quickbuf.Descriptors.Descriptor;
|
||||
|
||||
public class DCMotorProto implements Protobuf<DCMotor, ProtobufDCMotor> {
|
||||
@Override
|
||||
public Class<DCMotor> getTypeClass() {
|
||||
return DCMotor.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Descriptor getDescriptor() {
|
||||
return ProtobufDCMotor.getDescriptor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProtobufDCMotor createMessage() {
|
||||
return ProtobufDCMotor.newInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DCMotor unpack(ProtobufDCMotor msg) {
|
||||
return new DCMotor(
|
||||
msg.getNominalVoltage(),
|
||||
msg.getStallTorque(),
|
||||
msg.getStallCurrent(),
|
||||
msg.getFreeCurrent(),
|
||||
msg.getFreeSpeed(),
|
||||
1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(ProtobufDCMotor msg, DCMotor value) {
|
||||
msg.setNominalVoltage(value.nominalVoltageVolts);
|
||||
msg.setStallTorque(value.stallTorqueNewtonMeters);
|
||||
msg.setStallCurrent(value.stallCurrentAmps);
|
||||
msg.setFreeCurrent(value.freeCurrentAmps);
|
||||
msg.setFreeSpeed(value.freeSpeedRadPerSec);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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.system.plant.struct;
|
||||
|
||||
import edu.wpi.first.math.system.plant.DCMotor;
|
||||
import edu.wpi.first.util.struct.Struct;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class DCMotorStruct implements Struct<DCMotor> {
|
||||
@Override
|
||||
public Class<DCMotor> getTypeClass() {
|
||||
return DCMotor.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeString() {
|
||||
return "struct:DCMotor";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
return kSizeDouble * 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSchema() {
|
||||
return "double nominal_voltage;double stall_torque;double stall_current;"
|
||||
+ "double free_current;double free_speed";
|
||||
}
|
||||
|
||||
@Override
|
||||
public DCMotor unpack(ByteBuffer bb) {
|
||||
double nominalVoltage = bb.getDouble();
|
||||
double stallTorque = bb.getDouble();
|
||||
double stallCurrent = bb.getDouble();
|
||||
double freeCurrent = bb.getDouble();
|
||||
double freeSpeed = bb.getDouble();
|
||||
return new DCMotor(nominalVoltage, stallTorque, stallCurrent, freeCurrent, freeSpeed, 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pack(ByteBuffer bb, DCMotor value) {
|
||||
bb.putDouble(value.nominalVoltageVolts);
|
||||
bb.putDouble(value.stallTorqueNewtonMeters);
|
||||
bb.putDouble(value.stallCurrentAmps);
|
||||
bb.putDouble(value.freeCurrentAmps);
|
||||
bb.putDouble(value.freeSpeedRadPerSec);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user