Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpimath] Add cosineScale and instance optimize to SwerveModuleState #7113

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,24 @@ public void setDesiredState(SwerveModuleState desiredState) {
var encoderRotation = new Rotation2d(m_turningEncoder.getDistance());

// Optimize the reference state to avoid spinning further than 90 degrees
SwerveModuleState state = SwerveModuleState.optimize(desiredState, encoderRotation);
desiredState.optimize(encoderRotation);

// Scale speed by cosine of angle error. This scales down movement perpendicular to the desired
// direction of travel that can occur when modules change directions. This results in smoother
// driving.
state.speedMetersPerSecond *= state.angle.minus(encoderRotation).getCos();
desiredState.cosineScale(encoderRotation);

// Calculate the drive output from the drive PID controller.

final double driveOutput =
m_drivePIDController.calculate(m_driveEncoder.getRate(), state.speedMetersPerSecond);
m_drivePIDController.calculate(m_driveEncoder.getRate(), desiredState.speedMetersPerSecond);

final double driveFeedforward =
m_driveFeedforward.calculate(MetersPerSecond.of(state.speedMetersPerSecond)).in(Volts);
m_driveFeedforward.calculate(MetersPerSecond.of(desiredState.speedMetersPerSecond)).in(Volts);

// Calculate the turning motor output from the turning PID controller.
final double turnOutput =
m_turningPIDController.calculate(m_turningEncoder.getDistance(), state.angle.getRadians());
m_turningPIDController.calculate(m_turningEncoder.getDistance(), desiredState.angle.getRadians());

final double turnFeedforward =
m_turnFeedforward
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ public void setDesiredState(SwerveModuleState desiredState) {
var encoderRotation = new Rotation2d(m_turningEncoder.getDistance());

// Optimize the reference state to avoid spinning further than 90 degrees
SwerveModuleState state = SwerveModuleState.optimize(desiredState, encoderRotation);
desiredState.optimize(encoderRotation);

// Scale speed by cosine of angle error. This scales down movement perpendicular to the desired
// direction of travel that can occur when modules change directions. This results in smoother
// driving.
state.speedMetersPerSecond *= state.angle.minus(encoderRotation).getCos();
desiredState.cosineScale(encoderRotation);

// Calculate the drive output from the drive PID controller.
final double driveOutput =
m_drivePIDController.calculate(m_driveEncoder.getRate(), state.speedMetersPerSecond);
m_drivePIDController.calculate(m_driveEncoder.getRate(), desiredState.speedMetersPerSecond);

// Calculate the turning motor output from the turning PID controller.
final double turnOutput =
m_turningPIDController.calculate(m_turningEncoder.getDistance(), state.angle.getRadians());
m_turningPIDController.calculate(m_turningEncoder.getDistance(), desiredState.angle.getRadians());

// Calculate the turning motor output from the turning PID controller.
m_driveMotor.set(driveOutput);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,23 @@ public void setDesiredState(SwerveModuleState desiredState) {
var encoderRotation = new Rotation2d(m_turningEncoder.getDistance());

// Optimize the reference state to avoid spinning further than 90 degrees
SwerveModuleState state = SwerveModuleState.optimize(desiredState, encoderRotation);
desiredState.optimize(encoderRotation);

// Scale speed by cosine of angle error. This scales down movement perpendicular to the desired
// direction of travel that can occur when modules change directions. This results in smoother
// driving.
state.speedMetersPerSecond *= state.angle.minus(encoderRotation).getCos();
desiredState.cosineScale(encoderRotation);

// Calculate the drive output from the drive PID controller.
final double driveOutput =
m_drivePIDController.calculate(m_driveEncoder.getRate(), state.speedMetersPerSecond);
m_drivePIDController.calculate(m_driveEncoder.getRate(), desiredState.speedMetersPerSecond);

final double driveFeedforward =
m_driveFeedforward.calculate(MetersPerSecond.of(state.speedMetersPerSecond)).in(Volts);
m_driveFeedforward.calculate(MetersPerSecond.of(desiredState.speedMetersPerSecond)).in(Volts);

// Calculate the turning motor output from the turning PID controller.
final double turnOutput =
m_turningPIDController.calculate(m_turningEncoder.getDistance(), state.angle.getRadians());
m_turningPIDController.calculate(m_turningEncoder.getDistance(), desiredState.angle.getRadians());

final double turnFeedforward =
m_turnFeedforward
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ public class SwerveModuleState
public static final SwerveModuleStateStruct struct = new SwerveModuleStateStruct();

/** Constructs a SwerveModuleState with zeros for speed and angle. */
public SwerveModuleState() {}
public SwerveModuleState() {
}

/**
* Constructs a SwerveModuleState.
*
* @param speedMetersPerSecond The speed of the wheel of the module.
* @param angle The angle of the module.
* @param angle The angle of the module.
*/
public SwerveModuleState(double speedMetersPerSecond, Rotation2d angle) {
this.speedMetersPerSecond = speedMetersPerSecond;
Expand Down Expand Up @@ -66,7 +67,8 @@ public int hashCode() {
}

/**
* Compares two swerve module states. One swerve module is "greater" than the other if its speed
* Compares two swerve module states. One swerve module is "greater" than the
* other if its speed
* is higher than the other.
*
* @param other The other swerve module.
Expand All @@ -84,14 +86,50 @@ public String toString() {
}

/**
* Minimize the change in heading the desired swerve module state would require by potentially
* reversing the direction the wheel spins. If this is used with the PIDController class's
* continuous input functionality, the furthest a wheel will ever rotate is 90 degrees.
* Minimize the change in heading the desired swerve module state would require
* by potentially
* reversing the direction the wheel spins. If this is used with the
* PIDController class's
* continuous input functionality, the furthest a wheel will ever rotate is 90
* degrees.
*
* @param currentAngle The current module angle.
*/
public void optimize(Rotation2d currentAngle) {
var delta = angle.minus(currentAngle);
if (Math.abs(delta.getDegrees()) > 90.0) {
speedMetersPerSecond *= -1;
angle = angle.rotateBy(Rotation2d.kPi);
}
}

/**
* Scale speed by cosine of angle error. This scales down movement perpendicular
* to the desired
* direction of travel that can occur when modules change directions. This
* results in smoother
* driving.
*
* @param currentAngle The current module angle.
*/
public void cosineScale(Rotation2d currentAngle) {
speedMetersPerSecond *= angle.minus(currentAngle).getCos();
}

/**
* Minimize the change in heading the desired swerve module state would require
* by potentially
* reversing the direction the wheel spins. If this is used with the
* PIDController class's
* continuous input functionality, the furthest a wheel will ever rotate is 90
* degrees.
*
* @param desiredState The desired state.
* @param currentAngle The current module angle.
* @return Optimized swerve module state.
* @deprecated use the optimize instance method instead.
*/
@Deprecated
public static SwerveModuleState optimize(
SwerveModuleState desiredState, Rotation2d currentAngle) {
var delta = desiredState.angle.minus(currentAngle);
Expand Down
Loading