[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

@@ -4,22 +4,22 @@
#include "Drivetrain.hpp"
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::UpdateOdometry() {

View File

@@ -15,19 +15,20 @@ class Robot : public wpi::TimedRobot {
}
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);
}
private:
@@ -35,7 +36,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

@@ -45,13 +45,14 @@ class Drivetrain {
m_rightEncoder.Reset();
}
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
void SetSpeeds(const wpi::math::DifferentialDriveWheelSpeeds& speeds);
void Drive(wpi::units::meters_per_second_t xSpeed,
void SetVelocities(
const wpi::math::DifferentialDriveWheelVelocities& velocities);
void Drive(wpi::units::meters_per_second_t xVelocity,
wpi::units::radians_per_second_t rot);
void UpdateOdometry();