mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-05 03:21:42 +00:00
[wpimath] Add optimize() to SwerveModuleState (#3065)
Co-authored-by: Tyler Veness <calcmogul@gmail.com>
This commit is contained in:
@@ -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>());
|
||||
|
||||
@@ -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>());
|
||||
|
||||
@@ -27,7 +27,7 @@ class SwerveModule {
|
||||
|
||||
frc::SwerveModuleState GetState();
|
||||
|
||||
void SetDesiredState(frc::SwerveModuleState& state);
|
||||
void SetDesiredState(const frc::SwerveModuleState& state);
|
||||
|
||||
void ResetEncoders();
|
||||
|
||||
|
||||
@@ -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>());
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user