Class MathUtil
Every method is "pure": it only uses its inputs, so it is safe to call anywhere, any number of times. Docs: docs/modules/util.md
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleclamp(double value, double min, double max) Keeps a number inside a range.static intclamp(int value, int min, int max) Same asclamp(double, double, double)for whole numbers (for example encoder ticks).static doubledeadband(double value, double band) Ignores small values near zero.static doubledeadbandScaled(double value, double band) Likedeadband(double, double), but the output starts again from 0 at the edge of the band, so there is no sudden jump.static booleanisNear(double a, double b, double tolerance) Tells if two numbers are close enough.static doublelerp(double a, double b, double t) Finds the number part of the way fromatob.static double[]normalize(double[] values, double limit) Scales a group of numbers down together so that none is bigger thanlimit.
-
Method Details
-
clamp
public static double clamp(double value, double min, double max) Keeps a number inside a range.Example:
clamp(1.3, -1, 1)gives1.0.- Parameters:
value- the number to limitmin- the smallest allowed resultmax- the largest allowed result- Returns:
minif value is below it,maxif value is above it, else value. Returns0if value is NaN (not a number), so a bad reading never reaches a motor.
-
clamp
public static int clamp(int value, int min, int max) Same asclamp(double, double, double)for whole numbers (for example encoder ticks). (Whole numbers cannot be NaN, so there is no NaN check here.) -
deadband
public static double deadband(double value, double band) Ignores small values near zero.Joysticks rarely rest at exactly 0. Without a deadband the robot creeps. Example:
deadband(0.03, 0.05)gives0;deadband(0.4, 0.05)gives0.4.- Parameters:
value- the input, for example a stick valueband- values with a size below this become 0 (use a positive number, e.g. 0.05)- Returns:
- 0 inside the band, else the value unchanged
-
deadbandScaled
public static double deadbandScaled(double value, double band) Likedeadband(double, double), but the output starts again from 0 at the edge of the band, so there is no sudden jump. Use this for smooth, slow driving.Example with band 0.1: 0.05 gives 0, 0.1 gives 0, 0.55 gives 0.5, 1.0 gives 1.0.
- Parameters:
value- the input, from -1 to 1band- the dead zone size, from 0 to just under 1- Returns:
- the rescaled value, from -1 to 1
-
lerp
public static double lerp(double a, double b, double t) Finds the number part of the way fromatob.Example:
lerp(0, 10, 0.25)gives2.5. ("lerp" means linear interpolation.)- Parameters:
a- the result when t is 0b- the result when t is 1t- how far to go, usually 0 to 1 (not limited)- Returns:
- the number that far along, from a toward b
-
isNear
public static boolean isNear(double a, double b, double tolerance) Tells if two numbers are close enough.Example:
isNear(100, 103, 5)givestrue.- Parameters:
a- first numberb- second numbertolerance- the largest allowed difference- Returns:
- true if a and b differ by no more than the tolerance
-
normalize
public static double[] normalize(double[] values, double limit) Scales a group of numbers down together so that none is bigger thanlimit. The ratios between them stay the same. Used for wheel powers.Why keep the ratios? If one wheel asks for 2 and the others for 1, just cutting the 2 down to 1 would make the wheels equal and the robot would drive the wrong way. Scaling them all keeps the direction right.
Example:
normalize([2, 1, -1, 0], 1)gives[1, 0.5, -0.5, 0].- Parameters:
values- the numbers (changed in place and also returned)limit- the largest allowed size, usually 1- Returns:
- the same array, scaled
-