mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpimath] Add copySignPow to MathUtil for joystick input shaping (#8013)
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user