Skip to content

Commit 2cb046b

Browse files
committed
first draft
1 parent 92f8c89 commit 2cb046b

File tree

13 files changed

+273
-187
lines changed

13 files changed

+273
-187
lines changed

wpiunits/generate_units.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def output(output_dir, outfn: str, contents: str):
7777
},
7878
"AngularVelocity": {
7979
"base_unit": "RadiansPerSecond",
80-
"multiply": {"Time": "Angle", "Frequency": "AngularAcceleration"},
80+
"multiply": {"Time": "Angle", "Frequency": "AngularAcceleration", "Torque" : "Power"},
8181
"divide": {"Time": "AngularAcceleration"},
8282
"extra": inspect.cleandoc(
8383
"""
@@ -222,7 +222,7 @@ def output(output_dir, outfn: str, contents: str):
222222
"multiply": {
223223
"Time": "Energy",
224224
},
225-
"divide": {"Voltage": "Current", "Current": "Voltage", "Energy": "Frequency"},
225+
"divide": {"Voltage": "Current", "Current": "Voltage", "Energy": "Frequency", "Torque" : "AngularVelocity", "AngularVelocity" : "Torque"},
226226
},
227227
"Resistance": {
228228
"base_unit": "Ohms",
@@ -258,7 +258,7 @@ def output(output_dir, outfn: str, contents: str):
258258
},
259259
"Torque": {
260260
"base_unit": "NewtonMeters",
261-
"multiply": {},
261+
"multiply": {"AngularVelocity : Power"},
262262
"divide": {"Distance": "Force", "Force": "Distance"},
263263
},
264264
"Velocity": {

wpiunits/src/main/java/edu/wpi/first/units/AngularVelocityUnit.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package edu.wpi.first.units;
66

7+
import static edu.wpi.first.units.Units.Watts;
8+
79
import edu.wpi.first.units.measure.AngularVelocity;
810
import edu.wpi.first.units.measure.ImmutableAngularVelocity;
911
import edu.wpi.first.units.measure.MutAngularVelocity;
@@ -72,6 +74,24 @@ public MutAngularVelocity mutable(double initialMagnitude) {
7274
return new MutAngularVelocity(initialMagnitude, toBaseUnits(initialMagnitude), this);
7375
}
7476

77+
/**
78+
* Constructs a unit of power equivalent to this unit of angular velocity multiplied by another
79+
* unit of torque. For example, {@code NewtonMeters.times(RadiansPerSecond)} will return a unit of
80+
* power equivalent to one Watt.
81+
*
82+
* @param torque the unit of torque
83+
* @param name the name of the resulting unit of power
84+
* @param symbol the symbol used to represent the unit of power
85+
* @return the power unit
86+
*/
87+
public PowerUnit mult(TorqueUnit torque, String name, String symbol) {
88+
double baseUnitEquivalent = torque.toBaseUnits(1) / this.toBaseUnits(1);
89+
UnaryFunction toBaseConverter = x -> x * baseUnitEquivalent;
90+
UnaryFunction fromBaseConverter = x -> x / baseUnitEquivalent;
91+
PowerUnit powerUnit = new PowerUnit(Watts, toBaseConverter, fromBaseConverter, name, symbol);
92+
return Units.derive(powerUnit).named(name).symbol(symbol).make();
93+
}
94+
7595
@Override
7696
public AngularAccelerationUnit per(TimeUnit period) {
7797
return AngularAccelerationUnit.combine(this, period);

wpiunits/src/main/java/edu/wpi/first/units/BaseUnits.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,9 @@ private BaseUnits() {
2525
/** The standard "unitless" unit. */
2626
public static final DimensionlessUnit Value = new DimensionlessUnit(null, 1, "<?>", "<?>");
2727

28-
/** The standard unit of voltage, volts. */
29-
public static final VoltageUnit VoltageUnit = new VoltageUnit(null, 1, "Volt", "V");
30-
3128
/** The standard unit of electric current, amperes. */
3229
public static final CurrentUnit CurrentUnit = new CurrentUnit(null, 1, "Amp", "A");
3330

34-
/** The standard unit of energy, joules. */
35-
public static final EnergyUnit EnergyUnit = new EnergyUnit(null, 1, "Joule", "J");
36-
3731
/** The standard unit of temperature, kelvin. */
3832
public static final TemperatureUnit TemperatureUnit =
3933
new TemperatureUnit(null, x -> x, x -> x, "Kelvin", "K");

wpiunits/src/main/java/edu/wpi/first/units/CurrentUnit.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package edu.wpi.first.units;
66

7+
import static edu.wpi.first.units.Units.Watts;
8+
79
import edu.wpi.first.units.measure.Current;
810
import edu.wpi.first.units.measure.ImmutableCurrent;
911
import edu.wpi.first.units.measure.MutCurrent;
@@ -43,12 +45,20 @@ public CurrentUnit getBaseUnit() {
4345
* milliwatt, and so on.
4446
*
4547
* @param voltage the voltage unit to multiply by
46-
* @param name the name of the resulting unit of power
47-
* @param symbol the symbol used to represent the unit of power
4848
* @return the power unit
4949
*/
50-
public PowerUnit mult(VoltageUnit voltage, String name, String symbol) {
51-
return Units.derive(PowerUnit.combine(voltage, this)).named(name).symbol(symbol).make();
50+
public PowerUnit mult(VoltageUnit voltage) {
51+
double baseUnitEquivalent = voltage.toBaseUnits(1) * this.toBaseUnits(1);
52+
UnaryFunction toBaseConverter = x -> x * baseUnitEquivalent;
53+
UnaryFunction fromBaseConverter = x -> x / baseUnitEquivalent;
54+
PowerUnit powerUnit =
55+
new PowerUnit(
56+
Watts,
57+
toBaseConverter,
58+
fromBaseConverter,
59+
this.name() + "-" + voltage.name(),
60+
this.symbol() + "*" + voltage.symbol());
61+
return powerUnit;
5262
}
5363

5464
@Override

wpiunits/src/main/java/edu/wpi/first/units/DistanceUnit.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,13 @@ public TorqueUnit multAsTorque(ForceUnit force) {
9898
return TorqueUnit.combine(this, force);
9999
}
100100

101-
// TODO: Add a multAsEnergy equivalent
101+
/**
102+
* Multiplies this distance unit by a unit of force to create a unit of energy.
103+
*
104+
* @param force the unit of force
105+
* @return the combined energy unit
106+
*/
107+
public EnergyUnit multAsEnergy(ForceUnit force) {
108+
return EnergyUnit.combine(force, this);
109+
}
102110
}

wpiunits/src/main/java/edu/wpi/first/units/EnergyUnit.java

Lines changed: 55 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@
1717
* <p>Actual units (such as {@link Units#Joules} and {@link Units#Kilojoules}) can be found in the
1818
* {@link Units} class.
1919
*/
20-
public final class EnergyUnit extends Unit {
20+
public final class EnergyUnit extends MultUnit<ForceUnit, DistanceUnit> {
21+
private static final CombinatoryUnitCache<ForceUnit, DistanceUnit, EnergyUnit> cache =
22+
new CombinatoryUnitCache<>(EnergyUnit::new);
23+
24+
EnergyUnit(ForceUnit force, DistanceUnit distance) {
25+
super(
26+
force.isBaseUnit() && distance.isBaseUnit()
27+
? null
28+
: combine(force.getBaseUnit(), distance.getBaseUnit()),
29+
force,
30+
distance);
31+
}
32+
2133
EnergyUnit(
2234
EnergyUnit baseUnit,
2335
UnaryFunction toBaseConverter,
@@ -27,46 +39,20 @@ public final class EnergyUnit extends Unit {
2739
super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
2840
}
2941

30-
EnergyUnit(EnergyUnit baseUnit, double baseUnitEquivalent, String name, String symbol) {
31-
super(baseUnit, baseUnitEquivalent, name, symbol);
32-
}
33-
34-
@Override
35-
public EnergyUnit getBaseUnit() {
36-
return (EnergyUnit) super.getBaseUnit();
37-
}
38-
39-
/**
40-
* Combines this unit of energy with a unit of time to create a unit of power.
41-
*
42-
* @param period the period of the change in energy
43-
* @return the combined unit of power
44-
*/
45-
@Override
46-
public PowerUnit per(TimeUnit period) {
47-
return PowerUnit.combine(this, period);
48-
}
49-
5042
/**
51-
* Creates a ratio unit between this unit and an arbitrary other unit.
43+
* Combines a force and distance to form a unit of energy.
5244
*
53-
* @param other the other unit
54-
* @param <U> the type of the other unit
55-
* @return the ratio unit
45+
* @param force the unit of force
46+
* @param distance the unit of distance
47+
* @return the combined unit of energy
5648
*/
57-
public <U extends Unit> PerUnit<EnergyUnit, U> per(U other) {
58-
return PerUnit.combine(this, other);
49+
public static EnergyUnit combine(ForceUnit force, DistanceUnit distance) {
50+
return cache.combine(force, distance);
5951
}
6052

61-
/**
62-
* Converts a measurement value in terms of another unit to this unit.
63-
*
64-
* @param magnitude the magnitude of the measurement in terms of the other unit
65-
* @param otherUnit the other unit
66-
* @return the value of the measurement in terms of this unit
67-
*/
68-
public double convertFrom(double magnitude, EnergyUnit otherUnit) {
69-
return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
53+
@Override
54+
public EnergyUnit getBaseUnit() {
55+
return (EnergyUnit) super.getBaseUnit();
7056
}
7157

7258
@Override
@@ -89,8 +75,41 @@ public Energy one() {
8975
return (Energy) super.one();
9076
}
9177

78+
/**
79+
* Converts a measurement value in terms of another unit to this unit.
80+
*
81+
* @param magnitude the magnitude of the measurement in terms of the other unit
82+
* @param otherUnit the other unit
83+
* @return the value of the measurement in terms of this unit
84+
*/
85+
public double convertFrom(double magnitude, EnergyUnit otherUnit) {
86+
return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
87+
}
88+
9289
@Override
9390
public MutEnergy mutable(double initialMagnitude) {
9491
return new MutEnergy(initialMagnitude, toBaseUnits(initialMagnitude), this);
9592
}
93+
94+
/**
95+
* Combines this unit of energy with a unit of time to create a unit of power.
96+
*
97+
* @param period the period of the change in energy
98+
* @return the combined unit of power
99+
*/
100+
@Override
101+
public PowerUnit per(TimeUnit period) {
102+
return PowerUnit.combine(this, period);
103+
}
104+
105+
/**
106+
* Creates a ratio unit between this unit and an arbitrary other unit.
107+
*
108+
* @param other the other unit
109+
* @param <U> the type of the other unit
110+
* @return the ratio unit
111+
*/
112+
public <U extends Unit> PerUnit<EnergyUnit, U> per(U other) {
113+
return PerUnit.combine(this, other);
114+
}
96115
}

wpiunits/src/main/java/edu/wpi/first/units/ForceUnit.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ public TorqueUnit multAsTorque(DistanceUnit distance) {
5757
return TorqueUnit.combine(distance, this);
5858
}
5959

60-
// TODO: Add a multAsEnergy equivalent
60+
/**
61+
* Multiplies this force unit by a unit of distance to create a unit of energy.
62+
*
63+
* @param distance the unit of distance
64+
* @return the combined energy unit
65+
*/
66+
public EnergyUnit multAsEnergy(DistanceUnit distance) {
67+
return EnergyUnit.combine(this, distance);
68+
}
6169

6270
@Override
6371
public Force of(double magnitude) {

wpiunits/src/main/java/edu/wpi/first/units/PowerUnit.java

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
package edu.wpi.first.units;
66

7-
import static edu.wpi.first.units.Units.Joules;
8-
import static edu.wpi.first.units.Units.Seconds;
7+
import static edu.wpi.first.units.Units.Amps;
98

109
import edu.wpi.first.units.measure.ImmutablePower;
1110
import edu.wpi.first.units.measure.MutPower;
@@ -53,58 +52,6 @@ public static PowerUnit combine(EnergyUnit energy, TimeUnit period) {
5352
return cache.combine(energy, period);
5453
}
5554

56-
/**
57-
* Combines voltage and current into power.
58-
*
59-
* @param voltage the unit of voltage
60-
* @param current the unit of current
61-
* @return the combined unit of power
62-
*/
63-
public static PowerUnit combine(VoltageUnit voltage, CurrentUnit current) {
64-
return combine(
65-
new EnergyUnit(
66-
Joules,
67-
voltage.toBaseUnits(1) * current.toBaseUnits(1),
68-
voltage.name() + "-" + current.name(),
69-
voltage.symbol() + "*" + current.symbol()),
70-
Seconds);
71-
}
72-
73-
/**
74-
* Combines voltage and current into power.
75-
*
76-
* @param current the unit of current
77-
* @param voltage the unit of voltage
78-
* @return the combined unit of power
79-
*/
80-
public static PowerUnit combine(CurrentUnit current, VoltageUnit voltage) {
81-
return combine(voltage, current);
82-
}
83-
84-
/**
85-
* Combines angular velocity and torque into power. Useful when dealing with motors and flywheels.
86-
*
87-
* @param angularVelocity the unit of angular velocity
88-
* @param torque the unit of torque
89-
* @return the combined unit of power
90-
*/
91-
public static PowerUnit combine(AngularVelocityUnit angularVelocity, TorqueUnit torque) {
92-
return combine(
93-
new EnergyUnit(Joules, angularVelocity.toBaseUnits(1) * torque.toBaseUnits(1), "", ""),
94-
Seconds);
95-
}
96-
97-
/**
98-
* Combines angular velocity and torque into power. Useful when dealing with motors and flywheels.
99-
*
100-
* @param torque the unit of torque
101-
* @param angularVelocity the unit of angular velocity
102-
* @return the combined unit of power
103-
*/
104-
public static PowerUnit combine(TorqueUnit torque, AngularVelocityUnit angularVelocity) {
105-
return combine(angularVelocity, torque);
106-
}
107-
10855
@Override
10956
public PowerUnit getBaseUnit() {
11057
return (PowerUnit) super.getBaseUnit();
@@ -135,6 +82,32 @@ public MutPower mutable(double initialMagnitude) {
13582
return new MutPower(initialMagnitude, toBaseUnits(initialMagnitude), this);
13683
}
13784

85+
public VoltageUnit per(CurrentUnit current) {
86+
return VoltageUnit.combine(this, current);
87+
}
88+
89+
/**
90+
* Constructs a unit of current equivalent to this unit of power divided by another unit of
91+
* voltage. For example, {@code Watts.per(Volts)} will return a unit of power equivalent to one
92+
* Amp.
93+
*
94+
* @param voltage the voltage unit to multiply by
95+
* @return the power unit
96+
*/
97+
public CurrentUnit per(VoltageUnit voltage) {
98+
double baseUnitEquivalent = this.toBaseUnits(1) / voltage.toBaseUnits(1);
99+
UnaryFunction toBaseConverter = x -> x * baseUnitEquivalent;
100+
UnaryFunction fromBaseConverter = x -> x / baseUnitEquivalent;
101+
CurrentUnit currentUnit =
102+
new CurrentUnit(
103+
Amps,
104+
toBaseConverter,
105+
fromBaseConverter,
106+
this.name() + " per " + voltage.name(),
107+
this.symbol() + "/" + voltage.symbol());
108+
return Units.derive(currentUnit).make();
109+
}
110+
138111
@Override
139112
public VelocityUnit<PowerUnit> per(TimeUnit time) {
140113
return VelocityUnit.combine(this, time);

wpiunits/src/main/java/edu/wpi/first/units/TorqueUnit.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package edu.wpi.first.units;
66

7+
import static edu.wpi.first.units.Units.Watts;
8+
79
import edu.wpi.first.units.measure.ImmutableTorque;
810
import edu.wpi.first.units.measure.MutTorque;
911
import edu.wpi.first.units.measure.Torque;
@@ -72,6 +74,24 @@ public MutTorque mutable(double initialMagnitude) {
7274
return new MutTorque(initialMagnitude, toBaseUnits(initialMagnitude), this);
7375
}
7476

77+
/**
78+
* Constructs a unit of power equivalent to this unit of torque multiplied by another unit of
79+
* angular velocity. For example, {@code NewtonMeters.times(RadiansPerSecond)} will return a unit
80+
* of power equivalent to one Watt.
81+
*
82+
* @param angularVelocity the unit of angular velocity
83+
* @param name the name of the resulting unit of power
84+
* @param symbol the symbol used to represent the unit of power
85+
* @return the power unit
86+
*/
87+
public PowerUnit mult(AngularVelocityUnit angularVelocity, String name, String symbol) {
88+
double baseUnitEquivalent = this.toBaseUnits(1) / angularVelocity.toBaseUnits(1);
89+
UnaryFunction toBaseConverter = x -> x * baseUnitEquivalent;
90+
UnaryFunction fromBaseConverter = x -> x / baseUnitEquivalent;
91+
PowerUnit powerUnit = new PowerUnit(Watts, toBaseConverter, fromBaseConverter, name, symbol);
92+
return Units.derive(powerUnit).named(name).symbol(symbol).make();
93+
}
94+
7595
@Override
7696
public VelocityUnit<TorqueUnit> per(TimeUnit time) {
7797
return VelocityUnit.combine(this, time);

0 commit comments

Comments
 (0)