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:
"""
Gamepad Module
Daniel J. Gonzalez
dgonz@mit.edu
Based off code from: http://robots.dacloughb.com/project-1/logitech-game-pad/
"""
import pygame
pygame.init()
j = pygame.joystick.Joystick(0)
j.init()
print 'Initialized Joystick : %s' % j.get_name()
"""
Returns a vector of the following form:
[LThumbstickX, LThumbstickY, Unknown Coupled Axis???,
RThumbstickX, RThumbstickY,
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]
Note:
No D-Pad.
Triggers are switches, not variable.
Your controller may be different
"""
def get():
out = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
it = 0 #iterator
pygame.event.pump()
#Read input from the two joysticks
for i in range(0, j.get_numaxes()):
out[it] = j.get_axis(i)
it+=1
#Read input from buttons
for i in range(0, j.get_numbuttons()):
out[it] = j.get_button(i)
it+=1
return out
def test():
while True:
print get()
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!
Thanks Bro! That's what I needed!
ReplyDeleteThanks for this!
ReplyDeleteThanks for this!
ReplyDeleteHey!
ReplyDeleteYou should be able to get the d-pad values with j.get_hatsnum() and j.get_hat(i). And i is only 0 since there is only one d-pad. (The output is a tuple)
If tired of pygame, you can try mine gamepad grabber
ReplyDeletehttp://www.gr4viton.cz/2017/03/python-gamepad-cli-grabber-drawille-cli-stick-direction-visualiser/
Hi, I just read your article and as a gamer I found it very helpful for other gamers. Well, if someone is looking to buy a game controller for PC gaming then I'd love to recommend this list of best PC game controllers. I bet you will surely find the right one for your needs on this link.
ReplyDeleteSo we can reason that while incorporated card must build the cost of a chip, it's not by any means enough to legitimize expenses of making 2 lines of chips: with emerging tech and without. This is likely why Intel chose to put a GPU on each and every customer CPU. It's additionally helpful if your primary video card's blown and you don't have an extra. I'm certain AMD has something comparable on their wayray navion APUs - yet I've not utilized them as of late - they're fairly more great than intel's models, and matched with a discrete radeon may do switchable designs to spare power.
ReplyDeleteI really happy found these website eventually. Really informative and inoperative, Thanks for the post and effort! Please keep sharing more such blog.
ReplyDeleteTrend Micro Geek Squad
Generally I do not read article on blogs, but I wish to say that this write-up very compelled me to try and do so! Your writing style has been amazed me. Thanks, quite nice post.
thx QSL https://henryBRhd.wixsite.com/utf931/tz41
ReplyDeletethx oe5RDS 107.5Mhz 77.5Khz
công ty thám tử
ReplyDeletethám tử sài gòn
thám tử hà nội
http://www.thamtuhoangkim.com/bang-gia-thue-tham-tu-theo-doi-ngoai-tinh.html
f you are looking to learn about the service, please take a moment to read the article below!
Adultery is the highest risk leading