|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Xbox Controller
I got help earlier but I have tried to program one in and just cant figure it out. I am completely lost and need lots of help so if anyone out there is willing id be forever grateful.
THANKS! |
|
#2
|
||||
|
||||
|
Re: Xbox Controller
Its just like any other controller, except with more axes and buttons. See the following post: http://www.chiefdelphi.com/forums/sh...51&postcount=4. Go to "Run" on your computer and type in "joy.cpl" (without quotes, with the controller plugged in). Then go to properties to see the button numbers.
|
|
#3
|
||||
|
||||
|
Re: Xbox Controller
|
|
#4
|
||||
|
||||
|
Re: Xbox Controller
I sorry but I have tried for hours now and just cant seem to figure out how to implement both your ways. Like how do I use buttons - because the regular "button.whenPressed(new exampleCommand());" isn't working. How do I make it work with tankdrive when there are not two joysticks connected but rather one controller. I am sooooo confused and I tried to figure it out on my own but I have gotten nowhere.
THANKS for the help. |
|
#5
|
||||
|
||||
|
Re: Xbox Controller
Can we see the code you have so far?
Assuming you're using a RobotDrive object to control your drivetrain, and the tankDrive method, your Xbox controller (like ekapalka said) should behave much like a pair of joysticks in tank drive - with each Xbox stick controlling one side of the drivetrain. Code:
drive.tankDrive(gamepad.getLeftStickY(), gamepad.getRightStickY(); |
|
#6
|
||||
|
||||
|
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);
}
}
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);
}
}
Code:
boolean button = xcon2.getWHATEVER() Code:
button.whenPressed or button.when released 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();
}
}
|
|
#7
|
||||
|
||||
|
Re: Xbox Controller
Quote:
The JoystickButton constructor takes a human interface device (Xbox controller object) and a button number as parameters. In your code, you could do the following: Code:
package edu.wpi.first.XBoxBot2014;
import edu.wpi.first.XBoxBot2014.subsystems.XBoxController;
public class OI {
public XBoxController xcon2 = new XBoxController(RobotMap.conPort);
private Button button = new JoystickButton(xcon2, 1); //Button A = 1
public OI() {
button.whenPressed(doThing());
}
}
Code:
public void driveTele(Joystick leftStick, Joystick rightStick){
drive.tankDrive(oi.xcon2.getLeftJoyY(), oi.xcon2.getRightJoyY());
}
Replace the parameters with doubles, and take advantage of the fact that tankDrive() will also take a leftValue and rightValue as input: Code:
public void driveTele(double leftValue, double rightValue){
drive.tankDrive(leftValue, rightValue);
}
Hope it works! Last edited by NWChen : 23-06-2014 at 02:33. |
|
#8
|
||||
|
||||
|
Re: Xbox Controller
Thanks soooo much. It all worked except for the
Code:
private Button button = new JoystickButton(xcon2, 1); Quote:
|
|
#9
|
|||||
|
|||||
|
Re: Xbox Controller
Quote:
So for the A button it would be: Code:
Joystick joystick = new Joystick(1); private Button button = new JoystickButton(joystick, 0); Code:
oi.joystick.getRawAxis(7); //Left stick oi.joystick.getRawAxis(5); //Rightstick -create getter methods for each of the buttons and axis -create static constants that hold the mapped button numbers and axis If you'd like more information on how to do this I can assist you there too. Last edited by notmattlythgoe : 23-06-2014 at 13:19. |
|
#10
|
||||
|
||||
|
Re: Xbox Controller
Okay I get it - but why cant I just continue to use
Code:
Joystick joystick = new Joystick(1); private Button button = new JoystickButton(joystick, 0); Code:
private Button button = new JoystickButton(joystick, joystick.getRawButton(0)); And if it is completely necessary to do so I would need some help - I have an idea of what to do but I would still need some guidance. THANKS! |
|
#11
|
|||||
|
|||||
|
Re: Xbox Controller
Quote:
Just using the JoystickButton along with the Joystick class is a perfectly fine way to do it. So for you I'd suggest you stick with the below for now. Code:
Joystick joystick = new Joystick(1); private Button button = new JoystickButton(joystick, 0); |
|
#12
|
||||
|
||||
|
Re: Xbox Controller
Quote:
Code:
public class XBoxController extends GenericHID{
...
}
notmattlythgoe's suggestion is much better, but for a quick fix right now this should work. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|