WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
SlewRateLimiter.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <algorithm>
8
10#include "wpi/units/time.hpp"
11
12namespace wpi::math {
13/**
14 * A class that limits the rate of change of an input value. Useful for
15 * implementing voltage, setpoint, and/or output ramps. A slew-rate limit
16 * is most appropriate when the quantity being controlled is a velocity or
17 * a voltage; when controlling a position, consider using a TrapezoidProfile
18 * instead.
19 *
20 * @see TrapezoidProfile
21 */
22template <class Unit>
24 public:
25 using Unit_t = wpi::units::unit_t<Unit>;
26 using Rate =
27 wpi::units::compound_unit<Unit, wpi::units::inverse<wpi::units::seconds>>;
28 using Rate_t = wpi::units::unit_t<Rate>;
29
30 /**
31 * Creates a new SlewRateLimiter with the given positive and negative rate
32 * limits and initial value.
33 *
34 * @param positiveRateLimit The rate-of-change limit in the positive
35 * direction, in units per second. This is expected
36 * to be positive.
37 * @param negativeRateLimit The rate-of-change limit in the negative
38 * direction, in units per second. This is expected
39 * to be negative.
40 * @param initialValue The initial value of the input.
41 */
42 SlewRateLimiter(Rate_t positiveRateLimit, Rate_t negativeRateLimit,
43 Unit_t initialValue = Unit_t{0})
44 : m_positiveRateLimit{positiveRateLimit},
45 m_negativeRateLimit{negativeRateLimit},
46 m_prevVal{initialValue},
47 m_prevTime{wpi::units::microsecond_t{
48 wpi::math::MathSharedStore::GetTimestamp()}} {}
49
50 /**
51 * Creates a new SlewRateLimiter with the given positive rate limit and
52 * negative rate limit of -rateLimit.
53 *
54 * @param rateLimit The rate-of-change limit.
55 */
56 explicit SlewRateLimiter(Rate_t rateLimit)
57 : SlewRateLimiter(rateLimit, -rateLimit) {}
58
59 /**
60 * Filters the input to limit its slew rate.
61 *
62 * @param input The input value whose slew rate is to be limited.
63 * @return The filtered value, which will not change faster than the slew
64 * rate.
65 */
67 wpi::units::second_t currentTime =
69 wpi::units::second_t elapsedTime = currentTime - m_prevTime;
70 m_prevVal +=
71 std::clamp(input - m_prevVal, m_negativeRateLimit * elapsedTime,
72 m_positiveRateLimit * elapsedTime);
73 m_prevTime = currentTime;
74 return m_prevVal;
75 }
76
77 /**
78 * Returns the value last calculated by the SlewRateLimiter.
79 *
80 * @return The last value.
81 */
82 Unit_t LastValue() const { return m_prevVal; }
83
84 /**
85 * Resets the slew rate limiter to the specified value; ignores the rate limit
86 * when doing so.
87 *
88 * @param value The value to reset to.
89 */
90 void Reset(Unit_t value) {
91 m_prevVal = value;
93 }
94
95 /**
96 * Sets the rate-of-change limit to the given positive and negative rate
97 * limits.
98 *
99 * @param positiveRateLimit The rate-of-change limit in the positive
100 * direction, in units per second. This is expected to be positive.
101 * @param negativeRateLimit The rate-of-change limit in the negative
102 * direction, in units per second. This is expected to be negative.
103 */
104 void SetLimit(Rate_t positiveRateLimit, Rate_t negativeRateLimit) {
105 m_positiveRateLimit = positiveRateLimit;
106 m_negativeRateLimit = negativeRateLimit;
107 }
108
109 /**
110 * Sets the rate-of-change limit to the given positive rate limit and negative
111 * rate limit of -rateLimit.
112 *
113 * @param rateLimit The rate-of-change limit in both directions, in units per
114 * second. This is expected to be positive.
115 */
116 void SetLimit(Rate_t rateLimit) {
117 m_positiveRateLimit = rateLimit;
118 m_negativeRateLimit = -rateLimit;
119 }
120
121 private:
122 Rate_t m_positiveRateLimit;
123 Rate_t m_negativeRateLimit;
124 Unit_t m_prevVal;
125 wpi::units::second_t m_prevTime;
126};
127} // namespace wpi::math
Definition MathShared.hpp:37
static wpi::units::second_t GetTimestamp()
Definition MathShared.hpp:65
wpi::units::compound_unit< Unit, wpi::units::inverse< wpi::units::seconds > > Rate
Definition SlewRateLimiter.hpp:26
SlewRateLimiter(Rate_t positiveRateLimit, Rate_t negativeRateLimit, Unit_t initialValue=Unit_t{0})
Creates a new SlewRateLimiter with the given positive and negative rate limits and initial value.
Definition SlewRateLimiter.hpp:42
Unit_t LastValue() const
Returns the value last calculated by the SlewRateLimiter.
Definition SlewRateLimiter.hpp:82
void SetLimit(Rate_t positiveRateLimit, Rate_t negativeRateLimit)
Sets the rate-of-change limit to the given positive and negative rate limits.
Definition SlewRateLimiter.hpp:104
void Reset(Unit_t value)
Resets the slew rate limiter to the specified value; ignores the rate limit when doing so.
Definition SlewRateLimiter.hpp:90
Unit_t Calculate(Unit_t input)
Filters the input to limit its slew rate.
Definition SlewRateLimiter.hpp:66
wpi::units::unit_t< Rate > Rate_t
Definition SlewRateLimiter.hpp:28
void SetLimit(Rate_t rateLimit)
Sets the rate-of-change limit to the given positive rate limit and negative rate limit of -rateLimit.
Definition SlewRateLimiter.hpp:116
wpi::units::unit_t< Unit > Unit_t
Definition SlewRateLimiter.hpp:25
SlewRateLimiter(Rate_t rateLimit)
Creates a new SlewRateLimiter with the given positive rate limit and negative rate limit of -rateLimi...
Definition SlewRateLimiter.hpp:56
Definition LinearSystem.hpp:20
Definition CvSource.hpp:15