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