How do I get input from an XBox 360 controller?
Asked Answered
A

4

14

I'm writing a program that needs to take input from an XBox 360 controller. The input will then be sent wirelessly to an RC Helicopter that I am building.

So far, I've learned that this can be done using either the XInput library from DirectX, or the Input framework in XNA.

I'm wondering if there are any other options available. The scope of my program is rather small, and having to install a large gaming library like DirectX or XNA seems like excessive. Further, I'd like the program to be cross platform and not Microsoft specific.

Is there a simple lightweight way I can grab the controller input with something like Python?

Edit to answer some comments:

The copter will have 6 total propellers, arranged in 3 co-axial pairs. Basically, it will be very similar to this, only it will cost about $1,000 rather than $15,000. It will use an Arduino for onboard processing, and Zigbee for wireless control.

The 360 controller was selected because it is well designed. It is very ergonomic and has all of the control inputs needed. For those familiar with helicopter controls, the left joystick will control the collective, the right joystick with control the pitch and roll, and the analog triggers will control the yaw. The analog triggers are a big feature for the 360 controller. PS and most others do not have them.

I have a webpage for the project, but it is still pretty sparse. I do plan on documenting the whole design though, so eventually it will be interesting.
http://tricopter.googlecode.com
On a side note, would it kill Google to have a blog feature for googlecode projects?

I would like the 360 controller input program to run in both Linux and Windows if possible. Eventually though, I'd like to hook the controller directly to an embedded microcontroller board (such as Arduino) so that I don't have to go through a computer, but its not a high priority at the moment.

Ation answered 31/3, 2009 at 1:55 Comment(7)
Does it really have to be an xBox 360 controller? Why not a more standard computer joystick. What's so special about the 360 controller?Experimentalize
Are you going to blog or do a write up when you finish it? You should link to it, I'd be interested :)Handicraftsman
Maybe he owns a 360 controller, but not a standard computer joystick. (I don't personally know anyone these days who owns a standard computer joystick.)Demb
Regardless of what kind of controller it uses, the project sounds awesome.Borehole
@Kibbee, the 360 controller is really nice :)Handicraftsman
360 is standard. It follows the USB HID spec, works just fine.Czarra
What's your target platform? You'd have to go back a fair bit now to find a version of Windows that doesn't have DirectInput installed as standard.Un
I
7

PyGame can read joysticks, which is what the X360 controller shows up as on a PC.

Irrationality answered 31/3, 2009 at 2:16 Comment(5)
Doesn't PyGame use DirectInput to actually read the sticks, though? So it wouldn't work if you wanted to avoid DInput as a requirement.Un
I tried PyGame out last night and it worked pretty well. I was able to grab controller input pretty easily. I haven't tested it in Linux yet though.Ation
PyGame is essentially a Python layer on top of SDL. I don't think it has any directX dependencies. Like SDL, it can't do force feedback. It also treats the two analog triggers as a single axis. I believe that DirectInput does the same. Only XInput and XNA allow full featured use.Ation
Update: I've noticed that the latest version of PyGame treats the two analog triggers as separate axes. Still no force feedback though.Ation
Relevant section of the documentation: pygame.org/docs/ref/joystick.htmlFoote
T
9

It is not all that difficult. As the earlier guy mentioned, you can use the SDL libraries to read the status of the xbox controller and then you can do whatever you'd like with it.

There is a SDL tutorial: http://sdl.beuc.net/sdl.wiki/Handling_Joysticks which is fairly useful.

Note that an Xbox controller has the following:

  • two joysticks:
    • left joystick is axis 0 & 1;
    • left trigger is axis 2;
    • right joystick is axis 3 & 4;
    • right trigger is axis 5
  • one hat (the D-pad)
  • 11 SDL buttons
    • two of them are joystick center presses
  • two triggers (act as axis, see above)

The upcoming SDL v1.3 also will support force feedback (aka. haptic).

I assume, since this thread is several years old, you have already done something, so this post is primarily to inform future visitors.

Timtima answered 13/3, 2011 at 21:32 Comment(0)
I
7

PyGame can read joysticks, which is what the X360 controller shows up as on a PC.

Irrationality answered 31/3, 2009 at 2:16 Comment(5)
Doesn't PyGame use DirectInput to actually read the sticks, though? So it wouldn't work if you wanted to avoid DInput as a requirement.Un
I tried PyGame out last night and it worked pretty well. I was able to grab controller input pretty easily. I haven't tested it in Linux yet though.Ation
PyGame is essentially a Python layer on top of SDL. I don't think it has any directX dependencies. Like SDL, it can't do force feedback. It also treats the two analog triggers as a single axis. I believe that DirectInput does the same. Only XInput and XNA allow full featured use.Ation
Update: I've noticed that the latest version of PyGame treats the two analog triggers as separate axes. Still no force feedback though.Ation
Relevant section of the documentation: pygame.org/docs/ref/joystick.htmlFoote
N
1

Well, if you really don't want to add a dependency on DirectX, you can use the old Windows Joystick API -- Windows Multimedia -> Joystick Reference in the platform SDK.

The standard free cross plaform game library is Simple DirectMedia Layer, originally written to port Windows games to Unix (Linux) systems. It's a very basic, lightweight API that tends to support the minimal subset of features on each system, and it has bindings for most major languages. It has very basic joystick and gamepad support (no force feedback, for example) but it might be sufficient for your needs.

Nosebleed answered 31/3, 2009 at 2:36 Comment(0)
F
0

Perhaps the Mono.Xna library has added GamePad support, which would provide the cross platform functionality you were looking for:
http://code.google.com/p/monoxna/

As far as the concerns about the library being too heavy weight, sure, for this option it may be true ... however, it could open up opportunities to do some nice visualization in the future.

disclaimer: I'm not familiar with the status of the mono xna project, so it may not have added this feature yet. But still, 'tis an option :-)

Forbade answered 4/6, 2009 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.