-
Notifications
You must be signed in to change notification settings - Fork 1
/
place_rec.py
375 lines (280 loc) · 11.3 KB
/
place_rec.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
#!/usr/bin/env python
# By Jacek Zienkiewicz and Andrew Davison, Imperial College London, 2014
# Based on original C code by Adrien Angeli, 2009
import random
import os
import time
# More init #
from particleDataStructures import *
can = Canvas()
mymap = Map(can);
particle = Particles(can)
mymap.add_wall((0,0,0,168)); # a
mymap.add_wall((0,168,84,168)); # b
mymap.add_wall((84,126,84,210)); # c
mymap.add_wall((84,210,168,210)); # d
mymap.add_wall((168,210,168,84)); # e
mymap.add_wall((168,84,210,84)); # f
mymap.add_wall((210,84,210,0)); # g
mymap.add_wall((210,0,0,0)); # h
mymap.draw();
time.sleep(3)
# End init#
PI = 3.1416
NO_BINS = 72
NO_BINS_HIST = 100
MAX_MSRT = 250 # the ma# the maximum length up to which sonar measurements are reliable
# Location signature class: stores a signature characterizing one location
class LocationSignature:
def __init__(self, no_bins = NO_BINS): # s: changed from 360 to 72
self.sig = [0] * no_bins
def print_signature(self):
for i in range(len(self.sig)):
print self.sig[i]
# --------------------- File management class ---------------
class SignatureContainer():
def __init__(self, size = 5):
self.no_bins = NO_BINS;
self.size = size; # max number of signatures that can be stored
self.filenames = [];
# Fills the filenames variable with names like loc_%%.dat
# where %% are 2 digits (00, 01, 02...) indicating the location number.
for i in range(self.size):
self.filenames.append('loc_{0:02d}.dat'.format(i))
# Get the index of a filename for the new signature. If all filenames are
# used, it returns -1;
def get_free_index(self):
n = 0
while n < self.size:
if (os.path.isfile(self.filenames[n]) == False):
break
n += 1
if (n >= self.size):
return -1;
else:
return n;
# Delete all loc_%%.dat files
def delete_loc_files(self):
print "STATUS: All signature files removed."
for n in range(self.size):
if os.path.isfile(self.filenames[n]):
os.remove(self.filenames[n])
# Writes the signature to the file identified by index (e.g, if index is 1
# it will be file loc_01.dat). If file already exists, it will be replaced.
def save(self, signature, index):
filename = self.filenames[index]
if os.path.isfile(filename):
os.remove(filename)
f = open(filename, 'w')
for i in range(len(signature.sig)):
s = str(signature.sig[i]) + "\n"
f.write(s)
f.close();
# Read signature file identified by index. If the file doesn't exist
# it returns an empty signature.
def read(self, index):
ls = LocationSignature()
filename = self.filenames[index]
if os.path.isfile(filename):
f = open(filename, 'r')
for i in range(len(ls.sig)):
s = f.readline()
if (s != ''):
ls.sig[i] = int(s)
f.close();
else:
print "WARNING: Signature does not exist."
return ls
####################################################################################
# s: DONE (spins sonar to capture a signature and store it in ls)
def characterize_location():
# print "TODO: You should implement the function that captures a signature."
rotation = 360.0/NO_BINS
new_sig = []
# for each of the NO_BINS bins
for measure in range(NO_BINS):
print 'making measurement'
# make measurement
depth = controller.getSonarReading()
#time.sleep(1)
# rotate sonar
controller.turnSonar(rotation)
#time.sleep(1)
# save measurement
new_sig.append(depth)
# copy this to a signature object
ls = LocationSignature()
ls.sig = new_sig
# get the sonar back to original position
controller.turnSonar(-NO_BINS*rotation) #-rotations*NO_BINS)
#print the signature
ls.print_signature()
# return the newly created signature
return ls
# s: DONE
def compare_signatures(ls1, ls2):
dist = 0
print "Comparing."
for i in range(len(ls1.sig)):
dist += (ls1.sig[i]-ls2.sig[i])**2
return dist
# This function characterizes the current location, and stores the obtained
# signature into the next available file.
def learn_location():
print 'learning'
ls = characterize_location()
idx = signatures.get_free_index();
if (idx == -1): # run out of signature files
print "\nWARNING:"
print "No signature file is available. NOTHING NEW will be learned and stored."
print "Please remove some loc_%%.dat files.\n"
return
signatures.save(ls,idx)
print "STATUS: Location " + str(idx) + " learned and saved."
# s: DONE
# This function tries to recognize the current location.
# 1. Characterize current location
# 2. For every learned locations
# 2.1. Read signature of learned location from file
# 2.2. Compare signature to signature coming from actual characterization
# 3. Retain the learned location whose minimum distance with
# actual characterization is the smallest.
# 4. Display the index of the recognized location on the screen
def recognize_location():
ls_obs = characterize_location();
idx_most_similar = -1
smallest_dist = 10000000
for idx in range(signatures.size):
print 'STATUS: Comparing signature ' + str(idx) + ' with the observed signature.'
ls_read = signatures.read(idx);
dist = compare_signatures(ls_obs, ls_read)
if (dist < smallest_dist):
idx_most_similar = idx
if (idx_most_similar == -1):
print 'Problem in the recognizeLocation()'
else:
print 'Most similar location found is: ' + idx_most_similar
####### >> Part 4.3 (Invariant measurement & recognition) : #######
# This function characterizes the current location, and stores the obtained
# HISTOGRAM into the next available file.
def learn_location_invariant():
print 'learning'
ls = characterize_location_invariant()
idx = histograms.get_free_index();
if (idx == -1): # run out of signature files
print "\nWARNING:"
print "No signature file is available. NOTHING NEW will be learned and stored."
print "Please remove some loc_%%.dat files.\n"
return
histograms.save(ls,idx)
print "STATUS: Location " + str(idx) + " learned and saved."
#s: DONE
def characterize_location_invariant():
rotation = 360.0/NO_BINS
new_histogram = [0] * (NO_BINS_HIST+1)
# for each of the NO_BINS bins
for measure in range(NO_BINS):
# make measurement
depth = controller.getSonarReading()
print 'making measurement'
# rotate sonar
controller.turnSonar(rotation)
# save measurement in the appropriate bin of the depth histogramm
if (depth < MAX_MSRT):
index = int(depth/(MAX_MSRT / NO_BINS_HIST))
if (index > NO_BINS_HIST): index = NO_BINS_HIST
new_histogram[index] += 1
# get the sonar back to original position
controller.turnSonar(-NO_BINS*rotation) #-rotations*NO_BINS)
# copy the histogram to a signature object
ls = LocationSignature(NO_BINS_HIST)
ls.sig = new_histogram
# return the newly created signature (histogram)
return ls
# s: NOT COMPLETE (SEE CAPITALIZED BIT IN PART 4)
# This function tries to recognize the current location.
# 1. Characterize current location: histogram AND signature
# 2. For every learned locations
# 2.1. Read histogram of learned location from file, compare with measured histogram
# 2.2. Compare signature to signature coming from actual characterization in order to determine angle of rotation
# 4. Display the index of the recognized location on the screen AND THE ROTATION ANGLE
def recognize_location_invariant():
# 1. Characterize current location: histogram AND signature
sig_obs = characterize_location();
hist_obs = characterize_location_invariant();
idx_most_similar = -1
smallest_dist = 10000000
# 2. For every learned locations
for idx in range(signatures.size):
# 2.1. Read histogram of learned location from file, compare with measured histogram
hist_read = histograms.read(idx);
dist = compare_signatures(hist_read, hist_obs)
if (dist < smallest_dist):
idx_most_similar = idx
if (idx_most_similar == -1):
print 'Problem in the recognizeLocationInvariant()'
else:
print 'Most similar location found is: ' + idx_most_similar + '. Now getting the angle.'
ref_sig = signatures.read(idx_most_similar)
angle = getAngle(sig_obs, ref_sig)
return [idx_most_popular, angle]
# given two signatures of a same place, get the shift]
def getAngle(sig1, sig2):
angle=-1
score_min = -1
candidate = LocationSignature()
# incrementally rotate teh observed sig to get the candidate
for i in range(NO_BINS):
candidate = shift(sig1,i)
# compare candidate with saved sig
score = compare(sig2, candidate)
if (score<score_min):
score = score_min
angle = i
# convert angle to degrees
angle = angle * 360/NO_BINS
return angle
# returns a shifted version of sig1
def shift(sig1, i):
candidate = LocationSignature()
lengthSig = len(sig1)
for j in range(lengthSig):
candidate[(j+i) % lengthSig] = sig1[j]
return candidate
###############################################################################################
#
#
# MAIN PROGRAM
#
#
###############################################################################################
# Prior to starting learning the locations, it should delete files from previous
# learning either manually or by calling signatures.delete_loc_files().
# Then, either learn a location, until all the locations are learned, or try to
# recognize one of them, if locations have already been learned.
'''
signatures = SignatureContainer(6)
histograms = SignatureContainer(6)
signatures.delete_loc_files()
histograms.delete_loc_files()
# > 4.2
# step 1 - Learn signatures (this part is to be commented out after it has been run)
for i in range(5):
learn_location()
time.sleep(10) # must be put manually to next key location :/ !
# step 2 - Recognize signatures (i.e., testing previous bit):
for i in range(5):
recognize_location();
time.sleep(10) # must be put manually to next key location :/ !
# > 4.3
signatures_invariant = SignatureContainer(5);
signatures_invariant.delete_loc_files()
# step 1 - make invariant measurement of a measurement
for i in range(5):
learn_location_invariant()
time.sleep(20) # must be put manually to next key location :/ !
# step 2 - Recognize invariant signatures (i.e., testing previous bit) + displays the shift :
for i in range(5):
recognize_location_invariant();
time.sleep(20) # must be put manually to next key location :/ !
'''