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