Binding Commands to Triggers¶
Apart from autonomous commands, which are scheduled at the start of the autonomous period, and default commands, which are automatically scheduled whenever their subsystem is not currently in-use, the most common way to run a command is by binding it to a triggering event, such as a button being pressed by a human operator. The command-based paradigm makes this extremely easy to do.
As mentioned earlier, command-based is a declarative paradigm. Accordingly, binding buttons to commands is done declaratively; the association of a button and a command is “declared” once, during robot initialization. The library then does all the hard work of checking the button state and scheduling (or cancelling) the command as needed, behind-the-scenes. Users only need to worry about designing their desired UI setup - not about implementing it!
Trigger class (including its
Button subclasses) can be composed to create composite triggers through the
negate() methods (or, in C++, the
! operators). For example:
Note that these methods return a
Trigger, not a
Button, so the
Trigger binding method names must be used even when buttons are composed.
Creating Your Own Custom Trigger¶
While binding to HID buttons is by far the most common use case, advanced users may occasionally want to bind commands to arbitrary triggering events. This can be easily done by simply writing your own subclass of
Alternatively, this can also be done inline by passing a lambda to the constructor of