generated from wpilibsuite/vendor-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UnitUtils class with clampMeasurement method (#67)
- Loading branch information
Showing
3 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
wpilib_interface/src/main/java/coppercore/wpilib_interface/UnitUtils.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package coppercore.wpilib_interface; | ||
|
||
import edu.wpi.first.units.Measure; | ||
import edu.wpi.first.units.Unit; | ||
|
||
/** Utilities for dealing with the WPILib units library. */ | ||
public final class UnitUtils { | ||
/** | ||
* Given a measurement, a minimum value, and a maximum value, return a clamped measurement | ||
* within those bounds. | ||
* | ||
* <p>Note: If for some reason min is greater than max, measure < min takes precedence over | ||
* measure < max, which takes precedence over min < measure < max. Therefore, when | ||
* debugging, if this function is strangely always returning min it is highly likely there is an | ||
* error in the generation of bounds. | ||
* | ||
* @param <M> A measure, the type of the measures being compoared. This should be able to be | ||
* inferred automatically. | ||
* @param <U> The Unit measured by M. This should be able to be inferred automatically. | ||
* @param measure The measure being clamped. If it is greater than min and less than max, it | ||
* will be the result. | ||
* @param min The minimum value. If measure < min, min will be returned | ||
* @param max The maximum value. If measure > max, max will be returned. | ||
* @return If measure is within the bounds, measure. If measure is less than min, min. If | ||
* measure is greater than max, max. | ||
*/ | ||
public static final <M extends Measure<U>, U extends Unit> M clampMeasure( | ||
M measure, M min, M max) { | ||
if (measure.lt(min)) { | ||
return min; | ||
} else if (measure.gt(max)) { | ||
return max; | ||
} | ||
|
||
return measure; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import coppercore.wpilib_interface.UnitUtils; | ||
import edu.wpi.first.units.Units; | ||
// import edu.wpi.first.units.Units.Meters; | ||
import edu.wpi.first.units.Units.*; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class UnitUtilsTests { | ||
@Test | ||
public void clampWithinBounds() { | ||
Assertions.assertTrue( | ||
UnitUtils.clampMeasure( | ||
Units.Meters.of(2.0), Units.Meters.of(1.0), Units.Meters.of(3.0)) | ||
.equals(Units.Meters.of(2.0))); | ||
} | ||
|
||
@Test | ||
public void clampBelowBounds() { | ||
Assertions.assertTrue( | ||
UnitUtils.clampMeasure( | ||
Units.Meters.of(0.0), Units.Meters.of(1.0), Units.Meters.of(3.0)) | ||
.equals(Units.Meters.of(1.0))); | ||
} | ||
|
||
@Test | ||
public void clampAboveBounds() { | ||
Assertions.assertTrue( | ||
UnitUtils.clampMeasure( | ||
Units.Meters.of(4.0), Units.Meters.of(1.0), Units.Meters.of(3.0)) | ||
.equals(Units.Meters.of(3.0))); | ||
} | ||
} |