Basic Joystick/Controller Input Lesson

Basic Joystick/Controller Input

This lesson bridges the gap between the driver and the robot. We'll learn how to read inputs from joysticks and controllers to translate human actions into meaningful robot movements.

The Operator Interface (OI)

The "Operator Interface" or "OI" refers to all the physical controls drivers use. In our code, the OI is represented by objects that read the state of these devices. WPILib provides classes that make it easy to get data from these controllers.

WPILib Controller Classes

You'll primarily use two classes to represent your controllers. You will create instances of these objects in `RobotContainer.java`.

  • `edu.wpi.first.wpilibj.Joystick`: For a standard flight-stick style joystick.
  • `edu.wpi.first.wpilibj.XboxController`: Specifically for an Xbox-style gamepad, with helpful methods for accessing its unique buttons and axes.

Reading Controller Inputs

Once you have your controller objects, you can "poll" them for their current state. Let's look at the different types of inputs.

1. Reading Axes (Analog Sticks)

Axes return a `double` value, usually between -1.0 and 1.0, representing the position of a joystick.

// Assume 'm_driverController' is an XboxController object
// The Y-axis is for forward/backward movement. 
// Pushing forward is typically -1.0.
double forwardSpeed = -m_driverController.getLeftY();

// The X-axis is for left/right movement or turning.
double turnSpeed = m_driverController.getRightX();
    

Deadbanding: A Critical Step

Joysticks don't always return perfectly to `0.0` when you let go. They might "drift" slightly, causing the robot to creep. To prevent this, we use a "deadband"—we ignore any input value that is very close to zero.

double rawSpeed = -m_driverController.getLeftY();
double deadbandedSpeed = 0.0;

// Only use the speed if its absolute value is greater than our threshold (e.g., 0.1)
if (Math.abs(rawSpeed) > 0.1) {
    deadbandedSpeed = rawSpeed;
}

// Now, use deadbandedSpeed to control the motor.
m_drivetrain.setSpeed(deadbandedSpeed);
    

2. Reading Buttons

Buttons are digital inputs—they are either pressed (`true`) or not pressed (`false`).

// For an XboxController, you can use descriptive method names
boolean isAPressed = m_operatorController.getAButton();
boolean isBPressed = m_operatorController.getBButtonPressed(); // Only true on the initial press

if (isAPressed) {
    // Run the intake...
}
    

3. Reading Triggers (Analog Buttons)

The triggers on an Xbox controller are analog, reporting how far they are pressed from `0.0` (released) to `1.0` (fully pressed).

// Get the value of the left and right triggers
double leftTriggerValue = m_operatorController.getLeftTriggerAxis();
double rightTriggerValue = m_operatorController.getRightTriggerAxis();

// We could use this to control an arm with variable speed
m_arm.setSpeed(rightTriggerValue - leftTriggerValue);
    

Test Your Knowledge

Question: When reading a joystick's Y-axis for forward movement, a programmer notices the robot creeps slightly even when the joystick is untouched. What technique should they implement to fix this?