Go to Post Tinkerer math = engineer - MikeE [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rating: Thread Rating: 5 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 05-02-2011, 14:40
FenrisKiba's Avatar
FenrisKiba FenrisKiba is offline
Registered User
FRC #2603
 
Join Date: Jan 2011
Location: Medina, OH
Posts: 7
FenrisKiba is an unknown quantity at this point
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.
Reply With Quote
  #2   Spotlight this post!  
Unread 05-02-2011, 15:29
Arjun Namineni Arjun Namineni is offline
Registered User
FRC #4384
Team Role: Coach
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Troy, MI
Posts: 27
Arjun Namineni is an unknown quantity at this point
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.
__________________
2009-2013: FRC 226 - Student
2014-Present: FRC 4384 - Mentor
Reply With Quote
  #3   Spotlight this post!  
Unread 05-02-2011, 15:54
Chicken man Chicken man is offline
Registered User
AKA: James McNamara
FRC #1584 (Pirates)
Team Role: Programmer
 
Join Date: Feb 2011
Rookie Year: 2011
Location: Nederland, Colorado
Posts: 3
Chicken man is an unknown quantity at this point
Re: How do you use X-Box 360 controllers in C++?

if i was you i would use something else.
Reply With Quote
  #4   Spotlight this post!  
Unread 05-02-2011, 15:59
gadgetdevil gadgetdevil is offline
Registered User
FRC #0254
 
Join Date: Dec 2010
Location: San Jose, CA
Posts: 1
gadgetdevil is an unknown quantity at this point
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.
Reply With Quote
  #5   Spotlight this post!  
Unread 05-02-2011, 21:30
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,112
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: How do you use X-Box 360 controllers in C++?

Quote:
Originally Posted by nckureghian14 View Post
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.
Reply With Quote
  #6   Spotlight this post!  
Unread 05-02-2011, 23:05
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
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)
__________________
Reply With Quote
  #7   Spotlight this post!  
Unread 06-02-2011, 18:50
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: How do you use X-Box 360 controllers in C++?

Quote:
Originally Posted by Arjun Namineni View Post
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.
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?
Reply With Quote
  #8   Spotlight this post!  
Unread 06-02-2011, 21:39
Arjun Namineni Arjun Namineni is offline
Registered User
FRC #4384
Team Role: Coach
 
Join Date: Oct 2010
Rookie Year: 2010
Location: Troy, MI
Posts: 27
Arjun Namineni is an unknown quantity at this point
Re: How do you use X-Box 360 controllers in C++?

Quote:
Originally Posted by tomy View Post
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
__________________
2009-2013: FRC 226 - Student
2014-Present: FRC 4384 - Mentor
Reply With Quote
  #9   Spotlight this post!  
Unread 07-02-2011, 10:51
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: How do you use X-Box 360 controllers in C++?

Any one know what the custom math file is...anyone....?
Reply With Quote
  #10   Spotlight this post!  
Unread 07-02-2011, 13:04
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
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))
__________________

Last edited by mikets : 07-02-2011 at 13:10.
Reply With Quote
  #11   Spotlight this post!  
Unread 07-02-2011, 13:27
tomy tomy is offline
Registered User
FRC #3038 (I.C.E. Robotics)
Team Role: Mentor
 
Join Date: Jan 2009
Rookie Year: 2009
Location: Stacy, Minnesota
Posts: 494
tomy has a spectacular aura abouttomy has a spectacular aura about
Re: How do you use X-Box 360 controllers in C++?

i did end up to find it

it is located here

Last edited by tomy : 07-02-2011 at 13:27. Reason: *locater -> located (grammar)
Reply With Quote
  #12   Spotlight this post!  
Unread 09-02-2011, 22:31
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
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
Reply With Quote
  #13   Spotlight this post!  
Unread 13-02-2011, 12:26
FenrisKiba's Avatar
FenrisKiba FenrisKiba is offline
Registered User
FRC #2603
 
Join Date: Jan 2011
Location: Medina, OH
Posts: 7
FenrisKiba is an unknown quantity at this point
Re: How do you use X-Box 360 controllers in C++?

that pnp-class would be a awesome
thank you everyone for responding
Reply With Quote
  #14   Spotlight this post!  
Unread 13-02-2011, 17:54
Robototes2412's Avatar
Robototes2412 Robototes2412 is offline
1 * 4 != 14
FRC #2412 (Robototes)
Team Role: Programmer
 
Join Date: Jan 2010
Rookie Year: 2007
Location: Bellevue
Posts: 312
Robototes2412 is on a distinguished road
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;
    }
}
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 13:21.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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