-
Notifications
You must be signed in to change notification settings - Fork 3
/
Orchestrator.py
374 lines (290 loc) · 12.9 KB
/
Orchestrator.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
# built-in
import random
import math
import threading
import copy
import time
# third-party
# local
import SimEngine
import Wireless
import Utils as u
class ExceptionOpenLoop(Exception):
pass
class MapBuilder(object):
'''
A background task which consolidates the map.
It combines dots into lines
It declares when the map is complete.
'''
PERIOD = 1 # s, in simulated time
MINFEATURESIZE = 1 # shortest wall, narrowest opening
def __init__(self):
# store params
# local variables
self.simEngine = SimEngine.SimEngine()
self.dataLock = threading.RLock()
self.discoMap = {
'complete': False, # is the map complete?
'dots': [], # each bump becomes a dot
'lines': [], # closeby dots are aggregated into a line
}
# schedule first housekeeping activity
self.simEngine.schedule(self.simEngine.currentTime()+self.PERIOD,self._houseKeeping)
#======================== public ==========================================
def notifBump(self,x,y):
with self.dataLock:
self.discoMap['dots'] += [(x,y)]
def getMap(self):
with self.dataLock:
return copy.deepcopy(self.discoMap)
#======================== private =========================================
def _houseKeeping(self):
with self.dataLock:
# consolidate map
self._consolidateMap()
# decide whether map completed
self.discoMap['complete'] = self._isMapComplete()
# schedule next consolidation activity
self.simEngine.schedule(self.simEngine.currentTime()+self.PERIOD,self._houseKeeping)
def _consolidateMap(self):
# result list of lines
reslines = []
# remove duplicate dots
self.discoMap['dots'] = list(set(self.discoMap['dots']))
# horizontal
for direction in ['horizontal','vertical']:
refs = []
if direction=='horizontal':
refs += [y for (x,y) in self.discoMap['dots']] # all dots
refs += [lay for (lax,lay,lbx,lby) in self.discoMap['lines'] if lay==lby ] # all horizontal lines
else:
refs += [x for (x,y) in self.discoMap['dots']] # all dots
refs += [lax for (lax,lay,lbx,lby) in self.discoMap['lines'] if lax==lbx ] # all vertical lines
refs = set(refs)
for ref in refs:
# select all the dots which are aligned at this ref
if direction=='horizontal':
thesedots = [x for (x,y) in self.discoMap['dots'] if y==ref]
else:
thesedots = [y for (x,y) in self.discoMap['dots'] if x==ref]
# select the lines we already know of at this ref
if direction=='horizontal':
theselines = [(lax,lay,lbx,lby) for (lax,lay,lbx,lby) in self.discoMap['lines'] if lay==ref and lby==ref]
else:
theselines = [(lax,lay,lbx,lby) for (lax,lay,lbx,lby) in self.discoMap['lines'] if lax==ref and lbx==ref]
# remove dots which fall inside a line
if direction=='horizontal':
thesedots = [x for (x,y) in self._removeDotsOnLines([(x,ref) for x in thesedots] ,theselines)]
else:
thesedots = [y for (x,y) in self._removeDotsOnLines([(ref,y) for y in thesedots] ,theselines)]
# add vertices of all lines to the dots
for (lax,lay,lbx,lby) in theselines:
if direction=='horizontal':
thesedots += [lax]
thesedots += [lbx]
else:
thesedots += [lay]
thesedots += [lby]
# remove duplicates (in case dot falls on vertice of existing line)
thesedots = list(set(thesedots))
# sort dots by increasing value
thesedots = sorted(thesedots)
# create line between close dots
for (idx,v) in enumerate(thesedots):
if idx==len(thesedots)-1:
continue
vnext = thesedots[idx+1]
if vnext-v<=self.MINFEATURESIZE:
if direction=='horizontal':
theselines += [(v,ref,vnext,ref)]
else:
theselines += [(ref,v,ref,vnext)]
# remove line duplicates (caused by short lines which turn into close points)
theselines = list(set(theselines))
# join the lines that touch
if direction=='horizontal':
theselines = sorted(theselines,key = lambda l: l[0])
else:
theselines = sorted(theselines,key = lambda l: l[1])
idx = 0
while idx<len(theselines)-1:
(lax,lay,lbx,lby) = theselines[idx]
(nax,nay,nbx,nby) = theselines[idx+1]
if direction=='horizontal':
condition = (lbx==nax)
else:
condition = (lby==nay)
if condition:
theselines[idx] = (lax,lay,nbx,nby)
theselines.pop(idx+1)
else:
idx += 1
# store
reslines += theselines
# store
self.discoMap['lines'] = reslines
# remove duplicate dots
self.discoMap['dots'] = list(set(self.discoMap['dots']))
# remove dots which fall inside a line
self.discoMap['dots'] = self._removeDotsOnLines(self.discoMap['dots'],self.discoMap['lines'])
def _removeDotsOnLines(self,dots,lines):
idx = 0
while idx<len(dots):
(dx,dy) = dots[idx]
removed = False
for (lax,lay,lbx,lby) in lines:
if lay==lby and lay==dy:
# horizontal line, co-linear to point
condition = lax<=dx and dx<=lbx
elif lax==lbx and lax==dx:
# vertical line, co-linear to point
condition = lay<=dy and dy<=lby
else:
# not co-linear to point
condition = False
if condition:
dots.pop(idx)
removed = True
break
if removed==False:
idx += 1
return dots
def _isMapComplete(self):
while True: # "loop" only once
# map is never complete if there are dots remaining
if self.discoMap['dots']:
returnVal = False
break
# keep looping until no more todo lines
alllines = copy.deepcopy(self.discoMap['lines'])
try:
while alllines:
loop = self._walkloop(alllines,alllines[0])
for line in loop:
alllines.remove(line)
except ExceptionOpenLoop:
returnVal = False
break
# if I get here, map is complete
returnVal = True
break
return returnVal
def _walkloop(self,alllines,startline):
loop = []
loop += [startline]
while True:
# add close line to loop
foundCloseLine = False
for line in alllines:
if (self._areLinesClose(loop[-1],line)) and (line not in loop):
foundCloseLine = True
loop += [line]
break
# abort if no next line to hop to
if foundCloseLine==False:
raise ExceptionOpenLoop()
# success! last line in loop is close to first line
if len(loop)>2 and self._areLinesClose(loop[-1],loop[0]):
return loop
def _areLinesClose(self,line1,line2):
(l1ax,l1ay,l1bx,l1by) = line1
(l2ax,l2ay,l2bx,l2by) = line2
returnVal = False
while True: # "loop" only once
if u.distance((l1ax,l1ay),(l2ax,l2ay))<=self.MINFEATURESIZE:
returnVal = True
break
if u.distance((l1ax,l1ay),(l2bx,l2by))<=self.MINFEATURESIZE:
returnVal = True
break
if u.distance((l1bx,l1by),(l2ax,l2ay))<=self.MINFEATURESIZE:
returnVal = True
break
if u.distance((l1bx,l1by),(l2bx,l2by))<=self.MINFEATURESIZE:
returnVal = True
break
break
return returnVal
class Orchestrator(object):
'''
The central orchestrator of the expedition.
'''
def __init__(self,positions,floorplan):
# store params
self.positions = positions
self.floorplan = floorplan
# local variables
self.simEngine = SimEngine.SimEngine()
self.wireless = Wireless.Wireless()
self.dotbotsview = [ # the Orchestrator's internal view of the DotBots
{
'x': x,
'y': y,
'posTs': 0,
'heading': 0,
'speed': 0,
'commandId': 0,
} for (x,y) in self.positions
]
self.mapBuilder = MapBuilder()
#======================== public ==========================================
def startExploration(self):
'''
Simulation engine, start exploring
'''
for dotbot in self.dotbotsview:
dotbot['heading'] = random.randint(0,359)
dotbot['speed'] = 1
self._sendDownstreamCommands()
def fromDotBot(self,msg):
'''
A DotBot indicates its bump sensor was activated at a certain time
'''
# shorthand
dotbot = self.dotbotsview[msg['dotBotId']]
# compute new theoretical position
dotbot['x'] += (msg['bumpTs']-dotbot['posTs'])*math.cos(math.radians(dotbot['heading']-90))*dotbot['speed']
dotbot['y'] += (msg['bumpTs']-dotbot['posTs'])*math.sin(math.radians(dotbot['heading']-90))*dotbot['speed']
dotbot['posTs'] = msg['bumpTs']
# round
dotbot['x'] = round(dotbot['x'],3)
dotbot['y'] = round(dotbot['y'],3)
# notify the self.mapBuilder the obstacle location
self.mapBuilder.notifBump(dotbot['x'],dotbot['y'])
# adjust the heading of the DotBot which bumped (avoid immediately bumping into the same wall)
dotbot['heading'] = random.randint( 0,359)
# set the DotBot's speed
dotbot['speed'] = 1
# bump command Id so DotBot knows this is not a duplicate command
dotbot['commandId'] += 1
# send commands to the robots
self._sendDownstreamCommands()
def getView(self):
# do NOT write back any results to the DotBot's state as race condition possible
# compute updated position
now = self.simEngine.currentTime() # shorthand
return {
'dotbots': [
{
'x': db['x']+(now-db['posTs'])*math.cos(math.radians(db['heading']-90))*db['speed'],
'y': db['y']+(now-db['posTs'])*math.sin(math.radians(db['heading']-90))*db['speed'],
} for db in self.dotbotsview
],
'discomap': self.mapBuilder.getMap(),
}
#======================== private =========================================
def _sendDownstreamCommands(self):
'''
Send the next heading and speed commands to the robots
'''
# format msg
msg = [
{
'commandId': dotbot['commandId'],
'heading': dotbot['heading'],
'speed': dotbot['speed'],
} for dotbot in self.dotbotsview
]
# hand over to wireless
self.wireless.toDotBots(msg)