[wpimath] Add direction to slew rate limiter (#4377)

This commit is contained in:
ohowe
2022-09-01 15:46:46 -06:00
committed by GitHub
parent fafc81ed1a
commit b5a17f762c
4 changed files with 94 additions and 16 deletions

View File

@@ -14,29 +14,50 @@ import edu.wpi.first.util.WPIUtilJNI;
* edu.wpi.first.math.trajectory.TrapezoidProfile} instead.
*/
public class SlewRateLimiter {
private final double m_rateLimit;
private final double m_positiveRateLimit;
private final double m_negativeRateLimit;
private double m_prevVal;
private double m_prevTime;
/**
* Creates a new SlewRateLimiter with the given rate limit and initial value.
* Creates a new SlewRateLimiter with the given positive and negative rate limits and initial
* value.
*
* @param rateLimit The rate-of-change limit, in units per second.
* @param positiveRateLimit The rate-of-change limit in the positive direction, in units per
* second. This is expected to be positive.
* @param negativeRateLimit The rate-of-change limit in the negative direction, in units per
* second. This is expected to be negative.
* @param initialValue The initial value of the input.
*/
public SlewRateLimiter(double rateLimit, double initialValue) {
m_rateLimit = rateLimit;
public SlewRateLimiter(double positiveRateLimit, double negativeRateLimit, double initialValue) {
m_positiveRateLimit = positiveRateLimit;
m_negativeRateLimit = negativeRateLimit;
m_prevVal = initialValue;
m_prevTime = WPIUtilJNI.now() * 1e-6;
}
/**
* Creates a new SlewRateLimiter with the given rate limit and an initial value of zero.
* Creates a new SlewRateLimiter with the given positive rate limit and negative rate limit of
* -rateLimit and initial value.
*
* @param rateLimit The rate-of-change limit, in units per second.
* @param initalValue The initial value of the input.
* @deprecated Use SlewRateLimiter(double positiveRateLimit, double negativeRateLimit, double
* initalValue) instead.
*/
@Deprecated(since = "2023", forRemoval = true)
public SlewRateLimiter(double rateLimit, double initalValue) {
this(rateLimit, -rateLimit, initalValue);
}
/**
* Creates a new SlewRateLimiter with the given positive rate limit and negative rate limit of
* -rateLimit.
*
* @param rateLimit The rate-of-change limit, in units per second.
*/
public SlewRateLimiter(double rateLimit) {
this(rateLimit, 0);
this(rateLimit, -rateLimit, 0);
}
/**
@@ -49,7 +70,10 @@ public class SlewRateLimiter {
double currentTime = WPIUtilJNI.now() * 1e-6;
double elapsedTime = currentTime - m_prevTime;
m_prevVal +=
MathUtil.clamp(input - m_prevVal, -m_rateLimit * elapsedTime, m_rateLimit * elapsedTime);
MathUtil.clamp(
input - m_prevVal,
m_negativeRateLimit * elapsedTime,
m_positiveRateLimit * elapsedTime);
m_prevTime = currentTime;
return m_prevVal;
}