-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.py
167 lines (129 loc) · 6.61 KB
/
alert.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
import threading
import json
import smokesignal
import Queue
import datetime
from easyNav_pi_dispatcher import DispatcherClient
class Alert(object):
""" This is the Nav class, which handles navigation on the Raspberry Pi.
It retrieves remote location information and navigates to the point. In addition,
this class implements the REST API endpoints from the server.
"""
HOST_ADDR = "http://localhost:1337"
THRESHOLD_DIST = 50
THRESHOLD_ANGLE = 5 * 0.0174532925
## Run Levels here
RUNLVL_NORMAL = 0
RUNLVL_WARNING_OBSTACLE = 1
def __init__(self):
## For interprocess comms
self.DISPATCHER_PORT = 9004
self._dispatcherClient = DispatcherClient(port=self.DISPATCHER_PORT)
## Attach event listeners upon instantiation (to prevent duplicates)
self.attachEvents()
self.sendflag=1
self.state=1 #1-proceed #2-stop #3 step right #4 step left
self.sendAlertInterval = datetime.datetime(seconds=5)
self.triggerAlertime=datetime.datetime(seconds=0)
def start(self):
""" Start the daemon and run persistently. Auto-retrieves new map in starting.
"""
## Start inter-process comms
self._dispatcherClient.start()
def attachEvents(self):
"""Configure event callbacks to attach to daemon on start.
All events must be a series of event functions.
1:345 2:60
Do not call this externally!
"""
## clear all signals
smokesignal.clear()
@smokesignal.on('sonarData')
def sonardata(args):
threshold=70
if(currentsoundtime-triggersoundtime>self.sendAlertInterval):
self.sendflag=1
else
self.sendflag=0
#1-proceed #2-stop #3 step right #4 step left
if(self.state ==1): #proceed state
payload =eval(args.get("payload"))
leftvalue= payload["1"]
rightvalue=payload["2"]
leftvalue=int(leftvalue.rstrip('\u0'))
rightvalue=int(rightvalue.rstrip('\u0'))
if((leftvalue <= threshold) OR(rightvalue <= threshold)):
self.state=2 #stop state
currentsoundtime=datetime.datetime.now().time()
if(self.sendflag==1):
self._dispatcherClient.send(9002, 'say', {'text': 'stop'})
triggersoundtime=datetime.datetime.now().time() # when it sends
else
self.state=1 # proceed state
self._dispatcherClient.send(9002, 'say', {'text': 'proceed'})
triggersoundtime=datetime.datetime.now().time() # when it sends
else if (self.state ==2): #stop state
if((leftvalue <= threshold) AND(rightvalue >= threshold)):
self.state=3 #go to step right state
currentsoundtime=datetime.datetime.now().time()
if(self.sendflag==1):
self._dispatcherClient.send(9002, 'say', {'text': 'keep right'})
triggersoundtime=datetime.datetime.now().time() # when it sends
else if((leftvalue >= threshold) AND(rightvalue <= threshold)):
self.state=4 #go to step left state
currentsoundtime=datetime.datetime.now().time()
if(self.sendflag==1):
self._dispatcherClient.send(9002, 'say', {'text': 'keep left'})
triggersoundtime=datetime.datetime.now().time() # when it sends
else
self.state =2 #go to stop state
payload =eval(args.get("payload"))
leftvalue= payload["1"]
rightvalue=payload["2"]
leftvalue=int(leftvalue.rstrip('\u0'))
rightvalue=int(rightvalue.rstrip('\u0'))
else if (self.state ==3):#keep right
payload =eval(args.get("payload"))
leftvalue= payload["1"]
rightvalue=payload["2"]
leftvalue=int(leftvalue.rstrip('\u0'))
rightvalue=int(rightvalue.rstrip('\u0'))
if((leftvalue >= threshold) OR(rightvalue >= threshold)):
self.state=1; #go to proceed state
currentsoundtime=datetime.datetime.now().time()
if(self.sendflag==1):
self._dispatcherClient.send(9002, 'say', {'text': 'proceed'})
triggersoundtime=datetime.datetime.now().time() # when it sends
else
self.state=2 #go to stop state
payload =eval(args.get("payload"))
leftvalue= payload["1"]
rightvalue=payload["2"]
leftvalue=int(leftvalue.rstrip('\u0'))
rightvalue=int(rightvalue.rstrip('\u0'))
else if (self.state == 4):#keep left state
payload =eval(args.get("payload"))
leftvalue= payload["1"]
rightvalue=payload["2"]
leftvalue=int(leftvalue.rstrip('\u0'))
rightvalue=int(rightvalue.rstrip('\u0'))
if((leftvalue >= threshold) OR(rightvalue >= threshold)):
self.state=1; #go to proceed state
currentsoundtime=datetime.datetime.now().time()
if(self.sendflag==1):
self._dispatcherClient.send(9002, 'say', {'text': 'proceed'})
triggersoundtime=datetime.datetime.now().time() # when it sends
else
self.state=2; #go to stop state
payload =eval(args.get("payload"))
leftvalue= payload["1"]
rightvalue=payload["2"]
leftvalue=int(leftvalue.rstrip('\u0'))
rightvalue=int(rightvalue.rstrip('\u0'))
def runMain():
""" Main function called when run as standalone daemon
"""
alert = Alert()
alert.start()
if __name__ == '__main__':
runMain()