2727STATE_SIZE = 3 # State size [x,y,yaw]
2828LM_SIZE = 2 # LM srate size [x,y]
2929N_PARTICLE = 100 # number of particle
30- NTH = N_PARTICLE / 1.5 # Number of particle for re-sampling
30+ NTH = N_PARTICLE / 1.0 # Number of particle for re-sampling
3131
3232show_animation = True
3333
@@ -61,15 +61,15 @@ def normalize_weight(particles):
6161
6262 sumw = sum ([p .w for p in particles ])
6363
64- if sumw <= 0.0000001 :
64+ try :
65+ for i in range (N_PARTICLE ):
66+ particles [i ].w /= sumw
67+ except ZeroDivisionError :
6568 for i in range (N_PARTICLE ):
6669 particles [i ].w = 1.0 / N_PARTICLE
6770
6871 return particles
6972
70- for i in range (N_PARTICLE ):
71- particles [i ].w /= sumw
72-
7373 return particles
7474
7575
@@ -188,8 +188,8 @@ def compute_weight(particle, z, Q):
188188 Pf = np .matrix (particle .lmP [2 * lm_id :2 * lm_id + 2 ])
189189 zp , Hv , Hf , Sf = compute_jacobians (particle , xf , Pf , Q )
190190
191- dx = z [0 , 0 : 2 ].T - zp
192- dx [1 , 0 ] = pi_2_pi (dx [1 , 0 ])
191+ dz = z [0 , 0 : 2 ].T - zp
192+ dz [1 , 0 ] = pi_2_pi (dz [1 , 0 ])
193193
194194 S = particle .lmP [2 * lm_id :2 * lm_id + 2 ]
195195 try :
@@ -198,7 +198,7 @@ def compute_weight(particle, z, Q):
198198 print ("singuler" )
199199 return 1.0
200200
201- num = math .exp (- 0.5 * dx .T * invS * dx )
201+ num = math .exp (- 0.5 * dz .T * invS * dz )
202202 den = 2.0 * math .pi * math .sqrt (np .linalg .det (S ))
203203
204204 w = num / den
@@ -247,8 +247,8 @@ def update_with_observation(particles, z):
247247 w = compute_weight (particles [ip ], z [iz , :], Q )
248248 particles [ip ].w *= w
249249
250- particles [ip ] = proposal_sampling (particles [ip ], z [iz , :], Q )
251250 particles [ip ] = update_landmark (particles [ip ], z [iz , :], Q )
251+ particles [ip ] = proposal_sampling (particles [ip ], z [iz , :], Q )
252252
253253 return particles
254254
@@ -267,7 +267,7 @@ def resampling(particles):
267267 pw = np .matrix (pw )
268268
269269 Neff = 1.0 / (pw * pw .T )[0 , 0 ] # Effective particle number
270- # print(Neff)
270+ print (Neff )
271271
272272 if Neff < NTH : # resampling
273273 wcum = np .cumsum (pw )
@@ -319,7 +319,7 @@ def observation(xTrue, xd, u, RFID):
319319 dx = RFID [i , 0 ] - xTrue [0 , 0 ]
320320 dy = RFID [i , 1 ] - xTrue [1 , 0 ]
321321 d = math .sqrt (dx ** 2 + dy ** 2 )
322- angle = pi_2_pi ( math .atan2 (dy , dx ) - xTrue [2 , 0 ])
322+ angle = math .atan2 (dy , dx ) - xTrue [2 , 0 ]
323323 if d <= MAX_RANGE :
324324 dn = d + np .random .randn () * Qsim [0 , 0 ] # add noise
325325 anglen = angle + np .random .randn () * Qsim [1 , 1 ] # add noise
@@ -414,6 +414,11 @@ def main():
414414 plt .cla ()
415415 plt .plot (RFID [:, 0 ], RFID [:, 1 ], "*k" )
416416
417+ for iz in range (len (z [:, 0 ])):
418+ lmid = int (z [iz , 2 ])
419+ plt .plot ([xEst [0 , 0 ], RFID [lmid , 0 ]], [
420+ xEst [1 , 0 ], RFID [lmid , 1 ]], "-k" )
421+
417422 for i in range (N_PARTICLE ):
418423 plt .plot (particles [i ].x , particles [i ].y , ".r" )
419424 plt .plot (particles [i ].lm [:, 0 ], particles [i ].lm [:, 1 ], "xb" )
0 commit comments