Quadrotors are usually controlled with two thumbsticks (4 axes):
- Left Thumbstick Up/Down: Throttle
- Left Thumbstick Left/Right: Yaw
- Right Thumbstick Up/Down: Pitch
- Left Thumbstick Left/Right: Roll
Oh, and I can also use the controller to play games on my computer. Which is, you know, its intended purpose. N64 Emulator, Anyone?
I looked online for some Python example code, and the easiest came from this site.
I modified it to make it more simple and global for my, and hopefully your, purposes:
Daniel J. Gonzalez
Based off code from: http://robots.dacloughb.com/project-1/logitech-game-pad/
j = pygame.joystick.Joystick(0)
print 'Initialized Joystick : %s' % j.get_name()
Returns a vector of the following form:
[LThumbstickX, LThumbstickY, Unknown Coupled Axis???,
Button 1/X, Button 2/A, Button 3/B, Button 4/Y,
Left Bumper, Right Bumper, Left Trigger, Right Triller,
Select, Start, Left Thumb Press, Right Thumb Press]
Triggers are switches, not variable.
Your controller may be different
out = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
it = 0 #iterator
#Read input from the two joysticks
for i in range(0, j.get_numaxes()):
out[it] = j.get_axis(i)
#Read input from buttons
for i in range(0, j.get_numbuttons()):
out[it] = j.get_button(i)
To test the code with your gamepad, simply plug it in, open a python shell, make sure the file is saved as gamepad.py, import gamepad and run gamepad.test(). Works for me in Ubuntu 12.04. To use this in your own application, simply place gamepad.py in your working directory, import gamepad, and call gamepad.get() every time you want to view the current state vector.
Button 4 is not pressed
Button 4 is pressed. Note the "1" in the vector output on screen.
Left Thumbstick not touched, all at 0.
Going kinda right on the Right Stick gives a value of -.0567, and the value is linear relative to how far I push the stick.
Going full right on the left stick brings the value up to -1.
Groovy. I don't know what's up with the third axis the joystick is giving me. The first two values in the vector make sense: Left Thumbstick up/down and left/right. But the third value changes when I move the Left Thumbstick left/right and when I move the Right Thumbstick up/down and when i hit the Start button (Button 10). It sure as hell isn't an accelerometer or anything fun like that, so I'm just going to ignore it for now.
Hope the code proves useful!