[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

@@ -37,22 +37,22 @@ Drivetrain::Drivetrain() {
wpi::SmartDashboard::PutData("Approximation", &m_fieldApproximation);
}
void Drivetrain::SetSpeeds(
const wpi::math::DifferentialDriveWheelSpeeds& speeds) {
const auto leftFeedforward = m_feedforward.Calculate(speeds.left);
const auto rightFeedforward = m_feedforward.Calculate(speeds.right);
void Drivetrain::SetVelocities(
const wpi::math::DifferentialDriveWheelVelocities& velocities) {
const auto leftFeedforward = m_feedforward.Calculate(velocities.left);
const auto rightFeedforward = m_feedforward.Calculate(velocities.right);
const double leftOutput = m_leftPIDController.Calculate(
m_leftEncoder.GetRate(), speeds.left.value());
m_leftEncoder.GetRate(), velocities.left.value());
const double rightOutput = m_rightPIDController.Calculate(
m_rightEncoder.GetRate(), speeds.right.value());
m_rightEncoder.GetRate(), velocities.right.value());
m_leftLeader.SetVoltage(wpi::units::volt_t{leftOutput} + leftFeedforward);
m_rightLeader.SetVoltage(wpi::units::volt_t{rightOutput} + rightFeedforward);
}
void Drivetrain::Drive(wpi::units::meters_per_second_t xSpeed,
void Drivetrain::Drive(wpi::units::meters_per_second_t xVelocity,
wpi::units::radians_per_second_t rot) {
SetSpeeds(m_kinematics.ToWheelSpeeds({xSpeed, 0_mps, rot}));
SetVelocities(m_kinematics.ToWheelVelocities({xVelocity, 0_mps, rot}));
}
void Drivetrain::PublishCameraToObject(
@@ -122,10 +122,11 @@ void Drivetrain::SimulationPeriodic() {
// To update our simulation, we set motor voltage inputs, update the
// simulation, and write the simulated positions and velocities to our
// simulated encoder and gyro.
m_drivetrainSimulator.SetInputs(wpi::units::volt_t{m_leftLeader.Get()} *
wpi::RobotController::GetInputVoltage(),
wpi::units::volt_t{m_rightLeader.Get()} *
wpi::RobotController::GetInputVoltage());
m_drivetrainSimulator.SetInputs(
wpi::units::volt_t{m_leftLeader.GetDutyCycle()} *
wpi::RobotController::GetInputVoltage(),
wpi::units::volt_t{m_rightLeader.GetDutyCycle()} *
wpi::RobotController::GetInputVoltage());
m_drivetrainSimulator.Update(20_ms);
m_leftEncoderSim.SetDistance(m_drivetrainSimulator.GetLeftPosition().value());

View File

@@ -17,19 +17,20 @@ class Robot : public wpi::TimedRobot {
void RobotPeriodic() override { m_drive.Periodic(); }
void TeleopPeriodic() override {
// Get the x speed. We are inverting this because gamepads return
// Get the x velocity. We are inverting this because gamepads return
// negative values when we push forward.
const auto xSpeed = -m_speedLimiter.Calculate(m_controller.GetLeftY()) *
Drivetrain::kMaxSpeed;
const auto xVelocity =
-m_velocityLimiter.Calculate(m_controller.GetLeftY()) *
Drivetrain::kMaxVelocity;
// Get the rate of angular rotation. We are inverting this because we want a
// positive value when we pull to the left (remember, CCW is positive in
// mathematics). Gamepads return positive values when you pull to
// the right by default.
const auto rot = -m_rotLimiter.Calculate(m_controller.GetRightX()) *
Drivetrain::kMaxAngularSpeed;
Drivetrain::kMaxAngularVelocity;
m_drive.Drive(xSpeed, rot);
m_drive.Drive(xVelocity, rot);
}
void SimulationPeriodic() override { m_drive.SimulationPeriodic(); }
@@ -39,7 +40,7 @@ class Robot : public wpi::TimedRobot {
// Slew rate limiters to make joystick inputs more gentle; 1/3 sec from 0
// to 1.
wpi::math::SlewRateLimiter<wpi::units::scalar> m_speedLimiter{3 / 1_s};
wpi::math::SlewRateLimiter<wpi::units::scalar> m_velocityLimiter{3 / 1_s};
wpi::math::SlewRateLimiter<wpi::units::scalar> m_rotLimiter{3 / 1_s};
Drivetrain m_drive;

View File

@@ -36,24 +36,25 @@ class Drivetrain {
public:
Drivetrain();
static constexpr wpi::units::meters_per_second_t kMaxSpeed =
static constexpr wpi::units::meters_per_second_t kMaxVelocity =
3.0_mps; // 3 meters per second
static constexpr wpi::units::radians_per_second_t kMaxAngularSpeed{
static constexpr wpi::units::radians_per_second_t kMaxAngularVelocity{
std::numbers::pi}; // 1/2 rotation per second
/**
* Sets the desired wheel speeds.
* Sets the desired wheel velocities.
*
* @param speeds The desired wheel speeds.
* @param velocities The desired wheel velocities.
*/
void SetSpeeds(const wpi::math::DifferentialDriveWheelSpeeds& speeds);
void SetVelocities(
const wpi::math::DifferentialDriveWheelVelocities& velocities);
/** Drives the robot with the given linear velocity and angular velocity.
*
* @param xSpeed Linear velocity.
* @param xVelocity Linear velocity.
* @param rot Angular Velocity.
*/
void Drive(wpi::units::meters_per_second_t xSpeed,
void Drive(wpi::units::meters_per_second_t xVelocity,
wpi::units::radians_per_second_t rot);
/**