# 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` .

{% hint style="info" %}
CommandPS5Controllers and other ones work just fine. The only reason we're using CommandXboxControllers here is that we were trained on them.
{% endhint %}

***

### Creating a `CommandXboxController` Object

Similar 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:

```java
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.

<img src="https://1432020148-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FD93AURfj5NdwXjVDjMmD%2Fuploads%2FtOiMfbTOHguXo7Rb5HLZ%2Ffile.excalidraw.svg?alt=media&#x26;token=c65ff6ec-ba9a-4e7d-a8b2-3d4863d7726c" alt="How to connect controller" class="gitbook-drawing">

<img src="https://1432020148-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FD93AURfj5NdwXjVDjMmD%2Fuploads%2FEMO0D7b5xTK6PyhqUkEu%2Ffile.excalidraw.svg?alt=media&#x26;token=db87867f-1654-4942-82d5-0b708415352e" alt="Interface once a controller is added" class="gitbook-drawing">

{% hint style="info" %}
Once you add a controller to the joysticks. It will save until you disconnect your controller from your computer.
{% endhint %}

***

### Using the `XboxController` Object

There 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:

{% embed url="<https://first.wpi.edu/wpilib/allwpilib/docs/release/java/edu/wpi/first/wpilibj/XboxController.html>" %}

For the Romi Curriculum, you will only need to know 4 basic methods:

<pre class="language-java"><code class="lang-java">
<strong>/* Gets the right joystick's position on the 
</strong><strong>X-axis returning a double between -1 and 1. */
</strong>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. */
<strong>double leftX = controller.getLeftX(); 
</strong>
/* 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
));
</code></pre>

## 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.&#x20;

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 from[external-controllers](https://velocity-raptors-9450.gitbook.io/intro-to-frc-programming-romi/romi-command-based/external-controllers "mention").

All of these triggers can be found [here](https://github.wpilib.org/allwpilib/docs/release/java/edu/wpi/first/wpilibj2/command/button/CommandXboxController.html).

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: <kbd>WhileTrue()</kbd> 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.&#x20;

<figure><img src="https://1432020148-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FD93AURfj5NdwXjVDjMmD%2Fuploads%2F6KfUDpbCgeXTPiN4Hred%2Fimage.png?alt=media&#x26;token=20cb79bd-ed55-458c-8cfc-f144278bf8e9" alt=""><figcaption></figcaption></figure>

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

<figure><img src="https://1432020148-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FD93AURfj5NdwXjVDjMmD%2Fuploads%2FN453odaf0hoAhGUKLOKo%2Fimage.png?alt=media&#x26;token=5e15cb08-beec-4f86-b8c8-3cd45c647c31" alt=""><figcaption></figcaption></figure>

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.

***

## Challenge- Bind the MoveMotor() Command to a Trigger

This is the final step before you can move the robot! All you'll need to do is:

* [ ] Use the triggers from your CommandController, and put your command inside the parameters.
* [ ] Use the two joysticks on your controller to move the robot. Get their axises, then set them as the speed variables of your command.

\[insert hint about making new command but not assigning them to smth]
