-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.py
158 lines (132 loc) · 3.87 KB
/
Controller.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import hid, time, serial, time, sys, signal
import serial.tools.list_ports
def signal_handler(signal, frame):
print("closing program")
SerialPort.close()
sys.exit(0)
COM=input("Enter the COM Port\n")
BAUD=input("Enter the Baudrate\n")
"""
for i in serial.tools.list_ports.comports():
try:
SerialPort = serial.Serial(i.device,9600,timeout=0.1)
except:
pass
"""
SerialPort = serial.Serial(COM,BAUD,timeout=1)
for device in hid.enumerate():
if device['product_string'] == "Wireless Controller":
gamepad = hid.device()
gamepad.open(0x054c, 0x09cc)
gamepad.set_nonblocking(True)
while True:
report = gamepad.read(64)
new_report = []
if report:
left = report[1] * 2
up = report[4] * 2
if left < 509:
left = -left - 1
else:
left = left - 1
if up < 509:
up = -up - 1
else:
up = up - 1
if left == -1:
left = 0
elif up == -1:
up = 0
left = left + 255
up = up + 255
if left > 762:
left = -255
if up > 762:
up = -255
if left < 15 and left > -15:
left = 0
if up < 15 and up > -15:
up = 0
if up > 200:
up = 255
if left > 200:
left = 255
if up < -200:
up = -255
if left < -200:
left = -255
motor_left = 0
motor_right = 0
if up != 0 and left == 0:
motor_left = up
motor_right = up
if up == 0 and left != 0:
if left > 0:
motor_left = -left
motor_right = left
else:
motor_right = left
motor_left = abs(left)
else:
motor_left = up
motor_right = up
if left > 0 and up > 0:
motor_left = motor_left - left
elif left < 0 and up > 0:
motor_right = motor_right + left
else:
if left > 0:
motor_left = motor_left + left
else:
motor_right = motor_right - left
else:
left = 0
up = 0
motor_left = 0
motor_right = 0
directions = ""
if motor_left > motor_right and motor_left > -1 and motor_right > -1:
motor_right = 128
yey = True
elif motor_right > motor_left and motor_left > -1 and motor_right > -1:
motor_left = 128
yer = True
if motor_left < 0:
directions = directions + "R"
elif motor_left == 0:
directions = directions + "S"
else:
directions = directions + "F"
if motor_right < 0:
directions = directions + "R"
elif motor_left == 0:
directions = directions + "S"
else:
directions = directions + "F"
if directions == "SS":
directions = "S"
elif directions == "FF":
directions = "F"
elif directions == "RF":
directions = "J"
elif directions == "FR":
directions = "K"
elif directions == "RR":
directions = "R"
try:
if yer == True:
directions = "Z"
del yer
except:
pass
try:
if yey == True:
directions = "X"
del yey
except:
pass
#print(str(motor_left).replace('-', 'R')[0 + ":" + str(motor_right).replace('-', 'R'))
SerialPort.write(bytes(directions + "\n",'utf-8'))
#print(str(motor_left) + ":" + str(motor_right))
print(directions)
time.sleep(0.01)