Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   XBox controller (http://www.chiefdelphi.com/forums/showthread.php?t=109932)

ekapalka 09-12-2012 12:38

XBox controller
 
Hello!
First of all, let it be said that I am new to robot programming. What I have been trying to do is modify the code below to utilize an xBox 360 controller. I have literally gone through every forum entry with the keyword "xBox" which resulted in nothing more than being lead to other entries and dead links to websites. I have already installed the drivers and tried treating the controller as a typical joystick. If anyone could Help, I would greatly appreciate it! Thanks!
Code:

#include <WPILib.h>

class DefaultRobot : public SimpleRobot
{       
        Joystick *leftStick;                       
        Joystick *rightStick;       
                       
        RobotDrive *myRobot;       
       
public:

        DefaultRobot(void)
        {       
                leftStick = new Joystick(1);                       
                rightStick = new Joystick(2);
               
                myRobot = new RobotDrive(1, 3, 2, 4);

                myRobot->SetExpiration(0.005);
        }

        void Autonomous(void)
        {
                myRobot->SetSafetyEnabled(true);
                               
                        while(IsAutonomous())
                        {
                       
                        }
                               
        }


        void OperatorControl(void)
        {
                myRobot->SetSafetyEnabled(true);
               
                while (IsOperatorControl())
                {       
                        myRobot->ArcadeDrive(leftStick);
                        Wait(0.001);
                }
        }
};

START_ROBOT_CLASS(DefaultRobot);


Joe Ross 09-12-2012 13:32

Re: XBox controller
 
I haven't used an Xbox controller, nor do I use C++, but here's how I would attack the problem of getting any joystick or controller to work.

First I would make sure the controller is being recognized by the driver station software. It should show up on the setup tab as a joystick. When you press a button on the controller, the controller should change color in the joystick list. If it isn't detected or doesn't detect button presses, the problem is likely with your driver.

Second, I would modify your robot code to print the values of each axis and button. From there, you can see which button and stick on the controller are mapped to what axis and button in the code. From there, it should be very simple to make your code use the appropriate axis and buttons. For example, passing the joystick object to Arcade drive makes some assumptions about axis mappings that may not apply to the xBox controller. You might need to change to one of the other methods that allows you to specify specific axes, or read the appropriate axes yourself and pass them to the arcade drive method that takes numeric inputs.

Zuelu562 09-12-2012 17:15

Re: XBox controller
 
My team used Xbox 360 controllers last year. In our code, we used Joystick.GetRawAxis() and tied it to a float. When we called RobotDrive.ArcadeDrive(), we handed it our floats.

Code:

float driverLeftY = driverXbox.GetRawAxis(2);
float driverRightY = driverXbox.GetRawAxis(5);

robotDriver.ArcadeDrive(driverLeftY, driverLeftX);

If I recall correctly, we ended up in the same situation as you, having trouble with GetAxis not working with an xbox controller. Handing it the GetRawAxis floats worked, and we ended up running with it.

If you're having trouble with any of the button or axis setups, this should help.

Toa Circuit 09-12-2012 17:51

Re: XBox controller
 
I had tried an xBox controller once, and had poor experiences with it.
I could not:
1. Get the left joystick Y axis value
2. Independently access the analog triggers

I had subclassed it and used the Joystick.GetRawAxis() functions. Everything else worked, but I couldn't get those things out of it.

Then again, that was my experience. If you wanted it to work, you should use the GetRawAxis(x) functions as said before.

Lets say you setup everything like you had before and plugged the xBox controller in as the first controller. Here is what you would write:
Code:

        void Autonomous(void)
        {
                myRobot->SetSafetyEnabled(true);
                               
                        while(IsAutonomous())
                        {
                        myRobot->Drive(leftStick->GetRawAxis(1), leftStick->GetRawAxis(2));
                        }
        }

By doing this, we are doing what Zuelu562 had done somewhat, just minus creating extra variables that make code messier and use up more memory. You just pass the value you want the robot to move rather than passing a joystick to it. Zuelu562 also gave a spreadsheet that listed out all the values of buttons and axes, so you can replace the 1 and 2 with whichever values you like. This seemed to match up with what I had.

ekapalka 09-12-2012 18:51

Re: XBox controller
 
So does that mean we don't vave to use
Code:

                leftStick = new Joystick(1);                       
                rightStick = new Joystick(2);

or that in addition to the code you suggested? Thanks for your reply! I really appreciate it!

mprasolov 09-12-2012 22:38

Re: XBox controller
 
I went to a workshop with the Green Machine and they said that anyone could use their code as a reference, so here it is.

Zuelu562 10-12-2012 07:46

Re: XBox controller
 
Quote:

Originally Posted by ekapalka (Post 1200128)
So does that mean we don't vave to use
Code:

leftStick = new Joystick(1);                       
rightStick = new Joystick(2);

or that in addition to the code you suggested? Thanks for your reply! I really appreciate it!

You still need to create the joysticks for GetRawAxis() or GetRawButton() to work. If you do, it won't know what you're asking for! :)

