/
tts1.py
280 lines (248 loc) · 9.19 KB
/
tts1.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
#!/usr/bin/python
from espeak import espeak
import MySQLdb
import serial
import time
import os.path
import math
from espeak import core as espeak_core
timerSeconds = 2 #Time between command repeats itself in seconds
timerProximity = 1
wallElapsed = 0 #starting value for elapsed
objectElapsed = 0
openingBothElapsed = 0
openingLeftElapsed = 0
openingRightElapsed = 0
leftProximityElapsed = 0
rightProximityElapsed = 0
ser = serial.Serial('/dev/rfcomm1',9600,timeout = 5)#wall, right and left sensor rfcomm
ser2 = serial.Serial('/dev/rfcomm0',9600,timeout =5)#object rfcomm
# Call espeak.synth() and wait for utterence to be finished.
# From https://answers.launchpad.net/python-espeak/+question/244655
def say(*args):
done_synth = [False]
def synth_callback(event, pos, length):
if event == espeak_core.event_MSG_TERMINATED:
done_synth[0] = True
espeak.set_SynthCallback(synth_callback)
call_result = espeak.synth(*args)
while call_result and not done_synth[0]:
time.sleep(0.05)
return call_result
say("Turning on Device")
time.sleep(2)
say("The Bat")
time.sleep(1)
say("Indoor navigation and obstacle detection")
# Openning DB connection
# Get user measurement settings
def convchk():
db = MySQLdb.connect("localhost","root","hearmeout","thebat")
# Preparing cursor object using cursor() method
cursor = db.cursor()
sql = "SELECT * FROM currentUser"
try:
#Execute SQL commands
cursor.execute(sql)
results = cursor.fetchall()
flagMeas = results[0][3]
except:
print "Error: Unable to fetch measurement"
db.close
del results
return flagMeas
flagMeas=convchk()
#############NAVIGATION FUNCTION###########################
# Store users current location in DB
def userLoc(sensorData):
print sensorData
lat = float(sensorData.split(" ")[7]) #current Latitude
lon = float(sensorData.split(" ")[8]) #current Longitude
print lat, lon
db = MySQLdb.connect("localhost","root","hearmeout","thebat")
query = "UPDATE currentUser SET Latitude=%f, Longitude=%f"%(lat,lon)
cursor = db.cursor()
try:
cursor.execute(query)
db.commit()
print "Success"
except:
db.rollback()
print "Failure"
db.close()
return;
###########################################################
###############INCHES to FEET function###################
def conv(feetDistance):
feetDistance = float(feetDistance) #make distance a float
feetDistance = feetDistance/12 #converting inches to feet
feetDistance = round(feetDistance,0) #rounding to 0 dec place
feetDistance = int(feetDistance) #Removing ".0" from output
return feetDistance;
#####################################################
###############Inches to Meters##################
def conv2meter(meterDistance):
meterDistance = float(meterDistance) #make distance a float
meterDistance = meterDistance/39.37 #converting to meter
meterDistance= round(meterDistance,1) #rounding to 0 dec place
print meterDistance
return meterDistance;
##############################################
#############Calling meters or feet#######
def callmeas(flagMeas,distanceforconv2):
if flagMeas == 1 : #meters
distance5= conv2meter(distanceforconv2)
return distance5;
elif flagMeas == 0 : #feet
distance6 = conv(distanceforconv2)
return distance6;
def flagmeascheck(flagMeas):
if flagMeas == 1 :
say("Meters")
elif flagMeas == 0 :
say("Feet")
return;
c1=0 #counter for timeout for headphone bluetooth
c2=0 #counter for timeout for belt bluetooth
while 1:
flagMeas=convchk()
try:
serialdata = ser.readline() # storing serial data
except:
say("Headphone Bluetooth is disconnected")
print "headphone Bluetooth is disconnected"
time.sleep(5)
c1=c1+1
if c1==5:
print "Time out on headphone bluetooth, device shutting down"
say("Device is shutting down check Headphone bluetooth and try again")
# os.system("sudo shutdown -h now")
exit()
continue
try:
serialdata2 = ser2.readline() #storing serial data 2
except:
say("Belt Sensor is disconnected")
print "Belt sensor is disconnected"
time.sleep(5)
c2=c2+1
if c2==5:
print "Time out on Belt bluetooth, device shutting down"
say("Device is shutting down check Belt bluetooth and try again")
# os.system("sudo shutdown -h now")
exit()
continue
tempabc = serialdata.split(" ") #length of serialdata
tempabcd = serialdata2.split(" ")
stringlenSerial2 = len(tempabcd)
stringlenSerial = len(tempabc) #setting to
if stringlenSerial == 10:
userLoc(serialdata);
if stringlenSerial != 9:
print "Wait line for headphones"
print serialdata
#time.sleep(2)
ser.flushInput()
ser2.flushInput()
continue
elif stringlenSerial2 !=2 :
print "Wait line for belt"
print serialdata2
#time.sleep(2)
ser.flushInput()
ser2.flushInput()
continue
else:
sensorL = serialdata.split(" ")[2] #Left Head Sensor
sensorR = serialdata.split(" ")[4] #Right Head Sensor
sensor = serialdata.split(" ")[0] #Head Sensor
sensorO = serialdata2.split(" ")[0] #Belt Sensor
gpscheck = serialdata.split(" ")[6] #Gps Sensor
#############OBJECT SENSOR##############################
distanceO = serialdata2.split(" ")[1]
distanceObject = conv(distanceO)
distance16 = callmeas(flagMeas,distanceO)
distance2 = str(distance16)
#############FRONT SENSOR DISTANCE###########################
distance = serialdata.split(" ")[1]
distanceFront = conv(distance)
distance15 = callmeas(flagMeas,distance)
distance1 = str(distance15)#converting distance to string for tts
############LEFT SENSOR DISTANCE#################################
distanceL = serialdata.split(" ")[3]
distanceL = float(distanceL) #make distance a float
#distanceL = distanceL/12 #converting inches to feet
############RIGHT SENSOR DISTANCE#############################
distanceR = serialdata.split(" ")[5]
distanceR = float(distanceR) #make distance a float
#distanceR = distanceR/12 #converting inches to feet
print serialdata #Print Sensor and Distance Data from Arduino
print serialdata2 #Printing object sensor data
if distanceFront < 10 and distanceFront > 0: #between 10-5ft
if wallElapsed == 0:
say("Wall in")
say(distance1)
flagmeascheck(flagMeas)
wallTimerStart = time.time()
wallElapsed = 1
else:
wallElapsed = time.time() - wallTimerStart
if wallElapsed > timerSeconds:
wallElapsed = 0
#####################OBJECT SENSOR##############################################################
if distanceObject < 8 and distanceObject > 0 : #between 8-5ft
if objectElapsed == 0:
say("Object in")
say(distance2)
flagmeascheck(flagMeas)
objectTimerStart = time.time()
objectElapsed = 1
else:
objectElapsed = time.time() - objectTimerStart
if objectElapsed > timerProximity:
objectElapsed = 0
if distanceL > 144 and distanceR > 144 :
if openingBothElapsed == 0:
say("Opening on the right and Left")
openingBothTimerStart = time.time()
openingBothElapsed = 1
else:
openingBothElapsed = time.time() - openingBothTimerStart
if openingBothElapsed > timerSeconds:
openingBothElapsed = 0
if distanceL > 144 and distanceR < 120 :
if openingLeftElapsed == 0:
say("Opening on the Left")
openingLeftTimerStart = time.time()
openingLeftElapsed = 1
else:
openingLeftElapsed = time.time() - openingLeftTimerStart
if openingLeftElapsed > timerSeconds:
openingLeftElapsed = 0
if distanceR > 144 and distanceL < 120 :
if openingRightElapsed == 0:
say("Opening on the Right")
openingRightTimerStart = time.time()
openingRightElapsed = 1
else:
openingRightElapsed = time.time() - openingRightTimerStart
if openingRightElapsed > timerSeconds:
openingRightElapsed = 0
elif distanceR < 15:
if rightProximityElapsed == 0:
say("Right wall proximity")
rightProximityTimerStart = time.time()
rightProximityElapsed = 1
else:
rightProximityElapsed = time.time() - rightProximityTimerStart
if rightProximityElapsed > timerProximity:
rightProximityElapsed = 0
elif distanceL < 15:
if leftProximityElapsed == 0:
say("Left wall proximity")
leftProximityTimerStart = time.time()
leftProximityElapsed = 1
else:
leftProximityElapsed = time.time() - leftProximityTimerStart
if leftProximityElapsed > timerProximity:
leftProximityElapsed = 0