Skip to content

Commit 4072bf3

Browse files
Gold856calcmogul
andauthored
Update TrapezoidProfile docs (#2301)
* Update TrapezoidProfile docs * Update source/docs/software/advanced-controls/controllers/trapezoidal-profiles.rst Co-authored-by: Tyler Veness <[email protected]> --------- Co-authored-by: Tyler Veness <[email protected]>
1 parent 7ac0fed commit 4072bf3

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

source/docs/software/advanced-controls/controllers/trapezoidal-profiles.rst

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Putting It All Together
6565

6666
.. note:: C++ is often able to infer the type of the inner classes, and thus a simple initializer list (without the class name) can be sent as a parameter. The full class names are included in the example below for clarity.
6767

68-
Now that we know how to create a set of constraints and the desired start/end states, we are ready to create our motion profile. The ``TrapezoidProfile`` constructor takes 3 parameters, in order: the constraints, the goal state, and the initial state.
68+
Now that we know how to create a set of constraints and the desired start/end states, we are ready to create our motion profile. The ``TrapezoidProfile`` constructor takes 1 parameter: the constraints.
6969

7070
.. tabs::
7171

@@ -74,65 +74,63 @@ Now that we know how to create a set of constraints and the desired start/end st
7474
// Creates a new TrapezoidProfile
7575
// Profile will have a max vel of 5 meters per second
7676
// Profile will have a max acceleration of 10 meters per second squared
77-
// Profile will end stationary at 5 meters
78-
// Profile will start stationary at zero position
79-
TrapezoidProfile profile = new TrapezoidProfile(new TrapezoidProfile.Constraints(5, 10),
80-
new TrapezoidProfile.State(5, 0),
81-
new TrapezoidProfile.State(0, 0));
77+
TrapezoidProfile profile = new TrapezoidProfile(new TrapezoidProfile.Constraints(5, 10));
8278

8379
.. code-tab:: c++
8480

8581
// Creates a new TrapezoidProfile
8682
// Profile will have a max vel of 5 meters per second
8783
// Profile will have a max acceleration of 10 meters per second squared
88-
// Profile will end stationary at 5 meters
89-
// Profile will start stationary at zero position
9084
frc::TrapezoidProfile<units::meters> profile{
91-
frc::TrapezoidProfile<units::meters>::Constraints{5_mps, 10_mps_sq},
92-
frc::TrapezoidProfile<units::meters>::State{5_m, 0_mps},
93-
frc::TrapezoidProfile<units::meters>::State{0_m, 0_mps}};
85+
frc::TrapezoidProfile<units::meters>::Constraints{5_mps, 10_mps_sq}};
9486

9587
Using a ``TrapezoidProfile``
9688
----------------------------
9789

9890
Sampling the Profile
9991
^^^^^^^^^^^^^^^^^^^^
10092

101-
Once we've created a ``TrapezoidProfile``, using it is very simple: to get the profile state at the given time after the profile has started, call the ``calculate()`` method:
93+
Once we've created a ``TrapezoidProfile``, using it is very simple: to get the profile state at the given time after the profile has started, call the ``calculate()`` method with the goal state and initial state:
10294

10395
.. tabs::
10496

10597
.. code-tab:: java
10698

99+
// Profile will end stationary at 5 meters
100+
// Profile will start stationary at zero position
107101
// Returns the motion profile state after 5 seconds of motion
108-
profile.calculate(5);
102+
profile.calculate(5, new TrapezoidProfile.State(5, 0), new TrapezoidProfile.State(0, 0));
109103

110104
.. code-tab:: c++
111105

106+
// Profile will end stationary at 5 meters
107+
// Profile will start stationary at zero position
112108
// Returns the motion profile state after 5 seconds of motion
113-
profile.Calculate(5_s);
109+
profile.Calculate(5_s,
110+
frc::TrapezoidProfile<units::meters>::State{5_m, 0_mps},
111+
frc::TrapezoidProfile<units::meters>::State{0_m, 0_mps});
114112

