|
| 1 | +// Copyright (c) FIRST and other WPILib contributors. |
| 2 | +// Open Source Software; you can modify and/or share it under the terms of |
| 3 | +// the WPILib BSD license file in the root directory of this project. |
| 4 | + |
| 5 | +package frc.robot.examples; |
| 6 | + |
| 7 | +import edu.wpi.first.wpilibj.Joystick; |
| 8 | +import edu.wpi.first.wpilibj.XboxController; |
| 9 | +import edu.wpi.first.wpilibj2.command.Commands; |
| 10 | +import edu.wpi.first.wpilibj2.command.button.CommandXboxController; |
| 11 | + |
| 12 | +/** |
| 13 | + * Joystick examples. |
| 14 | + */ |
| 15 | +public abstract class JoystickExamples { |
| 16 | + // Get joystick using Port Number on Driver Station |
| 17 | + final int PortNumberOnDriverStation = 0; |
| 18 | + final Joystick joystick = new Joystick(PortNumberOnDriverStation); |
| 19 | + |
| 20 | + /** |
| 21 | + * Buttons - get if a specific button is currently pressed |
| 22 | + */ |
| 23 | + private void Joystick_GetButtonPress_Example() { |
| 24 | + // Buttons start at 1 and not 0 |
| 25 | + boolean isPressingButton1 = joystick.getRawButton(1); |
| 26 | + boolean isPressingButton8 = joystick.getRawButton(8); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Joystick Axis - X, Y, Z |
| 31 | + */ |
| 32 | + private void Joystick_GetAxis_Example() { |
| 33 | + // left/right |
| 34 | + // left is negative |
| 35 | + // right is positive |
| 36 | + // sometimes we negative this depending on the use case |
| 37 | + double xInput = joystick.getX(); |
| 38 | + |
| 39 | + // forwards/backwards |
| 40 | + // forwards is negative |
| 41 | + // backwards in postive |
| 42 | + // very common we negate this in our code |
| 43 | + double yInput = joystick.getY(); |
| 44 | + |
| 45 | + // some joysticks twist too |
| 46 | + // clockwise postive usually |
| 47 | + // counter-clockwise negative usually |
| 48 | + double zInput = joystick.getZ(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * CommandXboxController by WPILib |
| 53 | + */ |
| 54 | + private void XboxController_CommandBased_Example() { |
| 55 | + // WPILib offers an XboxController class for simplicity |
| 56 | + final int PortNumberOnDriverStationForXbox = 1; |
| 57 | + final CommandXboxController cmdXboxController = new CommandXboxController(PortNumberOnDriverStationForXbox); |
| 58 | + |
| 59 | + // Buttons to trigger commmands |
| 60 | + cmdXboxController.y().onTrue(Commands.print("'Y' button was pressed!")); |
| 61 | + cmdXboxController.b().onTrue(Commands.print("'B' button was pressed!")); |
| 62 | + cmdXboxController.a().onTrue(Commands.print("'A' button was pressed!")); |
| 63 | + cmdXboxController.x().onTrue(Commands.print("'X' button was pressed!")); |
| 64 | + cmdXboxController.start().onTrue(Commands.print("'Start' button was pressed!")); |
| 65 | + cmdXboxController.back().onTrue(Commands.print("'Back' button was pressed!")); |
| 66 | + cmdXboxController.rightStick().onTrue(Commands.print("'Right Stick' was pressed!")); |
| 67 | + cmdXboxController.leftStick().onTrue(Commands.print("'Left Stick' was pressed!")); |
| 68 | + cmdXboxController.rightBumper().onTrue(Commands.print("'Right Bumper' was pressed!")); |
| 69 | + cmdXboxController.leftBumper().onTrue(Commands.print("'Left Bumper' was pressed!")); |
| 70 | + |
| 71 | + // You can get the underlying XboxController for it using getHID() |
| 72 | + final XboxController xboxController = cmdXboxController.getHID(); |
| 73 | + |
| 74 | + // Buttons |
| 75 | + boolean isYPressed = xboxController.getYButton(); |
| 76 | + boolean isBPressed = xboxController.getBButton(); |
| 77 | + boolean isAPressed = xboxController.getAButton(); |
| 78 | + boolean isXPressed = xboxController.getXButton(); |
| 79 | + boolean isStartPressed = xboxController.getStartButton(); |
| 80 | + boolean isBackPressed = xboxController.getBackButton(); |
| 81 | + boolean isRightStickPressed = xboxController.getRightStickButton(); |
| 82 | + boolean isLeftStickPressed = xboxController.getLeftStickButton(); |
| 83 | + boolean isRightBumperPressed = xboxController.getRightBumper(); |
| 84 | + boolean isLeftBumperPressed = xboxController.getLeftBumper(); |
| 85 | + |
| 86 | + // Triggers |
| 87 | + // left ranges from [0, 1] |
| 88 | + // right ranges from [0, 1] |
| 89 | + double leftTriggerValue = xboxController.getLeftTriggerAxis(); |
| 90 | + double rightTriggerValue = xboxController.getRightTriggerAxis(); |
| 91 | + |
| 92 | + // D-Pad |
| 93 | + // The POV angles start at 0 in the up direction, and increase clockwise (e.g. right is 90, upper-left is 315). |
| 94 | + // Returns -1 if the POV is not pressed. |
| 95 | + double pov = xboxController.getPOV(); |
| 96 | + } |
| 97 | +} |
0 commit comments