WPILibC++ 2023.4.3
frc2::Trigger Class Reference

This class provides an easy way to link commands to conditions. More...

#include <frc2/command/button/Trigger.h>

Inheritance diagram for frc2::Trigger:
frc2::Button frc2::JoystickButton frc2::NetworkButton frc2::POVButton

Public Member Functions

 Trigger (std::function< bool()> condition)
 Creates a new trigger based on the given condition. More...
 
 Trigger (frc::EventLoop *loop, std::function< bool()> condition)
 Creates a new trigger based on the given condition. More...
 
 Trigger ()
 Create a new trigger that is always false. More...
 
 Trigger (const Trigger &other)
 
Trigger OnTrue (Command *command)
 Starts the given command whenever the condition changes from false to true. More...
 
Trigger OnTrue (CommandPtr &&command)
 Starts the given command whenever the condition changes from false to true. More...
 
Trigger OnFalse (Command *command)
 Starts the given command whenever the condition changes from true to false. More...
 
Trigger OnFalse (CommandPtr &&command)
 Starts the given command whenever the condition changes from true to false. More...
 
Trigger WhileTrue (Command *command)
 Starts the given command when the condition changes to true and cancels it when the condition changes to false. More...
 
Trigger WhileTrue (CommandPtr &&command)
 Starts the given command when the condition changes to true and cancels it when the condition changes to false. More...
 
Trigger WhileFalse (Command *command)
 Starts the given command when the condition changes to false and cancels it when the condition changes to true. More...
 
Trigger WhileFalse (CommandPtr &&command)
 Starts the given command when the condition changes to false and cancels it when the condition changes to true. More...
 
Trigger ToggleOnTrue (Command *command)
 Toggles a command when the condition changes from false to true. More...
 
Trigger ToggleOnTrue (CommandPtr &&command)
 Toggles a command when the condition changes from false to true. More...
 
Trigger ToggleOnFalse (Command *command)
 Toggles a command when the condition changes from true to the low state. More...
 
Trigger ToggleOnFalse (CommandPtr &&command)
 Toggles a command when the condition changes from true to false. More...
 
Trigger WhenActive (Command *command)
 Binds a command to start when the trigger becomes active. More...
 
template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger WhenActive (T &&command)
 Binds a command to start when the trigger becomes active. More...
 
Trigger WhenActive (std::function< void()> toRun, std::initializer_list< Subsystem * > requirements)
 Binds a runnable to execute when the trigger becomes active. More...
 
Trigger WhenActive (std::function< void()> toRun, std::span< Subsystem *const > requirements={})
 Binds a runnable to execute when the trigger becomes active. More...
 
Trigger WhileActiveContinous (Command *command)
 Binds a command to be started repeatedly while the trigger is active, and canceled when it becomes inactive. More...
 
template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger WhileActiveContinous (T &&command)
 Binds a command to be started repeatedly while the trigger is active, and canceled when it becomes inactive. More...
 
Trigger WhileActiveContinous (std::function< void()> toRun, std::initializer_list< Subsystem * > requirements)
 Binds a runnable to execute repeatedly while the trigger is active. More...
 
Trigger WhileActiveContinous (std::function< void()> toRun, std::span< Subsystem *const > requirements={})
 Binds a runnable to execute repeatedly while the trigger is active. More...
 
Trigger WhileActiveOnce (Command *command)
 Binds a command to be started when the trigger becomes active, and canceled when it becomes inactive. More...
 
template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger WhileActiveOnce (T &&command)
 Binds a command to be started when the trigger becomes active, and canceled when it becomes inactive. More...
 
Trigger WhenInactive (Command *command)
 Binds a command to start when the trigger becomes inactive. More...
 
template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger WhenInactive (T &&command)
 Binds a command to start when the trigger becomes inactive. More...
 
Trigger WhenInactive (std::function< void()> toRun, std::initializer_list< Subsystem * > requirements)
 Binds a runnable to execute when the trigger becomes inactive. More...
 
Trigger WhenInactive (std::function< void()> toRun, std::span< Subsystem *const > requirements={})
 Binds a runnable to execute when the trigger becomes inactive. More...
 
