WPILibC++ 2023.4.3
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 <functional>
8#include <initializer_list>
9#include <memory>
10#include <span>
11#include <string>
12#include <type_traits>
13#include <utility>
14#include <vector>
15
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]] CommandPtr None();
31
32// Action Commands
33
34/**
35 * Constructs a command that runs an action once and finishes.
36 *
37 * @param action the action to run
38 * @param requirements subsystems the action requires
39 */
40[[nodiscard]] CommandPtr RunOnce(
41 std::function<void()> action,
42 std::initializer_list<Subsystem*> requirements);
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]] CommandPtr RunOnce(std::function<void()> action,
51 std::span<Subsystem* const> 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 */
59[[nodiscard]] CommandPtr Run(std::function<void()> action,
60 std::initializer_list<Subsystem*> requirements);
61
62/**
63 * Constructs a command that runs an action every iteration until interrupted.
64 *
65 * @param action the action to run
66 * @param requirements subsystems the action requires
67 */
68[[nodiscard]] CommandPtr Run(std::function<void()> action,
69 std::span<Subsystem* const> requirements = {});
70
71/**
72 * Constructs a command that runs an action once and another action when the
73 * command is interrupted.
74 *
75 * @param start the action to run on start
76 * @param end the action to run on interrupt
77 * @param requirements subsystems the action requires
78 */
79[[nodiscard]] CommandPtr StartEnd(
80 std::function<void()> start, std::function<void()> end,
81 std::initializer_list<Subsystem*> requirements);
82
83/**
84 * Constructs a command that runs an action once and another action when the
85 * command is interrupted.
86 *
87 * @param start the action to run on start
88 * @param end the action to run on interrupt
89 * @param requirements subsystems the action requires
90 */
91[[nodiscard]] CommandPtr StartEnd(
92 std::function<void()> start, std::function<void()> end,
93 std::span<Subsystem* const> requirements = {});
94
95/**
96 * Constructs a command that runs an action every iteration until interrupted,
97 * and then runs a second action.
98 *
99 * @param run the action to run every iteration
100 * @param end the action to run on interrupt
101 * @param requirements subsystems the action requires
102 */
103[[nodiscard]] CommandPtr RunEnd(std::function<void()> run,
104 std::function<void()> end,
105 std::initializer_list<Subsystem*> requirements);
106
107/**
108 * Constructs a command that runs an action every iteration until interrupted,
109 * and then runs a second action.
110 *
111 * @param run the action to run every iteration
112 * @param end the action to run on interrupt
113 * @param requirements subsystems the action requires
114 */
115[[nodiscard]] CommandPtr RunEnd(std::function<void()> run,
116 std::function<void()> end,
117 std::span<Subsystem* const> requirements = {});
118
119/**
120 * Constructs a command that prints a message and finishes.
121 *
122 * @param msg the message to print
123 */
125
126// Idling Commands
127
128/**
129 * Constructs a command that does nothing, finishing after a specified duration.
130 *
131 * @param duration after how long the command finishes
132 */
133[[nodiscard]] CommandPtr Wait(units::second_t duration);
134
135/**
136 * Constructs a command that does nothing, finishing once a condition becomes
137 * true.
138 *
139 * @param condition the condition
140 */
141[[nodiscard]] CommandPtr WaitUntil(std::function<bool()> condition);
142
143// Selector Commands
144
145/**
146 * Runs one of two commands, based on the boolean selector function.
147 *
148 * @param onTrue the command to run if the selector function returns true
149 * @param onFalse the command to run if the selector function returns false
150 * @param selector the selector function
151 */
152[[nodiscard]] CommandPtr Either(CommandPtr&& onTrue, CommandPtr&& onFalse,
153 std::function<bool()> selector);
154
155/**
156 * Runs one of several commands, based on the selector function.
157 *
158 * @param selector the selector function
159 * @param commands map of commands to select from
160 */
161template <typename Key, class... Types>
162[[nodiscard]] CommandPtr Select(std::function<Key()> selector,
163 std::pair<Key, Types>&&... commands) {
164 std::vector<std::pair<Key, std::unique_ptr<Command>>> vec;
165
166 ((void)vec.emplace_back(commands.first, std::move(commands.second).Unwrap()),
167 ...);
168
169 return SelectCommand(std::move(selector), std::move(vec)).ToPtr();
170}
171
172// Command Groups
173
174namespace impl {
175
176/**
177 * Create a vector of commands.
178 */
179template <typename... 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 */
192[[nodiscard]] CommandPtr Sequence(std::vector<CommandPtr>&& commands);
193
194/**
195 * Runs a group of commands in series, one after the other.
196 */
197template <typename... Args>
198[[nodiscard]] CommandPtr Sequence(Args&&... commands) {
199 return Sequence(impl::MakeVector(std::forward<Args>(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 */
206[[nodiscard]] CommandPtr 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 <typename... Args>
213[[nodiscard]] CommandPtr RepeatingSequence(Args&&... commands) {
214 return RepeatingSequence(impl::MakeVector(std::forward<Args>(commands)...));
215}
216
217/**
218 * Runs a group of commands at the same time. Ends once all commands in the
219 * group finish.
220 */
221[[nodiscard]] CommandPtr Parallel(std::vector<CommandPtr>&& commands);
222
223/**
224 * Runs a group of commands at the same time. Ends once all commands in the
225 * group finish.
226 */
227template <typename... Args>
228[[nodiscard]] CommandPtr Parallel(Args&&... commands) {
229 return Parallel(impl::MakeVector(std::forward<Args>(commands)...));
230}
231
232/**
233 * Runs a group of commands at the same time. Ends once any command in the group
234 * finishes, and cancels the others.
235 */
236[[nodiscard]] CommandPtr Race(std::vector<CommandPtr>&& commands);
237
238/**
239 * Runs a group of commands at the same time. Ends once any command in the group
240 * finishes, and cancels the others.
241 */
242template <typename... Args>
243[[nodiscard]] CommandPtr Race(Args&&... commands) {
244 return Race(impl::MakeVector(std::forward<Args>(commands)...));
245}
246
247/**
248 * Runs a group of commands at the same time. Ends once a specific command
249 * finishes, and cancels the others.
250 */
251[[nodiscard]] CommandPtr Deadline(CommandPtr&& deadline,
252 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 <typename... Args>
259[[nodiscard]] CommandPtr Deadline(CommandPtr&& deadline, Args&&... commands) {
260 return Deadline(std::move(deadline),
261 impl::MakeVector(std::forward<Args>(commands)...));
262}
263
264} // namespace cmd
265
266} // namespace frc2
CommandPtr ToPtr() &&override
Definition: CommandHelper.h:32
A wrapper around std::unique_ptr<Command> so commands have move-only semantics.
Definition: CommandPtr.h:28
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:180
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 Select(std::function< Key()> selector, std::pair< Key, Types > &&... commands)
Runs one of several commands, based on the selector function.
Definition: Commands.h:162
CommandPtr RunOnce(std::function< void()> action, std::initializer_list< Subsystem * > requirements)
Constructs a command that runs an action once and finishes.
CommandPtr Race(std::vector< CommandPtr > &&commands)
Runs a group of commands at the same time.
Definition: InstantCommand.h:14
Definition: format.h:1544