/
voice.py
481 lines (369 loc) · 11.9 KB
/
voice.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#for interprocess communication
import smokesignal
from easyNav_pi_dispatcher import DispatcherClient
import json
import speaker
import KeypadLogic
import multiprocessing
import time
import GetLocations
import requests
import os
class Notifications(object):
def __init__(self):
self.speaker = speaker.newSpeaker()
# #interprocess
self.DISPATCHER_PORT = 9002
self.dispatcherClient = DispatcherClient(port=self.DISPATCHER_PORT)
## For collision detection: lock navigation until ready
self.collisionLocked = False
self.obstacle = None
self.OngoingNav = 0
self.cruncherAlert = 0
self.infotosay =None
self.cruncherInfotosay=""
## Attach event listeners
self.attachEvents(self.speaker)
def attachEvents(self, mic):
## clear all signals
smokesignal.clear()
text = ""
@smokesignal.on('say')
def onSay(args):
print "Info from Nav"
infoFromNav = eval(args.get('payload'))
print infoFromNav
self.infotosay = infoFromNav["text"]
try:
if(self.infotosay != None and self.infotosay == 'Retrieved new path.'):
self.OngoingNav = 1
#self.speaker.say(self.infotosay)
if self.infotosay != None and "Destination" in self.infotosay:
self.OngoingNav = 0
self.speaker.say(self.infotosay)
# if self.infotosay != None and "reset" in self.infotosay:
# self.OngoingNav = 0
# if "paused" in self.infotosay:
# self.OngoingNav = 0
# if "resumed" in self.infotosay:
# self.OngoingNav=1
if self.infotosay != None and "Checkpoint" in self.infotosay:
self.speaker.say(self.infotosay)
print self.infotosay
except:
pass
@smokesignal.on('cruncherAlert')
def onSay(args):
print "Info from Nav"
try:
if(self.OngoingNav == 1):
os.system("sudo pkill -SIGTERM -f \"aplay\" ")
infoFromCruncher = eval(args.get('payload'))
print infoFromCruncher
self.cruncherInfotosay = infoFromCruncher["text"]
self.cruncherAlert = 1
print self.cruncherInfotosay
except:
pass
@smokesignal.on('obstacle')
def onObstacle(args):
try:
response = int(json.loads(args.get('payload')).get('status'))
if (response == 0):
self.obstacle = None
return
elif (response == 1):
self.obstacle = 'FRONT'
self.collisionLocked = True
elif (response == 2):
self.obstacle = 'LEFT'
self.collisionLocked = True
elif (response == 3):
self.obstacle = 'RIGHT'
self.collisionLocked = True
# If collision, set to true
if(self.OngoingNav==1 and not self.collisionLocked):
os.system("sudo pkill -SIGTERM -f \"aplay\" ")
#self.collisionLocked = True
except:
pass
@smokesignal.on('reset')
def onObstacle(args):
self.speaker.say("Voice reset")
self.OngoingNav = 0 #clear ongoing nav
def start(self):
self.dispatcherClient.start()
#run notifier forever
while(1):
try:
regis = ns.sem
## Collision detection first
if (self.collisionLocked and self.OngoingNav == 1 and regis == 0):
if (self.obstacle == None):
# Unlock collisionLocked
self.speaker.say('Obstacle cleared.')
self.collisionLocked = False
elif (self.obstacle == 'FRONT'):
self.speaker.say('Obstacle ahead. Turn 45 degrees left or right!')
elif (self.obstacle == 'LEFT'):
self.speaker.say('Obstacle on the left!')
elif (self.obstacle == 'RIGHT'):
self.speaker.say('Obstacle on the right!')
elif(self.cruncherAlert == 1 and self.OngoingNav == 1 and regis == 0):
self.speaker.say(self.cruncherInfotosay)
self.cruncherAlert=0
self.cruncherInfotosay=None
else:
if(self.infotosay != None and regis == 0):
self.speaker.say(self.infotosay)
self.infotosay=None
except:
pass
time.sleep(0.1)
class Voice(object):
HOST_ADDR = "http://localhost:1337/"
def __init__(self):
self.speaker = speaker.newSpeaker()
#interprocess
self.DISPATCHER_PORT = 9002
self.dispatcherClient = DispatcherClient(port=self.DISPATCHER_PORT)
self.inputBuffer =[]
self.checkNavigation = []
#update locations
# self.getLocation = GetLocations.Locations()
# self.getLocation.getLoc()
def presentMenu(self):
self.speaker.say("Key * 1 # to choose map and level and locations")
self.speaker.say("Key * 2 # to choose locations")
self.speaker.say("Key * 3 # to change destination")
self.speaker.say("Key * 4 # to end")
def getLocations(self):
#get a list of locations
filename = "locations.txt"
locationFile = open(filename, "r")
print "Opened file!!"
locations = []
for line in locationFile.readlines():
line = line.replace("\n", "")
locations.append(line)
print locations
return locations
def getCoord(self, index):
filename = "CoordinateFile.txt"
CoordinateFile = open(filename, "r")
ctr = 0
startCoord = ""
for line in CoordinateFile.readlines():
if ctr == index:
line = line.replace("\n", "")
startCoord = line
ctr+=1
return startCoord
def getSUIDIndex(self, SUID):
print "in method"
filename = "SUIDFile.txt"
SUIDFile = open(filename, "r")
print "Opened file"
index = 0
for line in SUIDFile.readlines():
line = line.replace("\n", "")
print line
if int(line) == SUID:
return index
ctr+=1
return -1
def isCancel(self, inputVal):
if(inputVal == '999'):
self.speaker.say("Cancelling operation")
return True
return False
def start(self):
self.dispatcherClient.start()
#present menu
#self.presentMenu();
#keep listening for keypad activity
while(1):
try:
# self.checkNavigation[0] = ns.OngoingNavigation
print self.checkNavigation
self.inputBuffer = KeypadLogic.getInput(self.speaker)
strInput = ''.join(self.inputBuffer)
print strInput
if (strInput == '*1'):
startBuildingBuf = []
startLevelBuff=[]
startNodeBuff=[]
endBuildingBuf = []
endLevelBuff=[]
endNodeBuff=[]
self.speaker.say("Key in start building ID, one for com one, two for com two")
startBuildingBuf = KeypadLogic.getInput(self.speaker)
strStartBuildingID = ''.join(startBuildingBuf)
if(self.isCancel(strStartBuildingID)):
continue
else:
self.speaker.say("you have chosen building " + strStartBuildingID)
self.speaker.say("Key in start level ID, zero is basement.")
startLevelBuff = KeypadLogic.getInput(self.speaker)
strStartLevelID = ''.join(startLevelBuff)
if(self.isCancel(strStartLevelID)):
continue
else:
self.speaker.say("you have entered level " + strStartLevelID)
self.speaker.say("Key in start node ID")
startNodeBuff = KeypadLogic.getInput(self.speaker)
strStartNodeID = ''.join(startNodeBuff)
if(self.isCancel(strStartNodeID)):
continue
else:
self.speaker.say("you have entered start node " + strStartNodeID)
self.speaker.say("Key in destination building ID ")
endBuildingBuf = KeypadLogic.getInput(self.speaker)
strEndBuildingID = ''.join(endBuildingBuf)
if(self.isCancel(strEndBuildingID)):
continue
else:
self.speaker.say("you have entered building " + strEndBuildingID)
self.speaker.say("Key in destination level ID")
endLevelBuff = KeypadLogic.getInput(self.speaker)
strEndLevelID = ''.join(endLevelBuff)
if(self.isCancel(strEndLevelID)):
continue
else:
self.speaker.say("you have entered level " + strEndLevelID)
self.speaker.say("Key in destination node ID")
endNodeBuff = KeypadLogic.getInput(self.speaker)
strEndNodeID = ''.join(endNodeBuff)
if(self.isCancel(strEndNodeID)):
continue
else:
self.speaker.say("you have entered destination node " + strEndNodeID)
#construct start point SUid
strStartSUID = strStartBuildingID+strStartLevelID+strStartNodeID
#construct dest SUid
strEndSUID = strEndBuildingID+strEndLevelID+strEndNodeID
try:
startSUID = int(strStartSUID)
endSUID = int(strEndSUID)
print startSUID
print endSUID
print "not int problem"
#get coord
r = requests.get(Voice.HOST_ADDR + "node/?SUID=" + strStartSUID)
print r
for location in r.json():
print location
startCoord = location['loc']
print startCoord
self.dispatcherClient.send(9003, "starting", eval(startCoord))
self.dispatcherClient.send(9001, "newPath", {"from":startSUID, "to": endSUID})
self.speaker.say("Routing!")
except:
self.speaker.say("Invalid ID")
#find builiding map and get coordinates
elif (strInput == '*2'):
print "note"
startNodeBuff=[]
endNodeBuff=[]
self.speaker.say("Key in start point ID")
startNodeBuff = KeypadLogic.getInput(self.speaker)
strStart = ''.join(startNodeBuff)
self.speaker.say("you have entered " + strStart)
print strStart
self.speaker.say("Key in destination ID")
endNodeBuff = KeypadLogic.getInput(self.speaker)
strEnd = ''.join(endNodeBuff)
self.speaker.say("you have entered " + strEnd)
try:
startSUID = int(strStart)
endSUID = int(strEnd)
print startSUID
print endSUID
#get coord
r = requests.get(Voice.HOST_ADDR + "node/?SUID=" + strStart)
print r
for location in r.json():
print location
startCoord = location['loc']
print startCoord
self.dispatcherClient.send(9003, "starting", eval(startCoord))
self.dispatcherClient.send(9001, "newPath", {"from":startSUID, "to": endSUID})
self.speaker.say("Routing!")
except:
self.speaker.say("Invalid ID")
elif (strInput == '*33'):
ns.sem=1
self.speaker.say("Resetting Nav")
self.dispatcherClient.send(9001, "reset", {"text": None})
# os.system("sudo pkill -SIGTERM -f \"nav\"")
# os.system("sudo pkill -SIGTERM -f \"voice\"")
ns.sem=0
elif(strInput == '*55'):
ns.sem=1
self.speaker.say("Resuming Nav")
self.dispatcherClient.send(9001, "unpause", {"text": None})
ns.sem=0
elif (strInput == '*4'):
self.speaker.say("Ending EasyNav, Are you sure?")
self.speaker.say("Key in 1 to confirm, 2 to cancel")
option = KeypadLogic.getInput(self.speaker)
strOption = ''.join(option)
try:
if(strOption == '1'):
self.speaker.say("Hope you had a good journey!")
self.speaker.say("Ending all processes, Please wait")
time.sleep(1)
self.speaker.say("Please remember to switch off the shoe and bag switches...GoodBye")
os.system("sudo pkill -SIGTERM -f \"node\" ")
time.sleep(1)
os.system("sudo sh /home/pi/repos/easyNav-IO/shutdown.sh")
else:
self.speaker.say("Cancelling")
except ValueError:
self.speaker.say("Error, key in a proper ID")
#troubleshooting commands
elif (strInput == "*66"):
#lock speech mechanism
ns.sem = 1
self.speaker.say("Finding i p")
os.system("ifconfig wlan0 | grep inet > myIp.txt")
ipFound = False
with open("myIp.txt") as IPText:
for line in IPText:
if "inet" in line:
ipFound=True
print line
lineDic = line.split(" ")
for word in lineDic:
print word
if "addr" in word:
print "here"
ipAddr = word.replace("addr:", "")
self.speaker.say("The I P is " + ipAddr)
print word
if(not ipFound):
self.speaker.say("No i p assigned")
#unlock speech mechanism
ns.sem = 0
except:
pass
time.sleep(0.1)
def runVoice(ns):
voice = Voice()
voice.start()
def runNotifications(ns):
notifications = Notifications()
notifications.start()
if __name__ == '__main__':
manager = multiprocessing.Manager()
ns = manager.Namespace()
ns.sem = 0
# ns.OngoingNavigation = 0
p1 = multiprocessing.Process(target=runVoice, args=(ns,))
p1.start()
p2 = multiprocessing.Process(target=runNotifications, args=(ns,))
p2.start()
while(1):
time.sleep(1)
p1.join()
p2.join()