Toa, our reasoning for using the floats was a double whammy type deal; we added a small deadband to the controller by keeping an eye on the values. We were aware of the issue that the robot could potentially move without a human interacting with the controller due to the minuscule difference between 0 (dead stop) and .25 (moving) on the xbox controllers. This also solved the issue with GetRawAxis() freaking out in the main loop.

Toa Circuit 11-12-2012 10:59

Re: XBox controller
 
Quote:

Originally Posted by Zuelu562 (Post 1200340)
Toa, our reasoning for using the floats was a double whammy type deal; we added a small deadband to the controller by keeping an eye on the values. We were aware of the issue that the robot could potentially move without a human interacting with the controller due to the minuscule difference between 0 (dead stop) and .25 (moving) on the xbox controllers. This also solved the issue with GetRawAxis() freaking out in the main loop.

Funny, the controller I used never had a deadband issue.

Quote:

Originally Posted by ekapalka (Post 1200128)
So does that mean we don't vave to use
Code:

                leftStick = new Joystick(1);                       
                rightStick = new Joystick(2);

or that in addition to the code you suggested? Thanks for your reply! I really appreciate it!

Oh, you will still need to do all the code you had before, I just wrote up the portion that you need to add/replace.

ekapalka 12-12-2012 08:28

Re: XBox controller
 
I haven't been back to this thread in a while, so I haven't had a chance to say THANK YOU ALL SO MUCH! I really appreciate it! I just had a few more questions...
THe code provided by Toa works, but I had a few questions about how exactly it works. It only references
Code:

leftStick->GetRawAxis(1), leftStick->GetRawAxis(2)
so why do I even need
Code:

rightStick = new Joystick(2);
? Also, why is it myRobot->Drive() and not robotDriver.ArcadeDrive() or myRobot->ArcadeDrive()? Any help would be great! Once again, thanks for all your help!

Joe Ross 14-12-2012 21:34

Re: XBox controller
 
Quote:

Originally Posted by ekapalka (Post 1201209)
.
THe code provided by Toa works, but I had a few questions about how exactly it works. It only references
Code:

leftStick->GetRawAxis(1), leftStick->GetRawAxis(2)
so why do I even need
Code:

rightStick = new Joystick(2);
?

You don't

Quote:

Originally Posted by ekapalka (Post 1201209)
Also, why is it myRobot->Drive() and not robotDriver.ArcadeDrive() or myRobot->ArcadeDrive()? Any help would be great! Once again, thanks for all your help!

There are several different methods provided by RobotDrive, including ArcadeDrive and Drive. They are described in the "WPI Robotics Library User’s Guide". You should pick the one that does what you want, or experiment and try them all.

Zuelu562 15-12-2012 00:59

Re: XBox controller
 
Quote:

Originally Posted by Toa Circuit (Post 1200897)
Funny, the controller I used never had a deadband issue.

We had fresh controllers as well (ie just out of the box). The thing we were worried about is a situation where the analog stick just isn't 100%, and sometimes would actually rest on what would be .25. Also, we didn't want to run it full power anyway, thinking about it now, because it would jump at the slightest touch.