Trigger ToggleWhenActive (Command *command)
 Binds a command to start when the trigger becomes active, and be canceled when it again becomes active. More...
 
template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger ToggleWhenActive (T &&command)
 Binds a command to start when the trigger becomes active, and be canceled when it again becomes active. More...
 
Trigger CancelWhenActive (Command *command)
 Binds a command to be canceled when the trigger becomes active. More...
 
Trigger operator&& (std::function< bool()> rhs)
 Composes two triggers with logical AND. More...
 
Trigger operator&& (Trigger rhs)
 Composes two triggers with logical AND. More...
 
Trigger operator|| (std::function< bool()> rhs)
 Composes two triggers with logical OR. More...
 
Trigger operator|| (Trigger rhs)
 Composes two triggers with logical OR. More...
 
Trigger operator! ()
 Composes a trigger with logical NOT. More...
 
Trigger Debounce (units::second_t debounceTime, frc::Debouncer::DebounceType type=frc::Debouncer::DebounceType::kRising)
 Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period. More...
 

Detailed Description

This class provides an easy way to link commands to conditions.

It is very easy to link a button to a command. For instance, you could link the trigger button of a joystick to a "score" command.

Triggers can easily be composed for advanced functionality using the operator!, operator||, operator&& operators.

This class is provided by the NewCommands VendorDep

Constructor & Destructor Documentation

◆ Trigger() [1/4]

frc2::Trigger::Trigger ( std::function< bool()>  condition)
inlineexplicit

Creates a new trigger based on the given condition.

Polled by the default scheduler button loop.

Parameters
conditionthe condition represented by this trigger

◆ Trigger() [2/4]

frc2::Trigger::Trigger ( frc::EventLoop loop,
std::function< bool()>  condition 
)
inline

Creates a new trigger based on the given condition.

Parameters
loopThe loop instance that polls this trigger.
conditionthe condition represented by this trigger

◆ Trigger() [3/4]

frc2::Trigger::Trigger ( )
inline

Create a new trigger that is always false.

◆ Trigger() [4/4]

frc2::Trigger::Trigger ( const Trigger other)

Member Function Documentation

◆ CancelWhenActive()

Trigger frc2::Trigger::CancelWhenActive ( Command command)

Binds a command to be canceled when the trigger becomes active.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan and scheduling of the command.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Pass this as a command end condition with Until() instead.

◆ Debounce()

Trigger frc2::Trigger::Debounce ( units::second_t  debounceTime,
frc::Debouncer::DebounceType  type = frc::Debouncer::DebounceType::kRising 
)

Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period.

Parameters
debounceTimeThe debounce period.
typeThe debounce type.
Returns
The debounced trigger.

◆ OnFalse() [1/2]

Trigger frc2::Trigger::OnFalse ( Command command)

Starts the given command whenever the condition changes from true to false.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to start
Returns
this trigger, so calls can be chained

◆ OnFalse() [2/2]

Trigger frc2::Trigger::OnFalse ( CommandPtr &&  command)

Starts the given command whenever the condition changes from true to false.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.

◆ OnTrue() [1/2]

Trigger frc2::Trigger::OnTrue ( Command command)

Starts the given command whenever the condition changes from false to true.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to start
Returns
this trigger, so calls can be chained

◆ OnTrue() [2/2]

Trigger frc2::Trigger::OnTrue ( CommandPtr &&  command)

Starts the given command whenever the condition changes from false to true.

Moves command ownership to the button scheduler.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.

◆ operator!()

Trigger frc2::Trigger::operator! ( )
inline

Composes a trigger with logical NOT.

Returns
A trigger which is active when the component trigger is inactive, and vice-versa.

◆ operator&&() [1/2]

Trigger frc2::Trigger::operator&& ( std::function< bool()>  rhs)
inline

Composes two triggers with logical AND.

Returns
A trigger which is active when both component triggers are active.

◆ operator&&() [2/2]

Trigger frc2::Trigger::operator&& ( Trigger  rhs)
inline

Composes two triggers with logical AND.

Returns
A trigger which is active when both component triggers are active.

◆ operator||() [1/2]

Trigger frc2::Trigger::operator|| ( std::function< bool()>  rhs)
inline

Composes two triggers with logical OR.

