Version 2 (modified by 6 years ago) (diff) | ,
---|
Simple Xbox Controller Example Code
- In your web browser go to the WPILib javadoc documentation. You can google “WPILib 2019 javadoc” to find them.
- Click on the “ALL CLASSES” hyperlink. Then scroll down until you see the XboxControler? class. Click on it.
- Read the class description. Look at the constructor; note the argument that is passed into it.
- Read the getYButtonPressed method; note the return type.
- Note where everywhere the xbox variable is used in the code below.
- Modify the program to print out which button was pressed, A, B, X, or Y.
- Read the documentation for the getX(GenericHID.Hand hand) and getY(GenericHID.Hand hand); note the return type of the methods.
- Click on the GenericHID link and notice that the documentation shows that it has two constants corresponding to the left and right xbox joysticks.
- 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. */ @Override 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. */ @Override 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