mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
[examples] Make swerve examples multiply desired module speeds by cosine of heading error (#5758)
Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
@@ -109,9 +109,15 @@ public class SwerveModule {
|
||||
* @param desiredState Desired state with speed and angle.
|
||||
*/
|
||||
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, new Rotation2d(m_turningEncoder.getDistance()));
|
||||
SwerveModuleState state = SwerveModuleState.optimize(desiredState, 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();
|
||||
|
||||
// Calculate the drive output from the drive PID controller.
|
||||
final double driveOutput =
|
||||
|
||||
@@ -105,9 +105,15 @@ public class SwerveModule {
|
||||
* @param desiredState Desired state with speed and angle.
|
||||
*/
|
||||
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, new Rotation2d(m_turningEncoder.getDistance()));
|
||||
SwerveModuleState state = SwerveModuleState.optimize(desiredState, 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();
|
||||
|
||||
// Calculate the drive output from the drive PID controller.
|
||||
final double driveOutput =
|
||||
|
||||
@@ -109,9 +109,15 @@ public class SwerveModule {
|
||||
* @param desiredState Desired state with speed and angle.
|
||||
*/
|
||||
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, new Rotation2d(m_turningEncoder.getDistance()));
|
||||
SwerveModuleState state = SwerveModuleState.optimize(desiredState, 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();
|
||||
|
||||
// Calculate the drive output from the drive PID controller.
|
||||
final double driveOutput =
|
||||
|
||||
Reference in New Issue
Block a user