Class MathUtil

java.lang.Object
com.crossbotics.ftchelper.util.MathUtil

public final class MathUtil extends Object
Small math helpers that robot code needs all the time.

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 Type
    Method
    Description
    static double
    clamp(double value, double min, double max)
    Keeps a number inside a range.
    static int
    clamp(int value, int min, int max)
    Same as clamp(double, double, double) for whole numbers (for example encoder ticks).
    static double
    deadband(double value, double band)
    Ignores small values near zero.
    static double
    deadbandScaled(double value, double band)
    Like deadband(double, double), but the output starts again from 0 at the edge of the band, so there is no sudden jump.
    static boolean
    isNear(double a, double b, double tolerance)
    Tells if two numbers are close enough.
    static double
    lerp(double a, double b, double t)
    Finds the number part of the way from a to b.
    static double[]
    normalize(double[] values, double limit)
    Scales a group of numbers down together so that none is bigger than limit.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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) gives 1.0.

      Parameters:
      value - the number to limit
      min - the smallest allowed result
      max - the largest allowed result
      Returns:
      min if value is below it, max if value is above it, else value. Returns 0 if 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 as clamp(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) gives 0; deadband(0.4, 0.05) gives 0.4.

      Parameters:
      value - the input, for example a stick value
      band - 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)
      Like deadband(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 1
      band - 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 from a to b.

      Example: lerp(0, 10, 0.25) gives 2.5. ("lerp" means linear interpolation.)

      Parameters:
      a - the result when t is 0
      b - the result when t is 1
      t - 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) gives true.

      Parameters:
      a - first number
      b - second number
      tolerance - 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 than limit. 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