Simple Xbox Controller Example Code

  1. In your web browser go to the WPILib javadoc documentation. You can google “WPILib 2019 javadoc” to find them.
  2. Click on the “ALL CLASSES” hyperlink. Then scroll down until you see the XboxController class. Click on it.
  3. Read the class description. Look at the constructor; note the argument that is passed into it.
  4. Read the getYButtonPressed method; note the return type.
  5. Note where everywhere the xbox variable is used in the code below.
  6. Modify the program to print out which button was pressed, A, B, X, or Y.
  7. Read the documentation for the getX(GenericHID.Hand hand) and getY(GenericHID.Hand hand); note the return type of the methods.
  8. Click on the GenericHID link and notice that the documentation shows that it has two constants corresponding to the left and right xbox joysticks.
  9. Modify the program to print out the x and y position of both joysticks every time the teleopPeriodic method is called. Use the following to make the call “xbox.getY(Hand.kLeft);” to get the left joystick y position.
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */

package frc.robot;

import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.wpilibj.smartdashboard.SendableChooser;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.XboxController;

 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the TimedRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the build.gradle file in the
 * project.
public class Robot extends TimedRobot {
  private static final String kDefaultAuto = "Default";
  private static final String kCustomAuto = "My Auto";
  private final SendableChooser<String> m_chooser = new SendableChooser<>();

  // Create xbox controller variable.
  private XboxController xbox;
  // Create a constant representing the port the xbox controller is plugged into
  private final int PORT = 0;

   * This function is run when the robot is first started up and should be
   * used for any initialization code.
  public void robotInit() {
    m_chooser.setDefaultOption("Default Auto", kDefaultAuto);
    m_chooser.addOption("My Auto", kCustomAuto);
    SmartDashboard.putData("Auto choices", m_chooser);

    //Instantiate xbox controller object
    // the argument is the USB port that controller is pluggedinto
    xbox = new XboxController(PORT);

   * This function is called periodically during operator control.
  public void teleopPeriodic() {

    // Print "Y button pressed" every time the button is pressed
    if (xbox.getYButtonPressed()) {
      System.out.println("Y button pressed");

If you're stuck and want to see the solution, it's here

  • NOTE: the Logitech F310 game controller works slightly differently from the standard xbox controller; to use the joysticks on the Logitech controller see here
Last modified 19 months ago Last modified on Nov 3, 2019, 10:09:51 PM