[wpimath] Java: add static instantiations of common rotations (#6563)

C++ doesn't need this because it supports value types, which are much
cheaper to construct. constexpr is also available to make construction
zero-cost.
This commit is contained in:
Tyler Veness
2024-05-03 12:39:35 -07:00
committed by GitHub
parent 9c7120e6bf
commit e172aa66f7
75 changed files with 499 additions and 429 deletions

View File

@@ -109,7 +109,7 @@ public class ChassisSpeeds implements ProtobufSerializable, StructSerializable {
// Find the chassis translation/rotation deltas in the robot frame that move the robot from its
// current pose to the desired pose
var twist = new Pose2d().log(desiredDeltaPose);
var twist = Pose2d.kZero.log(desiredDeltaPose);
// Turn the chassis translation/rotation deltas into average velocities
return new ChassisSpeeds(twist.dx / dtSeconds, twist.dy / dtSeconds, twist.dtheta / dtSeconds);

View File

@@ -70,7 +70,7 @@ public class DifferentialDriveOdometry extends Odometry<DifferentialDriveWheelPo
*/
public DifferentialDriveOdometry(
Rotation2d gyroAngle, double leftDistanceMeters, double rightDistanceMeters) {
this(gyroAngle, leftDistanceMeters, rightDistanceMeters, new Pose2d());
this(gyroAngle, leftDistanceMeters, rightDistanceMeters, Pose2d.kZero);
}
/**
@@ -82,7 +82,7 @@ public class DifferentialDriveOdometry extends Odometry<DifferentialDriveWheelPo
*/
public DifferentialDriveOdometry(
Rotation2d gyroAngle, Measure<Distance> leftDistance, Measure<Distance> rightDistance) {
this(gyroAngle, leftDistance, rightDistance, new Pose2d());
this(gyroAngle, leftDistance, rightDistance, Pose2d.kZero);
}
/**

View File

@@ -46,7 +46,7 @@ public class MecanumDriveKinematics
private final Translation2d m_rearLeftWheelMeters;
private final Translation2d m_rearRightWheelMeters;
private Translation2d m_prevCoR = new Translation2d();
private Translation2d m_prevCoR = Translation2d.kZero;
/** MecanumDriveKinematics protobuf for serialization. */
public static final MecanumDriveKinematicsProto proto = new MecanumDriveKinematicsProto();
@@ -140,7 +140,7 @@ public class MecanumDriveKinematics
*/
@Override
public MecanumDriveWheelSpeeds toWheelSpeeds(ChassisSpeeds chassisSpeeds) {
return toWheelSpeeds(chassisSpeeds, new Translation2d());
return toWheelSpeeds(chassisSpeeds, Translation2d.kZero);
}
/**

View File

@@ -45,6 +45,6 @@ public class MecanumDriveOdometry extends Odometry<MecanumDriveWheelPositions> {
MecanumDriveKinematics kinematics,
Rotation2d gyroAngle,
MecanumDriveWheelPositions wheelPositions) {
this(kinematics, gyroAngle, wheelPositions, new Pose2d());
this(kinematics, gyroAngle, wheelPositions, Pose2d.kZero);
}
}

View File

@@ -65,7 +65,7 @@ public class SwerveDriveKinematics
private final int m_numModules;
private final Translation2d[] m_modules;
private Rotation2d[] m_moduleHeadings;
private Translation2d m_prevCoR = new Translation2d();
private Translation2d m_prevCoR = Translation2d.kZero;
/**
* Constructs a swerve drive kinematics object. This takes in a variable number of module
@@ -84,7 +84,7 @@ public class SwerveDriveKinematics
m_numModules = moduleTranslationsMeters.length;
m_modules = Arrays.copyOf(moduleTranslationsMeters, m_numModules);
m_moduleHeadings = new Rotation2d[m_numModules];
Arrays.fill(m_moduleHeadings, new Rotation2d());
Arrays.fill(m_moduleHeadings, Rotation2d.kZero);
m_inverseKinematics = new SimpleMatrix(m_numModules * 2, 3);
for (int i = 0; i < m_numModules; i++) {
@@ -196,7 +196,7 @@ public class SwerveDriveKinematics
* @return An array containing the module states.
*/
public SwerveModuleState[] toSwerveModuleStates(ChassisSpeeds chassisSpeeds) {
return toSwerveModuleStates(chassisSpeeds, new Translation2d());
return toSwerveModuleStates(chassisSpeeds, Translation2d.kZero);
}
@Override

View File

@@ -51,7 +51,7 @@ public class SwerveDriveOdometry extends Odometry<SwerveDriveWheelPositions> {
SwerveDriveKinematics kinematics,
Rotation2d gyroAngle,
SwerveModulePosition[] modulePositions) {
this(kinematics, gyroAngle, modulePositions, new Pose2d());
this(kinematics, gyroAngle, modulePositions, Pose2d.kZero);
}
/**

View File

@@ -27,7 +27,7 @@ public class SwerveModulePosition
public double distanceMeters;
/** Angle of the module. */
public Rotation2d angle = Rotation2d.fromDegrees(0);
public Rotation2d angle = Rotation2d.kZero;
/** SwerveModulePosition protobuf for serialization. */
public static final SwerveModulePositionProto proto = new SwerveModulePositionProto();

View File

@@ -23,7 +23,7 @@ public class SwerveModuleState
public double speedMetersPerSecond;
/** Angle of the module. */
public Rotation2d angle = Rotation2d.fromDegrees(0);
public Rotation2d angle = Rotation2d.kZero;
/** SwerveModuleState protobuf for serialization. */
public static final SwerveModuleStateProto proto = new SwerveModuleStateProto();
@@ -102,8 +102,7 @@ public class SwerveModuleState
var delta = desiredState.angle.minus(currentAngle);
if (Math.abs(delta.getDegrees()) > 90.0) {
return new SwerveModuleState(
-desiredState.speedMetersPerSecond,
desiredState.angle.rotateBy(Rotation2d.fromDegrees(180.0)));
-desiredState.speedMetersPerSecond, desiredState.angle.rotateBy(Rotation2d.kPi));
} else {
return new SwerveModuleState(desiredState.speedMetersPerSecond, desiredState.angle);
}