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