[wpimath] Add copySignPow to MathUtil for joystick input shaping (#8013)

This commit is contained in:
Michael Lesirge
2025-06-15 14:08:41 -07:00
committed by GitHub
parent e2517b7a21
commit fb399eef3d
6 changed files with 173 additions and 8 deletions

View File

@@ -107,6 +107,42 @@ public final class MathUtil {
return applyDeadband(value, deadband, 1);
}
/**
* Raises the input to the power of the given exponent while preserving its sign.
*
* <p>The function normalizes the input value to the range [0, 1] based on the maximum magnitude,
* raises it to the power of the exponent, then scales the result back to the original range and
* copying the sign. This keeps the value in the original range and gives consistent curve
* behavior regardless of the input value's scale.
*
* <p>This is useful for applying smoother or more aggressive control response curves (e.g.
* joystick input shaping).
*
* @param value The input value to transform.
* @param exponent The exponent to apply (e.g. 1.0 = linear, 2.0 = squared curve). Must be
* positive.
* @param maxMagnitude The maximum expected absolute value of input. Must be positive.
* @return The transformed value with the same sign and scaled to the input range.
*/
public static double copySignPow(double value, double exponent, double maxMagnitude) {
return Math.copySign(Math.pow(Math.abs(value) / maxMagnitude, exponent), value) * maxMagnitude;
}
/**
* Raises the input to the power of the given exponent while preserving its sign.
*
* <p>This is useful for applying smoother or more aggressive control response curves (e.g.
* joystick input shaping).
*
* @param value The input value to transform.
* @param exponent The exponent to apply (e.g. 1.0 = linear, 2.0 = squared curve). Must be
* positive.
* @return The transformed value with the same sign.
*/
public static double copySignPow(double value, double exponent) {
return copySignPow(value, exponent, 1);
}
/**
* Returns modulus of input.
*