17
17
R = np .diag ([1.0 , np .deg2rad (20.0 )]) ** 2
18
18
19
19
# Simulation parameter
20
- Q_sim = np .diag ([0.3 , np .deg2rad (2.0 )]) ** 2
21
- R_sim = np .diag ([0.5 , np .deg2rad (10.0 )]) ** 2
20
+ Q_SIM = np .diag ([0.3 , np .deg2rad (2.0 )]) ** 2
21
+ R_SIM = np .diag ([0.5 , np .deg2rad (10.0 )]) ** 2
22
22
OFFSET_YAW_RATE_NOISE = 0.01
23
23
24
24
DT = 0.1 # time tick [s]
@@ -72,19 +72,18 @@ def normalize_weight(particles):
72
72
73
73
74
74
def calc_final_state (particles ):
75
- xEst = np .zeros ((STATE_SIZE , 1 ))
75
+ x_est = np .zeros ((STATE_SIZE , 1 ))
76
76
77
77
particles = normalize_weight (particles )
78
78
79
79
for i in range (N_PARTICLE ):
80
- xEst [0 , 0 ] += particles [i ].w * particles [i ].x
81
- xEst [1 , 0 ] += particles [i ].w * particles [i ].y
82
- xEst [2 , 0 ] += particles [i ].w * particles [i ].yaw
80
+ x_est [0 , 0 ] += particles [i ].w * particles [i ].x
81
+ x_est [1 , 0 ] += particles [i ].w * particles [i ].y
82
+ x_est [2 , 0 ] += particles [i ].w * particles [i ].yaw
83
83
84
- xEst [2 , 0 ] = pi_2_pi (xEst [2 , 0 ])
85
- # print(xEst)
84
+ x_est [2 , 0 ] = pi_2_pi (x_est [2 , 0 ])
86
85
87
- return xEst
86
+ return x_est
88
87
89
88
90
89
def predict_particles (particles , u ):
@@ -235,28 +234,27 @@ def resampling(particles):
235
234
pw = np .array (pw )
236
235
237
236
n_eff = 1.0 / (pw @ pw .T ) # Effective particle number
238
- # print(n_eff)
239
237
240
238
if n_eff < NTH : # resampling
241
239
w_cum = np .cumsum (pw )
242
240
base = np .cumsum (pw * 0.0 + 1 / N_PARTICLE ) - 1 / N_PARTICLE
243
241
resample_id = base + np .random .rand (base .shape [0 ]) / N_PARTICLE
244
242
245
- inds = []
246
- ind = 0
243
+ indexes = []
244
+ index = 0
247
245
for ip in range (N_PARTICLE ):
248
- while (ind < w_cum .shape [0 ] - 1 ) \
249
- and (resample_id [ip ] > w_cum [ind ]):
250
- ind += 1
251
- inds .append (ind )
246
+ while (index < w_cum .shape [0 ] - 1 ) \
247
+ and (resample_id [ip ] > w_cum [index ]):
248
+ index += 1
249
+ indexes .append (index )
252
250
253
251
tmp_particles = particles [:]
254
- for i in range (len (inds )):
255
- particles [i ].x = tmp_particles [inds [i ]].x
256
- particles [i ].y = tmp_particles [inds [i ]].y
257
- particles [i ].yaw = tmp_particles [inds [i ]].yaw
258
- particles [i ].lm = tmp_particles [inds [i ]].lm [:, :]
259
- particles [i ].lmP = tmp_particles [inds [i ]].lmP [:, :]
252
+ for i in range (len (indexes )):
253
+ particles [i ].x = tmp_particles [indexes [i ]].x
254
+ particles [i ].y = tmp_particles [indexes [i ]].y
255
+ particles [i ].yaw = tmp_particles [indexes [i ]].yaw
256
+ particles [i ].lm = tmp_particles [indexes [i ]].lm [:, :]
257
+ particles [i ].lmP = tmp_particles [indexes [i ]].lmP [:, :]
260
258
particles [i ].w = 1.0 / N_PARTICLE
261
259
262
260
return particles
@@ -275,34 +273,34 @@ def calc_input(time):
275
273
return u
276
274
277
275
278
- def observation (xTrue , xd , u , rfid ):
276
+ def observation (x_true , xd , u , rfid ):
279
277
# calc true state
280
- xTrue = motion_model (xTrue , u )
278
+ x_true = motion_model (x_true , u )
281
279
282
280
# add noise to range observation
283
281
z = np .zeros ((3 , 0 ))
284
282
for i in range (len (rfid [:, 0 ])):
285
283
286
- dx = rfid [i , 0 ] - xTrue [0 , 0 ]
287
- dy = rfid [i , 1 ] - xTrue [1 , 0 ]
284
+ dx = rfid [i , 0 ] - x_true [0 , 0 ]
285
+ dy = rfid [i , 1 ] - x_true [1 , 0 ]
288
286
d = math .hypot (dx , dy )
289
- angle = pi_2_pi (math .atan2 (dy , dx ) - xTrue [2 , 0 ])
287
+ angle = pi_2_pi (math .atan2 (dy , dx ) - x_true [2 , 0 ])
290
288
if d <= MAX_RANGE :
291
- dn = d + np .random .randn () * Q_sim [0 , 0 ] ** 0.5 # add noise
292
- angle_with_noize = angle + np .random .randn () * Q_sim [
289
+ dn = d + np .random .randn () * Q_SIM [0 , 0 ] ** 0.5 # add noise
290
+ angle_with_noize = angle + np .random .randn () * Q_SIM [
293
291
1 , 1 ] ** 0.5 # add noise
294
292
zi = np .array ([dn , pi_2_pi (angle_with_noize ), i ]).reshape (3 , 1 )
295
293
z = np .hstack ((z , zi ))
296
294
297
295
# add noise to input
298
- ud1 = u [0 , 0 ] + np .random .randn () * R_sim [0 , 0 ] ** 0.5
299
- ud2 = u [1 , 0 ] + np .random .randn () * R_sim [
296
+ ud1 = u [0 , 0 ] + np .random .randn () * R_SIM [0 , 0 ] ** 0.5
297
+ ud2 = u [1 , 0 ] + np .random .randn () * R_SIM [
300
298
1 , 1 ] ** 0.5 + OFFSET_YAW_RATE_NOISE
301
299
ud = np .array ([ud1 , ud2 ]).reshape (2 , 1 )
302
300
303
301
xd = motion_model (xd , ud )
304
302
305
- return xTrue , z , xd , ud
303
+ return x_true , z , xd , ud
306
304
307
305
308
306
def motion_model (x , u ):
@@ -331,7 +329,7 @@ def main():
331
329
time = 0.0
332
330
333
331
# RFID positions [x, y]
334
- RFID = np .array ([[10.0 , - 2.0 ],
332
+ rfid = np .array ([[10.0 , - 2.0 ],
335
333
[15.0 , 10.0 ],
336
334
[15.0 , 15.0 ],
337
335
[10.0 , 20.0 ],
@@ -340,53 +338,53 @@ def main():
340
338
[- 5.0 , 5.0 ],
341
339
[- 10.0 , 15.0 ]
342
340
])
343
- n_landmark = RFID .shape [0 ]
341
+ n_landmark = rfid .shape [0 ]
344
342
345
343
# State Vector [x y yaw v]'
346
- xEst = np .zeros ((STATE_SIZE , 1 )) # SLAM estimation
347
- xTrue = np .zeros ((STATE_SIZE , 1 )) # True state
348
- xDR = np .zeros ((STATE_SIZE , 1 )) # Dead reckoning
344
+ x_est = np .zeros ((STATE_SIZE , 1 )) # SLAM estimation
345
+ x_true = np .zeros ((STATE_SIZE , 1 )) # True state
346
+ x_dr = np .zeros ((STATE_SIZE , 1 )) # Dead reckoning
349
347
350
348
# history
351
- hxEst = xEst
352
- hxTrue = xTrue
353
- hxDR = xTrue
349
+ hist_x_est = x_est
350
+ hist_x_true = x_true
351
+ hist_x_dr = x_dr
354
352
355
353
particles = [Particle (n_landmark ) for _ in range (N_PARTICLE )]
356
354
357
355
while SIM_TIME >= time :
358
356
time += DT
359
357
u = calc_input (time )
360
358
361
- xTrue , z , xDR , ud = observation (xTrue , xDR , u , RFID )
359
+ x_true , z , x_dr , ud = observation (x_true , x_dr , u , rfid )
362
360
363
361
particles = fast_slam1 (particles , ud , z )
364
362
365
- xEst = calc_final_state (particles )
363
+ x_est = calc_final_state (particles )
366
364
367
- x_state = xEst [0 : STATE_SIZE ]
365
+ x_state = x_est [0 : STATE_SIZE ]
368
366
369
367
# store data history
370
- hxEst = np .hstack ((hxEst , x_state ))
371
- hxDR = np .hstack ((hxDR , xDR ))
372
- hxTrue = np .hstack ((hxTrue , xTrue ))
368
+ hist_x_est = np .hstack ((hist_x_est , x_state ))
369
+ hist_x_dr = np .hstack ((hist_x_dr , x_dr ))
370
+ hist_x_true = np .hstack ((hist_x_true , x_true ))
373
371
374
372
if show_animation : # pragma: no cover
375
373
plt .cla ()
376
374
# for stopping simulation with the esc key.
377
375
plt .gcf ().canvas .mpl_connect (
378
376
'key_release_event' , lambda event :
379
377
[exit (0 ) if event .key == 'escape' else None ])
380
- plt .plot (RFID [:, 0 ], RFID [:, 1 ], "*k" )
378
+ plt .plot (rfid [:, 0 ], rfid [:, 1 ], "*k" )
381
379
382
380
for i in range (N_PARTICLE ):
383
381
plt .plot (particles [i ].x , particles [i ].y , ".r" )
384
382
plt .plot (particles [i ].lm [:, 0 ], particles [i ].lm [:, 1 ], "xb" )
385
383
386
- plt .plot (hxTrue [0 , :], hxTrue [1 , :], "-b" )
387
- plt .plot (hxDR [0 , :], hxDR [1 , :], "-k" )
388
- plt .plot (hxEst [0 , :], hxEst [1 , :], "-r" )
389
- plt .plot (xEst [0 ], xEst [1 ], "xk" )
384
+ plt .plot (hist_x_true [0 , :], hist_x_true [1 , :], "-b" )
385
+ plt .plot (hist_x_dr [0 , :], hist_x_dr [1 , :], "-k" )
386
+ plt .plot (hist_x_est [0 , :], hist_x_est [1 , :], "-r" )
387
+ plt .plot (x_est [0 ], x_est [1 ], "xk" )
390
388
plt .axis ("equal" )
391
389
plt .grid (True )
392
390
plt .pause (0.001 )
0 commit comments