mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
[wpiunits] Add resistance units (#7168)
This commit is contained in:
committed by
GitHub
parent
a3b12b3bd9
commit
ac907f755a
@@ -25,6 +25,7 @@ import edu.wpi.first.units.measure.MomentOfInertia;
|
||||
import edu.wpi.first.units.measure.Mult;
|
||||
import edu.wpi.first.units.measure.Per;
|
||||
import edu.wpi.first.units.measure.Power;
|
||||
import edu.wpi.first.units.measure.Resistance;
|
||||
import edu.wpi.first.units.measure.Temperature;
|
||||
import edu.wpi.first.units.measure.Time;
|
||||
import edu.wpi.first.units.measure.Torque;
|
||||
@@ -246,6 +247,8 @@ public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
|
||||
return times(velocity);
|
||||
} else if (multiplier instanceof Voltage voltage) {
|
||||
return times(voltage);
|
||||
} else if (multiplier instanceof Resistance resistance) {
|
||||
return times(resistance);
|
||||
} else {
|
||||
// Dimensional analysis fallthrough or a generic input measure type
|
||||
// Do a basic unit multiplication
|
||||
@@ -530,6 +533,18 @@ public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
|
||||
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies this measure by a resistance and returns the resulting measure in the most
|
||||
* appropriate unit.
|
||||
*
|
||||
* @param multiplier the measurement to multiply by.
|
||||
* @return the multiplication result
|
||||
*/
|
||||
default Measure<?> times(Resistance multiplier) {
|
||||
return MultUnit.combine(unit(), multiplier.unit())
|
||||
.ofBaseUnits(baseUnitMagnitude() * multiplier.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
* Multiplies this measure by a conversion factor, returning the converted measurement. Unlike
|
||||
* {@link #times(Per)}, this allows for basic unit cancellation to return measurements of a known
|
||||
@@ -687,6 +702,8 @@ public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
|
||||
return divide(velocity);
|
||||
} else if (divisor instanceof Voltage voltage) {
|
||||
return divide(voltage);
|
||||
} else if (divisor instanceof Resistance resistance) {
|
||||
return divide(resistance);
|
||||
} else {
|
||||
// Dimensional analysis fallthrough or a generic input measure type
|
||||
// Do a basic unit multiplication
|
||||
@@ -957,6 +974,17 @@ public interface Measure<U extends Unit> extends Comparable<Measure<U>> {
|
||||
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides this measure by a resistance and returns the result in the most appropriate unit.
|
||||
*
|
||||
* @param divisor the measurement to divide by.
|
||||
* @return the division result
|
||||
*/
|
||||
default Measure<?> divide(Resistance divisor) {
|
||||
return PerUnit.combine(unit(), divisor.unit())
|
||||
.ofBaseUnits(baseUnitMagnitude() / divisor.baseUnitMagnitude());
|
||||
}
|
||||
|
||||
/**
|
||||
* Divides this measure by a ratio in terms of this measurement's unit to another unit, returning
|
||||
* a measurement in terms of the other unit.
|
||||
|
||||
104
wpiunits/src/main/java/edu/wpi/first/units/ResistanceUnit.java
Normal file
104
wpiunits/src/main/java/edu/wpi/first/units/ResistanceUnit.java
Normal file
@@ -0,0 +1,104 @@
|
||||
// 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 edu.wpi.first.units.measure.ImmutableResistance;
|
||||
import edu.wpi.first.units.measure.MutResistance;
|
||||
import edu.wpi.first.units.measure.Resistance;
|
||||
|
||||
/**
|
||||
* Unit of resistance dimension.
|
||||
*
|
||||
* <p>This is the base type for units of resistance dimension. It is also used to specify the
|
||||
* dimension for {@link Measure}: <code>Measure<ResistanceUnit></code>.
|
||||
*
|
||||
* <p>Actual units (such as {@link Units#Ohms} and {@link Units#KiloOhms}) can be found in the
|
||||
* {@link Units} class.
|
||||
*/
|
||||
public final class ResistanceUnit extends PerUnit<VoltageUnit, CurrentUnit> {
|
||||
private static final CombinatoryUnitCache<VoltageUnit, CurrentUnit, ResistanceUnit> cache =
|
||||
new CombinatoryUnitCache<>(ResistanceUnit::new);
|
||||
|
||||
ResistanceUnit(VoltageUnit numerator, CurrentUnit denominator) {
|
||||
super(
|
||||
numerator.isBaseUnit() && denominator.isBaseUnit()
|
||||
? null
|
||||
: combine(numerator.getBaseUnit(), denominator.getBaseUnit()),
|
||||
numerator,
|
||||
denominator);
|
||||
}
|
||||
|
||||
ResistanceUnit(
|
||||
ResistanceUnit baseUnit,
|
||||
UnaryFunction toBaseConverter,
|
||||
UnaryFunction fromBaseConverter,
|
||||
String name,
|
||||
String symbol) {
|
||||
super(baseUnit, toBaseConverter, fromBaseConverter, name, symbol);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines an voltage and a current unit to form a unit of resistance.
|
||||
*
|
||||
* @param voltage the unit of voltage
|
||||
* @param current the unit of current
|
||||
* @return the combined unit of resistance
|
||||
*/
|
||||
public static ResistanceUnit combine(VoltageUnit voltage, CurrentUnit current) {
|
||||
return cache.combine(voltage, current);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResistanceUnit getBaseUnit() {
|
||||
return (ResistanceUnit) super.getBaseUnit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resistance of(double magnitude) {
|
||||
return new ImmutableResistance(magnitude, toBaseUnits(magnitude), this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resistance ofBaseUnits(double baseUnitMagnitude) {
|
||||
return new ImmutableResistance(fromBaseUnits(baseUnitMagnitude), baseUnitMagnitude, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resistance zero() {
|
||||
return (Resistance) super.zero();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Resistance one() {
|
||||
return (Resistance) super.one();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MutResistance mutable(double initialMagnitude) {
|
||||
return new MutResistance(initialMagnitude, toBaseUnits(initialMagnitude), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ratio unit between this unit and an arbitrary other unit.
|
||||
*
|
||||
* @param other the other unit
|
||||
* @param <U> the type of the other unit
|
||||
* @return the ratio unit
|
||||
*/
|
||||
public <U extends Unit> PerUnit<ResistanceUnit, U> per(U other) {
|
||||
return PerUnit.combine(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a measurement value in terms of another power unit to this unit.
|
||||
*
|
||||
* @param magnitude the magnitude of the measurement in terms of the other power unit
|
||||
* @param otherUnit the other power unit
|
||||
* @return the value of the measurement in terms of this unit
|
||||
*/
|
||||
public double convertFrom(double magnitude, ResistanceUnit otherUnit) {
|
||||
return fromBaseUnits(otherUnit.toBaseUnits(magnitude));
|
||||
}
|
||||
}
|
||||
@@ -412,6 +412,25 @@ public final class Units {
|
||||
*/
|
||||
public static final CurrentUnit Milliamp = Milliamps; // alias
|
||||
|
||||
// ResistanceUnit
|
||||
/** The base unit of resistance. Equivalent to one {@link #Volt} per {@link #Amp}. */
|
||||
public static final ResistanceUnit Ohms = derive(Volts.per(Amp)).named("Ohm").symbol("Ω").make();
|
||||
|
||||
/** The base unit of resistance. Equivalent to one {@link #Volt} per {@link #Amp}. */
|
||||
public static final ResistanceUnit Ohm = Ohms; // alias
|
||||
|
||||
/** A unit equal to 1,000 {@link #Ohms}. */
|
||||
public static final ResistanceUnit KiloOhms = Kilo(Ohms);
|
||||
|
||||
/** A unit equal to 1,000 {@link #Ohms}. */
|
||||
public static final ResistanceUnit KiloOhm = KiloOhms; // alias
|
||||
|
||||
/** A unit equal to 1/1000 of a {@link #Ohm}. */
|
||||
public static final ResistanceUnit MilliOhms = Milli(Ohms);
|
||||
|
||||
/** A unit equal to 1/1000 of a {@link #Ohm}. */
|
||||
public static final ResistanceUnit MilliOhm = MilliOhms; // alias
|
||||
|
||||
// EnergyUnit
|
||||
/** The base unit of energy. */
|
||||
public static final EnergyUnit Joules = BaseUnits.EnergyUnit;
|
||||
|
||||
@@ -81,6 +81,16 @@ public final class VoltageUnit extends Unit {
|
||||
return VelocityUnit.combine(this, period);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines this unit of voltage with a unit of current to form a unit of resistance.
|
||||
*
|
||||
* @param currentUnit the unit of current
|
||||
* @return the combined resistance unit
|
||||
*/
|
||||
public ResistanceUnit per(CurrentUnit currentUnit) {
|
||||
return ResistanceUnit.combine(this, currentUnit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a generic ratio unit of this voltage to a different unit type.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user