-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_005_AgentTest.py
236 lines (189 loc) · 7.72 KB
/
_005_AgentTest.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
import thread
import datetime
##from serial import Serial
from ws4py.client.threadedclient import WebSocketClient
import time
import re
import socket
import sys
CRITICAL_BATTERY_LEVEL = 3.6
CRITICAL_ULTRASONIC = 15
BROADCAST_NETWORK = '192.168.1.255'
WEBSOCKET_SERVER_ENDPOINT = "ws://localhost:8080/Coordinator/websocketserver"
##-------------------- Battery Monitoring Service ---------------------------##
def monitorBatteries():
# serialPort = Serial("/dev/ttyAMA0", 9600, timeout=2)
# if (serialPort.isOpen() == False):
# serialPort.open()
#
# outStr = ''
# inStr = ''
#
# serialPort.flushInput()
# serialPort.flushOutput()
while True:
## response = serialPort.readline()
response = "3.81-----3.61"
# response = "3.2"
status = False
# if response < float(CRITICAL_BATTERY_LEVEL):
# print "critical"
# status = False
# else:
# status = True
# print "Not critical"
values = response.split("-----")
for value in values:
value = float(re.findall("\d+\.\d+", value)[0])
if value < float(CRITICAL_BATTERY_LEVEL):
status = True
else:
status = False
if status == True:
thread.interrupt_main()
print "Critical Battery Level"
break
# serialPort.close()
##-------------------- End of Battery Monitoring Service---------------------------##
##-------------------- Register to the Service via Coordinator---------------------##
def registerToService ():
addr = (BROADCAST_NETWORK, 33333) # broadcast address explicitly
UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Create socket
print "Starting the registering process"
data1 = "register"
# Almost infinite loop... ;)
while True:
if UDPSock.sendto(data1, addr):
print "Sending message '%s'..." % data1
try:
addr1 = ('', 33334)
UDPSock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
UDPSock2.bind(addr1)
UDPSock2.settimeout(10)
data, addr1 = UDPSock2.recvfrom(1024)
UDPSock2.close()
print "Server IP is %s" %(data)
data1 = data
UDPSock.close()
break
except:
print "Time out exception"
UDPSock.close() # Close socket
print 'Client stopped.'
return data1;
##-------------------- End of Register to the Service ------------------------------##
##-------------------- WebSocket Connection ----------------------------------------##
class DummyClient(WebSocketClient):
def opened(self):
print "Connection Established"
def closed(self, code, reason=None):
print "Connection Closed down", code, reason
def received_message(self, m):
print m
##-------------------- End of WebSocket Connection ---------------------------------##
##-------------------- Start of Ultrasonic Method---------------------------------##
def ultrasonicDistance():
print "-----ultrasonicDistance-----"
while True:
## response = serialPort.readline()
# //////call ULTRASONIC method here **********************************************************
response = 20
status = False
# print response
if response < float(CRITICAL_ULTRASONIC):
status = True
else:
status = False
if status == True:
# thread.interrupt_main()
# //////call Obstacle avoidance here **********************************************************
# check current location
# reconfiguration of the path
print "Obstacle Detected"
break
##-------------------- End of Ultrasonic Method---------------------------------##
def serverNavigation_IP():
from random import randint
while True:
number= (randint(0,100))
time.sleep(0.1)
status = False
print "Random:", number
if number == 10 or number == 2: # found
print "Stop"
status= True
thread.interrupt_main()
# from TurtleGUI_TSP import stopAgent as stopAgent
# stopAgent()
break
else:
status= False
# print "Not Found"
def main():
Rmap_grid= [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0,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
# [0, 2, 0, 0, 1, 0, 0, 1],
# [1, 1, 1, 0, 1, 0, 0, 0],
# [0, 0, 0, 0, 1, 0, 0, 0],
# [0, 0, 0, 0, 1, 0, 2, 0],
# [0, 2, 0, 0, 1, 0, 0, 0]]
try:
######### Start Battery Monitoring Thread #####
thread.start_new_thread(monitorBatteries, ())
# thread.start_new_thread(serverNavigation_IP, ())
f_degree=1
b_degree=2
r_degree=3
l_degree=4
# assignedArea=[[],[],[],[],[],[],[],[],]
# assignedArea=[[0, 0], [1, 7], [2, 12], [4, 14], [5, 1], [9, 0], [9, 8]]
assignedArea=[[0, 0], [9, 0], [5, 1], [1, 7], [9, 8], [2, 12],[4, 14]]
######### Getting key points path #############
from TSP import main as main_from_TSP
# main_from_TSP(Rmap_grid, f_degree, b_degree, r_degree, l_degree)
main_from_TSP(Rmap_grid, f_degree, b_degree, r_degree, l_degree,assignedArea)
######### End of getting key points path #############
######### Obstacle avoidance #############
thread.start_new_thread(ultrasonicDistance, ())
######### End Obstacle avoidance #############
######### Register to the Service #############
## serverIp = registerToService()
## print serverIp
######### Create Web Socket Connection ########
# ws = DummyClient(WEBSOCKET_SERVER_ENDPOINT)
# ws.connect()
######### Ask for other Agents in System #######
# ws.send("Test")
##-------------------------------------- synchronization error recovery methods
######### Wait for map #########################
except KeyboardInterrupt:
print "Exception in Agent Test"
# from TurtleGUI_TSP import stopAgent as stopAgent
# stopAgent()
# ws.close()
print "App closed"
if __name__ == "__main__":
main()