mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpimath] Move SlewRateLimiter from wpilib to wpimath (#3399)
Timer was replaced with wpi::Now() to avoid a dependency on other wpilib classes.
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
// 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.
|
||||
|
||||
package edu.wpi.first.math.filter;
|
||||
|
||||
import edu.wpi.first.math.MathUtil;
|
||||
import edu.wpi.first.wpiutil.WPIUtilJNI;
|
||||
|
||||
/**
|
||||
* A class that limits the rate of change of an input value. Useful for implementing voltage,
|
||||
* setpoint, and/or output ramps. A slew-rate limit is most appropriate when the quantity being
|
||||
* controlled is a velocity or a voltage; when controlling a position, consider using a {@link
|
||||
* edu.wpi.first.math.trajectory.TrapezoidProfile} instead.
|
||||
*/
|
||||
public class SlewRateLimiter {
|
||||
private final double m_rateLimit;
|
||||
private double m_prevVal;
|
||||
private double m_prevTime;
|
||||
|
||||
/**
|
||||
* Creates a new SlewRateLimiter with the given rate limit and initial value.
|
||||
*
|
||||
* @param rateLimit The rate-of-change limit, in units per second.
|
||||
* @param initialValue The initial value of the input.
|
||||
*/
|
||||
public SlewRateLimiter(double rateLimit, double initialValue) {
|
||||
m_rateLimit = rateLimit;
|
||||
m_prevVal = initialValue;
|
||||
m_prevTime = WPIUtilJNI.now() * 1e-6;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new SlewRateLimiter with the given rate limit and an initial value of zero.
|
||||
*
|
||||
* @param rateLimit The rate-of-change limit, in units per second.
|
||||
*/
|
||||
public SlewRateLimiter(double rateLimit) {
|
||||
this(rateLimit, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the input to limit its slew rate.
|
||||
*
|
||||
* @param input The input value whose slew rate is to be limited.
|
||||
* @return The filtered value, which will not change faster than the slew rate.
|
||||
*/
|
||||
public double calculate(double input) {
|
||||
double currentTime = WPIUtilJNI.now() * 1e-6;
|
||||
double elapsedTime = currentTime - m_prevTime;
|
||||
m_prevVal +=
|
||||
MathUtil.clamp(input - m_prevVal, -m_rateLimit * elapsedTime, m_rateLimit * elapsedTime);
|
||||
m_prevTime = currentTime;
|
||||
return m_prevVal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the slew rate limiter to the specified value; ignores the rate limit when doing so.
|
||||
*
|
||||
* @param value The value to reset to.
|
||||
*/
|
||||
public void reset(double value) {
|
||||
m_prevVal = value;
|
||||
m_prevTime = WPIUtilJNI.now() * 1e-6;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user