First Program: Xbox Controller

When driving a robot in teleoperated mode, we need a way to control its motion. Teams typically use gaming joysticks or xbox controllers. Xbox Controller Our first program will read the joystick position from an xbox or Logitech F310 game controller.

We write robot software in VSCode just as we did for Java, but we'll make use of the WPILib extensions to create, build, and run our robot programs. The WPILib extension is available via the WPILib VSCode Icon icon in the toolbar (see here). Click on the icon and then:

  • Select Create a new project
  • Select Project Type->Template->java->Timed Robot
  • Select a New Project Folder and select (or create and then select) Documents\Robot Projects
  • Enter project name: Xbox1
  • Enter team number: 2537
  • Press Generate Project
  • At the dialog select Yes (Current Window)

The WPILib extension then creates a new Robot project for you. The project contains many files and they're listed and organized in the left tree. The file we're intersted in is src->main->java->frc->robot-> open that file by double-clicking on it.

The class that was created for you contains the minimal set of functions required for a robot program operating in the WPIlib framework. The functions include:

  • robotInit() - called once when the robot starts
  • robotPeriodic() - called periodically (e.g. every 20ms)
  • autonomousInit() called one when the robot first enters autonomous mode
  • autonomousPeriodic() - called periodically (e.g. every 20ms) while the robot is in autonomous mode
  • teleopPeriodic() - called periodically while robot is in teleoperated mode
  • testPeriodic() - called periodically while robot is in test mode

You will put your setup code in the Init() functions and the code that should run repeatedly in the Periodic() functions. The code will make use of the pre-existing XboxController class provided in WPILib that makes it easy to use an Xbox controller.

Modify the newly created program as follows:

  • Import the XboxController and Hand classes:
          import edu.wpi.first.wpilibj.XboxController;
          import edu.wpi.first.wpilibj.GenericHID.Hand;
  • In the Robot class definition where variables are defined, add
          XboxController xbox; 
  • In the function robotInit() add:
         xbox = new XboxController(0); 
  • In the function robotPeriodic() add:
         SmartDashboard.putNumber("Left Joystick X", xbox.getX(Hand.kLeft)); 

Your program should look like this

  • Use the WPILib icon to build your new program: W->Build Robot Code
  • Connect to the Peanut WiFi Hotspot (the default password is password)
  • Use the WPILib icon to deploy your program to the robot: W->Deploy Robot Code
  • Launch the FRC Driver Station on your laptop. Two windows should open: FRC Driver Station and FRC PC Dashboard.
  • Plug in an xbox controller and make sure it is detected
  • In the FRC driver station, make sure the Communications, Robot Code, and Joysticks LED icons are all green
  • In the FRC PC Dashboard, select the Variables tab and scroll down to Smart Dashboard->Left Joystick X (that's the variable you created!)
  • Move the left X-box joystick right and left and watch the value shown for Left Joystick X change!

You can even debug your program on the robot using the VSCode debugger just as you did with Java programs running on your laptop:

  • Set a breakpoint on the line: xbox = new XboxController(0);
  • Use the WPILib icon to start debugging: W->Debug Robot Code
  • The program will run until it gets to that line and then stop. Press the continue button to continue running.
  • Even though the program is running on the robot, you can set breakpoints, step through code, examine and change variables, etc. from your laptop.

Extra credit

Extend the program to print a message or display a value on the Smart Dashboard when a button is pressed on the xbox controller.

You can also learn about Debouncing an input such as a button

Next steps

Examine some of the more sophisticated functions in this sample code

Last modified 3 years ago Last modified on Feb 14, 2020, 12:58:19 AM

Attachments (3)

Download all attachments as: .zip