View Single Post
  #2   Spotlight this post!  
Unread 08-01-2013, 21:41
F22Rapture's Avatar
F22Rapture F22Rapture is offline
College Student, Mentor
AKA: Daniel A
FRC #3737 (4H Rotoraptors)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Goldsboro, NC
Posts: 476
F22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant futureF22Rapture has a brilliant future
Re: Java programming of an XBox Controller

Quote:
Originally Posted by inkspell4 View Post
How could we use an xbox controller to control our robot?
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.

Quote:
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.

Code:
/*
 * 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:

Code:
    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.

Code:
driverXbox.A.whenPressed(doSomething());
__________________
Research is what I’m doing when I don’t know what I’m doing.
- Wernher von Braun
Attending: Raleigh NC Regional

Last edited by F22Rapture : 08-01-2013 at 21:48.
Reply With Quote