11 #include <initializer_list>
15 #include <wpi/ArrayRef.h>
17 #include "frc2/command/Command.h"
18 #include "frc2/command/CommandScheduler.h"
37 explicit Trigger(std::function<
bool()> isActive)
38 : m_isActive{std::move(isActive)} {}
45 m_isActive = [] {
return false; };
71 template <
class T,
typename = std::enable_if_t<std::is_base_of_v<
72 Command, std::remove_reference_t<T>>>>
75 [pressedLast = m_isActive(), *
this,
76 command = std::make_unique<std::remove_reference_t<T>>(
77 std::forward<T>(command)),
78 interruptible]()
mutable {
79 bool pressed = m_isActive();
81 if (!pressedLast && pressed) {
82 command->Schedule(interruptible);
85 pressedLast = pressed;
98 std::initializer_list<Subsystem*> requirements);
130 template <
class T,
typename = std::enable_if_t<std::is_base_of_v<
131 Command, std::remove_reference_t<T>>>>
134 [pressedLast = m_isActive(), *
this,
135 command = std::make_unique<std::remove_reference_t<T>>(
136 std::forward<T>(command)),
137 interruptible]()
mutable {
138 bool pressed = m_isActive();
141 command->Schedule(interruptible);
142 }
else if (pressedLast && !pressed) {
146 pressedLast = pressed;
158 std::initializer_list<Subsystem*> requirements);
190 template <
class T,
typename = std::enable_if_t<std::is_base_of_v<
191 Command, std::remove_reference_t<T>>>>
194 [pressedLast = m_isActive(), *
this,
195 command = std::make_unique<std::remove_reference_t<T>>(
196 std::forward<T>(command)),
197 interruptible]()
mutable {
198 bool pressed = m_isActive();
200 if (!pressedLast && pressed) {
201 command->Schedule(interruptible);
202 }
else if (pressedLast && !pressed) {
206 pressedLast = pressed;
232 template <
class T,
typename = std::enable_if_t<std::is_base_of_v<
233 Command, std::remove_reference_t<T>>>>
236 [pressedLast = m_isActive(), *
this,
237 command = std::make_unique<std::remove_reference_t<T>>(
238 std::forward<T>(command)),
239 interruptible]()
mutable {
240 bool pressed = m_isActive();
242 if (pressedLast && !pressed) {
243 command->Schedule(interruptible);
246 pressedLast = pressed;
258 std::initializer_list<Subsystem*> requirements);
290 template <
class T,
typename = std::enable_if_t<std::is_base_of_v<
291 Command, std::remove_reference_t<T>>>>
294 [pressedLast = m_isActive(), *
this,
295 command = std::make_unique<std::remove_reference_t<T>>(
296 std::forward<T>(command)),
297 interruptible]()
mutable {
298 bool pressed = m_isActive();
300 if (!pressedLast && pressed) {
301 if (command->IsScheduled()) {
304 command->Schedule(interruptible);
308 pressedLast = pressed;
329 return Trigger([*
this, rhs] {
return m_isActive() && rhs.m_isActive(); });
338 return Trigger([*
this, rhs] {
return m_isActive() || rhs.m_isActive(); });
348 return Trigger([*
this] {
return !m_isActive(); });
352 std::function<bool()> m_isActive;
A class used to bind command scheduling to events.
Definition: Trigger.h:30
Trigger ToggleWhenActive(T &&command, bool interruptible=true)
Binds a command to start when the trigger becomes active, and be cancelled when it again becomes acti...
Definition: Trigger.h:292
Trigger WhileActiveOnce(Command *command, bool interruptible=true)
Binds a command to be started when the trigger becomes active, and cancelled when it becomes inactive...
Trigger WhenInactive(T &&command, bool interruptible=true)
Binds a command to start when the trigger becomes inactive.
Definition: Trigger.h:234
Trigger CancelWhenActive(Command *command)
Binds a command to be cancelled when the trigger becomes active.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
static CommandScheduler & GetInstance()
Returns the Scheduler instance.
Trigger operator&&(Trigger rhs)
Composes two triggers with logical AND.
Definition: Trigger.h:328
Trigger WhileActiveOnce(T &&command, bool interruptible=true)
Binds a command to be started when the trigger becomes active, and cancelled when it becomes inactive...
Definition: Trigger.h:192
Trigger()
Create a new trigger that is never active (default constructor) - activity can be further determined ...
Definition: Trigger.h:44
A state machine representing a complete action to be performed by the robot.
Definition: Command.h:52
Trigger WhenActive(T &&command, bool interruptible=true)
Binds a command to start when the trigger becomes active.
Definition: Trigger.h:73
Trigger WhenInactive(Command *command, bool interruptible=true)
Binds a command to start when the trigger becomes inactive.
Trigger WhenActive(Command *command, bool interruptible=true)
Binds a command to start when the trigger becomes active.
void AddButton(wpi::unique_function< void()> button)
Adds a button binding to the scheduler, which will be polled to schedule commands.
Trigger operator||(Trigger rhs)
Composes two triggers with logical OR.
Definition: Trigger.h:337
Trigger(std::function< bool()> isActive)
Create a new trigger that is active when the given condition is true.
Definition: Trigger.h:37
Trigger ToggleWhenActive(Command *command, bool interruptible=true)
Binds a command to start when the trigger becomes active, and be cancelled when it again becomes acti...
Trigger operator!()
Composes a trigger with logical NOT.
Definition: Trigger.h:347
Trigger WhileActiveContinous(Command *command, bool interruptible=true)
Binds a command to be started repeatedly while the trigger is active, and cancelled when it becomes i...
Trigger WhileActiveContinous(T &&command, bool interruptible=true)
Binds a command to be started repeatedly while the trigger is active, and cancelled when it becomes i...
Definition: Trigger.h:132