[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

@@ -21,8 +21,9 @@ WPI_IGNORE_DEPRECATED
DifferentialDrive::DifferentialDrive(MotorController& leftMotor,
MotorController& rightMotor)
: DifferentialDrive{[&](double output) { leftMotor.Set(output); },
[&](double output) { rightMotor.Set(output); }} {
: DifferentialDrive{
[&](double output) { leftMotor.SetDutyCycle(output); },
[&](double output) { rightMotor.SetDutyCycle(output); }} {
wpi::util::SendableRegistry::AddChild(this, &leftMotor);
wpi::util::SendableRegistry::AddChild(this, &rightMotor);
}
@@ -37,7 +38,7 @@ DifferentialDrive::DifferentialDrive(std::function<void(double)> leftMotor,
wpi::util::SendableRegistry::Add(this, "DifferentialDrive", instances);
}
void DifferentialDrive::ArcadeDrive(double xSpeed, double zRotation,
void DifferentialDrive::ArcadeDrive(double xVelocity, double zRotation,
bool squareInputs) {
static bool reported = false;
if (!reported) {
@@ -45,10 +46,10 @@ void DifferentialDrive::ArcadeDrive(double xSpeed, double zRotation,
reported = true;
}
xSpeed = wpi::math::ApplyDeadband(xSpeed, m_deadband);
xVelocity = wpi::math::ApplyDeadband(xVelocity, m_deadband);
zRotation = wpi::math::ApplyDeadband(zRotation, m_deadband);
auto [left, right] = ArcadeDriveIK(xSpeed, zRotation, squareInputs);
auto [left, right] = ArcadeDriveIK(xVelocity, zRotation, squareInputs);
m_leftOutput = left * m_maxOutput;
m_rightOutput = right * m_maxOutput;
@@ -59,7 +60,7 @@ void DifferentialDrive::ArcadeDrive(double xSpeed, double zRotation,
Feed();
}
void DifferentialDrive::CurvatureDrive(double xSpeed, double zRotation,
void DifferentialDrive::CurvatureDrive(double xVelocity, double zRotation,
bool allowTurnInPlace) {
static bool reported = false;
if (!reported) {
@@ -67,10 +68,10 @@ void DifferentialDrive::CurvatureDrive(double xSpeed, double zRotation,
reported = true;
}
xSpeed = wpi::math::ApplyDeadband(xSpeed, m_deadband);
xVelocity = wpi::math::ApplyDeadband(xVelocity, m_deadband);
zRotation = wpi::math::ApplyDeadband(zRotation, m_deadband);
auto [left, right] = CurvatureDriveIK(xSpeed, zRotation, allowTurnInPlace);
auto [left, right] = CurvatureDriveIK(xVelocity, zRotation, allowTurnInPlace);
m_leftOutput = left * m_maxOutput;
m_rightOutput = right * m_maxOutput;
@@ -81,7 +82,7 @@ void DifferentialDrive::CurvatureDrive(double xSpeed, double zRotation,
Feed();
}
void DifferentialDrive::TankDrive(double leftSpeed, double rightSpeed,
void DifferentialDrive::TankDrive(double leftVelocity, double rightVelocity,
bool squareInputs) {
static bool reported = false;
if (!reported) {
@@ -89,10 +90,10 @@ void DifferentialDrive::TankDrive(double leftSpeed, double rightSpeed,
reported = true;
}
leftSpeed = wpi::math::ApplyDeadband(leftSpeed, m_deadband);
rightSpeed = wpi::math::ApplyDeadband(rightSpeed, m_deadband);
leftVelocity = wpi::math::ApplyDeadband(leftVelocity, m_deadband);
rightVelocity = wpi::math::ApplyDeadband(rightVelocity, m_deadband);
auto [left, right] = TankDriveIK(leftSpeed, rightSpeed, squareInputs);
auto [left, right] = TankDriveIK(leftVelocity, rightVelocity, squareInputs);
m_leftOutput = left * m_maxOutput;
m_rightOutput = right * m_maxOutput;
@@ -103,74 +104,75 @@ void DifferentialDrive::TankDrive(double leftSpeed, double rightSpeed,
Feed();
}
DifferentialDrive::WheelSpeeds DifferentialDrive::ArcadeDriveIK(
double xSpeed, double zRotation, bool squareInputs) {
xSpeed = std::clamp(xSpeed, -1.0, 1.0);
DifferentialDrive::WheelVelocities DifferentialDrive::ArcadeDriveIK(
double xVelocity, double zRotation, bool squareInputs) {
xVelocity = std::clamp(xVelocity, -1.0, 1.0);
zRotation = std::clamp(zRotation, -1.0, 1.0);
// Square the inputs (while preserving the sign) to increase fine control
// while permitting full power.
if (squareInputs) {
xSpeed = wpi::math::CopyDirectionPow(xSpeed, 2);
xVelocity = wpi::math::CopyDirectionPow(xVelocity, 2);
zRotation = wpi::math::CopyDirectionPow(zRotation, 2);
}
double leftSpeed = xSpeed - zRotation;
double rightSpeed = xSpeed + zRotation;
double leftVelocity = xVelocity - zRotation;
double rightVelocity = xVelocity + zRotation;
// Find the maximum possible value of (throttle + turn) along the vector that
// the joystick is pointing, then desaturate the wheel speeds
double greaterInput = (std::max)(std::abs(xSpeed), std::abs(zRotation));
double lesserInput = (std::min)(std::abs(xSpeed), std::abs(zRotation));
// the joystick is pointing, then desaturate the wheel velocities
double greaterInput = (std::max)(std::abs(xVelocity), std::abs(zRotation));
double lesserInput = (std::min)(std::abs(xVelocity), std::abs(zRotation));
if (greaterInput == 0.0) {
return {0.0, 0.0};
}
double saturatedInput = (greaterInput + lesserInput) / greaterInput;
leftSpeed /= saturatedInput;
rightSpeed /= saturatedInput;
leftVelocity /= saturatedInput;
rightVelocity /= saturatedInput;
return {leftSpeed, rightSpeed};
return {leftVelocity, rightVelocity};
}
DifferentialDrive::WheelSpeeds DifferentialDrive::CurvatureDriveIK(
double xSpeed, double zRotation, bool allowTurnInPlace) {
xSpeed = std::clamp(xSpeed, -1.0, 1.0);
DifferentialDrive::WheelVelocities DifferentialDrive::CurvatureDriveIK(
double xVelocity, double zRotation, bool allowTurnInPlace) {
xVelocity = std::clamp(xVelocity, -1.0, 1.0);
zRotation = std::clamp(zRotation, -1.0, 1.0);
double leftSpeed = 0.0;
double rightSpeed = 0.0;
double leftVelocity = 0.0;
double rightVelocity = 0.0;
if (allowTurnInPlace) {
leftSpeed = xSpeed - zRotation;
rightSpeed = xSpeed + zRotation;
leftVelocity = xVelocity - zRotation;
rightVelocity = xVelocity + zRotation;
} else {
leftSpeed = xSpeed - std::abs(xSpeed) * zRotation;
rightSpeed = xSpeed + std::abs(xSpeed) * zRotation;
leftVelocity = xVelocity - std::abs(xVelocity) * zRotation;
rightVelocity = xVelocity + std::abs(xVelocity) * zRotation;
}
// Desaturate wheel speeds
double maxMagnitude = std::max(std::abs(leftSpeed), std::abs(rightSpeed));
// Desaturate wheel velocities
double maxMagnitude =
std::max(std::abs(leftVelocity), std::abs(rightVelocity));
if (maxMagnitude > 1.0) {
leftSpeed /= maxMagnitude;
rightSpeed /= maxMagnitude;
leftVelocity /= maxMagnitude;
rightVelocity /= maxMagnitude;
}
return {leftSpeed, rightSpeed};
return {leftVelocity, rightVelocity};
}
DifferentialDrive::WheelSpeeds DifferentialDrive::TankDriveIK(
double leftSpeed, double rightSpeed, bool squareInputs) {
leftSpeed = std::clamp(leftSpeed, -1.0, 1.0);
rightSpeed = std::clamp(rightSpeed, -1.0, 1.0);
DifferentialDrive::WheelVelocities DifferentialDrive::TankDriveIK(
double leftVelocity, double rightVelocity, bool squareInputs) {
leftVelocity = std::clamp(leftVelocity, -1.0, 1.0);
rightVelocity = std::clamp(rightVelocity, -1.0, 1.0);
// Square the inputs (while preserving the sign) to increase fine control
// while permitting full power.
if (squareInputs) {
leftSpeed = wpi::math::CopyDirectionPow(leftSpeed, 2);
rightSpeed = wpi::math::CopyDirectionPow(rightSpeed, 2);
leftVelocity = wpi::math::CopyDirectionPow(leftVelocity, 2);
rightVelocity = wpi::math::CopyDirectionPow(rightVelocity, 2);
}
return {leftSpeed, rightSpeed};
return {leftVelocity, rightVelocity};
}
void DifferentialDrive::StopMotor() {
@@ -191,7 +193,7 @@ void DifferentialDrive::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("DifferentialDrive");
builder.SetActuator(true);
builder.AddDoubleProperty(
"Left Motor Speed", [&] { return m_leftOutput; }, m_leftMotor);
"Left Motor Velocity", [&] { return m_leftOutput; }, m_leftMotor);
builder.AddDoubleProperty(
"Right Motor Speed", [&] { return m_rightOutput; }, m_rightMotor);
"Right Motor Velocity", [&] { return m_rightOutput; }, m_rightMotor);
}

View File

@@ -23,10 +23,11 @@ MecanumDrive::MecanumDrive(MotorController& frontLeftMotor,
MotorController& rearLeftMotor,
MotorController& frontRightMotor,
MotorController& rearRightMotor)
: MecanumDrive{[&](double output) { frontLeftMotor.Set(output); },
[&](double output) { rearLeftMotor.Set(output); },
[&](double output) { frontRightMotor.Set(output); },
[&](double output) { rearRightMotor.Set(output); }} {
: MecanumDrive{
[&](double output) { frontLeftMotor.SetDutyCycle(output); },
[&](double output) { rearLeftMotor.SetDutyCycle(output); },
[&](double output) { frontRightMotor.SetDutyCycle(output); },
[&](double output) { rearRightMotor.SetDutyCycle(output); }} {
wpi::util::SendableRegistry::AddChild(this, &frontLeftMotor);
wpi::util::SendableRegistry::AddChild(this, &rearLeftMotor);
wpi::util::SendableRegistry::AddChild(this, &frontRightMotor);
@@ -48,7 +49,7 @@ MecanumDrive::MecanumDrive(std::function<void(double)> frontLeftMotor,
wpi::util::SendableRegistry::Add(this, "MecanumDrive", instances);
}
void MecanumDrive::DriveCartesian(double xSpeed, double ySpeed,
void MecanumDrive::DriveCartesian(double xVelocity, double yVelocity,
double zRotation,
wpi::math::Rotation2d gyroAngle) {
if (!reported) {
@@ -56,11 +57,11 @@ void MecanumDrive::DriveCartesian(double xSpeed, double ySpeed,
reported = true;
}
xSpeed = wpi::math::ApplyDeadband(xSpeed, m_deadband);
ySpeed = wpi::math::ApplyDeadband(ySpeed, m_deadband);
xVelocity = wpi::math::ApplyDeadband(xVelocity, m_deadband);
yVelocity = wpi::math::ApplyDeadband(yVelocity, m_deadband);
auto [frontLeft, frontRight, rearLeft, rearRight] =
DriveCartesianIK(xSpeed, ySpeed, zRotation, gyroAngle);
DriveCartesianIK(xVelocity, yVelocity, zRotation, gyroAngle);
m_frontLeftOutput = frontLeft * m_maxOutput;
m_rearLeftOutput = rearLeft * m_maxOutput;
@@ -100,28 +101,31 @@ void MecanumDrive::StopMotor() {
Feed();
}
MecanumDrive::WheelSpeeds MecanumDrive::DriveCartesianIK(
double xSpeed, double ySpeed, double zRotation,
MecanumDrive::WheelVelocities MecanumDrive::DriveCartesianIK(
double xVelocity, double yVelocity, double zRotation,
wpi::math::Rotation2d gyroAngle) {
xSpeed = std::clamp(xSpeed, -1.0, 1.0);
ySpeed = std::clamp(ySpeed, -1.0, 1.0);
xVelocity = std::clamp(xVelocity, -1.0, 1.0);
yVelocity = std::clamp(yVelocity, -1.0, 1.0);
// Compensate for gyro angle.
auto input = wpi::math::Translation2d{
wpi::units::meter_t{xSpeed},
wpi::units::meter_t{
ySpeed}}.RotateBy(-gyroAngle);
auto input = wpi::math::Translation2d{wpi::units::meter_t{xVelocity},
wpi::units::meter_t{yVelocity}}
.RotateBy(-gyroAngle);
double wheelSpeeds[4];
wheelSpeeds[kFrontLeft] = input.X().value() + input.Y().value() + zRotation;
wheelSpeeds[kFrontRight] = input.X().value() - input.Y().value() - zRotation;
wheelSpeeds[kRearLeft] = input.X().value() - input.Y().value() + zRotation;
wheelSpeeds[kRearRight] = input.X().value() + input.Y().value() - zRotation;
double wheelVelocities[4];
wheelVelocities[kFrontLeft] =
input.X().value() + input.Y().value() + zRotation;
wheelVelocities[kFrontRight] =
input.X().value() - input.Y().value() - zRotation;
wheelVelocities[kRearLeft] =
input.X().value() - input.Y().value() + zRotation;
wheelVelocities[kRearRight] =
input.X().value() + input.Y().value() - zRotation;
Desaturate(wheelSpeeds);
Desaturate(wheelVelocities);
return {wheelSpeeds[kFrontLeft], wheelSpeeds[kFrontRight],
wheelSpeeds[kRearLeft], wheelSpeeds[kRearRight]};
return {wheelVelocities[kFrontLeft], wheelVelocities[kFrontRight],
wheelVelocities[kRearLeft], wheelVelocities[kRearRight]};
}
std::string MecanumDrive::GetDescription() const {
@@ -132,15 +136,15 @@ void MecanumDrive::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("MecanumDrive");
builder.SetActuator(true);
builder.AddDoubleProperty(
"Front Left Motor Speed", [&] { return m_frontLeftOutput; },
"Front Left Motor Velocity", [&] { return m_frontLeftOutput; },
m_frontLeftMotor);
builder.AddDoubleProperty(
"Front Right Motor Speed", [&] { return m_frontRightOutput; },
"Front Right Motor Velocity", [&] { return m_frontRightOutput; },
m_frontRightMotor);
builder.AddDoubleProperty(
"Rear Left Motor Speed", [&] { return m_rearLeftOutput; },
"Rear Left Motor Velocity", [&] { return m_rearLeftOutput; },
m_rearLeftMotor);
builder.AddDoubleProperty(
"Rear Right Motor Speed", [&] { return m_rearRightOutput; },
"Rear Right Motor Velocity", [&] { return m_rearRightOutput; },
m_rearRightMotor);
}

View File

@@ -4,12 +4,9 @@
#include "wpi/drive/RobotDriveBase.hpp"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include "wpi/hardware/motor/MotorController.hpp"
using namespace wpi;
RobotDriveBase::RobotDriveBase() {
@@ -28,17 +25,17 @@ void RobotDriveBase::FeedWatchdog() {
Feed();
}
void RobotDriveBase::Desaturate(std::span<double> wheelSpeeds) {
double maxMagnitude = std::abs(wheelSpeeds[0]);
for (size_t i = 1; i < wheelSpeeds.size(); i++) {
double temp = std::abs(wheelSpeeds[i]);
void RobotDriveBase::Desaturate(std::span<double> wheelVelocities) {
double maxMagnitude = std::abs(wheelVelocities[0]);
for (size_t i = 1; i < wheelVelocities.size(); i++) {
double temp = std::abs(wheelVelocities[i]);
if (maxMagnitude < temp) {
maxMagnitude = temp;
}
}
if (maxMagnitude > 1.0) {
for (size_t i = 0; i < wheelSpeeds.size(); i++) {
wheelSpeeds[i] = wheelSpeeds[i] / maxMagnitude;
for (size_t i = 0; i < wheelVelocities.size(); i++) {
wheelVelocities[i] = wheelVelocities[i] / maxMagnitude;
}
}
}

View File

@@ -92,10 +92,10 @@ ExpansionHubMotor::~ExpansionHubMotor() noexcept {
m_hub.UnreserveMotor(m_channel);
}
void ExpansionHubMotor::SetPercentagePower(double power) {
void ExpansionHubMotor::SetDutyCycle(double dutyCycle) {
SetEnabled(true);
m_modePublisher.Set(kPercentageMode);
m_setpointPublisher.Set(power);
m_setpointPublisher.Set(dutyCycle);
}
void ExpansionHubMotor::SetVoltage(wpi::units::volt_t voltage) {

View File

@@ -69,7 +69,7 @@ LEDPattern LEDPattern::OffsetBy(int offset) {
});
}
LEDPattern LEDPattern::ScrollAtRelativeSpeed(wpi::units::hertz_t velocity) {
LEDPattern LEDPattern::ScrollAtRelativeVelocity(wpi::units::hertz_t velocity) {
// velocity is in terms of LED lengths per second (1_hz = full cycle per
// second, 0.5_hz = half cycle per second, 2_hz = two cycles per second)
// Invert and multiply by 1,000,000 to get microseconds
@@ -88,7 +88,7 @@ LEDPattern LEDPattern::ScrollAtRelativeSpeed(wpi::units::hertz_t velocity) {
});
}
LEDPattern LEDPattern::ScrollAtAbsoluteSpeed(
LEDPattern LEDPattern::ScrollAtAbsoluteVelocity(
wpi::units::meters_per_second_t velocity, wpi::units::meter_t ledSpacing) {
// Velocity is in terms of meters per second
// Multiply by 1,000,000 to use microseconds instead of seconds

View File

@@ -8,7 +8,7 @@
using namespace wpi;
void MotorController::SetVoltage(wpi::units::volt_t output) {
void MotorController::SetVoltage(wpi::units::volt_t voltage) {
// NOLINTNEXTLINE(bugprone-integer-division)
Set(output / RobotController::GetBatteryVoltage());
SetDutyCycle(voltage / RobotController::GetBatteryVoltage());
}

View File

@@ -14,33 +14,28 @@
using namespace wpi;
void PWMMotorController::Set(double speed) {
void PWMMotorController::SetDutyCycle(double dutyCycle) {
if (m_isInverted) {
speed = -speed;
dutyCycle = -dutyCycle;
}
SetSpeed(speed);
SetDutyCycleInternal(dutyCycle);
for (auto& follower : m_nonowningFollowers) {
follower->Set(speed);
follower->SetDutyCycle(dutyCycle);
}
for (auto& follower : m_owningFollowers) {
follower->Set(speed);
follower->SetDutyCycle(dutyCycle);
}
Feed();
}
void PWMMotorController::SetVoltage(wpi::units::volt_t output) {
// NOLINTNEXTLINE(bugprone-integer-division)
Set(output / RobotController::GetBatteryVoltage());
}
double PWMMotorController::Get() const {
return GetSpeed() * (m_isInverted ? -1.0 : 1.0);
double PWMMotorController::GetDutyCycle() const {
return GetDutyCycleInternal() * (m_isInverted ? -1.0 : 1.0);
}
wpi::units::volt_t PWMMotorController::GetVoltage() const {
return Get() * RobotController::GetBatteryVoltage();
return GetDutyCycle() * RobotController::GetBatteryVoltage();
}
void PWMMotorController::SetInverted(bool isInverted) {
@@ -54,8 +49,8 @@ bool PWMMotorController::GetInverted() const {
void PWMMotorController::Disable() {
m_pwm.SetDisabled();
if (m_simSpeed) {
m_simSpeed.Set(0.0);
if (m_simDutyCycle) {
m_simDutyCycle.Set(0.0);
}
for (auto& follower : m_nonowningFollowers) {
@@ -94,7 +89,7 @@ PWMMotorController::PWMMotorController(std::string_view name, int channel)
m_simDevice = wpi::hal::SimDevice{"PWMMotorController", channel};
if (m_simDevice) {
m_simSpeed = m_simDevice.CreateDouble("Speed", true, 0.0);
m_simDutyCycle = m_simDevice.CreateDouble("DutyCycle", true, 0.0);
m_pwm.SetSimDevice(m_simDevice);
}
}
@@ -105,8 +100,8 @@ void PWMMotorController::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("Motor Controller");
builder.SetActuator(true);
builder.AddDoubleProperty(
"Value", [=, this] { return Get(); },
[=, this](double value) { Set(value); });
"Value", [=, this] { return GetDutyCycle(); },
[=, this](double value) { SetDutyCycle(value); });
}
wpi::units::microsecond_t PWMMotorController::GetMinPositivePwm() const {
@@ -133,34 +128,34 @@ wpi::units::microsecond_t PWMMotorController::GetNegativeScaleFactor() const {
return GetMaxNegativePwm() - m_minPwm;
}
void PWMMotorController::SetSpeed(double speed) {
if (std::isfinite(speed)) {
speed = std::clamp(speed, -1.0, 1.0);
void PWMMotorController::SetDutyCycleInternal(double dutyCycle) {
if (std::isfinite(dutyCycle)) {
dutyCycle = std::clamp(dutyCycle, -1.0, 1.0);
} else {
speed = 0.0;
dutyCycle = 0.0;
}
if (m_simSpeed) {
m_simSpeed.Set(speed);
if (m_simDutyCycle) {
m_simDutyCycle.Set(dutyCycle);
}
wpi::units::microsecond_t rawValue;
if (speed == 0.0) {
if (dutyCycle == 0.0) {
rawValue = m_centerPwm;
} else if (speed > 0.0) {
rawValue = wpi::units::microsecond_t{static_cast<double>(
std::lround((speed * GetPositiveScaleFactor()).value()))} +
} else if (dutyCycle > 0.0) {
rawValue = wpi::units::microsecond_t{static_cast<double>(std::lround(
(dutyCycle * GetPositiveScaleFactor()).value()))} +
GetMinPositivePwm();
} else {
rawValue = wpi::units::microsecond_t{static_cast<double>(
std::lround((speed * GetNegativeScaleFactor()).value()))} +
rawValue = wpi::units::microsecond_t{static_cast<double>(std::lround(
(dutyCycle * GetNegativeScaleFactor()).value()))} +
GetMaxNegativePwm();
}
m_pwm.SetPulseTime(rawValue);
}
double PWMMotorController::GetSpeed() const {
double PWMMotorController::GetDutyCycleInternal() const {
wpi::units::microsecond_t rawValue = m_pwm.GetPulseTime();
if (rawValue == 0_us) {

View File

@@ -150,7 +150,8 @@ void Encoder::InitSendable(wpi::util::SendableBuilder& builder) {
builder.SetSmartDashboardType("Encoder");
}
builder.AddDoubleProperty("Speed", [=, this] { return GetRate(); }, nullptr);
builder.AddDoubleProperty(
"Velocity", [=, this] { return GetRate(); }, nullptr);
builder.AddDoubleProperty(
"Distance", [=, this] { return GetDistance(); }, nullptr);
builder.AddDoubleProperty(

View File

@@ -6,7 +6,6 @@
#include "wpi/hal/SimDevice.h"
#include "wpi/simulation/SimDeviceSim.hpp"
#include "wpi/units/length.hpp"
using namespace wpi;
using namespace wpi::sim;
@@ -17,9 +16,9 @@ PWMMotorControllerSim::PWMMotorControllerSim(
PWMMotorControllerSim::PWMMotorControllerSim(int channel) {
wpi::sim::SimDeviceSim deviceSim{"PWMMotorController", channel};
m_simSpeed = deviceSim.GetDouble("Speed");
m_simDutyCycle = deviceSim.GetDouble("DutyCycle");
}
double PWMMotorControllerSim::GetSpeed() const {
return m_simSpeed.Get();
double PWMMotorControllerSim::GetDutyCycle() const {
return m_simDutyCycle.Get();
}

View File

@@ -15,11 +15,11 @@
namespace wpi {
/**
* Tachometer for getting rotational speed from a device.
* Tachometer for getting rotational velocity from a device.
*
* <p>The Tachometer class measures the time between digital pulses to
* determine the rotation speed of a mechanism. Examples of devices that could
* be used with the tachometer class are a hall effect sensor, break beam
* determine the rotation velocity of a mechanism. Examples of devices that
* could be used with the tachometer class are a hall effect sensor, break beam
* sensor, or optical sensor detecting tape on a shooter wheel. Unlike
* encoders, this class only needs a single digital input.
*/

View File

@@ -59,14 +59,14 @@ class DifferentialDrive : public RobotDriveBase,
public wpi::util::SendableHelper<DifferentialDrive> {
public:
/**
* Wheel speeds for a differential drive.
* Wheel velocities for a differential drive.
*
* Uses normalized voltage [-1.0..1.0].
*/
struct WheelSpeeds {
/// Left wheel speed.
struct WheelVelocities {
/// Left wheel velocity.
double left = 0.0;
/// Right wheel speed.
/// Right wheel velocity.
double right = 0.0;
};
@@ -110,41 +110,46 @@ class DifferentialDrive : public RobotDriveBase,
* Note: Some drivers may prefer inverted rotation controls. This can be done
* by negating the value passed for rotation.
*
* @param xSpeed The speed at which the robot should drive along the X
* axis [-1.0..1.0]. Forward is positive.
* @param zRotation The rotation rate of the robot around the Z axis
* [-1.0..1.0]. Counterclockwise is positive.
* @param squareInputs If set, decreases the input sensitivity at low speeds.
* @param xVelocity The velocity at which the robot should drive along the X
* axis [-1.0..1.0]. Forward is positive.
* @param zRotation The rotation rate of the robot around the Z axis
* [-1.0..1.0]. Counterclockwise is positive.
* @param squareInputs If set, decreases the input sensitivity at low
* velocities.
*/
void ArcadeDrive(double xSpeed, double zRotation, bool squareInputs = true);
void ArcadeDrive(double xVelocity, double zRotation,
bool squareInputs = true);
/**
* Curvature drive method for differential drive platform.
*
* The rotation argument controls the curvature of the robot's path rather
* than its rate of heading change. This makes the robot more controllable at
* high speeds.
* high velocities.
*
* @param xSpeed The robot's speed along the X axis [-1.0..1.0].
* Forward is positive.
* @param zRotation The normalized curvature [-1.0..1.0].
* Counterclockwise is positive.
* @param xVelocity The robot's velocity along the X axis [-1.0..1.0]. Forward
* is positive.
* @param zRotation The normalized curvature [-1.0..1.0]. Counterclockwise is
* positive.
* @param allowTurnInPlace If set, overrides constant-curvature turning for
* turn-in-place maneuvers. zRotation will control
* turning rate instead of curvature.
* turn-in-place maneuvers. zRotation will control turning rate instead of
* curvature.
*/
void CurvatureDrive(double xSpeed, double zRotation, bool allowTurnInPlace);
void CurvatureDrive(double xVelocity, double zRotation,
bool allowTurnInPlace);
/**
* Tank drive method for differential drive platform.
*
* @param leftSpeed The robot left side's speed along the X axis
* [-1.0..1.0]. Forward is positive.
* @param rightSpeed The robot right side's speed along the X axis
* [-1.0..1.0]. Forward is positive.
* @param squareInputs If set, decreases the input sensitivity at low speeds.
* @param leftVelocity The robot left side's velocity along the X axis
* [-1.0..1.0]. Forward is positive.
* @param rightVelocity The robot right side's velocity along the X axis
* [-1.0..1.0]. Forward is positive.
* @param squareInputs If set, decreases the input sensitivity at low
* velocities.
*/
void TankDrive(double leftSpeed, double rightSpeed, bool squareInputs = true);
void TankDrive(double leftVelocity, double rightVelocity,
bool squareInputs = true);
/**
* Arcade drive inverse kinematics for differential drive platform.
@@ -152,47 +157,49 @@ class DifferentialDrive : public RobotDriveBase,
* Note: Some drivers may prefer inverted rotation controls. This can be done
* by negating the value passed for rotation.
*
* @param xSpeed The speed at which the robot should drive along the X
* axis [-1.0..1.0]. Forward is positive.
* @param zRotation The rotation rate of the robot around the Z axis
* [-1.0..1.0]. Clockwise is positive.
* @param squareInputs If set, decreases the input sensitivity at low speeds.
* @return Wheel speeds [-1.0..1.0].
* @param xVelocity The velocity at which the robot should drive along the X
* axis [-1.0..1.0]. Forward is positive.
* @param zRotation The rotation rate of the robot around the Z axis
* [-1.0..1.0]. Clockwise is positive.
* @param squareInputs If set, decreases the input sensitivity at low
* velocities.
* @return Wheel velocities [-1.0..1.0].
*/
static WheelSpeeds ArcadeDriveIK(double xSpeed, double zRotation,
bool squareInputs = true);
static WheelVelocities ArcadeDriveIK(double xVelocity, double zRotation,
bool squareInputs = true);
/**
* Curvature drive inverse kinematics for differential drive platform.
*
* The rotation argument controls the curvature of the robot's path rather
* than its rate of heading change. This makes the robot more controllable at
* high speeds.
* high velocities.
*
* @param xSpeed The robot's speed along the X axis [-1.0..1.0].
* Forward is positive.
* @param zRotation The normalized curvature [-1.0..1.0]. Clockwise is
* positive.
* @param xVelocity The robot's velocity along the X axis [-1.0..1.0]. Forward
* is positive.
* @param zRotation The normalized curvature [-1.0..1.0]. Clockwise is
* positive.
* @param allowTurnInPlace If set, overrides constant-curvature turning for
* turn-in-place maneuvers. zRotation will control
* turning rate instead of curvature.
* @return Wheel speeds [-1.0..1.0].
* turn-in-place maneuvers. zRotation will control turning rate instead of
* curvature.
* @return Wheel velocities [-1.0..1.0].
*/
static WheelSpeeds CurvatureDriveIK(double xSpeed, double zRotation,
bool allowTurnInPlace);
static WheelVelocities CurvatureDriveIK(double xVelocity, double zRotation,
bool allowTurnInPlace);
/**
* Tank drive inverse kinematics for differential drive platform.
*
* @param leftSpeed The robot left side's speed along the X axis
* [-1.0..1.0]. Forward is positive.
* @param rightSpeed The robot right side's speed along the X axis
* [-1.0..1.0]. Forward is positive.
* @param squareInputs If set, decreases the input sensitivity at low speeds.
* @return Wheel speeds [-1.0..1.0].
* @param leftVelocity The robot left side's velocity along the X axis
* [-1.0..1.0]. Forward is positive.
* @param rightVelocity The robot right side's velocity along the X axis
* [-1.0..1.0]. Forward is positive.
* @param squareInputs If set, decreases the input sensitivity at low
* velocities.
* @return Wheel velocities [-1.0..1.0].
*/
static WheelSpeeds TankDriveIK(double leftSpeed, double rightSpeed,
bool squareInputs = true);
static WheelVelocities TankDriveIK(double leftVelocity, double rightVelocity,
bool squareInputs = true);
void StopMotor() override;
std::string GetDescription() const override;

View File

@@ -56,18 +56,18 @@ class MecanumDrive : public RobotDriveBase,
public wpi::util::SendableHelper<MecanumDrive> {
public:
/**
* Wheel speeds for a mecanum drive.
* Wheel velocities for a mecanum drive.
*
* Uses normalized voltage [-1.0..1.0].
*/
struct WheelSpeeds {
/// Front-left wheel speed.
struct WheelVelocities {
/// Front-left wheel velocity.
double frontLeft = 0.0;
/// Front-right wheel speed.
/// Front-right wheel velocity.
double frontRight = 0.0;
/// Rear-left wheel speed.
/// Rear-left wheel velocity.
double rearLeft = 0.0;
/// Rear-right wheel speed.
/// Rear-right wheel velocity.
double rearRight = 0.0;
};
@@ -113,31 +113,31 @@ class MecanumDrive : public RobotDriveBase,
* Drive method for Mecanum platform.
*
* Angles are measured counterclockwise from the positive X axis. The robot's
* speed is independent from its angle or rotation rate.
* velocity is independent from its angle or rotation rate.
*
* @param xSpeed The robot's speed along the X axis [-1.0..1.0]. Forward is
* positive.
* @param ySpeed The robot's speed along the Y axis [-1.0..1.0]. Left is
* positive.
* @param xVelocity The robot's velocity along the X axis [-1.0..1.0]. Forward
* is positive.
* @param yVelocity The robot's velocity along the Y axis [-1.0..1.0]. Left is
* positive.
* @param zRotation The robot's rotation rate around the Z axis [-1.0..1.0].
* Counterclockwise is positive.
* Counterclockwise is positive.
* @param gyroAngle The gyro heading around the Z axis. Use this to implement
* field-oriented controls.
* field-oriented controls.
*/
void DriveCartesian(double xSpeed, double ySpeed, double zRotation,
void DriveCartesian(double xVelocity, double yVelocity, double zRotation,
wpi::math::Rotation2d gyroAngle = 0_rad);
/**
* Drive method for Mecanum platform.
*
* Angles are measured counterclockwise from the positive X axis. The robot's
* speed is independent from its angle or rotation rate.
* velocity is independent from its angle or rotation rate.
*
* @param magnitude The robot's speed at a given angle [-1.0..1.0]. Forward is
* positive.
* @param angle The angle around the Z axis at which the robot drives.
* @param magnitude The robot's velocity at a given angle [-1.0..1.0]. Forward
* is positive.
* @param angle The angle around the Z axis at which the robot drives.
* @param zRotation The robot's rotation rate around the Z axis [-1.0..1.0].
* Counterclockwise is positive.
* Counterclockwise is positive.
*/
void DrivePolar(double magnitude, wpi::math::Rotation2d angle,
double zRotation);
@@ -146,21 +146,21 @@ class MecanumDrive : public RobotDriveBase,
* Cartesian inverse kinematics for Mecanum platform.
*
* Angles are measured counterclockwise from the positive X axis. The robot's
* speed is independent from its angle or rotation rate.
* velocity is independent from its angle or rotation rate.
*
* @param xSpeed The robot's speed along the X axis [-1.0..1.0]. Forward is
* positive.
* @param ySpeed The robot's speed along the Y axis [-1.0..1.0]. Left is
* positive.
* @param xVelocity The robot's velocity along the X axis [-1.0..1.0]. Forward
* is positive.
* @param yVelocity The robot's velocity along the Y axis [-1.0..1.0]. Left is
* positive.
* @param zRotation The robot's rotation rate around the Z axis [-1.0..1.0].
* Counterclockwise is positive.
* Counterclockwise is positive.
* @param gyroAngle The gyro heading around the Z axis. Use this to implement
* field-oriented controls.
* @return Wheel speeds [-1.0..1.0].
* field-oriented controls.
* @return Wheel velocities [-1.0..1.0].
*/
static WheelSpeeds DriveCartesianIK(double xSpeed, double ySpeed,
double zRotation,
wpi::math::Rotation2d gyroAngle = 0_rad);
static WheelVelocities DriveCartesianIK(
double xVelocity, double yVelocity, double zRotation,
wpi::math::Rotation2d gyroAngle = 0_rad);
void StopMotor() override;
std::string GetDescription() const override;

View File

@@ -61,7 +61,7 @@ class RobotDriveBase : public MotorSafety {
* a mode other than PercentVbus or to limit the maximum output.
*
* @param maxOutput Multiplied with the output percentage computed by the
* drive functions.
* drive functions.
*/
void SetMaxOutput(double maxOutput);
@@ -84,10 +84,10 @@ class RobotDriveBase : public MotorSafety {
static constexpr double kDefaultMaxOutput = 1.0;
/**
* Renormalize all wheel speeds if the magnitude of any wheel is greater than
* 1.0.
* Renormalize all wheel velocities if the magnitude of any wheel is greater
* than 1.0.
*/
static void Desaturate(std::span<double> wheelSpeeds);
static void Desaturate(std::span<double> wheelVelocities);
/// Input deadband.
double m_deadband = kDefaultDeadband;

View File

@@ -29,14 +29,16 @@ class ExpansionHubMotor {
* @param channel The motor channel
*/
ExpansionHubMotor(int usbId, int channel);
~ExpansionHubMotor() noexcept;
/**
* Sets the percentage power to run the motor at, between -1 and 1.
* Sets the duty cycle.
*
* @param power The power to drive the motor at
* @param dutyCycle The duty cycle between -1 and 1 (sign indicates
* direction).
*/
void SetPercentagePower(double power);
void SetDutyCycle(double dutyCycle);
/**
* Sets the voltage to run the motor at. This value will be continously scaled

View File

@@ -121,7 +121,7 @@ class LEDPattern {
* long (assuming equal LED density on both segments).
*/
[[nodiscard]]
LEDPattern ScrollAtRelativeSpeed(wpi::units::hertz_t velocity);
LEDPattern ScrollAtRelativeVelocity(wpi::units::hertz_t velocity);
/**
* Creates a pattern that plays this one scrolling up an LED strip. A negative
@@ -136,9 +136,8 @@ class LEDPattern {
* wpi::units::meter_t{1 /60.0};
*
* wpi::LEDPattern rainbow = wpi::LEDPattern::Rainbow();
* wpi::LEDPattern scrollingRainbow =
* rainbow.ScrollAtAbsoluteSpeed(wpi::units::feet_per_second_t{1 / 3.0},
* LED_SPACING);
* wpi::LEDPattern scrollingRainbow = rainbow.ScrollAtAbsoluteVelocity(
* wpi::units::feet_per_second_t{1 / 3.0}, LED_SPACING);
* </pre>
*
* <p>Note that this pattern will scroll <i>faster</i> if applied to a less
@@ -147,12 +146,12 @@ class LEDPattern {
*
* @param velocity how fast the pattern should move along a physical LED strip
* @param ledSpacing the distance between adjacent LEDs on the physical LED
* strip
* strip
* @return the scrolling pattern
*/
[[nodiscard]]
LEDPattern ScrollAtAbsoluteSpeed(wpi::units::meters_per_second_t velocity,
wpi::units::meter_t ledSpacing);
LEDPattern ScrollAtAbsoluteVelocity(wpi::units::meters_per_second_t velocity,
wpi::units::meter_t ledSpacing);
/**
* Creates a pattern that switches between playing this pattern and turning
@@ -170,7 +169,7 @@ class LEDPattern {
* "off" time is exactly equal to the "on" time.
*
* @param onTime how long the pattern should play for (and be turned off for),
* per cycle
* per cycle
* @return the blinking pattern
*/
[[nodiscard]]
@@ -264,7 +263,7 @@ class LEDPattern {
* </pre>
*
* @param relativeBrightness the multiplier to apply to all channels to modify
* brightness
* brightness
* @return the input pattern, displayed at
*/
[[nodiscard]]
@@ -305,8 +304,8 @@ class LEDPattern {
* </pre>
*
* @param progressFunction the function to call to determine the progress.
* This should return values in the range [0, 1]; any values outside that
* range will be clamped.
* This should return values in the range [0, 1]; any values outside that
* range will be clamped.
* @return the mask pattern
*/
static LEDPattern ProgressMaskLayer(std::function<double()> progressFunction);
@@ -320,7 +319,7 @@ class LEDPattern {
* there's a 0 -> black step by default).
*
* @param steps a map of progress to the color to start displaying at that
* position along the LED strip
* position along the LED strip
* @return a motionless step pattern
*/
static LEDPattern Steps(
@@ -335,7 +334,7 @@ class LEDPattern {
* there's a 0 -> black step by default).
*
* @param steps a map of progress to the color to start displaying at that
* position along the LED strip
* position along the LED strip
* @return a motionless step pattern
*/
static LEDPattern Steps(

View File

@@ -16,49 +16,52 @@ class MotorController {
virtual ~MotorController() = default;
/**
* Common interface for setting the speed of a motor controller.
* Sets the duty cycle of the motor controller.
*
* @param speed The speed to set. Value should be between -1.0 and 1.0.
* @param dutyCycle The duty cycle between -1 and 1 (sign indicates
* direction).
*/
virtual void Set(double speed) = 0;
virtual void SetDutyCycle(double dutyCycle) = 0;
/**
* Sets the voltage output of the MotorController. Compensates for
* the current bus voltage to ensure that the desired voltage is output even
* if the battery voltage is below 12V - highly useful when the voltage
* outputs are "meaningful" (e.g. they come from a feedforward calculation).
* Sets the voltage output of the motor controller.
*
* <p>NOTE: This function *must* be called regularly in order for voltage
* Compensates for the current bus voltage to ensure that the desired voltage
* is output even if the battery voltage is below 12V - highly useful when the
* voltage outputs are "meaningful" (e.g. they come from a feedforward
* calculation).
*
* NOTE: This function *must* be called regularly in order for voltage
* compensation to work properly - unlike the ordinary set function, it is not
* "set it and forget it."
*
* @param output The voltage to output.
* @param voltage The voltage.
*/
virtual void SetVoltage(wpi::units::volt_t output);
virtual void SetVoltage(wpi::units::volt_t voltage);
/**
* Common interface for getting the current set speed of a motor controller.
* Gets the duty cycle of the motor controller.
*
* @return The current set speed. Value is between -1.0 and 1.0.
* @return The duty cycle between -1 and 1 (sign indicates direction).
*/
virtual double Get() const = 0;
virtual double GetDutyCycle() const = 0;
/**
* Common interface for inverting direction of a motor controller.
* Sets the inversion state of the motor controller.
*
* @param isInverted The state of inversion, true is inverted.
* @param isInverted The inversion state.
*/
virtual void SetInverted(bool isInverted) = 0;
/**
* Common interface for returning the inversion state of a motor controller.
* Gets the inversion state of the motor controller.
*
* @return isInverted The state of inversion, true is inverted.
* @return The inversion state.
*/
virtual bool GetInverted() const = 0;
/**
* Common interface for disabling a motor.
* Disables the motor controller.
*/
virtual void Disable() = 0;
};

View File

@@ -37,45 +37,16 @@ class PWMMotorController
PWMMotorController(PWMMotorController&&) = default;
PWMMotorController& operator=(PWMMotorController&&) = default;
/**
* Set the PWM value.
*
* The PWM value is set using a range of -1.0 to 1.0, appropriately scaling
* the value for the FPGA.
*
* @param value The speed value between -1.0 and 1.0 to set.
*/
void Set(double value) override;
void SetDutyCycle(double dutyCycle) override;
/**
* Sets the voltage output of the PWMMotorController. Compensates for
* the current bus voltage to ensure that the desired voltage is output even
* if the battery voltage is below 12V - highly useful when the voltage
* outputs are "meaningful" (e.g. they come from a feedforward calculation).
*
* <p>NOTE: This function *must* be called regularly in order for voltage
* compensation to work properly - unlike the ordinary set function, it is not
* "set it and forget it."
*
* @param output The voltage to output.
*/
void SetVoltage(wpi::units::volt_t output) override;
/**
* Get the recently set value of the PWM. This value is affected by the
* inversion property. If you want the value that is sent directly to the
* MotorController, use PWM::GetSpeed() instead.
*
* @return The most recently set value for the PWM between -1.0 and 1.0.
*/
double Get() const override;
double GetDutyCycle() const override;
/**
* Gets the voltage output of the motor controller, nominally between -12 V
* and 12 V.
*
* @return The voltage of the motor controller, nominally between -12 V and 12
* V.
* V.
*/
virtual wpi::units::volt_t GetVoltage() const;
@@ -134,8 +105,8 @@ class PWMMotorController
/// PWM instances for motor controller.
PWM m_pwm;
void SetSpeed(double speed);
double GetSpeed() const;
void SetDutyCycleInternal(double dutyCycle);
double GetDutyCycleInternal() const;
void SetBounds(wpi::units::microsecond_t maxPwm,
wpi::units::microsecond_t deadbandMaxPwm,
@@ -149,7 +120,7 @@ class PWMMotorController
std::vector<std::unique_ptr<PWMMotorController>> m_owningFollowers;
wpi::hal::SimDevice m_simDevice;
wpi::hal::SimDouble m_simSpeed;
wpi::hal::SimDouble m_simDutyCycle;
bool m_eliminateDeadband{0};
wpi::units::microsecond_t m_minPwm{0};

View File

@@ -6,7 +6,6 @@
#include "wpi/hal/SimDevice.h"
#include "wpi/hardware/motor/PWMMotorController.hpp"
#include "wpi/units/length.hpp"
namespace wpi {
@@ -20,10 +19,10 @@ class PWMMotorControllerSim {
explicit PWMMotorControllerSim(int channel);
double GetSpeed() const;
double GetDutyCycle() const;
private:
wpi::hal::SimDouble m_simSpeed;
wpi::hal::SimDouble m_simDutyCycle;
};
} // namespace sim
} // namespace wpi