WPILibC++ 2024.3.2-108-gab315e2
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]]
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. As a replacement, consider using
173 * `Defer(supplier).AsProxy()`.
174 *
175 * @param supplier the command supplier
176 */
178[[nodiscard]] [[deprecated(
179 "The ProxyCommand supplier constructor has been deprecated. Use "
180 "Defer(supplier).AsProxy() instead.")]]
182
183/**
184 * Constructs a command that schedules the command returned from the supplier
185 * when initialized, and ends when it is no longer scheduled. The supplier is
186 * called when the command is initialized.
187 *
188 * @param supplier the command supplier
189 */
190[[nodiscard]] [[deprecated(
191 "The ProxyCommand supplier constructor has been deprecated. Use "
192 "Defer(supplier).AsProxy() instead.")]]
195// Command Groups
196
197namespace impl {
198
199/**
200 * Create a vector of commands.
201 */
202template <std::convertible_to<CommandPtr>... Args>
203std::vector<CommandPtr> MakeVector(Args&&... args) {
204 std::vector<CommandPtr> data;
205 data.reserve(sizeof...(Args));
206 (data.emplace_back(std::forward<Args>(args)), ...);
207 return data;
208}
209
210} // namespace impl
211
212/**
213 * Runs a group of commands in series, one after the other.
214 */
215[[nodiscard]]
216CommandPtr Sequence(std::vector<CommandPtr>&& commands);
217
218/**
219 * Runs a group of commands in series, one after the other.
220 */
221template <std::convertible_to<CommandPtr>... CommandPtrs>
222[[nodiscard]]
223CommandPtr Sequence(CommandPtrs&&... commands) {
224 return Sequence(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
225}
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 */
231[[nodiscard]]
232CommandPtr RepeatingSequence(std::vector<CommandPtr>&& commands);
233
234/**
235 * Runs a group of commands in series, one after the other. Once the last
236 * command ends, the group is restarted.
237 */
238template <std::convertible_to<CommandPtr>... CommandPtrs>
239[[nodiscard]]
240CommandPtr RepeatingSequence(CommandPtrs&&... commands) {
241 return RepeatingSequence(
242 impl::MakeVector(std::forward<CommandPtrs>(commands)...));
243}
244
245/**
246 * Runs a group of commands at the same time. Ends once all commands in the
247 * group finish.
248 */
249[[nodiscard]]
250CommandPtr Parallel(std::vector<CommandPtr>&& commands);
251
252/**
253 * Runs a group of commands at the same time. Ends once all commands in the
254 * group finish.
255 */
256template <std::convertible_to<CommandPtr>... CommandPtrs>
257[[nodiscard]]
258CommandPtr Parallel(CommandPtrs&&... commands) {
259 return Parallel(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
260}
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 */
266[[nodiscard]]
267CommandPtr Race(std::vector<CommandPtr>&& commands);
268
269/**
270 * Runs a group of commands at the same time. Ends once any command in the group
271 * finishes, and cancels the others.
272 */
273template <std::convertible_to<CommandPtr>... CommandPtrs>
274[[nodiscard]]
275CommandPtr Race(CommandPtrs&&... commands) {
276 return Race(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
277}
278
279/**
280 * Runs a group of commands at the same time. Ends once a specific command
281 * finishes, and cancels the others.
282 */
283[[nodiscard]]
284CommandPtr Deadline(CommandPtr&& deadline, std::vector<CommandPtr>&& others);
285
286/**
287 * Runs a group of commands at the same time. Ends once a specific command
288 * finishes, and cancels the others.
289 */
290template <std::convertible_to<CommandPtr>... CommandPtrs>
291[[nodiscard]]
292CommandPtr Deadline(CommandPtr&& deadline, CommandPtrs&&... commands) {
293 return Deadline(std::move(deadline),
294 impl::MakeVector(std::forward<CommandPtrs>(commands)...));
295}
296
297} // namespace cmd
298
299} // 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:58
basic_string_view< char > string_view
Definition: core.h:518
#define WPI_IGNORE_DEPRECATED
Definition: deprecated.h:16
#define WPI_UNIGNORE_DEPRECATED
Definition: deprecated.h:27
std::vector< CommandPtr > MakeVector(Args &&... args)
Create a vector of commands.
Definition: Commands.h:203
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 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.
WPI_IGNORE_DEPRECATED CommandPtr DeferredProxy(wpi::unique_function< Command *()> supplier)
Constructs a command that schedules the command returned from the supplier when initialized,...
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: NotifierCommand.h:16