| 1 | {{{ |
| 2 | /*----------------------------------------------------------------------------*/ |
| 3 | /* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */ |
| 4 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 5 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 6 | /* the project. */ |
| 7 | /*----------------------------------------------------------------------------*/ |
| 8 | |
| 9 | package frc.robot; |
| 10 | |
| 11 | import edu.wpi.first.wpilibj.TimedRobot; |
| 12 | import edu.wpi.first.wpilibj.smartdashboard.SendableChooser; |
| 13 | import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; |
| 14 | import edu.wpi.first.wpilibj.XboxController; |
| 15 | import edu.wpi.first.wpilibj.GenericHID.Hand; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * The VM is configured to automatically run this class, and to call the |
| 20 | * functions corresponding to each mode, as described in the TimedRobot |
| 21 | * documentation. If you change the name of this class or the package after |
| 22 | * creating this project, you must also update the build.gradle file in the |
| 23 | * project. |
| 24 | */ |
| 25 | public class Robot extends TimedRobot { |
| 26 | private static final String kDefaultAuto = "Default"; |
| 27 | private static final String kCustomAuto = "My Auto"; |
| 28 | private final SendableChooser<String> m_chooser = new SendableChooser<>(); |
| 29 | |
| 30 | // Create xbox controller variable. |
| 31 | private XboxController xbox; |
| 32 | // Create a constant representing the port the xbox controller is plugged into |
| 33 | private final int PORT = 0; |
| 34 | |
| 35 | /** |
| 36 | * This function is run when the robot is first started up and should be |
| 37 | * used for any initialization code. |
| 38 | */ |
| 39 | @Override |
| 40 | public void robotInit() { |
| 41 | m_chooser.setDefaultOption("Default Auto", kDefaultAuto); |
| 42 | m_chooser.addOption("My Auto", kCustomAuto); |
| 43 | SmartDashboard.putData("Auto choices", m_chooser); |
| 44 | |
| 45 | //Instantiate xbox controller object |
| 46 | // the argument is the USB port that controller is plugged into |
| 47 | xbox = new XboxController(PORT); |
| 48 | |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * This function is called periodically during operator control. |
| 53 | */ |
| 54 | @Override |
| 55 | public void teleopPeriodic() { |
| 56 | |
| 57 | // Print "Y button pressed" every time the button is pressed |
| 58 | if (xbox.getYButtonPressed()) { |
| 59 | System.out.println("Y button pressed"); |
| 60 | } |
| 61 | else if (xbox.getXButtonPressed()){ |
| 62 | System.out.println("X button presssed"); |
| 63 | } |
| 64 | else if (xbox.getAButtonPressed()) { |
| 65 | System.out.println("A button pressed"); |
| 66 | } |
| 67 | else if (xbox.getBButtonPressed()) { |
| 68 | System.out.println("B button pressed"); |
| 69 | } |
| 70 | |
| 71 | // Get and print x,y values of left joystick |
| 72 | double x = xbox.getX(Hand.kLeft); |
| 73 | double y = xbox.getY(Hand.kLeft); |
| 74 | System.out.println("left Joystick (" + x + ", " + y + ")"); |
| 75 | |
| 76 | // Get and print x,y values of right joystick |
| 77 | x = xbox.getX(Hand.kRight); |
| 78 | y = xbox.getY(Hand.kRight); |
| 79 | System.out.println("right Joystick (" + x + ", " + y + ")"); |
| 80 | } |
| 81 | } |
| 82 | }}} |