== 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. [[Image(xboxController.jpg,right,250px,margin=10)]] Our first program will read the joystick position from an xbox or [wiki:ControlSystems/GameControllers/LogitechF310 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 [[Image(wpilibIcon.png,25px)]] icon in the toolbar (see [https://wpilib.screenstepslive.com/s/currentCS/m/java/l/1027060-visual-studio-code-basics-and-the-wpilib-extension 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->Robot.java open that file by double-clicking on it. The Robot.java 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 [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/XboxController.html XboxController] class provided in WPILib that makes it easy to use an Xbox controller. Modify the newly created program as follows: * Import the [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/XboxController.html XboxController] and [https://first.wpi.edu/FRC/roborio/beta/docs/java/edu/wpi/first/wpilibj/GenericHID.Hand.html 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 [wiki://ControlSystems/SoftwareTeam/Training/IntroRobotJava/XboxExample 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 [wiki:/ControlSystems/SoftwareTeam/Training/GettingStarted/Debouncing Debouncing] an input such as a button === Next steps Examine some of the more sophisticated functions in [wiki:///ControlSystems/SampleCode/XboxController this sample code]