This repository has been archived by the owner on May 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rov_joystick.py
125 lines (110 loc) · 3.74 KB
/
rov_joystick.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import pygame
from math import floor
from pylibftdi import Device # I2C over USB
import time
#import servo
dev = Device(mode='b')
dev.baudrate = 9600
# allow multiple joysticks
joy = []
def fix(bits):
if bits & 0b00100000:
return 63-bits
else:
return bits
# handle joystick event
def handleJoyEvent(e):
if e.type == pygame.JOYAXISMOTION:
axis = "unknown"
if (e.dict['axis'] == 0):
axis = "X"
bits = 0b10000000 # Throw away; don't change anything
if (e.dict['axis'] == 1):
axis = "Y"
bits = 0b10000000
if (e.dict['axis'] == 2):
axis = "Throttle"
bits = 0b01000000
if (e.dict['axis'] == 3):
axis = "Z"
bits = 0b11000000
if (axis != "unknown"):
scale = 63 # From 0 to __
scale /= 2
bottom5 = int( scale*(e.dict['value']+1) )
print "From joystick: %s %d" % (axis, bottom5)
print "bottom5 ==", bottom5
print "bits+bottom5 == ", bits+bottom5
print
dev.write(chr(bits+bottom5))
print "%s %f" % (axis, e.dict['value']+1)
from_arduino = dev.read(1)
print "from_arduino:", from_arduino
time.sleep(.030)
# for num in range(256):
# print "Sending " + str(num)
# dev.write(chr(num))
# time.sleep(.005)
# Arduino joystick-servo hack
if (axis == "X"):
pos = e.dict['value']
# convert joystick position to servo increment, 0-180
move = round(pos * 90, 0)
if (move < 0):
serv = int(90 - abs(move))
else:
serv = int(move + 90)
# convert position to ASCII character
servoPosition = serv
# and send to Arduino over serial connection
##print "servo.move(1, %s)" % servoPosition
# Arduino joystick-servo hack
if (axis == "Y"):
pos = e.dict['value']
# convert joystick position to servo increment, 0-180
move = round(pos * 90, 0)
if (move < 0):
serv = int(90 - abs(move))
else:
serv = int(move + 90)
# convert position to ASCII character
servoPosition = serv
# and send to Arduino over serial connection
##print "servo.move(2, %s)" % servoPosition
# 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 "Pew Pew You're DEAD!!\n"
# if (e.dict['button'] == 8):
# print "Bye!\n"
# quit()
# else:
# pass
# 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"
quit()
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()
# run joystick listener loop
joystickControl()
# allow use as a module or standalone script
if __name__ == "__main__":
main()