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 4 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,15 +48,22 @@

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(

Check failure on line 55 in wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'frc::SwerveModuleState::Optimize': function does not take 1 arguments
referenceState, units::radian_t{encoderRotation});

Check failure on line 56 in wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'<function-style-cast>': cannot convert from 'initializer list' to 'units::angle::radian_t'
ori-coval marked this conversation as resolved.
Show resolved Hide resolved

// Adjust speed using the cosine of angle error for smoother and more precise
// control
calcmogul marked this conversation as resolved.
Show resolved Hide resolved
state.speed *= (state.angle - encoderRotation).Cos();

Check failure on line 60 in wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'state': cannot be used before it is initialized

// Calculate the drive output from the drive PID controller.
const auto driveOutput = m_drivePIDController.Calculate(
m_driveEncoder.GetRate(), state.speed.value());

const auto driveFeedforward = m_driveFeedforward.Calculate(state.speed);

Check failure on line 66 in wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'driveFeedforward': const object must be initialized

// Calculate the turning motor output from the turning PID controller.
const auto turnOutput = m_turningPIDController.Calculate(
Expand All @@ -66,6 +73,6 @@
m_turningPIDController.GetSetpoint().velocity);

// Set the motor outputs.
m_driveMotor.SetVoltage(units::volt_t{driveOutput} + driveFeedforward);

Check failure on line 76 in wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'driveFeedforward': cannot be used before it is initialized

Check failure on line 76 in wpilibcExamples/src/main/cpp/examples/SwerveBot/cpp/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'void frc::MotorController::SetVoltage(units::voltage::volt_t)': cannot convert argument 1 from 'int' to 'units::voltage::volt_t'
m_turningMotor.SetVoltage(units::volt_t{turnOutput} + turnFeedforward);
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,16 @@

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(

Check failure on line 62 in wpilibcExamples/src/main/cpp/examples/SwerveControllerCommand/cpp/subsystems/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'frc::SwerveModuleState::Optimize': function does not take 1 arguments
referenceState, units::radian_t{encoderRotation});

Check failure on line 63 in wpilibcExamples/src/main/cpp/examples/SwerveControllerCommand/cpp/subsystems/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'<function-style-cast>': cannot convert from 'initializer list' to 'units::angle::radian_t'

// Adjust speed using the cosine of angle error for smoother and more precise
// control
state.speed *= (state.angle - encoderRotation).Cos();

Check failure on line 67 in wpilibcExamples/src/main/cpp/examples/SwerveControllerCommand/cpp/subsystems/SwerveModule.cpp

View workflow job for this annotation

GitHub Actions / Build - Windows

'state': cannot be used before it is initialized

// 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,16 @@ 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, units::radian_t{encoderRotation});

// Adjust speed using the cosine of angle error for smoother and more precise
// control
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.optimize(desiredState, encoderRotation);

// Adjust speed using the cosine of angle error for smoother and more precise control
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.optimize(desiredState, encoderRotation);

// Adjust speed using the cosine of angle error for smoother and more precise control
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.optimize(desiredState, encoderRotation);

// Adjust speed using the cosine of angle error for smoother and more precise control
state.speedMetersPerSecond *=
(state.angle.minus(encoderRotation)).getCos();

// Calculate the drive output from the drive PID controller.
final double driveOutput =
Expand Down
Loading