WPILibC++ 2023.4.3
ProfiledPIDCommand.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 <functional>
8#include <initializer_list>
9#include <span>
10#include <utility>
11
13#include <units/time.h>
14
17
18namespace frc2 {
19/**
20 * A command that controls an output with a ProfiledPIDController. Runs forever
21 * by default - to add exit conditions and/or other behavior, subclass this
22 * class. The controller calculation and output are performed synchronously in
23 * the command's execute() method.
24 *
25 * This class is provided by the NewCommands VendorDep
26 *
27 * @see ProfiledPIDController<Distance>
28 */
29template <class Distance>
31 : public CommandHelper<CommandBase, ProfiledPIDCommand<Distance>> {
33 using Velocity =
36 using State = typename frc::TrapezoidProfile<Distance>::State;
37
38 public:
39 /**
40 * Creates a new PIDCommand, which controls the given output with a
41 * ProfiledPIDController.
42 *
43 * @param controller the controller that controls the output.
44 * @param measurementSource the measurement of the process variable
45 * @param goalSource the controller's goal
46 * @param useOutput the controller's output
47 * @param requirements the subsystems required by this command
48 */
50 std::function<Distance_t()> measurementSource,
51 std::function<State()> goalSource,
52 std::function<void(double, State)> useOutput,
53 std::initializer_list<Subsystem*> requirements)
54 : m_controller{controller},
55 m_measurement{std::move(measurementSource)},
56 m_goal{std::move(goalSource)},
57 m_useOutput{std::move(useOutput)} {
58 this->AddRequirements(requirements);
59 }
60
61 /**
62 * Creates a new PIDCommand, which controls the given output with a
63 * ProfiledPIDController.
64 *
65 * @param controller the controller that controls the output.
66 * @param measurementSource the measurement of the process variable
67 * @param goalSource the controller's goal
68 * @param useOutput the controller's output
69 * @param requirements the subsystems required by this command
70 */
72 std::function<Distance_t()> measurementSource,
73 std::function<State()> goalSource,
74 std::function<void(double, State)> useOutput,
75 std::span<Subsystem* const> requirements = {})
76 : m_controller{controller},
77 m_measurement{std::move(measurementSource)},
78 m_goal{std::move(goalSource)},
79 m_useOutput{std::move(useOutput)} {
80 this->AddRequirements(requirements);
81 }
82
83 /**
84 * Creates a new PIDCommand, which controls the given output with a
85 * ProfiledPIDController.
86 *
87 * @param controller the controller that controls the output.
88 * @param measurementSource the measurement of the process variable
89 * @param goalSource the controller's goal
90 * @param useOutput the controller's output
91 * @param requirements the subsystems required by this command
92 */
94 std::function<Distance_t()> measurementSource,
95 std::function<Distance_t()> goalSource,
96 std::function<void(double, State)> useOutput,
97 std::initializer_list<Subsystem*> requirements)
99 controller, measurementSource,
100 [goalSource = std::move(goalSource)]() {
101 return State{goalSource(), Velocity_t{0}};
102 },
103 useOutput, requirements) {}
104
105 /**
106 * Creates a new PIDCommand, which controls the given output with a
107 * ProfiledPIDController.
108 *
109 * @param controller the controller that controls the output.
110 * @param measurementSource the measurement of the process variable
111 * @param goalSource the controller's goal
112 * @param useOutput the controller's output
113 * @param requirements the subsystems required by this command
114 */
116 std::function<Distance_t()> measurementSource,
117 std::function<Distance_t()> goalSource,
118 std::function<void(double, State)> useOutput,
119 std::span<Subsystem* const> requirements = {})
121 controller, measurementSource,
122 [goalSource = std::move(goalSource)]() {
123 return State{goalSource(), Velocity_t{0}};
124 },
125 useOutput, requirements) {}
126
127 /**
128 * Creates a new PIDCommand, which controls the given output with a
129 * ProfiledPIDController with a constant goal.
130 *
131 * @param controller the controller that controls the output.
132 * @param measurementSource the measurement of the process variable
133 * @param goal the controller's goal
134 * @param useOutput the controller's output
135 * @param requirements the subsystems required by this command
136 */
138 std::function<Distance_t()> measurementSource, State goal,
139 std::function<void(double, State)> useOutput,
140 std::initializer_list<Subsystem*> requirements)
142 controller, measurementSource, [goal] { return goal; }, useOutput,
143 requirements) {}
144
145 /**
146 * Creates a new PIDCommand, which controls the given output with a
147 * ProfiledPIDController with a constant goal.
148 *
149 * @param controller the controller that controls the output.
150 * @param measurementSource the measurement of the process variable
151 * @param goal the controller's goal
152 * @param useOutput the controller's output
153 * @param requirements the subsystems required by this command
154 */
156 std::function<Distance_t()> measurementSource, State goal,
157 std::function<void(double, State)> useOutput,
158 std::span<Subsystem* const> requirements = {})
160 controller, measurementSource, [goal] { return goal; }, useOutput,
161 requirements) {}
162
163 /**
164 * Creates a new PIDCommand, which controls the given output with a
165 * ProfiledPIDController with a constant goal.
166 *
167 * @param controller the controller that controls the output.
168 * @param measurementSource the measurement of the process variable
169 * @param goal the controller's goal
170 * @param useOutput the controller's output
171 * @param requirements the subsystems required by this command
172 */
174 std::function<Distance_t()> measurementSource,
175 Distance_t goal,
176 std::function<void(double, State)> useOutput,
177 std::initializer_list<Subsystem*> requirements)
179 controller, measurementSource, [goal] { return goal; }, useOutput,
180 requirements) {}
181
182 /**
183 * Creates a new PIDCommand, which controls the given output with a
184 * ProfiledPIDController with a constant goal.
185 *
186 * @param controller the controller that controls the output.
187 * @param measurementSource the measurement of the process variable
188 * @param goal the controller's goal
189 * @param useOutput the controller's output
190 * @param requirements the subsystems required by this command
191 */
193 std::function<Distance_t()> measurementSource,
194 Distance_t goal,
195 std::function<void(double, State)> useOutput,
196 std::span<Subsystem* const> requirements = {})
198 controller, measurementSource, [goal] { return goal; }, useOutput,
199 requirements) {}
200
202
203 ProfiledPIDCommand(const ProfiledPIDCommand& other) = default;
204
205 void Initialize() override { m_controller.Reset(m_measurement()); }
206
207 void Execute() override {
209 m_controller.GetSetpoint());
210 }
211
212 void End(bool interrupted) override {
213 m_useOutput(0, State{Distance_t(0), Velocity_t(0)});
214 }
215
216 /**
217 * Returns the ProfiledPIDController used by the command.
218 *
219 * @return The ProfiledPIDController
220 */
222
223 protected:
225 std::function<Distance_t()> m_measurement;
226 std::function<State()> m_goal;
227 std::function<void(double, State)> m_useOutput;
228};
229} // namespace frc2
void AddRequirements(std::initializer_list< Subsystem * > requirements)
Adds the specified Subsystem requirements to the command.
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A command that controls an output with a ProfiledPIDController.
Definition: ProfiledPIDCommand.h:31
ProfiledPIDCommand(ProfiledPIDCommand &&other)=default
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, Distance_t goal, std::function< void(double, State)> useOutput, std::initializer_list< Subsystem * > requirements)
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController with a constan...
Definition: ProfiledPIDCommand.h:173
std::function< Distance_t()> m_measurement
Definition: ProfiledPIDCommand.h:225
std::function< void(double, State)> m_useOutput
Definition: ProfiledPIDCommand.h:227
void Initialize() override
The initial subroutine of a command.
Definition: ProfiledPIDCommand.h:205
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, std::function< State()> goalSource, std::function< void(double, State)> useOutput, std::initializer_list< Subsystem * > requirements)
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController.
Definition: ProfiledPIDCommand.h:49
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, State goal, std::function< void(double, State)> useOutput, std::span< Subsystem *const > requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController with a constan...
Definition: ProfiledPIDCommand.h:155
std::function< State()> m_goal
Definition: ProfiledPIDCommand.h:226
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, State goal, std::function< void(double, State)> useOutput, std::initializer_list< Subsystem * > requirements)
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController with a constan...
Definition: ProfiledPIDCommand.h:137
void Execute() override
The main body of a command.
Definition: ProfiledPIDCommand.h:207
void End(bool interrupted) override
The action to take when the command ends.
Definition: ProfiledPIDCommand.h:212
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, std::function< Distance_t()> goalSource, std::function< void(double, State)> useOutput, std::span< Subsystem *const > requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController.
Definition: ProfiledPIDCommand.h:115
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, std::function< Distance_t()> goalSource, std::function< void(double, State)> useOutput, std::initializer_list< Subsystem * > requirements)
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController.
Definition: ProfiledPIDCommand.h:93
frc::ProfiledPIDController< Distance > m_controller
Definition: ProfiledPIDCommand.h:224
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, Distance_t goal, std::function< void(double, State)> useOutput, std::span< Subsystem *const > requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController with a constan...
Definition: ProfiledPIDCommand.h:192
frc::ProfiledPIDController< Distance > & GetController()
Returns the ProfiledPIDController used by the command.
Definition: ProfiledPIDCommand.h:221
ProfiledPIDCommand(frc::ProfiledPIDController< Distance > controller, std::function< Distance_t()> measurementSource, std::function< State()> goalSource, std::function< void(double, State)> useOutput, std::span< Subsystem *const > requirements={})
Creates a new PIDCommand, which controls the given output with a ProfiledPIDController.
Definition: ProfiledPIDCommand.h:71
ProfiledPIDCommand(const ProfiledPIDCommand &other)=default
Implements a PID control loop whose setpoint is constrained by a trapezoid profile.
Definition: ProfiledPIDController.h:35
Definition: TrapezoidProfile.h:67
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1434
Definition: InstantCommand.h:14
Definition: StdDeque.h:50