[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

@@ -32,7 +32,12 @@ frc::SwerveModuleState SwerveModule::GetState() const {
frc::Rotation2d(units::radian_t(m_turningEncoder.Get()))};
}
void SwerveModule::SetDesiredState(const frc::SwerveModuleState& state) {
void SwerveModule::SetDesiredState(
const frc::SwerveModuleState& referenceState) {
// Optimize the reference state to avoid spinning further than 90 degrees
const auto state = frc::SwerveModuleState::Optimize(
referenceState, units::radian_t(m_turningEncoder.Get()));
// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
m_driveEncoder.GetRate(), state.speed.to<double>());

View File

@@ -43,7 +43,12 @@ frc::SwerveModuleState SwerveModule::GetState() {
frc::Rotation2d(units::radian_t(m_turningEncoder.Get()))};
}
void SwerveModule::SetDesiredState(frc::SwerveModuleState& state) {
void SwerveModule::SetDesiredState(
const frc::SwerveModuleState& referenceState) {
// Optimize the reference state to avoid spinning further than 90 degrees
const auto state = frc::SwerveModuleState::Optimize(
referenceState, units::radian_t(m_turningEncoder.Get()));
// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
m_driveEncoder.GetRate(), state.speed.to<double>());

View File

@@ -27,7 +27,7 @@ class SwerveModule {
frc::SwerveModuleState GetState();
void SetDesiredState(frc::SwerveModuleState& state);
void SetDesiredState(const frc::SwerveModuleState& state);
void ResetEncoders();

View File

@@ -32,7 +32,12 @@ frc::SwerveModuleState SwerveModule::GetState() const {
frc::Rotation2d(units::radian_t(m_turningEncoder.Get()))};
}
void SwerveModule::SetDesiredState(const frc::SwerveModuleState& state) {
void SwerveModule::SetDesiredState(
const frc::SwerveModuleState& referenceState) {
// Optimize the reference state to avoid spinning further than 90 degrees
const auto state = frc::SwerveModuleState::Optimize(
referenceState, units::radian_t(m_turningEncoder.Get()));
// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
m_driveEncoder.GetRate(), state.speed.to<double>());

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);

View File

@@ -48,4 +48,24 @@ public class SwerveModuleState implements Comparable<SwerveModuleState> {
return String.format(
"SwerveModuleState(Speed: %.2f m/s, Angle: %s)", speedMetersPerSecond, angle);
}
/**
* 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.
*/
public static SwerveModuleState optimize(
SwerveModuleState desiredState, Rotation2d currentAngle) {
var delta = desiredState.angle.minus(currentAngle);
if (Math.abs(delta.getDegrees()) > 90.0) {
return new SwerveModuleState(
-desiredState.speedMetersPerSecond,
desiredState.angle.rotateBy(Rotation2d.fromDegrees(180.0)));
} else {
return new SwerveModuleState(desiredState.speedMetersPerSecond, desiredState.angle);
}
}
}

View File

@@ -5,6 +5,8 @@
#pragma once
#include "frc/geometry/Rotation2d.h"
#include "units/angle.h"
#include "units/math.h"
#include "units/velocity.h"
namespace frc {
@@ -21,5 +23,24 @@ struct SwerveModuleState {
* Angle of the module.
*/
Rotation2d angle;
/**
* 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.
*/
static SwerveModuleState Optimize(const SwerveModuleState& desiredState,
const Rotation2d& currentAngle) {
auto delta = desiredState.angle - currentAngle;
if (units::math::abs(delta.Degrees()) > 90_deg) {
return {-desiredState.speed, desiredState.angle + Rotation2d{180_deg}};
} else {
return {desiredState.speed, desiredState.angle};
}
}
};
} // namespace frc