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

[examples] Make swerve examples multiply desired module speeds by cosine of heading error #5758

Merged
merged 8 commits into from
Oct 13, 2023
Merged
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 @@ -48,9 +48,17 @@ frc::SwerveModulePosition SwerveModule::GetPosition() const {

void SwerveModule::SetDesiredState(
const frc::SwerveModuleState& referenceState) {
frc::Rotation2d encoderRotation{
units::radian_t{m_turningEncoder.GetDistance()}};

// Optimize the reference state to avoid spinning further than 90 degrees
const auto state = frc::SwerveModuleState::Optimize(
referenceState, units::radian_t{m_turningEncoder.GetDistance()});
auto state =
frc::SwerveModuleState::Optimize(referenceState, 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.speed *= (state.angle - encoderRotation).Cos();

// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ frc::SwerveModulePosition SwerveModule::GetPosition() {

void SwerveModule::SetDesiredState(
const frc::SwerveModuleState& referenceState) {
frc::Rotation2d encoderRotation{
units::radian_t{m_turningEncoder.GetDistance()}};

// Optimize the reference state to avoid spinning further than 90 degrees
const auto state = frc::SwerveModuleState::Optimize(
referenceState, units::radian_t{m_turningEncoder.GetDistance()});
auto state =
frc::SwerveModuleState::Optimize(referenceState, 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.speed *= (state.angle - encoderRotation).Cos();

// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ frc::SwerveModulePosition SwerveModule::GetPosition() const {

void SwerveModule::SetDesiredState(
const frc::SwerveModuleState& referenceState) {
frc::Rotation2d encoderRotation{
units::radian_t{m_turningEncoder.GetDistance()}};

// Optimize the reference state to avoid spinning further than 90 degrees
const auto state = frc::SwerveModuleState::Optimize(
referenceState, units::radian_t{m_turningEncoder.GetDistance()});
auto state =
frc::SwerveModuleState::Optimize(referenceState, 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.speed *= (state.angle - encoderRotation).Cos();

// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ public SwerveModulePosition getPosition() {
* @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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,15 @@ public SwerveModulePosition getPosition() {
* @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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ public SwerveModulePosition getPosition() {
* @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 =
Expand Down
Loading