WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Subsystem.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 <string>
10#include <utility>
11
14
15namespace wpi::cmd {
16class Command;
17class CommandPtr;
18/**
19 * A robot subsystem. Subsystems are the basic unit of robot organization in
20 * the Command-based framework; they encapsulate low-level hardware objects
21 * (motor controllers, sensors, etc) and provide methods through which they can
22 * be used by Commands. Subsystems are used by the CommandScheduler's resource
23 * management system to ensure multiple robot actions are not "fighting" over
24 * the same hardware; Commands that use a subsystem should include that
25 * subsystem in their GetRequirements() method, and resources used within a
26 * subsystem should generally remain encapsulated and not be shared by other
27 * parts of the robot.
28 *
29 * <p>Subsystems must be registered with the scheduler with the
30 * CommandScheduler.RegisterSubsystem() method in order for the
31 * Periodic() method to be called. It is recommended that this method be called
32 * from the constructor of users' Subsystem implementations. The
33 * SubsystemBase class offers a simple base for user implementations
34 * that handles this.
35 *
36 * This class is provided by the NewCommands VendorDep
37 *
38 * @see Command
39 * @see CommandScheduler
40 * @see SubsystemBase
41 */
42class Subsystem {
43 public:
44 virtual ~Subsystem();
45 /**
46 * This method is called periodically by the CommandScheduler. Useful for
47 * updating subsystem-specific state that you don't want to offload to a
48 * Command. Teams should try to be consistent within their own codebases
49 * about which responsibilities will be handled by Commands, and which will be
50 * handled here.
51 */
52 virtual void Periodic();
53
54 /**
55 * This method is called periodically by the CommandScheduler. Useful for
56 * updating subsystem-specific state that needs to be maintained for
57 * simulations, such as for updating simulation classes and setting simulated
58 * sensor readings.
59 */
60 virtual void SimulationPeriodic();
61
62 /**
63 * Gets the name of this Subsystem.
64 *
65 * @return Name
66 */
67 virtual std::string GetName() const;
68
69 /**
70 * Sets the default Command of the subsystem. The default command will be
71 * automatically scheduled when no other commands are scheduled that require
72 * the subsystem. Default commands should generally not end on their own, i.e.
73 * their IsFinished() method should always return false. Will automatically
74 * register this subsystem with the CommandScheduler.
75 *
76 * @param defaultCommand the default command to associate with this subsystem
77 */
78 template <std::derived_from<Command> T>
79 void SetDefaultCommand(T&& defaultCommand) {
81 this, std::forward<T>(defaultCommand));
82 }
83
84 /**
85 * Sets the default Command of the subsystem. The default command will be
86 * automatically scheduled when no other commands are scheduled that require
87 * the subsystem. Default commands should generally not end on their own, i.e.
88 * their IsFinished() method should always return false. Will automatically
89 * register this subsystem with the CommandScheduler.
90 *
91 * @param defaultCommand the default command to associate with this subsystem
92 */
93 void SetDefaultCommand(CommandPtr&& defaultCommand);
94
95 /**
96 * Removes the default command for the subsystem. This will not cancel the
97 * default command if it is currently running.
98 */
100
101 /**
102 * Gets the default command for this subsystem. Returns null if no default
103 * command is currently associated with the subsystem.
104 *
105 * @return the default command associated with this subsystem
106 */
108
109 /**
110 * Returns the command currently running on this subsystem. Returns null if
111 * no command is currently scheduled that requires this subsystem.
112 *
113 * @return the scheduled command currently requiring this subsystem
114 */
116
117 /**
118 * Registers this subsystem with the CommandScheduler, allowing its
119 * Periodic() method to be called when the scheduler runs.
120 */
121 void Register();
122
123 /**
124 * Constructs a command that does nothing until interrupted. Requires this
125 * subsystem.
126 *
127 * @return the command
128 */
129 [[nodiscard]]
131
132 /**
133 * Constructs a command that runs an action once and finishes. Requires this
134 * subsystem.
135 *
136 * @param action the action to run
137 */
138 CommandPtr RunOnce(std::function<void()> action);
139
140 /**
141 * Constructs a command that runs an action every iteration until interrupted.
142 * Requires this subsystem.
143 *
144 * @param action the action to run
145 */
146 CommandPtr Run(std::function<void()> action);
147
148 /**
149 * Constructs a command that runs an action once and another action when the
150 * command is interrupted. Requires this subsystem.
151 *
152 * @param start the action to run on start
153 * @param end the action to run on interrupt
154 */
155 CommandPtr StartEnd(std::function<void()> start, std::function<void()> end);
156
157 /**
158 * Constructs a command that runs an action every iteration until interrupted,
159 * and then runs a second action. Requires this subsystem.
160 *
161 * @param run the action to run every iteration
162 * @param end the action to run on interrupt
163 */
164 CommandPtr RunEnd(std::function<void()> run, std::function<void()> end);
165
166 /**
167 * Constructs a command that runs an action once, and then runs an action
168 * every iteration until interrupted. Requires this subsystem.
169 *
170 * @param start the action to run on start
171 * @param run the action to run every iteration
172 */
173 CommandPtr StartRun(std::function<void()> start, std::function<void()> run);
174
175 /**
176 * Constructs a DeferredCommand with the provided supplier. This subsystem is
177 * added as a requirement.
178 *
179 * @param supplier the command supplier.
180 * @return the command.
181 */
183};
184} // namespace wpi::cmd
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
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
void SetDefaultCommand(Subsystem *subsystem, T &&defaultCommand)
Sets the default command for a subsystem.
Definition CommandScheduler.hpp:214
static CommandScheduler & GetInstance()
Returns the Scheduler instance.
A robot subsystem.
Definition Subsystem.hpp:42
CommandPtr RunOnce(std::function< void()> action)
Constructs a command that runs an action once and finishes.
CommandPtr StartEnd(std::function< void()> start, std::function< void()> end)
Constructs a command that runs an action once and another action when the command is interrupted.
CommandPtr Run(std::function< void()> action)
Constructs a command that runs an action every iteration until interrupted.
void SetDefaultCommand(CommandPtr &&defaultCommand)
Sets the default Command of the subsystem.
CommandPtr Idle()
Constructs a command that does nothing until interrupted.
virtual void Periodic()
This method is called periodically by the CommandScheduler.
Command * GetCurrentCommand() const
Returns the command currently running on this subsystem.
CommandPtr Defer(wpi::util::unique_function< CommandPtr()> supplier)
Constructs a DeferredCommand with the provided supplier.
CommandPtr StartRun(std::function< void()> start, std::function< void()> run)
Constructs a command that runs an action once, and then runs an action every iteration until interrup...
virtual std::string GetName() const
Gets the name of this Subsystem.
CommandPtr RunEnd(std::function< void()> run, std::function< void()> end)
Constructs a command that runs an action every iteration until interrupted, and then runs a second ac...
void SetDefaultCommand(T &&defaultCommand)
Sets the default Command of the subsystem.
Definition Subsystem.hpp:79
virtual void SimulationPeriodic()
This method is called periodically by the CommandScheduler.
void RemoveDefaultCommand()
Removes the default command for the subsystem.
Command * GetDefaultCommand() const
Gets the default command for this subsystem.
void Register()
Registers this subsystem with the CommandScheduler, allowing its Periodic() method to be called when ...
unique_function is a type-erasing functor similar to std::function.
Definition FunctionExtras.hpp:57
Definition CommandNiDsStadiaController.hpp:15