What is a trigger?

We’ve moved to command based from iterative robot a little while ago, and I saw this… thing called Trigger (that I can in eclipse go new -> Trigger). What is it, what’re it’s uses and how do I use it?

Answering this from memory, but here goes…

The are several ways in wlilib to schedule commands to run. The most common is something like this:

myButton.whenPressed(new MyCommand());

If you look at the wpilib source code, you will notice that Button is a subclass of Trigger (and JoystickButton is a subclass of Button). All the whenPressed code does is turn around and do this:

this.whenActive(command);

where whenActive is the Trigger method that really makes the scheduling happen.

So, whenever you use a Button, you are just using a thin, more button like veneer over a Trigger. So, you already know how to use Triggers but just did not realize it yet.

The thing you came across in Eclipse is that you can create you own custom Trigger subclasses (siblings of Button) and use them the exact same way. Teams I have mentored have occasionally done this. Usually it is because we want to check multiple conditions before letting the command get scheduled. One example is that we once used a custom trigger to require 2 buttons to both be down at the same time to reset our gyro during a match. It was something we rarely if ever did, wanted to have it available just in case but also wanted to avoid having it happen accidentally. I have also seen custom triggers that check a button and a limit switch.

If I recall correctly, you override the Trigger’s get() method to specify your condition. Something like:

public boolean get() {
    button1.get() && button2.get();
}

Where more than likely, you passed the two buttons into your CustomTrigger class’ constructor.

I hope this helps,
Steve