Returns
A trigger which is active when either component trigger is active.

◆ operator||() [2/2]

Trigger frc2::Trigger::operator|| ( Trigger  rhs)
inline

Composes two triggers with logical OR.

Returns
A trigger which is active when either component trigger is active.

◆ ToggleOnFalse() [1/2]

Trigger frc2::Trigger::ToggleOnFalse ( Command command)

Toggles a command when the condition changes from true to the low state.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to toggle
Returns
this trigger, so calls can be chained

◆ ToggleOnFalse() [2/2]

Trigger frc2::Trigger::ToggleOnFalse ( CommandPtr &&  command)

Toggles a command when the condition changes from true to false.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to toggle
Returns
this trigger, so calls can be chained

◆ ToggleOnTrue() [1/2]

Trigger frc2::Trigger::ToggleOnTrue ( Command command)

Toggles a command when the condition changes from false to true.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to toggle
Returns
this trigger, so calls can be chained

◆ ToggleOnTrue() [2/2]

Trigger frc2::Trigger::ToggleOnTrue ( CommandPtr &&  command)

Toggles a command when the condition changes from false to true.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to toggle
Returns
this trigger, so calls can be chained

◆ ToggleWhenActive() [1/2]

Trigger frc2::Trigger::ToggleWhenActive ( Command command)

Binds a command to start when the trigger becomes active, and be canceled when it again becomes active.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use ToggleOnTrue(Command) instead.

◆ ToggleWhenActive() [2/2]

template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger frc2::Trigger::ToggleWhenActive ( T &&  command)
inline

Binds a command to start when the trigger becomes active, and be canceled when it again becomes active.

Transfers command ownership to the button scheduler, so the user does not have to worry about lifespan - rvalue refs will be moved, lvalue refs will be copied.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use ToggleOnTrue(Command) instead.

◆ WhenActive() [1/4]

Trigger frc2::Trigger::WhenActive ( Command command)

Binds a command to start when the trigger becomes active.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use OnTrue(Command) instead

◆ WhenActive() [2/4]

Trigger frc2::Trigger::WhenActive ( std::function< void()>  toRun,
std::initializer_list< Subsystem * >  requirements 
)

Binds a runnable to execute when the trigger becomes active.

Parameters
toRunthe runnable to execute.
requirementsthe required subsystems.
Deprecated:
Use OnTrue(Command) instead and construct the InstantCommand manually

◆ WhenActive() [3/4]

Trigger frc2::Trigger::WhenActive ( std::function< void()>  toRun,
std::span< Subsystem *const >  requirements = {} 
)

Binds a runnable to execute when the trigger becomes active.

Parameters
toRunthe runnable to execute.
requirementsthe required subsystems.
Deprecated:
Use OnTrue(Command) instead and construct the InstantCommand manually

◆ WhenActive() [4/4]

template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger frc2::Trigger::WhenActive ( T &&  command)
inline

Binds a command to start when the trigger becomes active.

Transfers command ownership to the button scheduler, so the user does not have to worry about lifespan - rvalue refs will be moved, lvalue refs will be copied.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use OnTrue(Command) instead

◆ WhenInactive() [1/4]

Trigger frc2::Trigger::WhenInactive ( Command command)

Binds a command to start when the trigger becomes inactive.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use OnFalse(Command) instead.

◆ WhenInactive() [2/4]

Trigger frc2::Trigger::WhenInactive ( std::function< void()>  toRun,
std::initializer_list< Subsystem * >  requirements 
)

Binds a runnable to execute when the trigger becomes inactive.

Parameters
toRunthe runnable to execute.
requirementsthe required subsystems.
Deprecated:
Use OnFalse(Command) instead and construct the InstantCommand manually

◆ WhenInactive() [3/4]

Trigger frc2::Trigger::WhenInactive ( std::function< void()>  toRun,
std::span< Subsystem *const >  requirements = {} 
)

Binds a runnable to execute when the trigger becomes inactive.

Parameters
toRunthe runnable to execute.
requirementsthe required subsystems.
Deprecated:
Use OnFalse(Command) instead and construct the InstantCommand manually

◆ WhenInactive() [4/4]

template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger frc2::Trigger::WhenInactive ( T &&  command)
inline

