WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
Commands.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 <concepts>
8#include <functional>
9#include <memory>
10#include <string>
11#include <type_traits>
12#include <utility>
13#include <vector>
14
15#include <wpi/deprecated.h>
16
20
21namespace frc2 {
22class Subsystem;
23
24/**
25 * Namespace for command factories.
26 */
27namespace cmd {
28
29/**
30 * Constructs a command that does nothing, finishing immediately.
31 */
32[[nodiscard]]
34
35/**
36 * Constructs a command that does nothing until interrupted.
37 *
38 * @param requirements Subsystems to require
39 * @return the command
40 */
41[[nodiscard]]
42CommandPtr Idle(Requirements requirements = {});
43
44// Action Commands
45
46/**
47 * Constructs a command that runs an action once and finishes.
48 *
49 * @param action the action to run
50 * @param requirements subsystems the action requires
51 */
52[[nodiscard]]
53CommandPtr RunOnce(std::function<void()> action,
54 Requirements requirements = {});
55
56/**
57 * Constructs a command that runs an action every iteration until interrupted.
58 *
59 * @param action the action to run
60 * @param requirements subsystems the action requires
61 */
62[[nodiscard]]
63CommandPtr Run(std::function<void()> action, Requirements requirements = {});
64
65/**
66 * Constructs a command that runs an action once and another action when the
67 * command is interrupted.
68 *
69 * @param start the action to run on start
70 * @param end the action to run on interrupt
71 * @param requirements subsystems the action requires
72 */
73[[nodiscard]]
74CommandPtr StartEnd(std::function<void()> start, std::function<void()> end,
75 Requirements requirements = {});
76
77/**
78 * Constructs a command that runs an action every iteration until interrupted,
79 * and then runs a second action.
80 *
81 * @param run the action to run every iteration
82 * @param end the action to run on interrupt
83 * @param requirements subsystems the action requires
84 */
85[[nodiscard]]
86CommandPtr RunEnd(std::function<void()> run, std::function<void()> end,
87 Requirements requirements = {});
88
89/**
90 * Constructs a command that runs an action once, and then runs an action every
91 * iteration until interrupted.
92 *
93 * @param start the action to run on start
94 * @param run the action to run every iteration
95 * @param requirements subsystems the action requires
96 */
97[[nodiscard]]
98CommandPtr StartRun(std::function<void()> start, std::function<void()> run,
99 Requirements requirements = {});
100
101/**
102 * Constructs a command that prints a message and finishes.
103 *
104 * @param msg the message to print
105 */
106[[nodiscard]]
107CommandPtr Print(std::string_view msg);
108
109// Idling Commands
110
111/**
112 * Constructs a command that does nothing, finishing after a specified duration.
113 *
114 * @param duration after how long the command finishes
115 */
116[[nodiscard]]
117CommandPtr Wait(units::second_t duration);
118
119/**
120 * Constructs a command that does nothing, finishing once a condition becomes
121 * true.
122 *
123 * @param condition the condition
124 */
125[[nodiscard]]
126CommandPtr WaitUntil(std::function<bool()> condition);
127
128// Selector Commands
129
130/**
131 * Runs one of two commands, based on the boolean selector function.
132 *
133 * @param onTrue the command to run if the selector function returns true
134 * @param onFalse the command to run if the selector function returns false
135 * @param selector the selector function
136 */
137[[nodiscard]]
139 std::function<bool()> selector);
140
141/**
142 * Runs one of several commands, based on the selector function.
143 *
144 * @param selector the selector function
145 * @param commands map of commands to select from
146 */
147template <typename Key, std::convertible_to<CommandPtr>... CommandPtrs>
148[[nodiscard]]
149CommandPtr Select(std::function<Key()> selector,
150 std::pair<Key, CommandPtrs>&&... commands) {
151 std::vector<std::pair<Key, std::unique_ptr<Command>>> vec;
152
153 ((void)vec.emplace_back(commands.first, std::move(commands.second).Unwrap()),
154 ...);
155
156 return SelectCommand(std::move(selector), std::move(vec)).ToPtr();
157}
158
159/**
160 * Runs the command supplied by the supplier.
161 *
162 * @param supplier the command supplier
163 * @param requirements the set of requirements for this command
164 */
165[[nodiscard]]
167 Requirements requirements);
168
169/**
170 * Constructs a command that schedules the command returned from the supplier
171 * when initialized, and ends when it is no longer scheduled. The supplier is
172 * called when the command is initialized.
173 *
174 * @param supplier the command supplier
175 */
176[[nodiscard]]
178
179/**
180 * Constructs a command that schedules the command returned from the supplier
181 * when initialized, and ends when it is no longer scheduled. The supplier is
182 * called when the command is initialized.
183 *
184 * @param supplier the command supplier
185 */
186[[nodiscard]]
188// Command Groups
189
190namespace impl {
191
192/**
193 * Create a vector of commands.
194 */
195template <std::convertible_to<CommandPtr>... Args>
196std::vector<CommandPtr> MakeVector(Args&&... args) {
197 std::vector<CommandPtr> data;
198 data.reserve(sizeof...(Args));
199 (data.emplace_back(std::forward<Args>(args)), ...);
200 return data;
201}
202
203} // namespace impl
204
205/**
206 * Runs a group of commands in series, one after the other.
207 */
208[[nodiscard]]
209CommandPtr Sequence(std::vector<CommandPtr>&& commands);
210
211/**
212 * Runs a group of commands in series, one after the other.
213 */
214template <std::convertible_to<CommandPtr>... CommandPtrs>
215[[nodiscard]]
216CommandPtr Sequence(CommandPtrs&&... commands) {
217 return Sequence(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
218}
219
220/**
221 * Runs a group of commands in series, one after the other. Once the last
222 * command ends, the group is restarted.
223 */
224[[nodiscard]]
225CommandPtr RepeatingSequence(std::vector<CommandPtr>&& commands);
226
227/**
228 * Runs a group of commands in series, one after the other. Once the last
229 * command ends, the group is restarted.
230 */
231template <std::convertible_to<CommandPtr>... CommandPtrs>
232[[nodiscard]]
233CommandPtr RepeatingSequence(CommandPtrs&&... commands) {
234 return RepeatingSequence(
235 impl::MakeVector(std::forward<CommandPtrs>(commands)...));
236}
237
238/**
239 * Runs a group of commands at the same time. Ends once all commands in the
240 * group finish.
241 */
242[[nodiscard]]
243CommandPtr Parallel(std::vector<CommandPtr>&& commands);
244
245/**
246 * Runs a group of commands at the same time. Ends once all commands in the
247 * group finish.
248 */
249template <std::convertible_to<CommandPtr>... CommandPtrs>
250[[nodiscard]]
251CommandPtr Parallel(CommandPtrs&&... commands) {
252 return Parallel(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
253}
254
255/**
256 * Runs a group of commands at the same time. Ends once any command in the group
257 * finishes, and cancels the others.
258 */
259[[nodiscard]]
260CommandPtr Race(std::vector<CommandPtr>&& commands);
261
262/**
263 * Runs a group of commands at the same time. Ends once any command in the group
264 * finishes, and cancels the others.
265 */
266template <std::convertible_to<CommandPtr>... CommandPtrs>
267[[nodiscard]]
268CommandPtr Race(CommandPtrs&&... commands) {
269 return Race(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
270}
271
272/**
273 * Runs a group of commands at the same time. Ends once a specific command
274 * finishes, and cancels the others.
275 */
276[[nodiscard]]
277CommandPtr Deadline(CommandPtr&& deadline, std::vector<CommandPtr>&& others);
278
279/**
280 * Runs a group of commands at the same time. Ends once a specific command
281 * finishes, and cancels the others.
282 */
283template <std::convertible_to<CommandPtr>... CommandPtrs>
284[[nodiscard]]
285CommandPtr Deadline(CommandPtr&& deadline, CommandPtrs&&... commands) {
286 return Deadline(std::move(deadline),
287 impl::MakeVector(std::forward<CommandPtrs>(commands)...));
288}
289
290} // namespace cmd
291
292} // namespace frc2
CommandPtr ToPtr() &&override
Definition CommandHelper.h:31
A state machine representing a complete action to be performed by the robot.
Definition Command.h:41
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition CommandPtr.h:28
Represents requirements for a command, which is a set of (pointers to) subsystems.
Definition Requirements.h:20
A command composition that runs one of a selection of commands using a selector and a key to command ...
Definition SelectCommand.h:38
unique_function is a type-erasing functor similar to std::function.
Definition FunctionExtras.h:57
std::vector< CommandPtr > MakeVector(Args &&... args)
Create a vector of commands.
Definition Commands.h:196
CommandPtr Deadline(CommandPtr &&deadline, std::vector< CommandPtr > &&others)
Runs a group of commands at the same time.
CommandPtr StartEnd(std::function< void()> start, std::function< void()> end, Requirements requirements={})
Constructs a command that runs an action once and another action when the command is interrupted.
CommandPtr WaitUntil(std::function< bool()> condition)
Constructs a command that does nothing, finishing once a condition becomes true.
CommandPtr Either(CommandPtr &&onTrue, CommandPtr &&onFalse, std::function< bool()> selector)
Runs one of two commands, based on the boolean selector function.
CommandPtr RunEnd(std::function< void()> run, std::function< void()> end, Requirements requirements={})
Constructs a command that runs an action every iteration until interrupted, and then runs a second ac...
CommandPtr None()
Constructs a command that does nothing, finishing immediately.
CommandPtr DeferredProxy(wpi::unique_function< Command *()> supplier)
Constructs a command that schedules the command returned from the supplier when initialized,...
CommandPtr Parallel(std::vector< CommandPtr > &&commands)
Runs a group of commands at the same time.
CommandPtr RepeatingSequence(std::vector< CommandPtr > &&commands)
Runs a group of commands in series, one after the other.
CommandPtr Sequence(std::vector< CommandPtr > &&commands)
Runs a group of commands in series, one after the other.
CommandPtr Idle(Requirements requirements={})
Constructs a command that does nothing until interrupted.
CommandPtr RunOnce(std::function< void()> action, Requirements requirements={})
Constructs a command that runs an action once and finishes.
CommandPtr Run(std::function< void()> action, Requirements requirements={})
Constructs a command that runs an action every iteration until interrupted.
CommandPtr Print(std::string_view msg)
Constructs a command that prints a message and finishes.
CommandPtr Wait(units::second_t duration)
Constructs a command that does nothing, finishing after a specified duration.
CommandPtr Defer(wpi::unique_function< CommandPtr()> supplier, Requirements requirements)
Runs the command supplied by the supplier.
CommandPtr Select(std::function< Key()> selector, std::pair< Key, CommandPtrs > &&... commands)
Runs one of several commands, based on the selector function.
Definition Commands.h:149
CommandPtr Race(std::vector< CommandPtr > &&commands)
Runs a group of commands at the same time.
CommandPtr StartRun(std::function< void()> start, std::function< void()> run, Requirements requirements={})
Constructs a command that runs an action once, and then runs an action every iteration until interrup...
Definition FunctionalCommand.h:13