WPILibC++ 2023.4.3
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 * Creates a new SlewRateLimiter with the given positive rate limit and
63 * negative rate limit of -rateLimit and initial value.
64 *
65 * @param rateLimit The rate-of-change limit.
66 * @param initialValue The initial value of the input.
67 */
68 WPI_DEPRECATED(
69 "Use SlewRateLimiter(Rate_t positiveRateLimit, Rate_t negativeRateLimit, "
70 "Unit_t initalValue) instead")
71 SlewRateLimiter(Rate_t rateLimit, Unit_t initialValue)
72 : SlewRateLimiter(rateLimit, -rateLimit, initialValue) {}
73
74 /**
75 * Filters the input to limit its slew rate.
76 *
77 * @param input The input value whose slew rate is to be limited.
78 * @return The filtered value, which will not change faster than the slew
79 * rate.
80 */
82 units::second_t currentTime = wpi::math::MathSharedStore::GetTimestamp();
83 units::second_t elapsedTime = currentTime - m_prevTime;
84 m_prevVal +=
85 std::clamp(input - m_prevVal, m_negativeRateLimit * elapsedTime,
86 m_positiveRateLimit * elapsedTime);
87 m_prevTime = currentTime;
88 return m_prevVal;
89 }
90
91 /**
92 * Resets the slew rate limiter to the specified value; ignores the rate limit
93 * when doing so.
94 *
95 * @param value The value to reset to.
96 */
98 m_prevVal = value;
100 }
101
102 private:
103 Rate_t m_positiveRateLimit;
104 Rate_t m_negativeRateLimit;
105 Unit_t m_prevVal;
106 units::second_t m_prevTime;
107};
108} // 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 Calculate(Unit_t input)
Filters the input to limit its slew rate.
Definition: SlewRateLimiter.h:81
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:97
Definition: core.h:1240
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: AprilTagFieldLayout.h:22