2020-12-26 14:12:05 -08:00
|
|
|
// 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.
|
2019-08-28 23:24:30 -07:00
|
|
|
|
2021-05-01 15:53:30 +00:00
|
|
|
package edu.wpi.first.math;
|
2019-08-28 23:24:30 -07:00
|
|
|
|
2019-12-04 20:39:12 -08:00
|
|
|
public final class MathUtil {
|
|
|
|
|
private MathUtil() {
|
2019-08-28 23:24:30 -07:00
|
|
|
throw new AssertionError("utility class");
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-30 01:01:20 -05:00
|
|
|
/**
|
|
|
|
|
* Returns value clamped between low and high boundaries.
|
|
|
|
|
*
|
|
|
|
|
* @param value Value to clamp.
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param low The lower boundary to which to clamp value.
|
|
|
|
|
* @param high The higher boundary to which to clamp value.
|
2021-06-10 20:46:47 -07:00
|
|
|
* @return The clamped value.
|
2019-12-30 01:01:20 -05:00
|
|
|
*/
|
|
|
|
|
public static int clamp(int value, int low, int high) {
|
|
|
|
|
return Math.max(low, Math.min(value, high));
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 23:24:30 -07:00
|
|
|
/**
|
|
|
|
|
* Returns value clamped between low and high boundaries.
|
|
|
|
|
*
|
|
|
|
|
* @param value Value to clamp.
|
2020-12-29 22:45:16 -08:00
|
|
|
* @param low The lower boundary to which to clamp value.
|
|
|
|
|
* @param high The higher boundary to which to clamp value.
|
2021-06-10 20:46:47 -07:00
|
|
|
* @return The clamped value.
|
2019-08-28 23:24:30 -07:00
|
|
|
*/
|
|
|
|
|
public static double clamp(double value, double low, double high) {
|
|
|
|
|
return Math.max(low, Math.min(value, high));
|
|
|
|
|
}
|
2020-07-13 15:53:16 -04:00
|
|
|
|
2021-08-28 20:52:05 -07:00
|
|
|
/**
|
|
|
|
|
* Returns 0.0 if the given value is within the specified range around zero. The remaining range
|
2022-04-30 23:29:48 -04:00
|
|
|
* between the deadband and the maximum magnitude is scaled from 0.0 to the maximum magnitude.
|
2021-08-28 20:52:05 -07:00
|
|
|
*
|
|
|
|
|
* @param value Value to clip.
|
|
|
|
|
* @param deadband Range around zero.
|
2022-04-30 23:29:48 -04:00
|
|
|
* @param maxMagnitude The maximum magnitude of the input. Can be infinite.
|
2021-08-28 20:52:05 -07:00
|
|
|
* @return The value after the deadband is applied.
|
|
|
|
|
*/
|
2022-04-30 23:29:48 -04:00
|
|
|
public static double applyDeadband(double value, double deadband, double maxMagnitude) {
|
2021-08-28 20:52:05 -07:00
|
|
|
if (Math.abs(value) > deadband) {
|
2022-04-30 23:29:48 -04:00
|
|
|
if (maxMagnitude / deadband > 1.0e12) {
|
|
|
|
|
// If max magnitude is sufficiently large, the implementation encounters
|
|
|
|
|
// roundoff error. Implementing the limiting behavior directly avoids
|
|
|
|
|
// the problem.
|
|
|
|
|
return value > 0.0 ? value - deadband : value + deadband;
|
|
|
|
|
}
|
2021-08-28 20:52:05 -07:00
|
|
|
if (value > 0.0) {
|
2022-04-30 23:29:48 -04:00
|
|
|
// Map deadband to 0 and map max to max.
|
|
|
|
|
//
|
|
|
|
|
// y - y₁ = m(x - x₁)
|
|
|
|
|
// y - y₁ = (y₂ - y₁)/(x₂ - x₁) (x - x₁)
|
|
|
|
|
// y = (y₂ - y₁)/(x₂ - x₁) (x - x₁) + y₁
|
|
|
|
|
//
|
|
|
|
|
// (x₁, y₁) = (deadband, 0) and (x₂, y₂) = (max, max).
|
|
|
|
|
// x₁ = deadband
|
|
|
|
|
// y₁ = 0
|
|
|
|
|
// x₂ = max
|
|
|
|
|
// y₂ = max
|
|
|
|
|
//
|
|
|
|
|
// y = (max - 0)/(max - deadband) (x - deadband) + 0
|
|
|
|
|
// y = max/(max - deadband) (x - deadband)
|
|
|
|
|
// y = max (x - deadband)/(max - deadband)
|
|
|
|
|
return maxMagnitude * (value - deadband) / (maxMagnitude - deadband);
|
2021-08-28 20:52:05 -07:00
|
|
|
} else {
|
2022-04-30 23:29:48 -04:00
|
|
|
// Map -deadband to 0 and map -max to -max.
|
|
|
|
|
//
|
|
|
|
|
// y - y₁ = m(x - x₁)
|
|
|
|
|
// y - y₁ = (y₂ - y₁)/(x₂ - x₁) (x - x₁)
|
|
|
|
|
// y = (y₂ - y₁)/(x₂ - x₁) (x - x₁) + y₁
|
|
|
|
|
//
|
|
|
|
|
// (x₁, y₁) = (-deadband, 0) and (x₂, y₂) = (-max, -max).
|
|
|
|
|
// x₁ = -deadband
|
|
|
|
|
// y₁ = 0
|
|
|
|
|
// x₂ = -max
|
|
|
|
|
// y₂ = -max
|
|
|
|
|
//
|
|
|
|
|
// y = (-max - 0)/(-max + deadband) (x + deadband) + 0
|
|
|
|
|
// y = max/(max - deadband) (x + deadband)
|
|
|
|
|
// y = max (x + deadband)/(max - deadband)
|
|
|
|
|
return maxMagnitude * (value + deadband) / (maxMagnitude - deadband);
|
2021-08-28 20:52:05 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return 0.0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-30 23:29:48 -04:00
|
|
|
/**
|
|
|
|
|
* Returns 0.0 if the given value is within the specified range around zero. The remaining range
|
|
|
|
|
* between the deadband and 1.0 is scaled from 0.0 to 1.0.
|
|
|
|
|
*
|
|
|
|
|
* @param value Value to clip.
|
|
|
|
|
* @param deadband Range around zero.
|
|
|
|
|
* @return The value after the deadband is applied.
|
|
|
|
|
*/
|
|
|
|
|
public static double applyDeadband(double value, double deadband) {
|
|
|
|
|
return applyDeadband(value, deadband, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-13 15:53:16 -04:00
|
|
|
/**
|
2021-01-01 16:22:00 -08:00
|
|
|
* Returns modulus of input.
|
2020-07-13 15:53:16 -04:00
|
|
|
*
|
2021-01-01 16:22:00 -08:00
|
|
|
* @param input Input value to wrap.
|
|
|
|
|
* @param minimumInput The minimum value expected from the input.
|
|
|
|
|
* @param maximumInput The maximum value expected from the input.
|
2021-06-10 20:46:47 -07:00
|
|
|
* @return The wrapped value.
|
2020-07-13 15:53:16 -04:00
|
|
|
*/
|
2021-01-01 16:22:00 -08:00
|
|
|
public static double inputModulus(double input, double minimumInput, double maximumInput) {
|
|
|
|
|
double modulus = maximumInput - minimumInput;
|
|
|
|
|
|
|
|
|
|
// Wrap input if it's above the maximum input
|
|
|
|
|
int numMax = (int) ((input - minimumInput) / modulus);
|
|
|
|
|
input -= numMax * modulus;
|
|
|
|
|
|
|
|
|
|
// Wrap input if it's below the minimum input
|
|
|
|
|
int numMin = (int) ((input - maximumInput) / modulus);
|
|
|
|
|
input -= numMin * modulus;
|
|
|
|
|
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wraps an angle to the range -pi to pi radians.
|
|
|
|
|
*
|
|
|
|
|
* @param angleRadians Angle to wrap in radians.
|
2021-06-10 20:46:47 -07:00
|
|
|
* @return The wrapped angle.
|
2021-01-01 16:22:00 -08:00
|
|
|
*/
|
|
|
|
|
public static double angleModulus(double angleRadians) {
|
|
|
|
|
return inputModulus(angleRadians, -Math.PI, Math.PI);
|
2020-07-13 15:53:16 -04:00
|
|
|
}
|
2021-12-30 20:08:05 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Perform linear interpolation between two values.
|
|
|
|
|
*
|
|
|
|
|
* @param startValue The value to start at.
|
|
|
|
|
* @param endValue The value to end at.
|
|
|
|
|
* @param t How far between the two values to interpolate. This is clamped to [0, 1].
|
|
|
|
|
* @return The interpolated value.
|
|
|
|
|
*/
|
|
|
|
|
public static double interpolate(double startValue, double endValue, double t) {
|
|
|
|
|
return startValue + (endValue - startValue) * MathUtil.clamp(t, 0, 1);
|
|
|
|
|
}
|
2019-08-28 23:24:30 -07:00
|
|
|
}
|