Created
July 29, 2017 10:59
-
-
Save headHUB/c887178d61b5be76d9f306886f368135 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import serial | |
import pygame | |
# allow multiple joysticks | |
joy = [] | |
# Arduino USB port address (try "COM5" on Win32) | |
usbport = "/dev/ttyUSB0" | |
# define usb serial connection to Arduino | |
ser = serial.Serial(usbport, 9600) | |
# handle joystick event | |
def handleJoyEvent(e): | |
if e.type == pygame.JOYAXISMOTION: | |
axis = "unknown" | |
if (e.dict['axis'] == 0): | |
axis = "X" | |
if (e.dict['axis'] == 1): | |
axis = "Y" | |
if (e.dict['axis'] == 4): | |
axis = "T" | |
if (e.dict['axis'] == 3): | |
axis = "P" | |
if (axis != "unknown"): | |
str = "Axis: %s; Value: %f" % (axis, e.dict['value']) | |
# uncomment to debug | |
#output(str, e.dict['joy']) | |
# Arduino joystick-servo hack | |
if (axis == "X"): | |
xpos = e.dict['value'] | |
# convert joystick position to servo increment, 0-180 | |
#move = round(pos * 90, 0) | |
if (xpos < -5000): | |
ser.write("M,") | |
ser.write("F,") | |
ser.write(xpos) | |
ser.write("*") | |
elif (xpos > 5000): | |
ser.write("M,") | |
ser.write("B,") | |
ser.write(xpos) | |
ser.write("*") | |
# convert position to ASCII character | |
#servoPosition = chr(servo) | |
# and send to Arduino over serial connection | |
#ser.write(servoPosition) | |
# uncomment to debug | |
#print servo, servoPosition | |
if (axis == "Y"): | |
ypos = e.dict['value'] | |
if (ypos< -5000): | |
ser.write("M,") | |
ser.write("L,") | |
ser.write(ypos) | |
ser.write("*") | |
elif (ypos > 5000): | |
ser.write("M,") | |
ser.write("R,") | |
ser.write(ypos) | |
ser.write("*") | |
if (axis == "T"): | |
tpos = e.dict['value'] | |
ser.write("T,") | |
ser.write("A,") | |
ser.write(tpos) | |
ser.write("*") | |
if (axis == "P"): | |
ppos = e.dict['value'] | |
ser.write("P,") | |
ser.write("A,") | |
ser.write(ppos) | |
ser.write("*") | |
elif e.type == pygame.JOYBUTTONDOWN: | |
str = "Button: %d" % (e.dict['button']) | |
# uncomment to debug | |
#output(str, e.dict['joy']) | |
# Button 0 (trigger) to quit | |
if (e.dict['button'] == 0): | |
print "Bye!n" | |
ser.close() | |
else: | |
pass | |
def output(line, stick): | |
print "Joystick: %d; %s" % (stick, line) | |
# wait for joystick input | |
def joystickControl(): | |
while True: | |
e = pygame.event.wait() | |
if (e.type == pygame.JOYAXISMOTION or e.type == pygame.JOYBUTTONDOWN): | |
handleJoyEvent(e) | |
# main method | |
def main(): | |
# initialize pygame | |
pygame.joystick.init() | |
pygame.display.init() | |
if not pygame.joystick.get_count(): | |
print "nPlease connect a joystick and run again.n" | |
print "n%d joystick(s) detected." % pygame.joystick.get_count() | |
# for i in range(pygame.joystick.get_count()): | |
# myjoy = pygame.joystick.Joystick(i) | |
# myjoy.init()# | |
# joy.append(myjoy) | |
# print "Joystick %d: " % (i) + joy[i].get_name() | |
print "Depress trigger (button 0) to quit.n" | |
# run joystick listener loop | |
joystickControl() | |
# allow use as a module or standalone script | |
if __name__ == "__main__": | |
main() | |
# print the jo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment