Skip to content

Commit

Permalink
added c++ CosineScaling method
Browse files Browse the repository at this point in the history
  • Loading branch information
narmstro2020 committed Sep 21, 2024
1 parent 2f476e6 commit 3df9817
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void SwerveModule::SetDesiredState(
// 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.
referenceState.speed *= (referenceState.angle - encoderRotation).Cos();
referenceState.CosineScale(encoderRotation);

// 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 @@ -64,7 +64,7 @@ void SwerveModule::SetDesiredState(
// 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.
referenceState.speed *= (referenceState.angle - encoderRotation).Cos();
referenceState.CosineScale(encoderRotation);

// 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 @@ -57,7 +57,7 @@ void SwerveModule::SetDesiredState(
// 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.
referenceState.speed *= (referenceState.angle - encoderRotation).Cos();
referenceState.CosineScale(encoderRotation);

// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
Expand Down
12 changes: 12 additions & 0 deletions wpimath/src/main/native/include/frc/kinematics/SwerveModuleState.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ struct WPILIB_DLLEXPORT SwerveModuleState {
[[deprecated("Use instance method instead.")]]
static SwerveModuleState Optimize(const SwerveModuleState& desiredState,
const Rotation2d& currentAngle);

/**
* Scales 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.
*
* @param currentAngle The current module angle.
*/
void CosineScale(const Rotation2d& currentAngle) {
speed *= (angle - currentAngle).Cos();
}

};
} // namespace frc

Expand Down

0 comments on commit 3df9817

Please sign in to comment.