Class Trigger

java.lang.Object
edu.wpi.first.wpilibj2.command.button.Trigger
All Implemented Interfaces:
BooleanSupplier
Direct Known Subclasses:
SpectrumState

public class Trigger extends Object implements BooleanSupplier
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 and(BooleanSupplier), or(BooleanSupplier), negate() operators.

This class is provided by the NewCommands VendorDep

Spectrum modified in Fall 2024 to allow triggers to default start condition of false, so if something is already true when bound it will activate the trigger. We needed this for a trigger to activate only if Teleop was enabled.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Trigger
     
    static final Trigger
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Trigger(edu.wpi.first.wpilibj.event.EventLoop loop, BooleanSupplier condition)
    Creates a new trigger based on the given condition.
    Creates a new trigger based on the given condition.
  • Method Summary

    Modifier and Type
    Method
    Description
    Composes two triggers with logical AND.
    and(BooleanSupplier... triggers)
    Combines multiple BooleanSupplier triggers using a logical AND operation.
    debounce(double seconds)
    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(double seconds, edu.wpi.first.math.filter.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.
    boolean
     
    Creates a new trigger that is active when this trigger is inactive, i.e.
    not()
    renamed negate
    onChange(edu.wpi.first.wpilibj2.command.Command command)
    Starts the command when the condition changes.
    onChangeToFalse(edu.wpi.first.wpilibj2.command.Command command)
    Starts the given command whenever the condition changes from `true` to `false`, but has to have run once prior
    onChangeToTrue(edu.wpi.first.wpilibj2.command.Command command)
    Starts the given command whenever the condition changes from `false` to `true`, but has to have run once prior
    onFalse(edu.wpi.first.wpilibj2.command.Command command)
    Starts the given command whenever the condition changes from `true` to `false`.
    onFalse(edu.wpi.first.wpilibj2.command.Command... commands)
     
    onTrue(edu.wpi.first.wpilibj2.command.Command command)
    Starts the given command whenever the condition changes from `false` to `true`.
    onTrue(edu.wpi.first.wpilibj2.command.Command... commands)
    Starts the given commands whenever the condition changes from `false` to `true`.
    Composes two triggers with logical OR.
    or(BooleanSupplier... triggers)
    Combines multiple BooleanSupplier triggers using a logical OR operation.
    runWithEndSequence(edu.wpi.first.wpilibj2.command.Command runCommand, edu.wpi.first.wpilibj2.command.Command endCommand, double endTimeout)
    Run a command while true.
    toggleOnFalse(edu.wpi.first.wpilibj2.command.Command command)
    Toggles a command when the condition changes from `true` to `false`.
    toggleOnTrue(edu.wpi.first.wpilibj2.command.Command command)
    Toggles a command when the condition changes from `false` to `true`.
    whileFalse(edu.wpi.first.wpilibj2.command.Command command)
    Starts the given command when the condition changes to `false` and cancels it when the condition changes to `true`.
    whileFalse(edu.wpi.first.wpilibj2.command.Command... commands)
     
    whileTrue(edu.wpi.first.wpilibj2.command.Command command)
    Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.
    whileTrue(edu.wpi.first.wpilibj2.command.Command... commands)
    Starts the given command when the condition changes to `true` and cancels it when the condition changes to `false`.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • kFalse

      public static final Trigger kFalse
    • kTrue

      public static final Trigger kTrue
  • Constructor Details

    • Trigger

      public Trigger(edu.wpi.first.wpilibj.event.EventLoop loop, BooleanSupplier condition)
      Creates a new trigger based on the given condition.
      Parameters:
      loop - The loop instance that polls this trigger.
      condition - the condition represented by this trigger
    • Trigger

      public Trigger(BooleanSupplier condition)
      Creates a new trigger based on the given condition.

      Polled by the default scheduler button loop.

      Parameters:
      condition - the condition represented by this trigger
  • Method Details

    • onChange

      public Trigger onChange(edu.wpi.first.wpilibj2.command.Command command)
      Starts the command when the condition changes.
      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • onTrue

      public Trigger onTrue(edu.wpi.first.wpilibj2.command.Command command)
      Starts the given command whenever the condition changes from `false` to `true`.
      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • onTrue

      public Trigger onTrue(edu.wpi.first.wpilibj2.command.Command... commands)
      Starts the given commands whenever the condition changes from `false` to `true`.
      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • onFalse

      public Trigger onFalse(edu.wpi.first.wpilibj2.command.Command command)
      Starts the given command whenever the condition changes from `true` to `false`.
      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • onFalse

      public Trigger onFalse(edu.wpi.first.wpilibj2.command.Command... commands)
    • onChangeToFalse

      public Trigger onChangeToFalse(edu.wpi.first.wpilibj2.command.Command command)
      Starts the given command whenever the condition changes from `true` to `false`, but has to have run once prior
      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • onChangeToTrue

      public Trigger onChangeToTrue(edu.wpi.first.wpilibj2.command.Command command)
      Starts the given command whenever the condition changes from `false` to `true`, but has to have run once prior
      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • whileTrue

      public Trigger whileTrue(edu.wpi.first.wpilibj2.command.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.

      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • whileTrue

      public Trigger whileTrue(edu.wpi.first.wpilibj2.command.Command... commands)
      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.

      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • whileFalse

      public Trigger whileFalse(edu.wpi.first.wpilibj2.command.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 `false`. If the command should restart, see RepeatCommand.

      Parameters:
      command - the command to start
      Returns:
      this trigger, so calls can be chained
    • whileFalse

      public Trigger whileFalse(edu.wpi.first.wpilibj2.command.Command... commands)
    • toggleOnTrue

      public Trigger toggleOnTrue(edu.wpi.first.wpilibj2.command.Command command)
      Toggles a command when the condition changes from `false` to `true`.
      Parameters:
      command - the command to toggle
      Returns:
      this trigger, so calls can be chained
    • toggleOnFalse

      public Trigger toggleOnFalse(edu.wpi.first.wpilibj2.command.Command command)
      Toggles a command when the condition changes from `true` to `false`.
      Parameters:
      command - the command to toggle
      Returns:
      this trigger, so calls can be chained
    • runWithEndSequence

      public Trigger runWithEndSequence(edu.wpi.first.wpilibj2.command.Command runCommand, edu.wpi.first.wpilibj2.command.Command endCommand, double endTimeout)
      Run a command while true. Also runs a command for a certain timeout when released.
      Parameters:
      trigger -
      runCommand -
      endCommand -
    • getAsBoolean

      public boolean getAsBoolean()
      Specified by:
      getAsBoolean in interface BooleanSupplier
    • and

      public Trigger and(BooleanSupplier trigger)
      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.
    • and

      public Trigger and(BooleanSupplier... triggers)
      Combines multiple BooleanSupplier triggers using a logical AND operation.
      Parameters:
      triggers - an array of BooleanSupplier triggers to be combined.
      Returns:
      a new Trigger that represents the logical AND of all provided triggers.
    • or

      public Trigger or(BooleanSupplier... triggers)
      Combines multiple BooleanSupplier triggers using a logical OR operation.
      Parameters:
      triggers - an array of BooleanSupplier triggers to be combined.
      Returns:
      a new NewTrigger instance that represents the logical OR of the provided triggers.
    • or

      public Trigger or(BooleanSupplier trigger)
      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

      public Trigger 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
    • not

      public Trigger not()
      renamed negate
      Returns:
      the negated trigger
    • debounce

      public Trigger debounce(double seconds)
      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:
      seconds - The debounce period.
      Returns:
      The debounced trigger (rising edges debounced only)
    • debounce

      public Trigger debounce(double seconds, edu.wpi.first.math.filter.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.
      Parameters:
      seconds - The debounce period.
      type - The debounce type.
      Returns:
      The debounced trigger.