Hello,
I’m having trouble with my swerve’s odometry. Its value keeps approaching zero, and from what I can tell, it’s because our SwerveModulePosition getPosition()
method isn’t working right. The value for distance that it is returning also keeps approaching zero (When it’s driving, it has a value, maybe its speed? but when you stop it goes back to zero).
Here is the code for getting the swerve module position:
public SwerveModulePosition getPosition() {
return new SwerveModulePosition(
ModuleConstants.drivetoMetersPerSecond * driveMotor.getSelectedSensorVelocity(),
Rotation2d.fromDegrees(getCANCoderABS())
);
}
Code for ModuleConstants.drivetoMetersPerSecond
public static final double wheelDiameterMeters = Units.inchesToMeters(4); // 4 inches
public static final double wheelCircumferenceMeters =
wheelDiameterMeters * Math.PI; // C = D * pi
public static final double drivetoMetersPerSecond =
(10 * wheelCircumferenceMeters) / (driveGearRatio * driveFXEncoderCPR);