Joystick Interface - frc-7603/VespaRobotics2024-25 GitHub Wiki
About
The joystick is a USB joystick which has many buttons which can be mapped in code. It is usually connected to the computer with DriverStation, and controls the robot remotely over its network.
This wiki page is a WIP!
Defining a joystick
Firstly, import the joystick class from the edu.wpi.first.wpilibj package.
import edu.wpi.first.wpilibj.Joystick;
Then, instantiate its class.
Joystick mainJoystick = new Joystick(0);
The number inside is the port number. In our case, it is port 0. Make sure that it is accessible to add methods (eg. By using the private keyword, and the static keyword so you don't have to instantiate the robot class).
Using/Interfacing with a Joystick
Getting directions in which it has been pushed
To get the directions, simply use the .getRawAxis() method with the axis number. Axis 0 is y, axis 1 is x.
// Y-Axis: Forwards and Backwards
double yaxis = mainJoystick.getRawAxis(0);
// X-Axis: Side to side
double xaxis = mainJoystick.getRawAxis(1);
Alternatively, you can use the .getX() and .getY() methods (returns esentially the same stuff). There is also a .getZ() method, but is is unknown what axis this is at the moment (this is here for documentation purposes).
// Y-Axis: Forwards and Backwards
double yaxis = mainJoystick.getY();
// X-Axis: Side to side
double xaxis = mainJoystick.getX();
// Z-Axis: Unknown
double zaxis = mainJoystick.getZ();
The y-axis is forwards and backwards, while the x-axis is sideways (robot turning). Negative y-axis is backwards, Negative x-axis is left. This is similar to a standard cartesian plane ("graph"). The values are percent values of how much the joystick is moved towards the edge. This could theroretically be plugged directly into the motors (as the format is the same), however, it is best to set a dead spot due to drift.
The code below is code that runs stuff only if the joystick's percentage is 0.1 in either direction
// Only run if the y-axis percentage is more than 0.1 in either direction
if (yaxis > -0.1 && yaxis < 0.1){
// Run code such as setting speed
}
// Only run if the x-axis percentage is more than 0.1 in either direction
if (xaxis > -0.1 && xaxis < 0.1){
// Run code such as setting speed
}
Getting button press events
To get whether or not a button is pressed, use the .getRawButtonPressed() method with the button number.
boolean buttonPressed = mainJoystick.getRawButtonPressed(int buttonnumber);
The button numbers are labeled directly on the joystick. This can be used within an if statement (eg. If you want an emergency stop, you can check for a specific button being pressed). If the button is pressed, the value is true.
As well, there are also 3 regular buttons, the top button (on top of the joystick), the trigger button (unknown location at the moment), and the bumper button (perhaps on the sides of the joystick, as in "bumper" of a car). The corresponding methods are getTop(), getTrigger(), and getBumper() respectively.
They all return booleans in a similar manner to .getRawButtonPressed().
boolean topButtonPressed = mainJoystick.getTop();
boolean triggerButtonPressed = mainJoystick.getTrigger();
boolean bumperButtonPressed = mainJoystick.getBumper();
Xbox Controller
The Xbox controller is a variation of the joystick used for swerve drive. It still needs deadzones (also now including the trigger) and port (always 0).
The left joystick is 0 and 1. It is the same thing as the joystick mentioned before. The other joystick is 4 and 5. (4 is X_AXIS, 5 is Y_AXIS).
Triggers (top of the controller, the tall button which can be pushed down) have axes 2 and 3. Left trigger is axis 2. Right trigger is axis 3.
Swerve Drive Integration
The speed of the robot is obtained through the left and right joysticks.
You need to use Math.abs()
The speed has to be divided by 5 since the robot is fast. Other values could be tried to make it faster or slower. It uses the left joystick.
The drive direction is just the direction in which the robot goes. It uses the left joystick.
The turn just turns your robot. It uses the right joystick.
For more info
Java class documentation (Joystick)
https://github.wpilib.org/allwpilib/docs/release/java/edu/wpi/first/wpilibj/Joystick.html