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. Other than just specifically FRC robotics, controllers are useful due to their various forms of inputs allowing users to control robots with ease. To get access to these controllers' various benefits, we will use the WPILIB XboxController object.

Though the object is an XboxController object, you can use any controller. Some controllers we know work are the Xbox controller, Switch Pro controller, and the PlayStation DualShock controller.


Creating an XboxController Object

Similar to Spark motors objects refer to motors on the Romi. The XboxController 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 XboxController controller = new XboxController(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 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]? The numbers are the same IDs you gave to your External Controllers to!

Drawing
How to connect controller
Drawing
Interface once a controller is added

Once you add a controller to the joysticks. It will save until you disconnect your controller from your computer.


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:

For the Romi though, 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();

Challenge: update the MoveRobot() method

Now that you know about the XboxController class, create a new XboxController object in the RomiDrivetrain class as well as a new method called goWithController.

Conditions:

Here is the answer code (the XboxController object is named controller). I also highly suggest testing your code by adding it to the Robot class in the teleopPeriodic() method.

Answer code
public void goWithController(){
    m_leftMotor.set(controller.getLeftY());
    m_rightMotor.set(controller.getRightY());

Last updated