Created
January 30, 2015 02:26
-
-
Save ThomasJClark/e690d76bb6d9ee8c0b8c to your computer and use it in GitHub Desktop.
Units and Measures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wpi.first.wpilibj.measures.dimensions; | |
public class Angle implements Dimension {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wpi.first.wpilibj.measures.dimensions; | |
public class AngularVelocity implements Dimension {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wpi.first.wpilibj.measures.dimensions; | |
public interface Dimension {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wpi.first.wpilibj.measures.dimensions; | |
public class Distance implements Dimension {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wpi.first.wpilibj.measures; | |
import edu.wpi.first.wpilibj.measures.dimensions.Dimension; | |
public class Measure<D extends Dimension> { | |
double value; | |
Unit<D> unit; | |
protected Measure(double value, Unit<D> unit) { | |
this.set(value, unit); | |
} | |
public void set(double value, Unit<D> unit) { | |
this.value = value; | |
this.unit = unit; | |
} | |
public double as(Unit<D> unit) { | |
return unit.fromBaseUnits(this.unit.toBaseUnits(this.value)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wpi.first.wpilibj.measures; | |
import edu.wpi.first.wpilibj.measures.dimensions.Dimension; | |
/** | |
* Instances of Unit define the conversion factors for each unit of a | |
* dimension. They do not hold specific values themselves. | |
* @see Units | |
*/ | |
public class Unit<D extends Dimension> { | |
double base; | |
/** | |
* @param base The amount of this unit per SI base unit. | |
*/ | |
public Unit(double base) { | |
this.base = base; | |
} | |
public double fromBaseUnits(double value) { | |
return value / this.base; | |
} | |
public double toBaseUnits(double value) { | |
return value * this.base; | |
} | |
/** | |
* Convenience method to create a measure of this unit. | |
*/ | |
public Measure<D> make(double value) { | |
return new Measure<D>(value, this); | |
} | |
/** | |
* Convenience method to create a unit that is a scalar multiple of this unit. | |
*/ | |
public Unit<D> times(double factor) { | |
return new Unit<D>(this.base * factor); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.wpi.first.wpilibj.measures; | |
import edu.wpi.first.wpilibj.measures.dimensions.*; | |
public class Units { | |
public final static Unit<Distance> Meter = new Unit<Distance>(1); | |
public final static Unit<Distance> Inch = Meter.times(2.54e-2); | |
public final static Unit<Distance> Foot = Inch.times(12); | |
public final static Unit<Distance> Yard = Foot.times(3); | |
public final static Unit<Velocity> MetersPerSecond = new Unit<Velocity>(1); | |
public final static Unit<Velocity> FeetPerSecond = new Unit<Velocity>(0.3048); | |
public final static Unit<Velocity> MilesPerHour = new Unit<Velocity>(0.44704); | |
public final static Unit<Angle> Radian = new Unit<Angle>(1); | |
public final static Unit<Angle> Revolution = new Unit<Angle>(2 * Math.PI); | |
public final static Unit<Angle> Degree = new Unit<Angle>(Math.PI / 180); | |
public final static Unit<AngularVelocity> RadiansPerSecond = new Unit<AngularVelocity>(1); | |
public final static Unit<AngularVelocity> DegreesPerSecond = RadiansPerSecond.times(180 / Math.PI); | |
public final static Unit<AngularVelocity> RPM = RadiansPerSecond.times(2*Math.PI / 60); | |
public final static Unit<ElectricPotential> Volt = new Unit<ElectricPotential>(1); | |
public final static Unit<ElectricCurrent> Amp = new Unit<ElectricCurrent>(1); | |
public final static Unit<ElectricCurrent> Milliamp = new Unit<ElectricCurrent>(0.001); | |
public final static Unit<Dimensionless> Value = new Unit<Dimensionless>(1); | |
public final static Unit<Dimensionless> Percent = Value.times(0.01); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment