[wpimath] Replace Speeds with Velocities (#8479)

I left "free speed" alone since that's the technical term for it. In
general, velocity is a vector quantity, and speed is a magnitude (i.e.,
a strictly positive value).

This PR also replaces the speed verbiage in MotorController with duty
cycle.

Fixes #8423.
This commit is contained in:
Tyler Veness
2026-03-06 14:19:15 -08:00
committed by GitHub
parent 1e39f39128
commit 9bd9656871
594 changed files with 8073 additions and 7875 deletions

View File

@@ -26,7 +26,7 @@ class Robot : public wpi::TimedRobot {
rightLeader.AddFollower(rightFollower);
}
void AutonomousPeriodic() override {
// Drives forward at half speed until the robot has moved 5 feet, then
// Drives forward at half velocity until the robot has moved 5 feet, then
// stops:
if (m_encoder.GetDistance() < 5) {
drive.TankDrive(0.5, 0.5);
@@ -43,8 +43,9 @@ class Robot : public wpi::TimedRobot {
wpi::Spark leftFollower{1};
wpi::Spark rightLeader{2};
wpi::Spark rightFollower{3};
wpi::DifferentialDrive drive{[&](double output) { leftLeader.Set(output); },
[&](double output) { rightLeader.Set(output); }};
wpi::DifferentialDrive drive{
[&](double output) { leftLeader.SetDutyCycle(output); },
[&](double output) { rightLeader.SetDutyCycle(output); }};
};
#ifndef RUNNING_WPILIB_TESTS

View File

@@ -14,12 +14,12 @@
class Robot : public wpi::TimedRobot {
public:
void AutonomousPeriodic() override {
// Runs the motor backwards at half speed until the limit switch is pressed
// then turn off the motor and reset the encoder
// Runs the motor backwards at half velocity until the limit switch is
// pressed then turn off the motor and reset the encoder
if (!m_limit.Get()) {
m_spark.Set(-0.5);
m_spark.SetDutyCycle(-0.5);
} else {
m_spark.Set(0);
m_spark.SetDutyCycle(0);
m_encoder.Reset();
}
}

View File

@@ -6,7 +6,6 @@
#include "wpi/framework/TimedRobot.hpp"
#include "wpi/hardware/discrete/DigitalInput.hpp"
#include "wpi/hardware/motor/PWMVictorSPX.hpp"
#include "wpi/hardware/rotation/Encoder.hpp"
/**
* Limit Switch snippets for frc-docs.
@@ -14,24 +13,25 @@
*/
class Robot : public wpi::TimedRobot {
public:
void TeleopPeriodic() override { SetMotorSpeed(m_joystick.GetRawAxis(2)); }
void SetMotorSpeed(double speed) {
if (speed > 0) {
void TeleopPeriodic() override { SetMotorVelocity(m_joystick.GetRawAxis(2)); }
void SetMotorVelocity(double velocity) {
if (velocity > 0) {
if (m_toplimitSwitch.Get()) {
// We are going up and top limit is tripped so stop
m_motor.Set(0);
m_motor.SetDutyCycle(0);
} else {
// We are going up but top limit is not tripped so go at commanded speed
m_motor.Set(speed);
// We are going up but top limit is not tripped so go at commanded
// velocity
m_motor.SetDutyCycle(velocity);
}
} else {
if (m_bottomlimitSwitch.Get()) {
// We are going down and bottom limit is tripped so stop
m_motor.Set(0);
m_motor.SetDutyCycle(0);
} else {
// We are going down but bottom limit is not tripped so go at commanded
// speed
m_motor.Set(speed);
// velocity
m_motor.SetDutyCycle(velocity);
}
}
}

View File

@@ -25,10 +25,11 @@ class Robot : public wpi::TimedRobot {
void GoToPosition(wpi::units::meter_t goalPosition) {
auto pidVal = m_controller.Calculate(
wpi::units::meter_t{m_encoder.GetDistance()}, goalPosition);
m_motor.SetVoltage(wpi::units::volt_t{pidVal} +
m_feedforward.Calculate(
m_lastSpeed, m_controller.GetSetpoint().velocity));
m_lastSpeed = m_controller.GetSetpoint().velocity;
m_motor.SetVoltage(
wpi::units::volt_t{pidVal} +
m_feedforward.Calculate(m_lastVelocity,
m_controller.GetSetpoint().velocity));
m_lastVelocity = m_controller.GetSetpoint().velocity;
}
void TeleopPeriodic() override {
@@ -44,7 +45,7 @@ class Robot : public wpi::TimedRobot {
wpi::Encoder m_encoder{0, 1};
wpi::PWMSparkMax m_motor{0};
wpi::units::meters_per_second_t m_lastSpeed = 0_mps;
wpi::units::meters_per_second_t m_lastVelocity = 0_mps;
};
#ifndef RUNNING_WPILIB_TESTS