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