How do you use X-Box 360 controllers in C++?

We want to use either an X-Box controller or a PS3 controller to control out non-drive operations like arm control. The only problem is that we can’t find the C++ code for how to use them. Any help would be greatly appreciated.

There have been a lot of threads about that, you should check it out, and team 2202 also made a class for it. Here are a some links to their documentation.

http://coolhub.imsa.edu/cybercollab/web/robotics-documentation/documentation/-/wiki/Main/Xbox360

http://coolhub.imsa.edu/cybercollab/web/robotics-documentation/programming-documents/-/document_library_display/S14y/view/157916

I have never tried it out, but it seems fun. Tell me how it works out for you, because I want to try it out during off-season.

Just google “Xbox controller chief delphi” and you should get a lot of CD threads. Read them over if you would like.:smiley:

if i was you i would use something else.

We have been using an XBox controller. Any Game Controller that can be recognized by Windows can be used for input. You can use a PS3 controller but you have to download drivers that may or may not work in competition.

First, construct a new Joystick object:

Joystick j1 (1)

The 1 indicates the first Joystick on your Driverstation, you can check how many joysticks the DS has recognized under the “Diagnostics” tab of the application.

You can then use the Joystick API to retrieve button and axis states.

Some people like using the built in getter methods like j1.getX() but I just like calling j1.GetRawAxis(1).
Here’s a basic guide to the axises:
1: Left Stick X
2: Left Stick Y
3: Triggers (Pulling the Right Stick returns .5-1, Pulling the left stick is 0-.5, neutral is .5)
4: Right Stick X
5: Right Stick Y

You can also run j1.GetRawButton(int button) which returns bool (true means the button is pressed down).

I don’t remember off the top of my head what the buttons map to, but in your driver station you can: Click Windows + R > type in joy.cpl > Click on the properties of the Xbox Controller > Watch the numbers light up as you press the buttons.

I know pressing A will make “Button 1” light up. This means running j1.GetRawButton(1) will return true when pressed down.

If the Driver Station can be coerced into recognizing something as a USB joystick, it will work in any of the FRC robot programming languages.

Any HID (Human Input Device) compliant joysticks can be used. XBox 360 game controllers have two joysticks, a D-pad and a number of buttons. The joystick portion is the same as any other joysticks or game controllers. However, the button mappings are different. Here is the button map I used:


#define Btn(n)                  (1 << (n))
//
// Logitech Joystick:
// UsagePage=0x01, Usage=0x04
//
#define Logitech_Trigger        Btn(0)
#define Logitech_Btn2           Btn(1)
#define Logitech_Btn3           Btn(2)
#define Logitech_Btn4           Btn(3)
#define Logitech_Btn5           Btn(4)
#define Logitech_Btn6           Btn(5)
#define Logitech_Btn7           Btn(6)
#define Logitech_Btn8           Btn(7)
#define Logitech_Btn9           Btn(8)
#define Logitech_Btn10          Btn(9)
#define Logitech_Btn11          Btn(10)
#define Logitech_Btn12          Btn(11)
//
// Microsoft SideWinder Joystick:
// UsagePage=0x01, Usage=0x04
//
#define SideWinder_Trigger      Btn(0)
#define SideWinder_Btn2         Btn(1)
#define SideWinder_Btn3         Btn(2)
#define SideWinder_Btn4         Btn(3)
#define SideWinder_BtnA         Btn(4)
#define SideWinder_BtnB         Btn(5)
#define SideWinder_BtnC         Btn(6)
#define SideWinder_BtnD         Btn(7)
#define SideWinder_Btn9         Btn(8)
//
// Microsoft Xbox Game Controller:
// UsagePage=0x01, Usage=0x05
//
#define Xbox_BtnA               Btn(0)
#define Xbox_BtnB               Btn(1)
#define Xbox_BtnX               Btn(2)
#define Xbox_BtnY               Btn(3)
#define Xbox_LB                 Btn(4)
#define Xbox_RB                 Btn(5)
#define Xbox_Back               Btn(6)
#define Xbox_Start              Btn(7)

if you read there .h file it says it needs a custom math code… if that the .ccp code or do we have to create that?

I believe they provide a file for that; however, I’m not sure how well it works.

I think this is what you want:

http://coolhub.imsa.edu/cybercollab/web/robotics-documentation/programming-documents

Any one know what the custom math file is…anyone…?

I scanned through the code in the Xbox360 class and didn’t really find any reason for needing any custom math. The only methods that need some math are to return the joystick values in polar coordinates instead of the cartesian coordinates (i.e. returning magnitude/angle instead of returning X and Y). There are two things you can do:

  • If you don’t really need polar coordinates, you can simply remove the methods from the class: GetLeftAngle, GetRightAngle, GetLeftMagnitude and GetRightMagnitude. Then you can remove the reference of custom math. Note: if you think you need polar coordinates because you have a mecanum drive train, you really don’t. The WPI library for mecanum support is doing all the math for you. So you can just call the Mecanum_Cartesian method passing it X, Y and rotation.
  • If you do really need polar coordinates translation. You can use the built-in math library. I don’t see why you need custom math. In any case, I would just use the following macros to translate cartesian coordinates into polar coordinates:

#define MAGNITUDE(x,y)          sqrt(pow(x, 2) + pow(y, 2))
#define RADIANS_TO_DEGREES(n)   ((n)*180.0/PI)
// Forward is 0-radian
#define DIR_RADIANS(x,y)        ((((x) == 0.0) && ((y) == 0.0))? 0.0: atan2(x, y))
#define DIR_DEGREES(x,y)        RADIANS_TO_DEGREES(DIR_RADIANS(x, y))

i did end up to find it

it is located here

i have a pnp-ready xbox class with a dead-zone if anyone wants it

that pnp-class would be a awesome:D
thank you everyone for responding:)

its in java, but here goes:

package com.robototes.abomasnow;

import edu.wpi.first.wpilibj.Joystick;

public class Xbox {
    Joystick joy;

    Xbox(int port) {
        joy = new Joystick(port);
    }

    int a = 1;
    int b = 2;
    int x = 4;
    int y = 3;
    int lb = 6;
    int rb = 5;
    int back = 7;
    int start = 8;

    double] getAxes(int x_axis, int y_axis) {
        double] res = new double[2];
        res[0] = joy.getRawAxis(x_axis);
        res[1] = joy.getRawAxis(y_axis);

        return res;
    }

    double] getLeftStickAxes() {
        return getAxes(1,2);
    }

    double] getRightStickAxes() {
        return getAxes(4,5);
    }

    boolean getTrigger() {
        return joy.getTrigger();
    }

    boolean get(int n) {
        return joy.getRawButton(n);
    }

    boolean] getAll() {
        //Warning, this is ugly
        boolean] r = new boolean[8];

        r[0] = get(1);
        r[1] = get(2);
        r[2] = get(3);
        r[3] = get(4);
        r[4] = get(5);
        r[5] = get(6);
        r[6] = get(7);
        r[7] = get(8);

        return r;
    }

    public Joystick getJoy()
    {
        return joy;
    }
}