Sunday, March 23, 2014

XBox 360 Controller and Python

The Raspberry Pi has spawned a never ending stream of cool tech projects.  With its low cost, smartly designed feature set, and strong sharing community, it will continue to satisfy hardcore and nooby geeks alike. 

Here's another cool project for the Pi that makes it easy to extend its capabilities by providing wireless remote control.  The XBox 360 controller is an excellent input device, having several analog and digital controls.  Combine that with the wireless USB adapter and you have a compact and feature-rich control method, perfect for robots or other remote control projects.

Interfacing with the Pi can be done using a driver that is available called xboxdrv at http://pingus.seul.org/~grumbel/xboxdrv/.  This driver takes care of reading raw controller input, but doesn't provide a useful interface for scripting.  I've created a Python class that makes it easy to read all of XBox controller inputs in real-time.  Digital inputs, such as the A, B, X, and Y buttons return 1 when pressed (True) or 0 when not pressed (False).  Analog inputs return floating point values between -1.0 and 1.0.  Here's a simple example of how to use the class:

import xbox
joy = xbox.Joystick() #Initialize joystick
if joy.A(): #Test state of the A button (1=pressed, 0=not pressed)
print 'A button pressed'
x_axis = joy.leftX() #X-axis of the left stick (values -1.0 to 1.0)
(x,y) = joy.leftStick() #Returns tuple containing left X and Y axes (values -1.0 to 1.0)
trigger = joy.rightTrigger() #Right trigger position (values 0 to 1.0)
joy.close() #Cleanup before exit
view raw sample.py hosted with ❤ by GitHub


Source code, setup instructions and more sample code for my xbox.py module are available on github

Note: xboxdrv will require sudo privileges to access the USB device.  There are workarounds to this, which can be found if you Google around.

7 comments:

  1. Hello. Thank you for sharing this, it's very useful for a project I'm working on. I have run into a problem whereby the right joystick doesn't output between -1 and 1, but between 1 and 1 instead. This is a pain because I need to use the joystick for steering a robot. Do you know why this might be happening?

    Thanks.

    ReplyDelete
    Replies
    1. Hi George. Sorry about the bug. It was an error I noticed some time ago, but the fix never made it back to github. I've made the update on github now, so give that a try.

      Steve

      Delete
    2. It works! Thanks for fixing the bug.

      Delete
  2. Thanks for your reply. I'll have a look on github.

    ReplyDelete
  3. This saved me. Thank you very much for your effort.

    ReplyDelete
  4. Hello, when I try to run the script appears to me the following error message: module 'xbox' has no attribute 'Joystick'.

    ReplyDelete
  5. The casino you want the most? | DrMCD
    If you're looking to enjoy 계룡 출장마사지 slots and casino games, The Casino has 논산 출장마사지 the 김해 출장샵 For many years, it's operated by the 서산 출장마사지 Microgaming group, 안성 출장안마

    ReplyDelete