File particle_filter.py changed (mode: 100644) (index 2856bfb..b2a5ae5) |
... |
... |
while True: |
176 |
176 |
# Read Robot's sensor |
# Read Robot's sensor |
177 |
177 |
r_d = nao.read_sensor(world) |
r_d = nao.read_sensor(world) |
178 |
178 |
|
|
179 |
|
# TODO: Update particle weight according to how good every particle matches |
|
180 |
|
# sensorupdate() |
|
|
179 |
|
# Aufgabe 3.1: sensor update |
|
180 |
|
# Update particle weight according to how good every particle matches |
|
181 |
|
#for p in particles: |
|
182 |
|
# p.w = ... |
|
183 |
|
|
|
184 |
|
for p in particles: |
|
185 |
|
p.w = w_gauss(p.read_sensor(world), r_d) |
181 |
186 |
|
|
182 |
187 |
# ---------- Show current state ---------- |
# ---------- Show current state ---------- |
183 |
188 |
world.show_particles(particles) |
world.show_particles(particles) |
|
... |
... |
while True: |
187 |
192 |
# ---------- Shuffle particles ---------- |
# ---------- Shuffle particles ---------- |
188 |
193 |
new_particles = [] |
new_particles = [] |
189 |
194 |
|
|
190 |
|
# TODO: Normalise weights |
|
191 |
|
# normalize() # fuer Resampling |
|
|
195 |
|
# Aufgabe 3.2: resampling |
|
196 |
|
# calculate new samples (new_particles) based on the weigths (p.w) of the old partiucles |
|
197 |
|
# Example: Stochastic universal sampling |
|
198 |
|
# https://en.wikipedia.org/wiki/Stochastic_universal_sampling |
|
199 |
|
|
|
200 |
|
# The index of the current particle |
|
201 |
|
current_particle_index = 0 |
|
202 |
|
|
|
203 |
|
# Delta of equidistant slices of weights |
|
204 |
|
dp = sum([p.w for p in particles])/len(particles) |
|
205 |
|
|
|
206 |
|
# Offset from where to start |
|
207 |
|
offset = random.uniform(0, dp) |
|
208 |
|
|
|
209 |
|
# The distance in weight space for the new particle: offset + i*dp |
|
210 |
|
new_part_weitgh_d = offset |
|
211 |
|
|
|
212 |
|
weight_sum = 0 |
|
213 |
|
|
|
214 |
|
new_particles = [] |
|
215 |
|
|
|
216 |
|
# Loop over all particles |
|
217 |
|
for i in range(len(particles)): |
|
218 |
|
|
|
219 |
|
# Add up weights of particles until first time larger than offset + i*dp |
|
220 |
|
while weight_sum + particles[current_particle_index].w <= new_part_weitgh_d: |
|
221 |
|
weight_sum += particles[current_particle_index].w |
|
222 |
|
current_particle_index += 1 |
|
223 |
|
|
|
224 |
|
# The particle around which to saple a new particle |
|
225 |
|
p = particles[current_particle_index-1] |
|
226 |
|
|
|
227 |
|
# Sample the new particle with some noise and increment the sum for the next particle |
|
228 |
|
x, y = add_little_noise(*p.xy) |
|
229 |
|
|
|
230 |
|
# Resample particle with increasing noise to avoid sampling points inside a wall or outside world |
|
231 |
|
c = 2 |
|
232 |
|
while not world.is_free(x, y): |
|
233 |
|
x, y = add_noise(c*0.02, *p.xy) |
|
234 |
|
c += 1 |
|
235 |
|
|
|
236 |
|
new_particles.append(Particle(x, y, p.h, p.w, True)) |
|
237 |
|
new_part_weitgh_d += dp |
192 |
238 |
|
|
193 |
|
# TODO: update Samples |
|
194 |
|
# resample() |
|
|
239 |
|
particles = new_particles |
195 |
240 |
|
|
196 |
241 |
# ---------- Move things ---------- |
# ---------- Move things ---------- |
197 |
242 |
old_heading = nao.h |
old_heading = nao.h |
|
... |
... |
while True: |
201 |
246 |
# Move particles according to my belief of movement |
# Move particles according to my belief of movement |
202 |
247 |
for p in particles: |
for p in particles: |
203 |
248 |
p.h += d_h # in case robot changed heading, swirl particle heading too |
p.h += d_h # in case robot changed heading, swirl particle heading too |
204 |
|
p.advance_by(nao.speed) |
|
|
249 |
|
p.advance_by(nao.speed, checker=lambda r, dx, dy: world.is_free(r.x+dx, r.y+dy)) |