-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathControllerOutput.py
More file actions
78 lines (61 loc) · 2.33 KB
/
ControllerOutput.py
File metadata and controls
78 lines (61 loc) · 2.33 KB
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
#!/usr/bin/env python
import sys
import time
import pygame
import serial
pygame.init() # initiaise pygame
controls = pygame.joystick.Joystick(0) # call the joystic controls
clock = pygame.time.Clock() # intialise pygame refresh and call it clock
controls.init() # initialise the controls
#arduino = serial.Serial('/dev/ttyUSB0', 9600,timeout = 1) # connect to the arduino's serial port Linux
#time.sleep(2)
arduino = serial.Serial('COM9', 9600,timeout = 1) # connect to the arduino's serial port Windows
time.sleep(2)
EXIT = False
old_min = -1
old_max = 1
new_min = 100
new_max = 355
def valueMap(old_value):
new_value = str(int(round(( ( old_value - old_min ) / (old_max - old_min) ) * (new_max - new_min) + new_min)))
return(new_value)
while not EXIT:
for event in pygame.event.get():
if event.type == pygame.QUIT:
EXIT = True
controllerName = str(controls.get_name())
axesNumber = controls.get_numaxes()
hatNumber = controls.get_numhats()
buttonNumber = controls.get_numbuttons()
a = valueMap(controls.get_axis(0))
b = valueMap(controls.get_axis(1))
c = valueMap(controls.get_axis(2))
d = valueMap(controls.get_axis(3))
e = '000'
f = '0000'
for x in range(buttonNumber):
if controls.get_button(x) == 1:
e = '0' + str(x+1)
if x < 9:
e = '00' + str(x+1)
for positionHat in range(hatNumber):
hat = controls.get_hat(positionHat)
if hat[0] == -1:
f = '1000'
elif hat[0] == 1:
f = '0100'
if hat[1] == 1:
f = '0010'
elif hat[1] == -1:
f = '0001'
control = ['<' + a,b,c,d,e,f + '>'] # save strings in a list
cstring = ",".join(control) # convert list to a single string with commas seperating values
print(cstring)
arduino.write(cstring) # print string to shell and write data to arduino with a 0.1ms delay
time.sleep(0.0001)
if controls.get_button(12) == 1:
pygame.quit() # This is used to quit pygame and use any internal program within the python
quit()
clock.tick(1000)
pygame.quit()
quit()