External Controllers & Triggers
Controllers that create commands.
Controllers such as Xbox or Playstation controllers are big parts of FRC robotics, as the robots can be directly controlled by them.
In addition to FRC robotics, controllers are useful due to their various input options, allowing users to control robots with ease. To get access to these controllers' various benefits, we will use Command controllers. For this example, we'll use CommandXboxController.
Creating a CommandXboxController Object
CommandXboxController ObjectSimilar to Spark motors objects refer to motors on the XRP robot, the CommandXboxController object refers to the controller that is connected to the computer. It takes one parameter- the port of the controller.
A controller's ID (called port in VSC) allows the code to differentiate connected controllers from one another just like the IDs of Spark motors and Encoders. Here is an example of an XboxController object:
private CommandXboxController controller = new CommandXboxController(1);Connecting an External Controller
There are 2 ways to connect an external controller to your computer to use in our project.
Using Bluetooth
Connecting the controller to the computer port (this one is much easier + more reliable)
After your controller is connected to your computer, check the System Joysticks tab on the bottom left of your screen. In this tab (if everything is working properly), look for the controller you just plugged in. Notice the conveniently placed Joysticks tab with a bunch of "Unassigned" slots. These are the different slots that you can assign your controllers. You see the number inside the squared brackets in each slot, like Joystick[0]? These numbers match the IDs you assign to your controllers.
Using the CommandXboxController Object
CommandXboxController ObjectThere are lots of methods in the XboxController class, which work . Here is a link that has the list of all the methods in the class if you want them.
If you are using a CommandPS4Controller, you can use this link.
For the XRP Curriculum, you will only need to know 4 basic methods. These methods can be used anywhere as long as you have access to the controller- such as if you were to move your robot with joysticks.
Triggers
Triggers are powerful objects that let you bind commands to various events. For example, we can use them to run commands when a button is pressed or when a sensor detects an event.
Some examples of things we can use triggers for are:
Getting inputs
Checking sensors
Checking state changes
However, on this page, we'll focus primarily on using triggers for moving the robot through the controller. When doing this, we don't create brand-new Trigger objects— we simply use the ones built-in into our controller.
All of these triggers can be found here.
Triggers have three built-in methods that we use to run commands depending on specific criteria. The most common ones are:
OnTrue(Command c)OnFalse(Command c)WhileTrue(Command c)WhileFalse(Command c)
As you can tell, all four of these methods take in a command as a parameter. All four of them also run whatever's in their parameters, but depending on their name, they run at completely different times. (for example: WhileTrue() runs its command while a condition is true)
If you want an example of how to use them, look back at configureBindings() at the second trigger statement.
As it stands, while the B button is held down, it will constantly schedule this example method command.

If I change it to onTrue(), the trigger will only schedule the command when the B button is pressed down for the first time.

By mastering triggers, you can create responsive, sophisticated robot behaviors that react intelligently to both operator inputs and changing field conditions, all while maintaining readable and maintainable code.
Last updated