115113
Using the State
116114
^^^^^^^^^^^^^^^
117115

118-
The ``calculate`` method returns a ``TrapezoidProfile.State`` class (the same one that was used to specify the initial/end states when constructing the profile). To use this for actual control, simply pass the contained position and velocity values to whatever controller you wish (for example, a PIDController):
116+
The ``calculate`` method returns a ``TrapezoidProfile.State`` class (the same one that was used to specify the initial/end states when calculating the profile state). To use this for actual control, simply pass the contained position and velocity values to whatever controller you wish (for example, a PIDController):
119117

120118
.. tabs::
121119

122120
.. code-tab:: java
123121

124-
var setpoint = profile.calculate(elapsedTime);
122+
var setpoint = profile.calculate(elapsedTime, goalState, initialState);
125123
controller.calculate(encoder.getDistance(), setpoint.position);
126124

127125
.. code-tab:: c++
128126

129-
auto setpoint = profile.Calculate(elapsedTime);
127+
auto setpoint = profile.Calculate(elapsedTime, goalState, initialState);
130128
controller.Calculate(encoder.GetDistance(), setpoint.position.value());
131129

132130
Complete Usage Example
133131
----------------------
134132

135-
.. note:: In this example, the profile is re-computed every timestep. This is a somewhat different usage technique than is detailed above, but works according to the same principles - the profile is sampled at a time corresponding to the loop period to get the setpoint for the next loop iteration.
133+
.. note:: In this example, the initial state is re-computed every timestep. This is a somewhat different usage technique than is detailed above, but works according to the same principles - the profile is sampled at a time corresponding to the loop period to get the setpoint for the next loop iteration.
136134

137135
A more complete example of ``TrapezoidProfile`` usage is provided in the ElevatorTrapezoidProfile example project (`Java <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/elevatortrapezoidprofile>`__, `C++ <https://github.com/wpilibsuite/allwpilib/tree/main/wpilibcExamples/src/main/cpp/examples/ElevatorTrapezoidProfile/cpp>`__):
138136

