WPILibC++ 2025.0.0-alpha-1-24-g6478ba6
SlewRateLimiter.h
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
9#include <wpi/timestamp.h>
10
11#include "units/time.h"
12#include "wpimath/MathShared.h"
13
14namespace frc {
15/**
16 * A class that limits the rate of change of an input value. Useful for
17 * implementing voltage, setpoint, and/or output ramps. A slew-rate limit
18 * is most appropriate when the quantity being controlled is a velocity or
19 * a voltage; when controlling a position, consider using a TrapezoidProfile
20 * instead.
21 *
22 * @see TrapezoidProfile
23 */
24template <class Unit>
26 public:
30
31 /**
32 * Creates a new SlewRateLimiter with the given positive and negative rate
33 * limits and initial value.
34 *
35 * @param positiveRateLimit The rate-of-change limit in the positive
36 * direction, in units per second. This is expected
37 * to be positive.
38 * @param negativeRateLimit The rate-of-change limit in the negative
39 * direction, in units per second. This is expected
40 * to be negative.
41 * @param initialValue The initial value of the input.
42 */
43 SlewRateLimiter(Rate_t positiveRateLimit, Rate_t negativeRateLimit,
44 Unit_t initialValue = Unit_t{0})
45 : m_positiveRateLimit{positiveRateLimit},
46 m_negativeRateLimit{negativeRateLimit},
47 m_prevVal{initialValue},
48 m_prevTime{
49 units::microsecond_t{wpi::math::MathSharedStore::GetTimestamp()}} {}
50
51 /**
52 * Creates a new SlewRateLimiter with the given positive rate limit and
53 * negative rate limit of -rateLimit.
54 *
55 * @param rateLimit The rate-of-change limit.
56 */
57 explicit SlewRateLimiter(Rate_t rateLimit)
58 : SlewRateLimiter(rateLimit, -rateLimit) {}
59
60 /**
61 * Filters the input to limit its slew rate.
62 *
63 * @param input The input value whose slew rate is to be limited.
64 * @return The filtered value, which will not change faster than the slew
65 * rate.
66 */
68 units::second_t currentTime = wpi::math::MathSharedStore::GetTimestamp();
69 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 private:
96 Rate_t m_positiveRateLimit;
97 Rate_t m_negativeRateLimit;
98 Unit_t m_prevVal;
99 units::second_t m_prevTime;
100};
101} // namespace frc
A class that limits the rate of change of an input value.
Definition: SlewRateLimiter.h:25
SlewRateLimiter(Rate_t rateLimit)
Creates a new SlewRateLimiter with the given positive rate limit and negative rate limit of -rateLimi...
Definition: SlewRateLimiter.h:57
units::unit_t< Rate > Rate_t
Definition: SlewRateLimiter.h:29
Unit_t LastValue() const
Returns the value last calculated by the SlewRateLimiter.
Definition: SlewRateLimiter.h:82
Unit_t Calculate(Unit_t input)
Filters the input to limit its slew rate.
Definition: SlewRateLimiter.h:67
units::unit_t< Unit > Unit_t
Definition: SlewRateLimiter.h:27
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.h:43
units::compound_unit< Unit, units::inverse< units::seconds > > Rate
Definition: SlewRateLimiter.h:28
void Reset(Unit_t value)
Resets the slew rate limiter to the specified value; ignores the rate limit when doing so.
Definition: SlewRateLimiter.h:90
static units::second_t GetTimestamp()
Definition: MathShared.h:77
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1437
Definition: AprilTagDetector_cv.h:11