We can look at it again and re-evaluate the need, but it worked for us.

ekapalka 02-01-2013 00:14

Re: XBox controller
 
Hey CD! I've had an epiphany! XBox is actually spelled xBox, just like Ipod is actually spelled iPod! I've taken into consideration all the information I received from this thread and the example code, and have constructed this:
Code:

#include <WPILib.h>
class DefaultRobot : public SimpleRobot
{       
        Joystick *xboxStickManip;             
        Joystick *leftStick;                             
        Joystick *rightStick;                 
        Jaguar *leftDrive;                   
        Jaguar *rightDrive;                   
        RobotDrive *myRobot;                 
public:
        DefaultRobot(void)
        {       
                xboxStickManip = new Joystick(1); 
                leftStick = new Joystick(2);
                rightStick = new Joystick(3);
                leftDrive = new Jaguar(1);
                rightDrive = new Jaguar(2);
                myRobot = new RobotDrive(leftDrive, rightDrive);
        }
        void Autonomous(void)
        {
                        while(IsAutonomous())
                        {
                        }                               
        }
        void OperatorControl(void)
        {
                myRobot->TankDrive(0.0, 0.0);               
                while (IsOperatorControl())
                {       
                        float leftStickYAxis = leftStick->GetY();
                        float rightStickYAxis = rightStick->GetY();       
                        myRobot->TankDrive(-leftStickYAxis, -rightStickYAxis);
                }
        }
};

START_ROBOT_CLASS(DefaultRobot);

It compiles (amazingly) but will not load onto the cRIO II, even though DefaultRobotProject compiles and runs. I've begun to think that my code may be at fault... Does anyone have any ideas? I'm pretty sure it's also a bit redundant. Any help would be awesome! Cheers!

Kit Fieldhouse 12-01-2013 14:39

Re: XBox controller
 
Hello
i was wondering if anyone had figured out how to implement tank drive with an Xbox controller? Our team has gotten one joystick to work (using basically the code that's already here), but not both. One of our members who formerly programmed in LabView said that one of the Joysticks is recognized as a joystick and the other is recognized as a button (somehow). Is there some way to do this? Thanks!

F22Rapture 12-01-2013 16:20

Re: XBox controller
 
Quote:

Originally Posted by Kit Fieldhouse (Post 1213710)
Hello
i was wondering if anyone had figured out how to implement tank drive with an Xbox controller? Our team has gotten one joystick to work (using basically the code that's already here), but not both. One of our members who formerly programmed in LabView said that one of the Joysticks is recognized as a joystick and the other is recognized as a button (somehow). Is there some way to do this? Thanks!

That's not correct, I don't think. The entire Xbox controller is a Joystick, and each individual stick is a pair of axes. Perhaps the labview libraries work differently from the text languages in that regard, I don't know, but the C++ and Java libraries should consider both thumbsticks as 4 axes on the Joystick (left x-axis, left y-axis, etc).

Ex.

Code:

public OI() {               
        leftTank = driverXbox.getRawAxis(2);
        rightTank = driverXbox.getRawAxis(5);
}

with

Code:

// 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;

As far as using an Xbox controller for tank drive, I don't recommend it. The fact that the thumbsticks are at different elevations vertically means it's going to be harder to operate it effectively. The Xbox controller also has a 20% deadzone in the center of the thumbsticks where you cannot accurately determine the position of the stick. This means that if you do not account for this, it will drift even when you're not touching it, and if you do, you will not be able to make movements of less than 20% power.

I would recommend the Logitech F310 instead, as both thumbsticks are at the same elevation, and the sticks themselves are more accurate (much less deadzone).

nightpool 12-01-2013 22:15

Re: XBox controller
 
No, the labview code works in the same way. One joystick, with 6 axis and 10-ish buttons. Maybe he was thinking in the was the triggers work? (They're an axis, with the left trigger -1 and the right trigger +1. Both triggers are 0).


All times are GMT -5. The time now is 12:34.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi