Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   How do you use X-Box 360 controllers in C++? (http://www.chiefdelphi.com/forums/showthread.php?t=91008)

FenrisKiba 02-05-2011 02:40 PM

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.

Arjun Namineni 02-05-2011 03:29 PM

Re: How do you use X-Box 360 controllers in C++?
 
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/...i/Main/Xbox360

http://coolhub.imsa.edu/cybercollab/...4y/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.:D

Chicken man 02-05-2011 03:54 PM

Re: How do you use X-Box 360 controllers in C++?
 
if i was you i would use something else.

gadgetdevil 02-05-2011 03:59 PM

Re: How do you use X-Box 360 controllers in C++?
 
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:
Code:

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.

Alan Anderson 02-05-2011 09:30 PM

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

Originally Posted by nckureghian14 (Post 1016317)
is it possible in java?

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.

mikets 02-05-2011 11:05 PM

Re: How do you use X-Box 360 controllers in C++?
 
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:
Code:

#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)


tomy 02-06-2011 06:50 PM

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

Originally Posted by Arjun Namineni (Post 1016283)
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/...i/Main/Xbox360

http://coolhub.imsa.edu/cybercollab/...4y/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.:D

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?

Arjun Namineni 02-06-2011 09:39 PM

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

Originally Posted by tomy (Post 1017055)
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/...ming-documents

tomy 02-07-2011 10:51 AM

Re: How do you use X-Box 360 controllers in C++?
 
Any one know what the custom math file is...anyone....?

mikets 02-07-2011 01:04 PM

Re: How do you use X-Box 360 controllers in C++?
 
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:
Code:

#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))


tomy 02-07-2011 01:27 PM

Re: How do you use X-Box 360 controllers in C++?
 
i did end up to find it

it is located here

Robototes2412 02-09-2011 10:31 PM

Re: How do you use X-Box 360 controllers in C++?
 
i have a pnp-ready xbox class with a dead-zone if anyone wants it

FenrisKiba 02-13-2011 12:26 PM

Re: How do you use X-Box 360 controllers in C++?
 
that pnp-class would be a awesome:D
thank you everyone for responding:)

Robototes2412 02-13-2011 05:54 PM

Re: How do you use X-Box 360 controllers in C++?
 
its in java, but here goes:

Code:

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



All times are GMT -5. The time now is 10:19 AM.

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