-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping_mode.py
344 lines (273 loc) · 12.5 KB
/
mapping_mode.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
##########################################################################
# Author: Griffin Della Grotte ([email protected])
#
# This module creates the UI for creating AP data maps and testing out
# localization against said maps.
##########################################################################
from map_pygame_structure import *
from functools import reduce
import mongodb_databases
from mapcmu_data import *
import ScannerUtils, StatModelForAPs
from ast import literal_eval
import datetime
import math
class MappingMode(MapPygameMode):
def __init__(self, changeModeFn=None):
MapPygameMode.__init__(self, bkcolor=(255,255,255), screenDims=(1000,800),
changeModeFn=changeModeFn)
self.initData()
self.recording = False
self.recStart = ""
self.finding = False
self.resultTick = 0
self.possiblePos = []
self.currentPos = None
# Set up the AP data map tables
self.mapByLoc = {}
self.db = mongodb_databases.MDBDatabase(Constants.database)
self.mapTable = mongodb_databases.MapTable(self.db.database)
self.initFromDatabase()
self.pointPopup.setTable(self.mapTable)
self.mapPoints.setTable(self.mapTable)
self.scanner = ScannerUtils.APScanner(trigFunc=self.scanResult)
self.scanner.daemon=True
self.scanner.start()
self.statmodel = StatModelForAPs.ProbabilisticMap(
self.scanner, self.mapTable)
def initData(self):
self.mapPoints = self.MapPointHandler(mToV=self.modelToView,
vToM=self.viewToModel)
self.mainSurf.objects.append(self.mapPoints)
self.pointPopup = self.MapPopup()
self.mainSurf.objects.append(self.pointPopup)
self.locPoint = self.FoundPoint(mToV=self.modelToView,
vToM=self.viewToModel)
self.locPoint.floorChange(self.zPos)
self.mainSurf.objects.append(self.locPoint)
def initFromDatabase(self):
self.regenerateMapPoints()
def scanResult(self, result):
# Results are stores with a repr of a tuple location
if self.recording:
self.resultTick += 1
for apRow in result:
location = "(%d, %d, %d)"%(self.arrowOffset[0],
self.arrowOffset[1], self.zPos)
self.statmodel.addResultRow(location, apRow["BSSID"],
apRow["RSSI"], self.recStart)
self.statmodel.regenerateModel()
self.regenerateMapPoints()
elif self.finding:
self.resultTick += 1
posResult = self.statmodel.findLocation(result)
if posResult != None:
self.possiblePos.append(literal_eval(self.statmodel.findLocation(result)))
# Do results reduction for testing out localization
count = len(self.possiblePos)
self.currentPos = reduce(lambda x,y: (
x[0]+y[0], x[1]+y[1], x[2]+y[2]),
self.possiblePos, (0,0,0))
self.currentPos = (self.currentPos[0] / count,
self.currentPos[1] / count,
self.coerceZPos(self.currentPos[2] / count))
self.locPoint.updatePos(self.currentPos)
def coerceZPos(self, z):
maxFloor = Constants.buildings[self.selBuilding].maxFloor
minFloor = Constants.buildings[self.selBuilding].minFloor
for floor in range(minFloor, maxFloor+1):
zPos = (floor-1) * Constants.floorHeight
zRange = (zPos - Constants.floorHeight/2,
zPos + Constants.floorHeight/2)
if z > zRange[0] and z <= zRange[1]:
return zPos
print("Out of range!")
def regenerateMapPoints(self):
self.mapPoints.objects = []
possibleLocs = self.mapTable.collection.distinct("Location")
# Get all distinct locations entered in
for loc in possibleLocs:
locT = literal_eval(loc)
if locT[2] != self.zPos: continue
self.mapPoints.addPoint(locT)
#######################################
class MapPoint(PygameObject):
# Green (or blue-selected) point for showing where data has been taken
def initData(self):
self.radius = 4
self.color = (0,255,0)
def draw(self, surface, dims, offset=(0,0)):
super().draw(surface, dims, offset)
coords = self.mToV(self.pos, surface.surf)
pygame.draw.circle(surface.surf, self.color, coords, self.radius)
def mousePressed(self, surface, x, y):
super().mousePressed(surface, x, y)
xr, yr = self.vToM((x,y), surface.surf)
distance = math.sqrt((xr - self.pos[0])**2 + \
(yr - self.pos[1])**2)
if distance < self.radius: # Clicked this point
return True
return False
class FoundPoint(PygameObject):
# Purple triangle that showns the user's current location during test
def initData(self):
self.radius = 6 # Size for triangle
self.color = (255,0,255)
self.enabled = False
self.zPos = None
self.pos = (0,0,0)
def updatePos(self, pos):
if pos != None:
self.enabled = True
self.pos = pos
def floorChange(self, zPos):
self.zPos = zPos
def draw(self, surface, dims, offset=(0,0)):
if not self.enabled: return
if self.pos[2] != self.zPos: return
coords = self.mToV(self.pos, surface.surf)
self.drawTriangleAtPoint(surface.surf, coords, self.radius)
def drawTriangleAtPoint(self, surface, pos, radius):
a_angle,b_angle,c_angle = 0,math.radians(120),math.radians(240)
pointA = (pos[0] - radius*math.sin(a_angle),
pos[1] - radius*math.cos(a_angle))
pointB = (pos[0] - radius*math.sin(b_angle),
pos[1] - radius*math.cos(b_angle))
pointC = (pos[0] - radius*math.sin(c_angle),
pos[1] - radius*math.cos(c_angle))
verticies = (pointA, pointB, pointC)
pygame.draw.polygon(surface, self.color, verticies)
class MapPointHandler(PygameObject):
# Hold all the map points and handle single-selection of them
def setTable(self, table):
self.table = table
def initData(self):
self.selectedPoint = None
def selectDeletion(self, num):
if self.selectedPoint != None:
loc = str(self.selectedPoint.pos)
stamps = self.table.collection.distinct("TIME", {"Location" : loc})
tS = stamps[num]
self.table.collection.remove({"Location" : loc, "TIME" : tS})
def addPoint(self, point):
self.objects.append(MappingMode.MapPoint(point, self.mToV, self.vToM))
def mousePressed(self, surface, x, y):
found = None
for obj in self.objects:
result = obj.mousePressed(surface, x, y)
if result:
found = obj
obj.color = (0,0,255)
else:
obj.color = (0,255,0)
self.selectedPoint = found
class MapPopup(PygameObject):
# Box that displays on the upper right with the dates when
# data was taken for that point (shows index numbers for deletion)
def setTable(self, table):
self.table = table
def initData(self):
self.width = 220
self.point = None
def draw(self, surface, dims, offset=(0,0)):
if self.point != None:
ptStr = str(self.point.pos)
stamps = self.table.collection.distinct("TIME", {"Location" : ptStr})
self.pos = (dims[0] - self.width, 0)
margin, size = 2, 15
height = max(len(stamps) * (2*margin+size),15)
pygame.draw.rect(surface.surf, Constants.backdrop, self.pos + (self.width, height), 0)
pygame.draw.rect(surface.surf, (0,0,0), self.pos + (self.width, height), 1)
dfont = pygame.font.SysFont("monospace", size)
for i in range(len(stamps)):
tS = stamps[i]
label = dfont.render(
str(i) + ": " + tS,
1, (10,10,10))
surface.surf.blit(label,
(self.pos[0]+margin,
self.pos[1]+margin + i*(2*margin+size)))
def drawCursor(self):
mid = (self.screenDims[0]//2, self.screenDims[1]//2)
cursorSize = min(self.screenDims[0]//15, self.screenDims[1]//15)
cursorVert = (mid[1] - cursorSize//2, mid[1] + cursorSize//2)
cursorHorz = (mid[0] - cursorSize//2, mid[0] + cursorSize//2)
pygame.draw.line(self.mainSurf.surf, (255,0,0), # Vertical Line
(mid[0], cursorVert[1]), (mid[0], cursorVert[0]))
pygame.draw.line(self.mainSurf.surf, (255,0,0), # Horizontal Line
(cursorHorz[1], mid[1]), (cursorHorz[0], mid[1]))
def drawView(self, screen):
self.drawMap() # Draws the map image
self.drawCursor() # Draws cursor in screen center
self.pointPopup.point = self.mapPoints.selectedPoint
self.mainSurf.drawObjects(offset=tuple(self.arrowOffset))
self.drawCornerMsg()
self.drawFooterHelp()
dfont = pygame.font.SysFont("monospace", 15)
label = dfont.render(
"Position (x,y): (%d,%d)" % (self.arrowOffset[0], self.arrowOffset[1]),
1, (10,10,10))
self.mainSurf.surf.blit(label, (5, 40))
if (self.recording or self.finding):
label = dfont.render(
"RECORDING (%d)" % self.resultTick if self.recording \
else "FINDING (%d): %s" % (self.resultTick,self.currentPos),
1, (255,0,0))
self.mainSurf.surf.blit(label, (5, 60))
if self.showHelp:
self.drawHelpOverlay(Constants.helpAPMap)
super().drawView(screen)
#######################################
def mousePressed(self, event):
self.mainSurf.mouseObjects(event, self.arrowOffset)
def keyPressed(self, event):
super().keyPressed(event)
keyPadNums = [pygame.K_KP0, pygame.K_KP1, pygame.K_KP2,
pygame.K_KP3, pygame.K_KP4, pygame.K_KP5,
pygame.K_KP6, pygame.K_KP7, pygame.K_KP8,
pygame.K_KP9]
mult = .1 if pygame.key.get_mods() & pygame.KMOD_SHIFT else 1
ctrl = pygame.key.get_mods() & pygame.KMOD_CTRL
if event.key == pygame.K_UP:
self.arrowOffset[1] -= int(100 * mult)
elif event.key == pygame.K_DOWN:
self.arrowOffset[1] += int(100 * mult)
elif event.key == pygame.K_LEFT:
self.arrowOffset[0] -= int(100 * mult)
elif event.key == pygame.K_RIGHT:
self.arrowOffset[0] += int(100 * mult)
elif event.key == pygame.K_PAGEUP:
self.upAFloor()
self.regenerateMapPoints()
self.locPoint.floorChange(self.zPos)
elif event.key == pygame.K_PAGEDOWN:
self.downAFloor()
self.regenerateMapPoints()
self.locPoint.floorChange(self.zPos)
elif event.key == pygame.K_h:
self.showHelp = not self.showHelp
elif event.key == pygame.K_r:
self.resultTick = 0
self.recording = not self.recording
self.recStart = (
'{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
self.finding = False
self.locPoint.enabled = False
self.possiblePos = []
self.currentPos = None
elif event.key == pygame.K_p:
self.resultTick = 0
self.finding = not self.finding
self.locPoint.enabled = not self.locPoint.enabled
self.recording = False
self.possiblePos = []
self.currentPos = None
elif event.key == pygame.K_ESCAPE:
self.changeModeFn()
elif event.key in keyPadNums:
num = keyPadNums.index(event.key)
self.handleKeypad(event, num)
def handleKeypad(self, event, num):
if pygame.key.get_mods() & pygame.KMOD_SHIFT:
self.mapPoints.selectDeletion(num)
self.regenerateMapPoints()