Class Trigger
- All Implemented Interfaces:
BooleanSupplier
- Direct Known Subclasses:
InternalButton, JoystickButton, NetworkButton, POVButton
CommandGamepad.faceDown()) or be customized to read sensor values or
any other arbitrary true/false condition.
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 and(BooleanSupplier), or(BooleanSupplier), negate() operators.
Trigger bindings created inside a running command will only be active while that command is running. This is useful for defining trigger-based behavior only in a certain scope and avoids needing to create dozens of global triggers. Any commands scheduled by these triggers will be canceled when the enclosing command exits.
Command shootWhileAiming = Command.noRequirements(co -> {
turret.atTarget.onTrue(shooter.shootOnce());
co.await(turret.lockOnGoal());
}).named("Shoot While Aiming");
controller.rightBumper().whileTrue(shootWhileAiming);
-
Constructor Summary
ConstructorsConstructorDescriptionTrigger(BooleanSupplier condition) Creates a new trigger based on the given condition.Trigger(Scheduler scheduler, BooleanSupplier condition) Creates a new trigger based on the given condition.Trigger(Scheduler scheduler, EventLoop loop, BooleanSupplier condition) Creates a new trigger based on the given condition. -
Method Summary
Modifier and TypeMethodDescriptionand(BooleanSupplier trigger) Composes two triggers with logical AND.Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period.debounce(Time duration, Debouncer.DebounceType type) Creates a new debounced trigger from this trigger - it will become active when this trigger has been active for longer than the specified period.Creates a trigger that activates on a falling edge of this trigger's signal.booleanGets the boolean state of the trigger.multiPress(int pressCount, Time duration) Creates a trigger that activates when this trigger has at leastpressCountrising edges within the specified duration.negate()Creates a new trigger that is active when this trigger is inactive, i.e.Starts the given command whenever the condition changes from `true` to `false`.Starts the given command whenever the condition changes from `false` to `true`.or(BooleanSupplier trigger) Composes two triggers with logical OR.retryWhileFalse(Command command) Starts the given command when the condition changes to `false` and cancels it when the condition changes to `true`.retryWhileTrue(Command command) Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.Creates a trigger that activates on a rising edge of this trigger's signal.toggleOnFalse(Command command) Toggles a command when the condition changes from `true` to `false`.toggleOnTrue(Command command) Toggles a command when the condition changes from `false` to `true`.voidunbind()Unbinds this trigger from the event loop and clears all command bindings; any bound commands that are currently running will be canceled.whileFalse(Command command) Starts the given command when the condition changes to `false` and cancels it when the condition changes to `true`.Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.
-
Constructor Details
-
Trigger
Creates a new trigger based on the given condition. Polled by the scheduler'sdefault event loop.- Parameters:
scheduler- The scheduler that should execute triggered commands.condition- the condition represented by this trigger
-
Trigger
Creates a new trigger based on the given condition. Triggered commands are executed by thedefault scheduler.Polled by the default scheduler button loop.
- Parameters:
condition- the condition represented by this trigger
-
Trigger
Creates a new trigger based on the given condition.- Parameters:
scheduler- The scheduler that should execute triggered commands.loop- The event loop to poll the trigger.condition- the condition represented by this trigger
-
-
Method Details
-
onTrue
-
onFalse
-
whileTrue
Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.Unlike
retryWhileTrue(Command), this does not restart the command if it ends while the condition is still `true`.- Parameters:
command- the command to start- Returns:
- this trigger, so calls can be chained
-
whileFalse
Starts the given command when the condition changes to `false` and cancels it when the condition changes to `true`.Unlike
retryWhileFalse(Command), this does not restart the command if it ends while the condition is still `false`.- Parameters:
command- the command to start- Returns:
- this trigger, so calls can be chained
-
retryWhileTrue
Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.Unlike
whileTrue(Command), the command is restarted if it ends while the condition is still `true`. If the command stopped because it was interrupted, restarting it will immediately interrupt the would-be interrupting command (if they have the same priority).- Parameters:
command- the command to start- Returns:
- this trigger, so calls can be chained
-
retryWhileFalse
Starts the given command when the condition changes to `false` and cancels it when the condition changes to `true`.Unlike
whileFalse(Command), the command is restarted if it ends while the condition is still `false`. If the command stopped because it was interrupted, restarting it will immediately interrupt the would-be interrupting command (if they have the same priority).- Parameters:
command- the command to start- Returns:
- this trigger, so calls can be chained
-
toggleOnTrue
-
toggleOnFalse
-
getAsBoolean
public boolean getAsBoolean()Gets the boolean state of the trigger. Because triggers are checked when their event loop is polled, which by default occurs whenScheduler.run()is called in user code, but may differ for custom event loops, the state is only valid immediately after the event loop is polled. If the underlying signal changes without a subsequent event loop poll, the return value fromgetAsBoolean()may not agree with the result of checking the signal directly.This method will always return
falsebefore being polled by an event loop.- Specified by:
getAsBooleanin interfaceBooleanSupplier- Returns:
- The state of the trigger.
-
and
Composes two triggers with logical AND.- Parameters:
trigger- the condition to compose with- Returns:
- A trigger which is active when both component triggers are active.
-
or
Composes two triggers with logical OR.- Parameters:
trigger- the condition to compose with- Returns:
- A trigger which is active when either component trigger is active.
-
negate
Creates a new trigger that is active when this trigger is inactive, i.e. that acts as the negation of this trigger.- Returns:
- the negated trigger
-
debounce
-
debounce
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:
duration- The debounce period.type- The debounce type.- Returns:
- The debounced trigger.
-
risingEdge
Creates a trigger that activates on a rising edge of this trigger's signal. The rising edge trigger is active in the same cycle that this trigger's condition istruewhile its condition in the previous cycle wasfalse. The resulting trigger will only be active for that single cycle before going inactive again; therefore,onTrue(Command)should be used instead ofwhileTrue(Command), as commands bound using the latter method will be immediately canceled after a single scheduler cycle.- Returns:
- A rising edge trigger.
-
fallingEdge
Creates a trigger that activates on a falling edge of this trigger's signal. The falling edge trigger is active in the same cycle that this trigger's condition isfalsewhile its condition in the previous cycle wastrue. The resulting trigger will only be active for that single cycle before going inactive again; therefore,onTrue(Command)should be used instead ofwhileTrue(Command), as commands bound using the latter method will be immediately canceled after a single scheduler cycle.- Returns:
- A falling edge trigger.
-
multiPress
Creates a trigger that activates when this trigger has at leastpressCountrising edges within the specified duration.- Parameters:
pressCount- The number of rising edges to require. If this is non-positive, the trigger will always be active.duration- The duration within which the rising edges must occur. If this is non-positive, the trigger will never activate.- Returns:
- A trigger that activates on multiple presses
-
unbind
public void unbind()Unbinds this trigger from the event loop and clears all command bindings; any bound commands that are currently running will be canceled. The trigger may be garbage collected if no other references exist in user code. Binding a command to a trigger viaonTrue(Command)or similar will re-bind the trigger to the event loop.Note: because triggers are only updated when they're bound to an event loop, calling
#unbind()will result ingetAsBoolean()continuing to return the same value until the trigger is re-bound.This method is automatically called by the associated
Schedulerwhen the trigger's creation scope becomes inactive: a trigger created inside a command will be unbound when that command completes, and may be eligible for garbage collection; and a trigger created while an opmode is running will be unbound when that opmode ends (and also may be eligible for garbage collection).
-