WPILibC++ 2024.3.2
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
18
19namespace frc2 {
20class Subsystem;
21
22/**
23 * Namespace for command factories.
24 */
25namespace cmd {
26
27/**
28 * Constructs a command that does nothing, finishing immediately.
29 */
30[[nodiscard]]
32
33/**
34 * Constructs a command that does nothing until interrupted.
35 *
36 * @param requirements Subsystems to require
37 * @return the command
38 */
39[[nodiscard]]
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 */
50[[nodiscard]]
51CommandPtr RunOnce(std::function<void()> action,
52 Requirements requirements = {});
53
54/**
55 * Constructs a command that runs an action every iteration until interrupted.
56 *
57 * @param action the action to run
58 * @param requirements subsystems the action requires
59 */
60[[nodiscard]]
61CommandPtr Run(std::function<void()> action, Requirements requirements = {});
62
63/**
64 * Constructs a command that runs an action once and another action when the
65 * command is interrupted.
66 *
67 * @param start the action to run on start
68 * @param end the action to run on interrupt
69 * @param requirements subsystems the action requires
70 */
71[[nodiscard]]
72CommandPtr StartEnd(std::function<void()> start, std::function<void()> end,
73 Requirements requirements = {});
74
75/**
76 * Constructs a command that runs an action every iteration until interrupted,
77 * and then runs a second action.
78 *
79 * @param run the action to run every iteration
80 * @param end the action to run on interrupt
81 * @param requirements subsystems the action requires
82 */
83[[nodiscard]]
84CommandPtr RunEnd(std::function<void()> run, std::function<void()> end,
85 Requirements requirements = {});
86
87/**
88 * Constructs a command that prints a message and finishes.
89 *
90 * @param msg the message to print
91 */
92[[nodiscard]]
94
95// Idling Commands
96
97/**
98 * Constructs a command that does nothing, finishing after a specified duration.
99 *
100 * @param duration after how long the command finishes
101 */
102[[nodiscard]]
103CommandPtr Wait(units::second_t duration);
104
105/**
106 * Constructs a command that does nothing, finishing once a condition becomes
107 * true.
108 *
109 * @param condition the condition
110 */
111[[nodiscard]]
112CommandPtr WaitUntil(std::function<bool()> condition);
113
114// Selector Commands
115
116/**
117 * Runs one of two commands, based on the boolean selector function.
118 *
119 * @param onTrue the command to run if the selector function returns true
120 * @param onFalse the command to run if the selector function returns false
121 * @param selector the selector function
122 */
123[[nodiscard]]
125 std::function<bool()> selector);
126
127/**
128 * Runs one of several commands, based on the selector function.
129 *
130 * @param selector the selector function
131 * @param commands map of commands to select from
132 */
133template <typename Key, std::convertible_to<CommandPtr>... CommandPtrs>
134[[nodiscard]]
135CommandPtr Select(std::function<Key()> selector,
136 std::pair<Key, CommandPtrs>&&... commands) {
137 std::vector<std::pair<Key, std::unique_ptr<Command>>> vec;
138
139 ((void)vec.emplace_back(commands.first, std::move(commands.second).Unwrap()),
140 ...);
141
142 return SelectCommand(std::move(selector), std::move(vec)).ToPtr();
143}
144
145/**
146 * Runs the command supplied by the supplier.
147 *
148 * @param supplier the command supplier
149 * @param requirements the set of requirements for this command
150 */
151[[nodiscard]]
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 */
162[[nodiscard]]
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 */
172[[nodiscard]]
174
175// Command Groups
176
177namespace impl {
178
179/**
180 * Create a vector of commands.
181 */
182template <std::convertible_to<CommandPtr>... Args>
183std::vector<CommandPtr> MakeVector(Args&&... args) {
184 std::vector<CommandPtr> data;
185 data.reserve(sizeof...(Args));
186 (data.emplace_back(std::forward<Args>(args)), ...);
187 return data;
188}
189
190} // namespace impl
191
192/**
193 * Runs a group of commands in series, one after the other.
194 */
195[[nodiscard]]
196CommandPtr Sequence(std::vector<CommandPtr>&& commands);
197
198/**
199 * Runs a group of commands in series, one after the other.
200 */
201template <std::convertible_to<CommandPtr>... CommandPtrs>
202[[nodiscard]]
203CommandPtr Sequence(CommandPtrs&&... commands) {
204 return Sequence(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
205}
206
207/**
208 * Runs a group of commands in series, one after the other. Once the last
209 * command ends, the group is restarted.
210 */
211[[nodiscard]]
212CommandPtr RepeatingSequence(std::vector<CommandPtr>&& commands);
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 */
218template <std::convertible_to<CommandPtr>... CommandPtrs>
219[[nodiscard]]
220CommandPtr RepeatingSequence(CommandPtrs&&... commands) {
221 return RepeatingSequence(
222 impl::MakeVector(std::forward<CommandPtrs>(commands)...));
223}
224
225/**
226 * Runs a group of commands at the same time. Ends once all commands in the
227 * group finish.
228 */
229[[nodiscard]]
230CommandPtr Parallel(std::vector<CommandPtr>&& commands);
231
232/**
233 * Runs a group of commands at the same time. Ends once all commands in the
234 * group finish.
235 */
236template <std::convertible_to<CommandPtr>... CommandPtrs>
237[[nodiscard]]
238CommandPtr Parallel(CommandPtrs&&... commands) {
239 return Parallel(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
240}
241
242/**
243 * Runs a group of commands at the same time. Ends once any command in the group
244 * finishes, and cancels the others.
245 */
246[[nodiscard]]
247CommandPtr Race(std::vector<CommandPtr>&& commands);
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 */
253template <std::convertible_to<CommandPtr>... CommandPtrs>
254[[nodiscard]]
255CommandPtr Race(CommandPtrs&&... commands) {
256 return Race(impl::MakeVector(std::forward<CommandPtrs>(commands)...));
257}
258
259/**
260 * Runs a group of commands at the same time. Ends once a specific command
261 * finishes, and cancels the others.
262 */
263[[nodiscard]]
264CommandPtr Deadline(CommandPtr&& deadline, std::vector<CommandPtr>&& others);
265
266/**
267 * Runs a group of commands at the same time. Ends once a specific command
268 * finishes, and cancels the others.
269 */
270template <std::convertible_to<CommandPtr>... CommandPtrs>
271[[nodiscard]]
272CommandPtr Deadline(CommandPtr&& deadline, CommandPtrs&&... commands) {
273 return Deadline(std::move(deadline),
274 impl::MakeVector(std::forward<CommandPtrs>(commands)...));
275}
276
277} // namespace cmd
278
279} // namespace frc2
CommandPtr ToPtr() &&override
Definition: CommandHelper.h:33
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:39
unique_function is a type-erasing functor similar to std::function.
Definition: FunctionExtras.h:57
basic_string_view< char > string_view
Definition: core.h:501
std::vector< CommandPtr > MakeVector(Args &&... args)
Create a vector of commands.
Definition: Commands.h:183
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:135
CommandPtr Race(std::vector< CommandPtr > &&commands)
Runs a group of commands at the same time.
Definition: TrapezoidProfileSubsystem.h:12