External Controllers
Controllers such as Xbox or Playstation controllers are big parts of FRC robotics and robotics in general. Controllers are important in FRC because competitions always have a player-controlled section. During this section, you switch to teleoperated mode, and a player controls the robot most likely through a controller. 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 Romi. The CommandXboxController object refers to the controller that is connected to the computer.
Parameters:
- Integer(int) -> Its ID 
A controller's ID (called port in VS code) 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 XboxController Object
XboxController ObjectThere are a multitude of methods in the XboxController class, some more important than others. Here is a link that has the list of all the methods in the class:
For the Romi Curriculum, you will only need to know 4 basic methods:
/* Gets the right joystick's position on the 
X-axis returning a double between -1 and 1. */
double rightX = controller.getRightX(); 
/* Gets the right joystick's position on the 
Y-axis returning a double between -1 and 1. */
double rightY = controller.getRightY();
/* Gets the left joystick's position on the 
X-axis returning a double between -1 and 1. */
double leftX = controller.getLeftX(); 
/* Gets the left joystick's position on the 
Y-axis returning a double between -1 and 1. */
double leftY = controller.getLeftY();
controller.x().whileTrue(new Command(// stuff
));Triggers
Triggers are a powerful feature in WPILIB's command-based framework that lets 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 use the built-in ones fromExternal Controllers.
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