Thread: Xbox Controller
View Single Post
  #6   Spotlight this post!  
Unread 22-06-2014, 14:54
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Re: Xbox Controller

Okay - im still confused about how to implement it in my DriveTele command and how to make buttons work.

Here's is the Chassis subsystem - I think I have this figured out:

Code:
package edu.wpi.first.XBoxBot2014.subsystems;

import edu.wpi.first.XBoxBot2014.OI;
import edu.wpi.first.XBoxBot2014.RobotMap;
import edu.wpi.first.XBoxBot2014.commands.DriveTele;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.command.Subsystem;

/**
 *
 * @author FrankyMonezz
 */
public class Chassis extends Subsystem {
    // Put methods for controlling this subsystem
    // here. Call these from Commands.
    RobotDrive drive;
    
    OI oi = new OI();

    public void initDefaultCommand() {
        // Set the default command for a subsystem here.
        setDefaultCommand(new DriveTele());
    }
    
    public Chassis(){
        drive = new RobotDrive(RobotMap.leftMotorPort, RobotMap.rightMotorPort);
        drive.setSafetyEnabled(false);
    }
    
    public void driveTele(Joystick leftStick, Joystick rightStick){
       drive.tankDrive(oi.xcon2.getLeftJoyY(), oi.xcon2.getRightJoyY());
    }
    
    public void setSpeed(double speed){
        drive.setMaxOutput(speed);
    }
     
    public void driveAuton(double speed, double turnRate)
{
	drive.arcadeDrive(speed, turnRate);
}

    
}
Here is the XBoxController class that im using - kindly given to me by cgmv123:

Code:
package edu.wpi.first.XBoxBot2014.subsystems;

import edu.wpi.first.wpilibj.DriverStation;

/**
 *
 * @author Jon Morton
 */
public class XBoxController {
    
    
    private DriverStation _ds;
    private final int _port;

    public XBoxController(int port) {
        _ds = DriverStation.getInstance();
        _port = port;
    }

    public double getRawAxis(final int axis) {
        return _ds.getStickAxis(_port, axis);
    }

    public boolean getRawButton(final int button) {
        return ((0x1 << (button - 1)) & _ds.getStickButtons(_port)) != 0;
    }

    /**
     * Warning! getRightTrigger() and getLeftTrigger() both use getRawAxis(3).
     * As getRawAxis(3) goes below zero, getRightTrigger() increases, and as
     * getRawAxis(3) goes above zero, getLeftTrigger() increases. If both
     * triggers are pressed, both of them will be treated as zero. You can only
     * use one trigger at a time.
     *
     * @return
     */
    public double getRightTrigger() {
        return -Math.min(getRawAxis(3), 0);
    }

    public double getLeftTrigger() {
        return Math.max(getRawAxis(3), 0);
    }

    public double getRightJoyX() {
        return getRawAxis(4);
    }

    public double getRightJoyY() {
        return getRawAxis(5);
    }

    public double getLeftJoyX() {
        return getRawAxis(1);
    }

    public double getLeftJoyY() {
        return getRawAxis(2);
    }

    public boolean getButtonA() {
        return getRawButton(1);
    }

    public boolean getButtonB() {
        return getRawButton(2);
    }

    public boolean getButtonX() {
        return getRawButton(3);
    }

    public boolean getButtonY() {
        return getRawButton(4);
    }

    public boolean getButtonBack() {
        return getRawButton(7);
    }

    public boolean getButtonStart() {
        return getRawButton(8);
    }

    public boolean getButtonRB() {
        return getRawButton(6);
    }

    public boolean getButtonLB() {
        return getRawButton(5);
    }

    public boolean getButtonLS() {
        return getRawButton(9);
    }

    public boolean getButtonRS() {
        return getRawButton(10);
    }
    
}
I instantiate XBoxController here(conPort is an integer value of 1 stored in RobotMap) - I have the
Code:
boolean button = xcon2.getWHATEVER()
figured out - but how do I use that to turn say a motor on - because for example
Code:
button.whenPressed or button.when released
does not work with boolean values:

Code:
package edu.wpi.first.XBoxBot2014;

import edu.wpi.first.XBoxBot2014.subsystems.XBoxController;

public class OI {
    
    public XBoxController xcon2 = new XBoxController(RobotMap.conPort);
    
    

public OI() {
        
        boolean button = xcon2.getButtonA();
    }    

 }
Reply With Quote