[wpimath] Add optimize() to SwerveModuleState (#3065)

Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
Matt
2021-01-10 22:49:46 -08:00
committed by GitHub
parent fb99910c23
commit e955037980
9 changed files with 79 additions and 11 deletions

View File

@@ -79,9 +79,13 @@ public class SwerveModule {
/**
* Sets the desired state for the module.
*
* @param state Desired state with speed and angle.
* @param desiredState Desired state with speed and angle.
*/
public void setDesiredState(SwerveModuleState state) {
public void setDesiredState(SwerveModuleState desiredState) {
// Optimize the reference state to avoid spinning further than 90 degrees
SwerveModuleState state =
SwerveModuleState.optimize(desiredState, new Rotation2d(m_turningEncoder.get()));
// Calculate the drive output from the drive PID controller.
final double driveOutput =
m_drivePIDController.calculate(m_driveEncoder.getRate(), state.speedMetersPerSecond);

View File

@@ -87,11 +87,15 @@ public class SwerveModule {
/**
* Sets the desired state for the module.
*
* @param state Desired state with speed and angle.
* @param desiredState Desired state with speed and angle.
*/
public void setDesiredState(SwerveModuleState state) {
public void setDesiredState(SwerveModuleState desiredState) {
// Optimize the reference state to avoid spinning further than 90 degrees
SwerveModuleState state =
SwerveModuleState.optimize(desiredState, new Rotation2d(m_turningEncoder.get()));
// Calculate the drive output from the drive PID controller.
final var driveOutput =
final double driveOutput =
m_drivePIDController.calculate(m_driveEncoder.getRate(), state.speedMetersPerSecond);
// Calculate the turning motor output from the turning PID controller.

View File

@@ -79,9 +79,13 @@ public class SwerveModule {
/**
* Sets the desired state for the module.
*
* @param state Desired state with speed and angle.
* @param desiredState Desired state with speed and angle.
*/
public void setDesiredState(SwerveModuleState state) {
public void setDesiredState(SwerveModuleState desiredState) {
// Optimize the reference state to avoid spinning further than 90 degrees
SwerveModuleState state =
SwerveModuleState.optimize(desiredState, new Rotation2d(m_turningEncoder.get()));
// Calculate the drive output from the drive PID controller.
final double driveOutput =
m_drivePIDController.calculate(m_driveEncoder.getRate(), state.speedMetersPerSecond);