source/docs/software/commandbased/profile-subsystems-commands.rst

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ The ``TrapezoidProfileCommand`` class (`Java <https://github.wpilib.org/allwpili
144144
Creating a TrapezoidProfileCommand
145145
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
146146

147-
A ``TrapezoidProfileCommand`` can be created two ways - by subclassing the ``TrapezoidProfileCommand`` class, or by defining the command :ref:`inline <docs/software/commandbased/organizing-command-based:Inline Commands>`. Both methods ultimately extremely similar, and ultimately the choice of which to use comes down to where the user desires that the relevant code be located.
147+
A ``TrapezoidProfileCommand`` can be created two ways - by subclassing the ``TrapezoidProfileCommand`` class, or by defining the command :ref:`inline <docs/software/commandbased/organizing-command-based:Inline Commands>`. Both methods are ultimately extremely similar, and ultimately the choice of which to use comes down to where the user desires that the relevant code be located.
148148

149149
.. note:: If subclassing ``TrapezoidProfileCommand`` and overriding any methods, make sure to call the ``super`` version of those methods! Otherwise, motion profiling functionality will not work properly.
150150

@@ -154,30 +154,40 @@ In either case, a ``TrapezoidProfileCommand`` is created by passing the necessar
154154

155155
.. group-tab:: Java
156156

157-
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2023.4.3/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/TrapezoidProfileCommand.java
157+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibNewCommands/src/main/java/edu/wpi/first/wpilibj2/command/TrapezoidProfileCommand.java
158158
:language: java
159-
:lines: 25-34
159+
:lines: 28-43
160160
:linenos:
161-
:lineno-start: 25
161+
:lineno-start: 28
162162

163163
.. group-tab:: C++
164164

165-
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2023.4.3/wpilibNewCommands/src/main/native/include/frc2/command/TrapezoidProfileCommand.h
165+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibNewCommands/src/main/native/include/frc2/command/TrapezoidProfileCommand.h
166166
:language: c++
167-
:lines: 35-45
167+
:lines: 35-49
168168
:linenos:
169169
:lineno-start: 35
170170

171171
profile
172172
~~~~~~~
173173

174-
The ``profile`` parameter is the ``TrapezoidProfile`` object that will be executed by the command. By passing this in, users specify the start state, end state, and motion constraints of the profile that the command will use.
174+
The ``profile`` parameter is the ``TrapezoidProfile`` object that will be executed by the command. By passing this in, users specify the motion constraints of the profile that the command will use.
175175

176176
output
177177
~~~~~~
178178

179179
The ``output`` parameter is a function (usually passed as a :ref:`lambda <docs/software/commandbased/index:Lambda Expressions (Java)>`) that consumes the output and setpoint of the control loop. Passing in the ``useOutput`` function in ``PIDCommand`` is functionally analogous to overriding the `useState()`_ function in ``PIDSubsystem``.
180180

181+
goal
182+
~~~~
183+
184+
The ``goal`` parameter is a function that supplies the desired state of the motion profile. This can be used to change the goal at runtime if desired.
185+
186+
currentState
187+
~~~~~~~~~~~~
188+
189+
The ``currentState`` parameter is a function that supplies the starting state of the motion profile. Combined with ``goal``, this can be used to dynamically generate and follow any motion profile at runtime.
190+
181191
requirements
182192
~~~~~~~~~~~~
183193

@@ -192,23 +202,23 @@ What does a ``TrapezoidProfileSubsystem`` look like when used in practice? The
192202

193203
.. group-tab:: Java
194204

195-
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2023.4.3/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/drivedistanceoffboard/commands/DriveDistanceProfiled.java
205+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/drivedistanceoffboard/commands/DriveDistanceProfiled.java
196206
:language: java
197207
:lines: 5-
198208
:linenos:
199209
:lineno-start: 5
200210

201211
.. group-tab:: C++ (Header)
202212

203-
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2023.4.3/wpilibcExamples/src/main/cpp/examples/DriveDistanceOffboard/include/commands/DriveDistanceProfiled.h
213+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/examples/DriveDistanceOffboard/include/commands/DriveDistanceProfiled.h
204214
:language: c++
205215
:lines: 5-
206216
:linenos:
207217
:lineno-start: 5
208218

209219
.. group-tab:: C++ (Source)
210220

211-
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2023.4.3/wpilibcExamples/src/main/cpp/examples/DriveDistanceOffboard/cpp/commands/DriveDistanceProfiled.cpp
221+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/examples/DriveDistanceOffboard/cpp/commands/DriveDistanceProfiled.cpp
212222
:language: c++
213223
:lines: 5-
214224
:linenos:
@@ -220,16 +230,16 @@ And, for an :ref:`inlined <docs/software/commandbased/organizing-command-based:I
220230

221231
.. group-tab:: Java
222232

223-
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2023.4.3/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/drivedistanceoffboard/RobotContainer.java
233+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibjExamples/src/main/java/edu/wpi/first/wpilibj/examples/drivedistanceoffboard/RobotContainer.java
224234
:language: java
225-
:lines: 66-83
235+
:lines: 66-85
226236
:linenos:
227237
:lineno-start: 66
228238

229239
.. group-tab:: C++
230240

231-
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/v2023.4.3/wpilibcExamples/src/main/cpp/examples/DriveDistanceOffboard/cpp/RobotContainer.cpp
241+
.. remoteliteralinclude:: https://raw.githubusercontent.com/wpilibsuite/allwpilib/main/wpilibcExamples/src/main/cpp/examples/DriveDistanceOffboard/cpp/RobotContainer.cpp
232242
:language: c++
233-
:lines: 37-56
243+
:lines: 37-60
234244
:linenos:
235245
:lineno-start: 37

0 commit comments

Comments
 (0)