-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_test.py
405 lines (327 loc) · 13.6 KB
/
menu_test.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python
# coding: Latin-1
# Load library functions we want
import os
import sys
from inputs import get_gamepad
import time
import subprocess
import socket
import fcntl
import struct
import PicoBorgRev
# Re-direct our output to standard error, we need to ignore standard out to hide some nasty print statements from pygame
sys.stdout = sys.stderr
# Setup the PicoBorg Reverse
PBR = PicoBorgRev.PicoBorgRev()
#PBR.i2cAddress = 0x44 # Uncomment and change the value if you have changed the board address
PBR.Init()
if not PBR.foundChip:
boards = PicoBorgRev.ScanForPicoBorgReverse()
if len(boards) == 0:
print ('No PicoBorg Reverse found, check you are attached ')
else:
print ('No PicoBorg Reverse at address %02X, but we did find boards:' % (PBR.i2cAddress))
for board in boards:
print (' %02X (%d)' % (board, board))
print ('If you need to change the I2C address change the setup line so it is correct, e.g.')
print ('PBR.i2cAddress = 0x%02X' % (boards[0]))
sys.exit()
# PBR.SetEpoIgnore(True) # Uncomment to disable EPO latch, needed if you do not have a switch / jumper
# Ensure the communications failsafe has been enabled!
failsafe = False
for i in range(5):
PBR.SetCommsFailsafe(True)
failsafe = PBR.GetCommsFailsafe()
if failsafe:
break
if not failsafe:
print 'Board %02X failed to report in failsafe mode!' % (PBR.i2cAddress)
sys.exit()
PBR.ResetEpo()
# Power settings
voltageIn = 12.4 # Total battery voltage to the PicoBorg Reverse
voltageOut = voltageIn*0.95 # Maximum motor voltage, we limit it to 95% to allow the RPi to get uninterrupted power
# Setup the power limits
if voltageOut > voltageIn:
maxPower = 1.0
else:
maxPower = voltageOut / float(voltageIn)
manual_mode_lock_flag = True
second_press_to_comfirm_flag = False
menu_flag = True
exit_flag = True
option_selected = "none"
options_key = ['Manual Mode', 'Line Following', 'Maze', 'Speed Run', 'Kicker', 'Radio', 'Shutdown', 'Reboot','Exit',
'I P address']
options_module = {'Manual Mode': False, 'Line Following': False, 'Maze': False, 'Speed Run': False, 'Kicker': False,
'Radio': False, 'Shutdown': False, 'Reboot': False,'I P address': False, 'Exit': False}
menu_position_flag = 0
power_left = 0.0
power_right = 0.0
x_axis = 0.0
y_axis = 0.0
def mixer(inYaw, inThrottle,):
left = inThrottle + inYaw
right = inThrottle - inYaw
scaleLeft = abs(left / 125.0)
scaleRight = abs(right / 125.0)
scaleMax = max(scaleLeft, scaleRight)
scaleMax = max(1, scaleMax)
out_left = int(constrain(left / scaleMax, -125, 125))
out_right = int(constrain(right / scaleMax, -125, 125))
results = [out_right, out_left]
return results
def constrain(val, min_val, max_val):
return min(max_val, max(min_val, val))
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def lineFollow():
line_follow_flag = True
while line_follow_flag:
print("#### line following ####")
def maze():
maze_flag = True
while maze_flag:
print("#### maze ####")
def speed():
speed_flag = True
while speed_flag:
print("#### speed ####")
def speakIPAddress():
wlan = get_ip_address('wlan0')
command = "flite -voice rms -t 'My I P address is " + wlan + "' "
print command
os.system(command)
speakIPAddress()
# main menu
try:
print 'Press CTRL+C to quit'
loopCount = 0
# Loop indefinitely
while exit_flag:
events = get_gamepad()
#print events
#print loopCount
#loopCount = loopCount + 1
for event in events:
#print(event.code, event.state)
if menu_flag:
if event.code == "BTN_EAST":
if event.state == True:
print("Cross")
second_press_to_comfirm_flag = False
command = 'flite -voice rms -t "I am afraid I can\'t do that Dave" '
print command
os.system(command)
if event.code == "BTN_C":
if event.state == True:
print("Circle")
if second_press_to_comfirm_flag:
command = "flite -voice rms -t '" + options_key[menu_position_flag] + " comfirmed' "
os.system(command)
menu_flag = False
options_module[options_key[menu_position_flag]] = True
print ("Passed to " + options_key[menu_position_flag])
second_press_to_comfirm_flag = False
else:
os.system("flite -voice rms -t 'press again to comfirm " + options_key[menu_position_flag] + " or cross to exit ' ")
second_press_to_comfirm_flag = True
if event.code == "BTN_TR2":
if event.state == True:
print("Start")
if manual_mode_lock_flag:
os.system("flite -voice rms -t 'Manual Mode Lock off' ")
manual_mode_lock_flag = False
else:
os.system("flite -voice rms -t 'Manual Mode Lock on' ")
manual_mode_lock_flag = True
if event.code == "BTN_TL2":
if event.state == True:
print("Select")
print(menu_position_flag)
command = "flite -voice rms -t '" + options_key[menu_position_flag] + "' &"
os.system(command)
if event.code == "ABS_HAT0Y":
if event.state == -1:
print("D pad Up")
if menu_position_flag == len(options_key)-1:
menu_position_flag = 0
else:
menu_position_flag = menu_position_flag + 1
elif event.state == 1:
print("D pad Down")
if menu_position_flag == 0:
menu_position_flag = len(options_key)-1
else:
menu_position_flag = menu_position_flag - 1
if event.state == 1 or event.state == -1:
print(menu_position_flag)
print options_key[menu_position_flag]
command = "flite -voice rms -t '" + options_key[menu_position_flag] + "' "
print command
os.system(command)
second_press_to_comfirm_flag = False
if event.code == "ABS_HAT0X":
if event.state == -1:
print("D pad Left")
elif event.state == 1:
print("D pad Right")
if event.code == "BTN_Z":
if event.state == True:
print("Top right")
os.system("python /home/pi/randomFart.py &")
if event.code == "BTN_MODE":
if event.state == True:
print("Home")
os.system("flite -voice rms -t 'Home Menu.'")
if manual_mode_lock_flag:
os.system("flite -voice rms -t 'Manual Mode Lock on' ")
else:
os.system("flite -voice rms -t 'Manual Mode Lock off.' ")
os.system("flite -voice rms -t 'use up down on the D Pad to select option, then press circle to select.' ")
print("#### Menu ####")
# print(event.ev_type, event.code, event.state)
# manual mode
if options_module['Manual Mode']:
if event.code == "ABS_Y":
if event.state > 130:
print("Backwards")
elif event.state < 125:
print("Forward")
y_axis = event.state
if y_axis > 130:
y_axis = -(y_axis - 130)
elif y_axis < 125:
y_axis = ((-y_axis) + 125)
else:
y_axis = 0.0
print("Y: " + str(-y_axis))
if event.code == "ABS_Z":
if event.state > 130:
print("Right")
elif event.state < 125:
print("Left")
x_axis = event.state
if x_axis > 130:
x_axis = (x_axis - 130)
elif x_axis < 125:
x_axis = -((-x_axis) + 125)
else:
x_axis = 0.0
print("X: " + str(x_axis))
if event.code == "BTN_TL":
if event.state == True:
print("Turbo")
if event.code == "BTN_TR":
if event.state == True:
print("Tank")
if event.code == "BTN_Z":
if event.state == True:
print("Top right")
os.system("python /home/pi/randomFart.py &")
if event.code == "BTN_WEST":
if event.state == True:
print("Top left")
os.system("play /home/pi/Sounds/squirrel01.mp3 &")
if event.code == "BTN_TL2":
if event.state == True:
print("Select")
menu_flag = True
options_module['Manual Mode'] = False
print("Manual Mode Exit")
os.system("flite -voice rms -t 'Manual Mode Exit' ")
x_axis = 0
y_axis = 0
mixer_results = mixer(x_axis, y_axis)
print (mixer_results)
power_left = mixer_results[0] / 125.0
power_right = mixer_results[1] / 125.0
print("left: " + str(power_left) + " right: " + str(power_right))
PBR.SetMotor1((-power_right * maxPower))
PBR.SetMotor2 (power_left * maxPower)
print("#### Manual ####")
# line following
if options_module['Line Following']:
lineFollow()
menu_flag = True
options_module['Line Following'] = False
print("Line Following Exit")
# Speed run
if options_module['Speed Run']:
speed()
options_module['Speed Run'] = False
menu_flag = True
print("#### speed run ####")
# maze
if options_module['Maze']:
maze()
options_module['Maze'] = False
menu_flag = True
print("Maze exit")
# kicker
if options_module['Kicker']:
options_module['Kicker'] = False
menu_flag = True
print("#### kicker ####")
# radio
if options_module['Radio']:
options_module['Radio'] = False
menu_flag = True
#subprocess.call("play /home/pi/Sounds/8bit/", shell=True)
print("#### Radio ####")
# shutdown
if options_module['Shutdown']:
print("#### Good Bye ####")
command = "flite -voice rms daisy.txt "
print command
os.system(command)
time.sleep(3)
command = "flite -voice rms -t 'Goodbye Dave' "
print command
os.system(command)
time.sleep(1)
options_module['Shutdown'] = False
menu_flag = True
subprocess.call("sudo halt", shell=True)
# ip address
if options_module['I P address']:
options_module['I P address'] = False
menu_flag = True
speakIPAddress()
print("#### IP Address ####")
# reboot
if options_module['Reboot']:
options_module['Reboot'] = False
menu_flag = True
command = "flite -voice rms -t 'My system is rebooting in 5' "
print command
os.system(command)
for i in range(4,0,-1):
command = "flite -voice rms -t \'" + str(i) + " \'"
print command
os.system(command)
time.sleep(0.98)
command = "flite -voice rms -t 'rebooting now!' "
print command
os.system(command)
time.sleep(1)
print("#### restarting ####")
subprocess.call("sudo reboot", shell=True)
# exit to command line
if options_module['Exit']:
options_module['Exit'] = False
exit_flag = False
print("#### exiting to command line ####")
except KeyboardInterrupt:
# CTRL+C exit, disable all drives
print("\nstopping")
PBR.SetMotor1(0)
PBR.SetMotor2(0)
os.system("flite -voice rms -t 'Exiting to the Command line' ")
print("bye")