[wpiunits] Add subproject for a Java typesafe unit system (#5371)
# Background
Unit safety has always been a problem in WPILib. Any value corresponding to a physical measurement, such as current draw or distance traveled, is represented by a bare number with no unit tied to it; it's up to the programmer to know what units they're working and take care to remember that while working on their robot program. This leads to bugs when programmers accidentally mix units without knowing, or measure something (such as a wheel diameter) in one unit and program using another. `wpiunits` is intended to eliminate that class of bugs.
Another source of friction is the controllers and models in `wpimath` that expect all inputs to be in terms of SI units (meter, kilogram, and so on), while most FRC teams are US-based and most commonly use imperial units. wpimath does a good job of noting unit types in method names and argument names; however, it still relies on users properly converting values (and knowing they even have to do so).
# API
There are really only two core classes in this library: `Unit` and `Measure`. A `Unit` represents some dimension like distance or time. `Unit` is subclassed to define specific dimensions (eg `Distance` and `Time`) and those subclasses are instantiated to defined particular units in those dimensions, such as `Meters` and `Feet` being instances of the `Distance` class.
A `Measure` is a value tied to a particular dimension like distance and knows what unit that value is tied to. `Measure` has two implementations - one immutable and one mutable. The `Measure` interface only defines *read-only* operations; any API working with measurements should use the interface. The default implementation is `ImmutableMeasure`, which only implements those read-only operations and is useful for tracking constants. `MutableMeasure` also adds some methods that will allow for mutation of its internal state; this class is intended for use for things like sensors and controllers that track internal state and don't want to allocate new `Measure` objects every time something like `myEncoder.getDistance()` is called. However, the APIs for those methods should still only expose the read-only `Measure` interface so users can't (without casting or reflection) change the internal values.
A `Units` class provides convenient definitions for most of the commonly used unit types, such as `Meters`, `Feet`, and `Milliseconds`. I recommend static importing these units eg `import static edu.wpi.first.units.Units.Meters`) so they can be used like `Meters.of(1.234)` instead of `Units.Meters.of(1.234)`
# Examples
These examples are admittedly contrived. Users shouldn't be interacting much with measure objects themselves, since wpimath and wpilibj classes will be updated to support working with them; users will often just have to take a `Measure` output from one place (such as an encoder) and feed it as input to something else (such as a PID controller or kinematics model)
```java
// Using raw units
Encoder encoder = ...
int kPulsesPerRev = 2048;
double kWheelDiameterMeters = Units.inchesToMeters(6);
double kGearRatio = 10.86;
// always have to remember this encoder will output in meters!
encoder.setDistancePerPulse(kWheelDiameterMeters * Math.PI / (kGearRatio * kPulsesPerRev));
Command driveDistance(double distance) {
// have to know the distance argument needs to be in meters!
return run(this::driveStraight).until(() -> encoder.getDistance() >= distance);
}
// Oops! This will go 16 feet, not 5!
Command driveFiveFeet = driveDistance(5);
Command driveOneMeter = driveDistance(1);
```
```java
// Using wpiunits
Encoder encoder = ...
int kPulsesPerRev = 2048;
Measure<Distance> kWheelDiameter = Inches.of(6);
double kGearRatio = 10.86;
encoder.setDistancePerPulse(kWheelDiameter.times(Math.PI).divide(kGearRatio * kPulsesPerRev));
Command driveDistance(Measure<Distance> distance) {
// Measure#gte automatically handles unit conversions
return run(this::driveStraight).until(() -> encoder.getDistance().gte(distance));
}
// Users HAVE to be explicit about their units
Command driveFiveFeet = driveDistance(Feet.of(5));
Command driveOneMeter = driveDistance(Meters.of(1));
```
```java
SmartDashboard.putNumber("Temperature (C)", pdp.getTemperature().in(Celsius));
SmartDashboard.putNumber("Temperature (F)", pdp.getTemperature().in(Fahrenheit));
```
```java
var InchSecond = Inch.mult(Second); // new combined unit types can be user-defined
var InchPerSecond = Inch.per(Second);
PIDController<Distance, ElectricPotential> heightController = new PIDController<>(
/* kP */ Volts.of(0.2).per(Inch),
/* kI */ Volts.of(0.002).per(InchSecond),
/* kD */ Volts.of(0.008).per(InchPerSecond)
);
var elevatorTop = Feet.of(4).plus(Inches.of(6.125));
elevatorMotor.setVoltage(heightController.calculate(encoder.getDistance(), elevatorTop));
```
2023-07-23 17:18:17 -04:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
|
|
|
|
|
|
|
|
|
package edu.wpi.first.units;
|
|
|
|
|
|
|
|
|
|
import static edu.wpi.first.units.Units.Amps;
|
|
|
|
|
import static edu.wpi.first.units.Units.Celsius;
|
|
|
|
|
import static edu.wpi.first.units.Units.Centimeters;
|
|
|
|
|
import static edu.wpi.first.units.Units.Degrees;
|
|
|
|
|
import static edu.wpi.first.units.Units.Fahrenheit;
|
|
|
|
|
import static edu.wpi.first.units.Units.Feet;
|
|
|
|
|
import static edu.wpi.first.units.Units.FeetPerSecond;
|
|
|
|
|
import static edu.wpi.first.units.Units.Grams;
|
|
|
|
|
import static edu.wpi.first.units.Units.Gs;
|
|
|
|
|
import static edu.wpi.first.units.Units.Horsepower;
|
|
|
|
|
import static edu.wpi.first.units.Units.Inches;
|
|
|
|
|
import static edu.wpi.first.units.Units.Kelvin;
|
|
|
|
|
import static edu.wpi.first.units.Units.Kilo;
|
|
|
|
|
import static edu.wpi.first.units.Units.Kilograms;
|
|
|
|
|
import static edu.wpi.first.units.Units.Meters;
|
|
|
|
|
import static edu.wpi.first.units.Units.MetersPerSecond;
|
|
|
|
|
import static edu.wpi.first.units.Units.MetersPerSecondPerSecond;
|
|
|
|
|
import static edu.wpi.first.units.Units.Microseconds;
|
|
|
|
|
import static edu.wpi.first.units.Units.Milli;
|
|
|
|
|
import static edu.wpi.first.units.Units.Milliamps;
|
|
|
|
|
import static edu.wpi.first.units.Units.Millimeters;
|
|
|
|
|
import static edu.wpi.first.units.Units.Milliseconds;
|
|
|
|
|
import static edu.wpi.first.units.Units.Millivolts;
|
|
|
|
|
import static edu.wpi.first.units.Units.Milliwatts;
|
|
|
|
|
import static edu.wpi.first.units.Units.Minutes;
|
|
|
|
|
import static edu.wpi.first.units.Units.Ounces;
|
|
|
|
|
import static edu.wpi.first.units.Units.Percent;
|
|
|
|
|
import static edu.wpi.first.units.Units.Pounds;
|
|
|
|
|
import static edu.wpi.first.units.Units.Radians;
|
|
|
|
|
import static edu.wpi.first.units.Units.Revolutions;
|
|
|
|
|
import static edu.wpi.first.units.Units.Second;
|
|
|
|
|
import static edu.wpi.first.units.Units.Seconds;
|
|
|
|
|
import static edu.wpi.first.units.Units.Value;
|
|
|
|
|
import static edu.wpi.first.units.Units.Volts;
|
|
|
|
|
import static edu.wpi.first.units.Units.Watts;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
|
|
class UnitsTest {
|
|
|
|
|
// Be accurate to 0.01%
|
|
|
|
|
private static final double thresh = 1e-5;
|
|
|
|
|
|
|
|
|
|
void testBaseUnit(Unit<?> baseUnit) {
|
|
|
|
|
assertEquals(0, baseUnit.of(0).baseUnitMagnitude(), 0);
|
|
|
|
|
assertEquals(1, baseUnit.of(1).baseUnitMagnitude(), 0);
|
|
|
|
|
assertEquals(-1, baseUnit.of(-1).baseUnitMagnitude(), 0);
|
|
|
|
|
assertEquals(100, baseUnit.of(100).baseUnitMagnitude(), 0);
|
|
|
|
|
assertEquals(8.281723, baseUnit.of(8.281723).baseUnitMagnitude(), 0);
|
|
|
|
|
assertEquals(Float.MAX_VALUE, baseUnit.of(Float.MAX_VALUE).baseUnitMagnitude(), 0);
|
|
|
|
|
assertEquals(Float.MIN_VALUE, baseUnit.of(Float.MIN_VALUE).baseUnitMagnitude(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Distances
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMeters() {
|
|
|
|
|
testBaseUnit(Meters);
|
|
|
|
|
assertEquals("Meter", Meters.name());
|
|
|
|
|
assertEquals("m", Meters.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMillimeters() {
|
|
|
|
|
assertEquals(1000, Millimeters.convertFrom(1, Meters), thresh);
|
|
|
|
|
assertEquals(1, Meters.convertFrom(1000, Millimeters), thresh);
|
|
|
|
|
assertEquals("Millimeter", Millimeters.name());
|
|
|
|
|
assertEquals("mm", Millimeters.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testCentimeters() {
|
|
|
|
|
assertEquals(100, Centimeters.convertFrom(1, Meters), thresh);
|
|
|
|
|
assertEquals(1, Meters.convertFrom(100, Centimeters), thresh);
|
|
|
|
|
assertEquals("Centimeter", Centimeters.name());
|
|
|
|
|
assertEquals("cm", Centimeters.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testInches() {
|
|
|
|
|
assertEquals(1, Meters.convertFrom(39.3701, Inches), thresh);
|
|
|
|
|
assertEquals(39.3701, Inches.convertFrom(1, Meters), 1e-4);
|
|
|
|
|
assertEquals(1 / 25.4, Inches.convertFrom(1, Millimeters), 0); // should be exact
|
|
|
|
|
assertEquals(12, Inches.convertFrom(1, Feet), thresh);
|
|
|
|
|
assertEquals("Inch", Inches.name());
|
|
|
|
|
assertEquals("in", Inches.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testFeet() {
|
|
|
|
|
assertEquals(3.28084, Feet.convertFrom(1, Meters), thresh);
|
|
|
|
|
assertEquals(1 / 12.0, Feet.convertFrom(1, Inches), thresh);
|
|
|
|
|
assertEquals("Foot", Feet.name());
|
|
|
|
|
assertEquals("ft", Feet.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Velocities
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMetersPerSecond() {
|
|
|
|
|
testBaseUnit(MetersPerSecond);
|
|
|
|
|
assertEquals("Meter per Second", MetersPerSecond.name());
|
|
|
|
|
assertEquals("m/s", MetersPerSecond.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testFeetPerSecond() {
|
|
|
|
|
assertEquals(3.28084, FeetPerSecond.convertFrom(1, MetersPerSecond), thresh);
|
|
|
|
|
assertEquals(1 / 3.28084, MetersPerSecond.convertFrom(1, FeetPerSecond), thresh);
|
|
|
|
|
assertEquals("Foot per Second", FeetPerSecond.name());
|
|
|
|
|
assertEquals("ft/s", FeetPerSecond.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Accelerations
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMetersPerSecondPerSecond() {
|
|
|
|
|
testBaseUnit(MetersPerSecondPerSecond);
|
|
|
|
|
assertEquals("Meter per Second per Second", MetersPerSecondPerSecond.name());
|
|
|
|
|
assertEquals("m/s/s", MetersPerSecondPerSecond.symbol());
|
|
|
|
|
assertEquals(MetersPerSecond, MetersPerSecondPerSecond.getUnit());
|
|
|
|
|
assertEquals(Seconds, MetersPerSecondPerSecond.getPeriod());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testGs() {
|
|
|
|
|
assertEquals(32.17405, Gs.of(1).in(FeetPerSecond.per(Second)), thresh);
|
|
|
|
|
assertEquals(9.80665, Gs.of(1).in(MetersPerSecondPerSecond), thresh);
|
|
|
|
|
assertEquals("G", Gs.name());
|
|
|
|
|
assertEquals("G", Gs.symbol());
|
|
|
|
|
assertEquals(Units.AnonymousBaseUnit, Gs.getUnit());
|
|
|
|
|
assertEquals(Seconds, Gs.getPeriod());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Time
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testSeconds() {
|
|
|
|
|
testBaseUnit(Seconds);
|
|
|
|
|
assertEquals("Second", Seconds.name());
|
|
|
|
|
assertEquals("s", Seconds.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMillisecond() {
|
|
|
|
|
assertEquals(1000, Milliseconds.convertFrom(1, Seconds), thresh);
|
|
|
|
|
assertEquals("Millisecond", Milliseconds.name());
|
|
|
|
|
assertEquals("ms", Milliseconds.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMicrosecond() {
|
|
|
|
|
assertEquals(1e6, Microseconds.convertFrom(1, Second), 0);
|
|
|
|
|
assertEquals("Microsecond", Microseconds.name());
|
|
|
|
|
assertEquals("us", Microseconds.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMinutes() {
|
|
|
|
|
assertEquals(60, Seconds.convertFrom(1, Minutes), thresh);
|
|
|
|
|
assertEquals("Minute", Minutes.name());
|
|
|
|
|
assertEquals("min", Minutes.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mass
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testKilograms() {
|
|
|
|
|
testBaseUnit(Kilograms);
|
|
|
|
|
assertEquals("Kilogram", Kilograms.name());
|
|
|
|
|
assertEquals("Kg", Kilograms.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testGrams() {
|
|
|
|
|
assertEquals(1000, Grams.convertFrom(1, Kilograms), thresh);
|
|
|
|
|
assertEquals("Gram", Grams.name());
|
|
|
|
|
assertEquals("g", Grams.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testPounds() {
|
|
|
|
|
assertEquals(453.592, Grams.convertFrom(1, Pounds), thresh);
|
|
|
|
|
assertEquals("Pound", Pounds.name());
|
|
|
|
|
assertEquals("lb.", Pounds.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testOunces() {
|
|
|
|
|
assertEquals(16, Ounces.convertFrom(1, Pounds), thresh);
|
|
|
|
|
assertEquals("Ounce", Ounces.name());
|
|
|
|
|
assertEquals("oz.", Ounces.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Angle
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testRevolutions() {
|
2023-11-17 11:45:04 -05:00
|
|
|
assertEquals(1, Revolutions.convertFrom(2 * Math.PI, Radians), thresh);
|
[wpiunits] Add subproject for a Java typesafe unit system (#5371)
# Background
Unit safety has always been a problem in WPILib. Any value corresponding to a physical measurement, such as current draw or distance traveled, is represented by a bare number with no unit tied to it; it's up to the programmer to know what units they're working and take care to remember that while working on their robot program. This leads to bugs when programmers accidentally mix units without knowing, or measure something (such as a wheel diameter) in one unit and program using another. `wpiunits` is intended to eliminate that class of bugs.
Another source of friction is the controllers and models in `wpimath` that expect all inputs to be in terms of SI units (meter, kilogram, and so on), while most FRC teams are US-based and most commonly use imperial units. wpimath does a good job of noting unit types in method names and argument names; however, it still relies on users properly converting values (and knowing they even have to do so).
# API
There are really only two core classes in this library: `Unit` and `Measure`. A `Unit` represents some dimension like distance or time. `Unit` is subclassed to define specific dimensions (eg `Distance` and `Time`) and those subclasses are instantiated to defined particular units in those dimensions, such as `Meters` and `Feet` being instances of the `Distance` class.
A `Measure` is a value tied to a particular dimension like distance and knows what unit that value is tied to. `Measure` has two implementations - one immutable and one mutable. The `Measure` interface only defines *read-only* operations; any API working with measurements should use the interface. The default implementation is `ImmutableMeasure`, which only implements those read-only operations and is useful for tracking constants. `MutableMeasure` also adds some methods that will allow for mutation of its internal state; this class is intended for use for things like sensors and controllers that track internal state and don't want to allocate new `Measure` objects every time something like `myEncoder.getDistance()` is called. However, the APIs for those methods should still only expose the read-only `Measure` interface so users can't (without casting or reflection) change the internal values.
A `Units` class provides convenient definitions for most of the commonly used unit types, such as `Meters`, `Feet`, and `Milliseconds`. I recommend static importing these units eg `import static edu.wpi.first.units.Units.Meters`) so they can be used like `Meters.of(1.234)` instead of `Units.Meters.of(1.234)`
# Examples
These examples are admittedly contrived. Users shouldn't be interacting much with measure objects themselves, since wpimath and wpilibj classes will be updated to support working with them; users will often just have to take a `Measure` output from one place (such as an encoder) and feed it as input to something else (such as a PID controller or kinematics model)
```java
// Using raw units
Encoder encoder = ...
int kPulsesPerRev = 2048;
double kWheelDiameterMeters = Units.inchesToMeters(6);
double kGearRatio = 10.86;
// always have to remember this encoder will output in meters!
encoder.setDistancePerPulse(kWheelDiameterMeters * Math.PI / (kGearRatio * kPulsesPerRev));
Command driveDistance(double distance) {
// have to know the distance argument needs to be in meters!
return run(this::driveStraight).until(() -> encoder.getDistance() >= distance);
}
// Oops! This will go 16 feet, not 5!
Command driveFiveFeet = driveDistance(5);
Command driveOneMeter = driveDistance(1);
```
```java
// Using wpiunits
Encoder encoder = ...
int kPulsesPerRev = 2048;
Measure<Distance> kWheelDiameter = Inches.of(6);
double kGearRatio = 10.86;
encoder.setDistancePerPulse(kWheelDiameter.times(Math.PI).divide(kGearRatio * kPulsesPerRev));
Command driveDistance(Measure<Distance> distance) {
// Measure#gte automatically handles unit conversions
return run(this::driveStraight).until(() -> encoder.getDistance().gte(distance));
}
// Users HAVE to be explicit about their units
Command driveFiveFeet = driveDistance(Feet.of(5));
Command driveOneMeter = driveDistance(Meters.of(1));
```
```java
SmartDashboard.putNumber("Temperature (C)", pdp.getTemperature().in(Celsius));
SmartDashboard.putNumber("Temperature (F)", pdp.getTemperature().in(Fahrenheit));
```
```java
var InchSecond = Inch.mult(Second); // new combined unit types can be user-defined
var InchPerSecond = Inch.per(Second);
PIDController<Distance, ElectricPotential> heightController = new PIDController<>(
/* kP */ Volts.of(0.2).per(Inch),
/* kI */ Volts.of(0.002).per(InchSecond),
/* kD */ Volts.of(0.008).per(InchPerSecond)
);
var elevatorTop = Feet.of(4).plus(Inches.of(6.125));
elevatorMotor.setVoltage(heightController.calculate(encoder.getDistance(), elevatorTop));
```
2023-07-23 17:18:17 -04:00
|
|
|
assertEquals("Revolution", Revolutions.name());
|
|
|
|
|
assertEquals("R", Revolutions.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testRadians() {
|
2023-11-17 11:45:04 -05:00
|
|
|
testBaseUnit(Radians);
|
[wpiunits] Add subproject for a Java typesafe unit system (#5371)
# Background
Unit safety has always been a problem in WPILib. Any value corresponding to a physical measurement, such as current draw or distance traveled, is represented by a bare number with no unit tied to it; it's up to the programmer to know what units they're working and take care to remember that while working on their robot program. This leads to bugs when programmers accidentally mix units without knowing, or measure something (such as a wheel diameter) in one unit and program using another. `wpiunits` is intended to eliminate that class of bugs.
Another source of friction is the controllers and models in `wpimath` that expect all inputs to be in terms of SI units (meter, kilogram, and so on), while most FRC teams are US-based and most commonly use imperial units. wpimath does a good job of noting unit types in method names and argument names; however, it still relies on users properly converting values (and knowing they even have to do so).
# API
There are really only two core classes in this library: `Unit` and `Measure`. A `Unit` represents some dimension like distance or time. `Unit` is subclassed to define specific dimensions (eg `Distance` and `Time`) and those subclasses are instantiated to defined particular units in those dimensions, such as `Meters` and `Feet` being instances of the `Distance` class.
A `Measure` is a value tied to a particular dimension like distance and knows what unit that value is tied to. `Measure` has two implementations - one immutable and one mutable. The `Measure` interface only defines *read-only* operations; any API working with measurements should use the interface. The default implementation is `ImmutableMeasure`, which only implements those read-only operations and is useful for tracking constants. `MutableMeasure` also adds some methods that will allow for mutation of its internal state; this class is intended for use for things like sensors and controllers that track internal state and don't want to allocate new `Measure` objects every time something like `myEncoder.getDistance()` is called. However, the APIs for those methods should still only expose the read-only `Measure` interface so users can't (without casting or reflection) change the internal values.
A `Units` class provides convenient definitions for most of the commonly used unit types, such as `Meters`, `Feet`, and `Milliseconds`. I recommend static importing these units eg `import static edu.wpi.first.units.Units.Meters`) so they can be used like `Meters.of(1.234)` instead of `Units.Meters.of(1.234)`
# Examples
These examples are admittedly contrived. Users shouldn't be interacting much with measure objects themselves, since wpimath and wpilibj classes will be updated to support working with them; users will often just have to take a `Measure` output from one place (such as an encoder) and feed it as input to something else (such as a PID controller or kinematics model)
```java
// Using raw units
Encoder encoder = ...
int kPulsesPerRev = 2048;
double kWheelDiameterMeters = Units.inchesToMeters(6);
double kGearRatio = 10.86;
// always have to remember this encoder will output in meters!
encoder.setDistancePerPulse(kWheelDiameterMeters * Math.PI / (kGearRatio * kPulsesPerRev));
Command driveDistance(double distance) {
// have to know the distance argument needs to be in meters!
return run(this::driveStraight).until(() -> encoder.getDistance() >= distance);
}
// Oops! This will go 16 feet, not 5!
Command driveFiveFeet = driveDistance(5);
Command driveOneMeter = driveDistance(1);
```
```java
// Using wpiunits
Encoder encoder = ...
int kPulsesPerRev = 2048;
Measure<Distance> kWheelDiameter = Inches.of(6);
double kGearRatio = 10.86;
encoder.setDistancePerPulse(kWheelDiameter.times(Math.PI).divide(kGearRatio * kPulsesPerRev));
Command driveDistance(Measure<Distance> distance) {
// Measure#gte automatically handles unit conversions
return run(this::driveStraight).until(() -> encoder.getDistance().gte(distance));
}
// Users HAVE to be explicit about their units
Command driveFiveFeet = driveDistance(Feet.of(5));
Command driveOneMeter = driveDistance(Meters.of(1));
```
```java
SmartDashboard.putNumber("Temperature (C)", pdp.getTemperature().in(Celsius));
SmartDashboard.putNumber("Temperature (F)", pdp.getTemperature().in(Fahrenheit));
```
```java
var InchSecond = Inch.mult(Second); // new combined unit types can be user-defined
var InchPerSecond = Inch.per(Second);
PIDController<Distance, ElectricPotential> heightController = new PIDController<>(
/* kP */ Volts.of(0.2).per(Inch),
/* kI */ Volts.of(0.002).per(InchSecond),
/* kD */ Volts.of(0.008).per(InchPerSecond)
);
var elevatorTop = Feet.of(4).plus(Inches.of(6.125));
elevatorMotor.setVoltage(heightController.calculate(encoder.getDistance(), elevatorTop));
```
2023-07-23 17:18:17 -04:00
|
|
|
assertEquals(2 * Math.PI, Radians.convertFrom(1, Revolutions), thresh);
|
|
|
|
|
assertEquals(2 * Math.PI, Radians.convertFrom(360, Degrees), thresh);
|
|
|
|
|
assertEquals("Radian", Radians.name());
|
|
|
|
|
assertEquals("rad", Radians.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testDegrees() {
|
|
|
|
|
assertEquals(360, Degrees.convertFrom(1, Revolutions), thresh);
|
|
|
|
|
assertEquals(360, Degrees.convertFrom(2 * Math.PI, Radians), thresh);
|
|
|
|
|
assertEquals("Degree", Degrees.name());
|
|
|
|
|
assertEquals("°", Degrees.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unitless
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testValue() {
|
|
|
|
|
testBaseUnit(Value);
|
|
|
|
|
assertEquals("<?>", Value.name());
|
|
|
|
|
assertEquals("<?>", Value.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testPercent() {
|
|
|
|
|
assertEquals(100, Percent.convertFrom(1, Value), thresh);
|
|
|
|
|
assertEquals("Percent", Percent.name());
|
|
|
|
|
assertEquals("%", Percent.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Electric potential
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testVolts() {
|
|
|
|
|
testBaseUnit(Volts);
|
|
|
|
|
assertEquals("Volt", Volts.name());
|
|
|
|
|
assertEquals("V", Volts.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMillivolts() {
|
|
|
|
|
assertEquals(1000, Millivolts.convertFrom(1, Volts), thresh);
|
|
|
|
|
assertEquals("Millivolt", Millivolts.name());
|
|
|
|
|
assertEquals("mV", Millivolts.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Electric current
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testAmps() {
|
|
|
|
|
testBaseUnit(Amps);
|
|
|
|
|
assertEquals("Amp", Amps.name());
|
|
|
|
|
assertEquals("A", Amps.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMilliamps() {
|
|
|
|
|
assertEquals(1000, Milliamps.convertFrom(1, Amps), thresh);
|
|
|
|
|
assertEquals("Milliamp", Milliamps.name());
|
|
|
|
|
assertEquals("mA", Milliamps.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Power
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testWatts() {
|
|
|
|
|
testBaseUnit(Watts);
|
|
|
|
|
assertEquals("Watt", Watts.name());
|
|
|
|
|
assertEquals("W", Watts.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMilliwatts() {
|
|
|
|
|
assertEquals(1000, Milliwatts.convertFrom(1, Watts), thresh);
|
|
|
|
|
assertEquals("Milliwatt", Milliwatts.name());
|
|
|
|
|
assertEquals("mW", Milliwatts.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testHorsepower() {
|
|
|
|
|
assertEquals(745.7, Watts.convertFrom(1, Horsepower), thresh);
|
|
|
|
|
assertEquals("Horsepower", Horsepower.name());
|
|
|
|
|
assertEquals("HP", Horsepower.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Temperature
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testKelvin() {
|
|
|
|
|
testBaseUnit(Kelvin);
|
|
|
|
|
assertEquals("Kelvin", Kelvin.name());
|
|
|
|
|
assertEquals("K", Kelvin.symbol()); // note: there's no degree symbol for Kelvin!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testCelsius() {
|
|
|
|
|
assertEquals(0, Celsius.of(-273.15).in(Kelvin), thresh);
|
|
|
|
|
assertEquals(273.15, Celsius.of(0).in(Kelvin), thresh);
|
|
|
|
|
assertEquals(0, Kelvin.of(273.15).in(Celsius), thresh);
|
|
|
|
|
assertTrue(Celsius.of(0).isEquivalent(Kelvin.of(273.15)));
|
|
|
|
|
assertTrue(Celsius.of(-273.15).isEquivalent(Kelvin.of(0)));
|
|
|
|
|
assertEquals("Celsius", Celsius.name());
|
|
|
|
|
assertEquals("°C", Celsius.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testFahrenheit() {
|
|
|
|
|
assertEquals(0, Fahrenheit.of(32).in(Celsius), thresh);
|
|
|
|
|
assertEquals(100, Fahrenheit.of(212).in(Celsius), thresh);
|
|
|
|
|
assertEquals(-459.67, Kelvin.of(0).in(Fahrenheit), thresh);
|
|
|
|
|
assertEquals(273.15, Fahrenheit.of(32).in(Kelvin), thresh);
|
|
|
|
|
assertEquals(32, Kelvin.of(273.15).in(Fahrenheit), thresh);
|
|
|
|
|
assertTrue(Fahrenheit.of(32).isEquivalent(Celsius.of(0)));
|
|
|
|
|
assertTrue(Fahrenheit.of(212).isEquivalent(Celsius.of(100)));
|
|
|
|
|
assertEquals("Fahrenheit", Fahrenheit.name());
|
|
|
|
|
assertEquals("°F", Fahrenheit.symbol());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testKilo() {
|
|
|
|
|
ExampleUnit unit = new ExampleUnit(1);
|
|
|
|
|
ExampleUnit kiloUnit = Kilo(unit);
|
|
|
|
|
assertEquals(1e3, unit.convertFrom(1, kiloUnit), 0);
|
|
|
|
|
assertEquals(1e-3, kiloUnit.convertFrom(1, unit), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void testMilli() {
|
|
|
|
|
ExampleUnit unit = new ExampleUnit(1);
|
|
|
|
|
ExampleUnit milliUnit = Milli(unit);
|
|
|
|
|
assertEquals(1e-3, unit.convertFrom(1, milliUnit), 0);
|
|
|
|
|
assertEquals(1e3, milliUnit.convertFrom(1, unit), 0);
|
|
|
|
|
}
|
|
|
|
|
}
|