| 1 | == Simple Xbox Controller Example Code |
| 2 | |
| 3 | 1. In your web browser go to the WPILib javadoc documentation. You can google “WPILib 2019 javadoc” to find them. |
| 4 | 2. Click on the “ALL CLASSES” hyperlink. Then scroll down until you see the XboxControler class. Click on it. |
| 5 | 3. Read the class description. Look at the constructor; note the argument that is passed into it. |
| 6 | 4. Read the getYButtonPressed method; note the return type. |
| 7 | 5. Note where everywhere the xbox variable is used in the code below. |
| 8 | 6. Modify the program to print out which button was pressed, A, B, X, or Y. |
| 9 | 7. Read the documentation for the getX([https://first.wpi.edu/FRC/roborio/release/docs/java/edu/wpi/first/wpilibj/GenericHID.Hand.html GenericHID.Hand] hand) and getY(GenericHID.Hand hand); note the return type of the methods. |
| 10 | 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. |
| 11 | 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. |
| 12 | |
| 13 | |
| 14 | {{{ |
| 15 | /*----------------------------------------------------------------------------*/ |
| 16 | /* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ |
| 17 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 18 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 19 | /* the project. */ |
| 20 | /*----------------------------------------------------------------------------*/ |
| 21 | |
| 22 | package frc.robot; |
| 23 | |
| 24 | import edu.wpi.first.wpilibj.TimedRobot; |
| 25 | import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; |
| 26 | import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
| 27 | import edu.wpi.first.wpilibj.XboxController; |
| 28 | |
| 29 | /** |
| 30 | * The VM is configured to automatically run this class, and to call the |
| 31 | * functions corresponding to each mode, as described in the TimedRobot |
| 32 | * documentation. If you change the name of this class or the package after |
| 33 | * creating this project, you must also update the build.gradle file in the |
| 34 | * project. |
| 35 | */ |
| 36 | public class Robot extends TimedRobot { |
| 37 | private static final String kDefaultAuto = "Default"; |
| 38 | private static final String kCustomAuto = "My Auto"; |
| 39 | private final SendableChooser<String> m_chooser = new SendableChooser<>(); |
| 40 | |
| 41 | // Create xbox controller variable. |
| 42 | private XboxController xbox; |
| 43 | // Create a constant representing the port the xbox controller is plugged into |
| 44 | private final int PORT = 0; |
| 45 | |
| 46 | /** |
| 47 | * This function is run when the robot is first started up and should be |
| 48 | * used for any initialization code. |
| 49 | */ |
| 50 | @Override |
| 51 | public void robotInit() { |
| 52 | m_chooser.setDefaultOption("Default Auto", kDefaultAuto); |
| 53 | m_chooser.addOption("My Auto", kCustomAuto); |
| 54 | SmartDashboard.putData("Auto choices", m_chooser); |
| 55 | |
| 56 | //Instantiate xbox controller object |
| 57 | // the argument is the USB port that controller is pluggedinto |
| 58 | xbox = new XboxController(PORT); |
| 59 | |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * This function is called periodically during operator control. |
| 64 | */ |
| 65 | @Override |
| 66 | public void teleopPeriodic() { |
| 67 | |
| 68 | // Print "Y button pressed" every time the button is pressed |
| 69 | if (xbox.getYButtonPressed()) { |
| 70 | System.out.println("Y button pressed"); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | }}} |