Our robot drives as intended when the target angle is less than 90 degrees. When near the 90 degree mark (when the optimization would kick in) the wheels jitter rapidly a couple times before righting themselves. Typically, only one or two jitter at a time. Removing the optimize function removes the jitters (but is obviously not ideal). We’ve tried messing with the PID values, and while the problem is less pronounced at very low speeds, it is still present.
We are using a Through Bore Encoder that is plugged into the top of the motor controller. Any help in resolving this issue would be greatly appreciated.
Swerve Module:
public void setDesiredState(SwerveModuleState desiredState) {
// Apply chassis angular offset to the desired state.
SwerveModuleState correctedDesiredState = new SwerveModuleState();
correctedDesiredState.speedMetersPerSecond = desiredState.speedMetersPerSecond;
correctedDesiredState.angle = desiredState.angle.plus(Rotation2d.fromRadians(m_chassisAngularOffset));
// Optimize the reference state to avoid spinning further than 90 degrees.
SwerveModuleState optimizedDesiredState = SwerveModuleState.optimize(correctedDesiredState,
new Rotation2d(m_turningEncoder.getPosition()));
System.out.println("Desired State - " + optimizedDesiredState);
// Command driving and turning SPARKS MAX towards their respective setpoints.
m_drivingPIDController.setReference(optimizedDesiredState.speedMetersPerSecond, CANSparkMax.ControlType.kVelocity);
m_turningPIDController.setReference(optimizedDesiredState.angle.getRadians(), CANSparkMax.ControlType.kPosition);
m_desiredState = desiredState;
}
Constants:
public static final double kDrivingEncoderPositionFactor = (WHEEL_DIAMETER_METERS * Math.PI)
/ DRIVING_MOTOR_REDUCTION; // meters
public static final double kDrivingEncoderVelocityFactor = ((WHEEL_DIAMETER_METERS * Math.PI)
/ DRIVING_MOTOR_REDUCTION) / 60.0; // meters per second
public static final double kTurningEncoderPositionFactor = (2 * Math.PI); // radians
public static final double kTurningEncoderVelocityFactor = (2 * Math.PI) / 60.0; // radians per second
public static final double kTurningEncoderPositionPIDMinInput = 0; // radians
public static final double kTurningEncoderPositionPIDMaxInput = 2 * Math.PI; // radians
public static final double P_MODULE_TURNING_CONTROLLER = 0.1;
public static final double P_MODULE_DRIVE_CONTROLLER = 0.2;