Binds a command to start when the trigger becomes inactive.

Transfers command ownership to the button scheduler, so the user does not have to worry about lifespan - rvalue refs will be moved, lvalue refs will be copied.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use OnFalse(Command) instead.

◆ WhileActiveContinous() [1/4]

Trigger frc2::Trigger::WhileActiveContinous ( Command command)

Binds a command to be started repeatedly while the trigger is active, and canceled when it becomes inactive.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use WhileTrue(Command) with RepeatCommand, or bind command::Schedule with IfHigh(std::function<void()>).

◆ WhileActiveContinous() [2/4]

Trigger frc2::Trigger::WhileActiveContinous ( std::function< void()>  toRun,
std::initializer_list< Subsystem * >  requirements 
)

Binds a runnable to execute repeatedly while the trigger is active.

Parameters
toRunthe runnable to execute.
requirementsthe required subsystems.
Deprecated:
Use WhileTrue(Command) and construct a RunCommand manually

◆ WhileActiveContinous() [3/4]

Trigger frc2::Trigger::WhileActiveContinous ( std::function< void()>  toRun,
std::span< Subsystem *const >  requirements = {} 
)

Binds a runnable to execute repeatedly while the trigger is active.

Parameters
toRunthe runnable to execute.
requirementsthe required subsystems.
Deprecated:
Use WhileTrue(Command) and construct a RunCommand manually

◆ WhileActiveContinous() [4/4]

template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger frc2::Trigger::WhileActiveContinous ( T &&  command)
inline

Binds a command to be started repeatedly while the trigger is active, and canceled when it becomes inactive.

Transfers command ownership to the button scheduler, so the user does not have to worry about lifespan - rvalue refs will be moved, lvalue refs will be copied.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use WhileTrue(Command) with RepeatCommand, or bind command::Schedule with IfHigh(std::function<void()>).

◆ WhileActiveOnce() [1/2]

Trigger frc2::Trigger::WhileActiveOnce ( Command command)

Binds a command to be started when the trigger becomes active, and canceled when it becomes inactive.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use WhileTrue(Command) instead.

◆ WhileActiveOnce() [2/2]

template<class T , typename = std::enable_if_t<std::is_base_of_v< Command, std::remove_reference_t<T>>>>
Trigger frc2::Trigger::WhileActiveOnce ( T &&  command)
inline

Binds a command to be started when the trigger becomes active, and canceled when it becomes inactive.

Transfers command ownership to the button scheduler, so the user does not have to worry about lifespan - rvalue refs will be moved, lvalue refs will be copied.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.
Deprecated:
Use WhileTrue(Command) instead.

◆ WhileFalse() [1/2]

Trigger frc2::Trigger::WhileFalse ( Command command)

Starts the given command when the condition changes to false and cancels it when the condition changes to true.

Doesn't re-start the command if it ends while the condition is still true. If the command should restart, see RepeatCommand.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to start
Returns
this trigger, so calls can be chained

◆ WhileFalse() [2/2]

Trigger frc2::Trigger::WhileFalse ( CommandPtr &&  command)

Starts the given command when the condition changes to false and cancels it when the condition changes to true.

Moves command ownership to the button scheduler.

Doesn't re-start the command if it ends while the condition is still false. If the command should restart, see RepeatCommand.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.

◆ WhileTrue() [1/2]

Trigger frc2::Trigger::WhileTrue ( Command command)

Starts the given command when the condition changes to true and cancels it when the condition changes to false.

Doesn't re-start the command if it ends while the condition is still true. If the command should restart, see RepeatCommand.

Takes a raw pointer, and so is non-owning; users are responsible for the lifespan of the command.

Parameters
commandthe command to start
Returns
this trigger, so calls can be chained

◆ WhileTrue() [2/2]

Trigger frc2::Trigger::WhileTrue ( CommandPtr &&  command)

Starts the given command when the condition changes to true and cancels it when the condition changes to false.

Moves command ownership to the button scheduler.

Doesn't re-start the command if it ends while the condition is still true. If the command should restart, see RepeatCommand.

Parameters
commandThe command to bind.
Returns
The trigger, for chained calls.

The documentation for this class was generated from the following file: