Java programming of an XBox Controller

How could we use an xbox controller to control our robot?

Does anyone have a drawing of which buttons correspond to what numbers and what axis’ correspond to what numbers?

Any help would be appreciated!

Yes, you could, but I would recommend against it. The Xbox Controller has fairly large (and inconsistent) deadzones around the center of the sticks. The logitech joysticks provide better accuracy.

Does anyone have a drawing of which buttons correspond to what numbers and what axis’ correspond to what numbers?

Any help would be appreciated!

If you just want the button bindings, scroll to the bottom. Otherwise, you can use this class to simplify things.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.usfirst.Rotoraptors.controls.hids;

import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.buttons.*;

/**
 *
 * @author Daniel
 */

public class XboxController {
    
    private Joystick m_pad;
    
    public XboxController(int port) {
        m_pad = new Joystick(port);
    }
    
    public double getLeftX() {
        return m_pad.getRawAxis(LEFT_X_AXIS);
    }
    
    public double getLeftY() {
        return m_pad.getRawAxis(LEFT_Y_AXIS);
    }
    
    public double getRightX() {
        return m_pad.getRawAxis(RIGHT_X_AXIS);
    }
    
    public double getRightY() {
        return m_pad.getRawAxis(RIGHT_Y_AXIS);
    }
    
    public double getTriggers() {
        return m_pad.getRawAxis(TRIGGERS);
    }
    
    public double getDpadX() {
        return m_pad.getRawAxis(DPAD_LR);
    }
             
    public double applyDeadband(int axis) {
        if(Math.abs(m_pad.getRawAxis(axis)) < .1) {
            return 0;
        } else {
            return axis;
        }
    }          
    
    // Creates buttons
    public Button X = new JoystickButton(m_pad, BUTTON_X);
    public Button Y = new JoystickButton(m_pad, BUTTON_Y);
    public Button A = new JoystickButton(m_pad, BUTTON_A);
    public Button B = new JoystickButton(m_pad, BUTTON_B);
    public Button lBumper = new JoystickButton(m_pad, BUMPER_L);
    public Button rBumper = new JoystickButton(m_pad, BUMPER_R);
    public Button start = new JoystickButton(m_pad, BUTTON_START);
    public Button back = new JoystickButton(m_pad, BUTTON_BACK);
    public Button lStick = new JoystickButton(m_pad, LEFT_STICK_PRESS);
    public Button rStick = new JoystickButton(m_pad, RIGHT_STICK_PRESS);
    
    public boolean getButton(int btn) {
        return m_pad.getRawButton(btn);
    }
    
    // Axis indexes:
    public static final int
            LEFT_X_AXIS = 1,
            LEFT_Y_AXIS = 2,
            TRIGGERS = 3,
            RIGHT_X_AXIS = 4,
            RIGHT_Y_AXIS = 5,
            DPAD_LR = 6;
    
    // Button mappings:
    public static final int
            BUTTON_A = 1,
            BUTTON_B = 2,
            BUTTON_X = 3,
            BUTTON_Y = 4,
            BUMPER_L = 5,
            BUMPER_R = 6,
            BUTTON_BACK = 7,
            BUTTON_START = 8,
            LEFT_STICK_PRESS = 9,
            RIGHT_STICK_PRESS = 10;
    
}

Which you can call the same way you call regular Joysticks (in regards to where you put it and the information you provide to the method), ie:


    XboxController driverXbox = new XboxController(1);
    XboxController operatorXbox = new XboxController(2);

It also creates the buttons for you so you can keep your OI code cleaner.

driverXbox.A.whenPressed(doSomething());

How much do said logitech controllers cost!

The Attack 3s should have come with your Rookie KoP… if not, $20-$25

If you’re dead-set on using a gamepad, the Logitech F310 is about the same price, and also has both thumbsticks at the bottom of the controller so tank drive is less awkward. And they seem to have less deadzone than the Xbox controllers.

How big is the deadzone on an xbox controller?

Depending on the controller, 15-20%

Have you personally tested these and which is this info based on.

I pulled up Xpadder and looked at the raw ouput. Mine was about 18%. Some seem to have more, some less.

Here’s our XBoxController class:

How effective has it been for you guys to use an xbox controller

Very. We’ve had no issues with it.

Do you feel the deadzones affected your playing

We don’t really use the joysticks (we do our driving with the Attack 3’s), but when we do we just have extra wide deadbands. We’ve found our controller sometimes doesn’t return exactly to zero.