[wpimath] Remove unit suffixes from variable names (#7529)

* Move units into API docs instead because suffixes make user code verbose and hard to read
* Rename trackWidth to trackwidth
* Make ultrasonic classes use meters instead of a mix of m, cm, mm, ft,
  and inches
This commit is contained in:
Tyler Veness
2025-02-10 07:23:04 -08:00
committed by GitHub
parent 764ada9b66
commit ac1705ae2b
250 changed files with 2953 additions and 3584 deletions

View File

@@ -7,6 +7,7 @@
#include <algorithm>
#include <hal/UsageReporting.h>
#include <units/length.h>
#include <wpi/sendable/SendableBuilder.h>
#include <wpi/sendable/SendableRegistry.h>
@@ -15,29 +16,30 @@
using namespace frc;
SharpIR SharpIR::GP2Y0A02YK0F(int channel) {
return SharpIR(channel, 62.28, -1.092, 20, 150.0);
return SharpIR(channel, 62.28, -1.092, 20_cm, 150_cm);
}
SharpIR SharpIR::GP2Y0A21YK0F(int channel) {
return SharpIR(channel, 26.449, -1.226, 10.0, 80.0);
return SharpIR(channel, 26.449, -1.226, 10_cm, 80_cm);
}
SharpIR SharpIR::GP2Y0A41SK0F(int channel) {
return SharpIR(channel, 12.354, -1.07, 4.0, 30.0);
return SharpIR(channel, 12.354, -1.07, 4_cm, 30_cm);
}
SharpIR SharpIR::GP2Y0A51SK0F(int channel) {
return SharpIR(channel, 5.2819, -1.161, 2.0, 15.0);
return SharpIR(channel, 5.2819, -1.161, 2_cm, 15_cm);
}
SharpIR::SharpIR(int channel, double a, double b, double minCM, double maxCM)
: m_sensor(channel), m_A(a), m_B(b), m_minCM(minCM), m_maxCM(maxCM) {
SharpIR::SharpIR(int channel, double a, double b, units::meter_t min,
units::meter_t max)
: m_sensor(channel), m_A(a), m_B(b), m_min(min), m_max(max) {
HAL_ReportUsage("IO", channel, "SharpIR");
wpi::SendableRegistry::Add(this, "SharpIR", channel);
m_simDevice = hal::SimDevice("SharpIR", m_sensor.GetChannel());
if (m_simDevice) {
m_simRange = m_simDevice.CreateDouble("Range (cm)", false, 0.0);
m_simRange = m_simDevice.CreateDouble("Range (m)", false, 0.0);
m_sensor.SetSimDevice(m_simDevice);
}
}
@@ -46,19 +48,16 @@ int SharpIR::GetChannel() const {
return m_sensor.GetChannel();
}
units::centimeter_t SharpIR::GetRange() const {
double distance;
units::meter_t SharpIR::GetRange() const {
if (m_simRange) {
distance = m_simRange.Get();
return std::clamp(units::meter_t{m_simRange.Get()}, m_min, m_max);
} else {
// Don't allow zero/negative values
auto v = std::max(m_sensor.GetVoltage(), 0.00001);
distance = m_A * std::pow(v, m_B);
}
// Always constrain output
return units::centimeter_t{std::max(std::min(distance, m_maxCM), m_minCM)};
return std::clamp(units::meter_t{m_A * std::pow(v, m_B) * 1e-2}, m_min,
m_max);
}
}
void SharpIR::InitSendable(wpi::SendableBuilder& builder) {

View File

@@ -18,11 +18,11 @@ using namespace frc;
using namespace frc::sim;
DifferentialDrivetrainSim::DifferentialDrivetrainSim(
LinearSystem<2, 2, 2> plant, units::meter_t trackWidth, DCMotor driveMotor,
LinearSystem<2, 2, 2> plant, units::meter_t trackwidth, DCMotor driveMotor,
double gearRatio, units::meter_t wheelRadius,
const std::array<double, 7>& measurementStdDevs)
: m_plant(std::move(plant)),
m_rb(trackWidth / 2.0),
m_rb(trackwidth / 2.0),
m_wheelRadius(wheelRadius),
m_motor(driveMotor),
m_originalGearing(gearRatio),
@@ -36,11 +36,11 @@ DifferentialDrivetrainSim::DifferentialDrivetrainSim(
DifferentialDrivetrainSim::DifferentialDrivetrainSim(
frc::DCMotor driveMotor, double gearing, units::kilogram_square_meter_t J,
units::kilogram_t mass, units::meter_t wheelRadius,
units::meter_t trackWidth, const std::array<double, 7>& measurementStdDevs)
units::meter_t trackwidth, const std::array<double, 7>& measurementStdDevs)
: DifferentialDrivetrainSim(
frc::LinearSystemId::DrivetrainVelocitySystem(
driveMotor, mass, wheelRadius, trackWidth / 2.0, J, gearing),
trackWidth, driveMotor, gearing, wheelRadius, measurementStdDevs) {}
driveMotor, mass, wheelRadius, trackwidth / 2.0, J, gearing),
trackwidth, driveMotor, gearing, wheelRadius, measurementStdDevs) {}
Eigen::Vector2d DifferentialDrivetrainSim::ClampInput(
const Eigen::Vector2d& u) {

View File

@@ -16,9 +16,9 @@ SharpIRSim::SharpIRSim(const SharpIR& sharpIR)
SharpIRSim::SharpIRSim(int channel) {
frc::sim::SimDeviceSim deviceSim{"SharpIR", channel};
m_simRange = deviceSim.GetDouble("Range (cm)");
m_simRange = deviceSim.GetDouble("Range (m)");
}
void SharpIRSim::SetRange(units::centimeter_t rng) {
m_simRange.Set(rng.value());
void SharpIRSim::SetRange(units::meter_t range) {
m_simRange.Set(range.value());
}