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