WPILibC++ 2024.1.1-beta-4
TrapezoidProfile.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 <wpi/deprecated.h>
8
9#include "units/time.h"
10#include "wpimath/MathShared.h"
11
12namespace frc {
13
14/**
15 * A trapezoid-shaped velocity profile.
16 *
17 * While this class can be used for a profiled movement from start to finish,
18 * the intended usage is to filter a reference's dynamics based on trapezoidal
19 * velocity constraints. To compute the reference obeying this constraint, do
20 * the following.
21 *
22 * Initialization:
23 * @code{.cpp}
24 * TrapezoidProfile::Constraints constraints{kMaxV, kMaxA};
25 * double previousProfiledReference = initialReference;
26 * TrapezoidProfile profile{constraints};
27 * @endcode
28 *
29 * Run on update:
30 * @code{.cpp}
31 * previousProfiledReference = profile.Calculate(timeSincePreviousUpdate,
32 * previousProfiledReference,
33 * unprofiledReference);
34 * @endcode
35 *
36 * where `unprofiledReference` is free to change between calls. Note that when
37 * the unprofiled reference is within the constraints, `Calculate()` returns the
38 * unprofiled reference unchanged.
39 *
40 * Otherwise, a timer can be started to provide monotonic values for
41 * `Calculate()` and to determine when the profile has completed via
42 * `IsFinished()`.
43 */
44template <class Distance>
46 public:
48 using Velocity =
54
56 public:
60 }
61 Constraints(Velocity_t maxVelocity_, Acceleration_t maxAcceleration_)
62 : maxVelocity{maxVelocity_}, maxAcceleration{maxAcceleration_} {
65 }
68 };
69
70 class State {
71 public:
74 bool operator==(const State&) const = default;
75 };
76
77 /**
78 * Construct a TrapezoidProfile.
79 *
80 * @param constraints The constraints on the profile, like maximum velocity.
81 */
82 TrapezoidProfile(Constraints constraints); // NOLINT
83
84 /**
85 * Construct a TrapezoidProfile.
86 *
87 * @param constraints The constraints on the profile, like maximum velocity.
88 * @param goal The desired state when the profile is complete.
89 * @param initial The initial state (usually the current state).
90 * @deprecated Pass the desired and current state into calculate instead of
91 * constructing a new TrapezoidProfile with the desired and current state
92 */
93 WPI_DEPRECATED(
94 "Pass the desired and current state into calculate instead of "
95 "constructing a new TrapezoidProfile with the desired and current "
96 "state")
97 TrapezoidProfile(Constraints constraints, State goal,
98 State initial = State{Distance_t{0}, Velocity_t{0}});
99
104
105 /**
106 * Calculate the correct position and velocity for the profile at a time t
107 * where the beginning of the profile was at time t = 0.
108 *
109 * @param t The time since the beginning of the profile.
110 * @deprecated Pass the desired and current state into calculate instead of
111 * constructing a new TrapezoidProfile with the desired and current state
112 */
113 [[deprecated(
114 "Pass the desired and current state into calculate instead of "
115 "constructing a new TrapezoidProfile with the desired and current "
116 "state")]]
117 State Calculate(units::second_t t) const;
118
119 /**
120 * Calculate the correct position and velocity for the profile at a time t
121 * where the beginning of the profile was at time t = 0.
122 *
123 * @param t The time since the beginning of the profile.
124 * @param current The initial state (usually the current state).
125 * @param goal The desired state when the profile is complete.
126 */
127 State Calculate(units::second_t t, State current, State goal);
128
129 /**
130 * Returns the time left until a target distance in the profile is reached.
131 *
132 * @param target The target distance.
133 */
134 units::second_t TimeLeftUntil(Distance_t target) const;
135
136 /**
137 * Returns the total time the profile takes to reach the goal.
138 */
139 units::second_t TotalTime() const { return m_endDeccel; }
140
141 /**
142 * Returns true if the profile has reached the goal.
143 *
144 * The profile has reached the goal if the time since the profile started
145 * has exceeded the profile's total time.
146 *
147 * @param t The time since the beginning of the profile.
148 */
149 bool IsFinished(units::second_t t) const { return t >= TotalTime(); }
150
151 private:
152 /**
153 * Returns true if the profile inverted.
154 *
155 * The profile is inverted if goal position is less than the initial position.
156 *
157 * @param initial The initial state (usually the current state).
158 * @param goal The desired state when the profile is complete.
159 */
160 static bool ShouldFlipAcceleration(const State& initial, const State& goal) {
161 return initial.position > goal.position;
162 }
163
164 // Flip the sign of the velocity and position if the profile is inverted
165 State Direct(const State& in) const {
166 State result = in;
167 result.position *= m_direction;
168 result.velocity *= m_direction;
169 return result;
170 }
171
172 // The direction of the profile, either 1 for forwards or -1 for inverted
173 int m_direction;
174
175 Constraints m_constraints;
176 State m_current;
177 State m_goal; // TODO: remove
178 bool m_newAPI; // TODO: remove
179
180 units::second_t m_endAccel;
181 units::second_t m_endFullSpeed;
182 units::second_t m_endDeccel;
183};
184} // namespace frc
185
186#include "TrapezoidProfile.inc"
Definition: TrapezoidProfile.h:55
Constraints()
Definition: TrapezoidProfile.h:57
Velocity_t maxVelocity
Definition: TrapezoidProfile.h:66
Constraints(Velocity_t maxVelocity_, Acceleration_t maxAcceleration_)
Definition: TrapezoidProfile.h:61
Acceleration_t maxAcceleration
Definition: TrapezoidProfile.h:67
Definition: TrapezoidProfile.h:70
Velocity_t velocity
Definition: TrapezoidProfile.h:73
bool operator==(const State &) const =default
Distance_t position
Definition: TrapezoidProfile.h:72
A trapezoid-shaped velocity profile.
Definition: TrapezoidProfile.h:45
TrapezoidProfile & operator=(const TrapezoidProfile &)=default
TrapezoidProfile(Constraints constraints)
Construct a TrapezoidProfile.
Definition: TrapezoidProfile.inc:14
units::compound_unit< Velocity, units::inverse< units::seconds > > Acceleration
Definition: TrapezoidProfile.h:52
units::compound_unit< Distance, units::inverse< units::seconds > > Velocity
Definition: TrapezoidProfile.h:49
TrapezoidProfile(const TrapezoidProfile &)=default
State Calculate(units::second_t t) const
Calculate the correct position and velocity for the profile at a time t where the beginning of the pr...
Definition: TrapezoidProfile.inc:67
units::second_t TimeLeftUntil(Distance_t target) const
Returns the time left until a target distance in the profile is reached.
Definition: TrapezoidProfile.inc:171
units::second_t TotalTime() const
Returns the total time the profile takes to reach the goal.
Definition: TrapezoidProfile.h:139
bool IsFinished(units::second_t t) const
Returns true if the profile has reached the goal.
Definition: TrapezoidProfile.h:149
TrapezoidProfile & operator=(TrapezoidProfile &&)=default
units::unit_t< Velocity > Velocity_t
Definition: TrapezoidProfile.h:50
TrapezoidProfile(TrapezoidProfile &&)=default
static void ReportUsage(MathUsageId id, int count)
Definition: MathShared.h:73
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1446
constexpr auto in(type t, int set) -> bool
Definition: core.h:611
Definition: AprilTagPoseEstimator.h:15