1+
12"""
23
34Ensemble Kalman Filter(EnKF) localization sample
@@ -34,7 +35,6 @@ def calc_input():
3435 return u
3536
3637
37-
3838def observation (xTrue , xd , u , RFID ):
3939
4040 xTrue = motion_model (xTrue , u )
@@ -50,7 +50,7 @@ def observation(xTrue, xd, u, RFID):
5050 if d <= MAX_RANGE :
5151 dn = d + np .random .randn () * Qsim [0 , 0 ] # add noise
5252 anglen = angle + np .random .randn () * Qsim [1 , 1 ] # add noise
53- zi = np .array ([dn , anglen ,RFID [i , 0 ], RFID [i , 1 ]])
53+ zi = np .array ([dn , anglen , RFID [i , 0 ], RFID [i , 1 ]])
5454 z = np .vstack ((z , zi ))
5555
5656 # add noise to input
@@ -78,18 +78,23 @@ def motion_model(x, u):
7878
7979
8080def calc_LM_Pos (x , landmarks ):
81- landmarks_pos = np .zeros ((2 * landmarks .shape [0 ],1 ))
82- for (i ,lm ) in enumerate (landmarks ):
83- landmarks_pos [2 * i ] = x [0 , 0 ] + lm [0 ] * math .cos (x [2 , 0 ] + lm [1 ]) + np .random .randn () * Qsim [0 , 0 ]/ np .sqrt (2 )
84- landmarks_pos [2 * i + 1 ] = x [1 , 0 ] + lm [0 ] * math .sin (x [2 , 0 ] + lm [1 ]) + np .random .randn () * Qsim [0 , 0 ]/ np .sqrt (2 )
81+ landmarks_pos = np .zeros ((2 * landmarks .shape [0 ], 1 ))
82+ for (i , lm ) in enumerate (landmarks ):
83+ landmarks_pos [2 * i ] = x [0 , 0 ] + lm [0 ] * \
84+ math .cos (x [2 , 0 ] + lm [1 ]) + np .random .randn () * \
85+ Qsim [0 , 0 ]/ np .sqrt (2 )
86+ landmarks_pos [2 * i + 1 ] = x [1 , 0 ] + lm [0 ] * \
87+ math .sin (x [2 , 0 ] + lm [1 ]) + np .random .randn () * \
88+ Qsim [0 , 0 ]/ np .sqrt (2 )
8589 return landmarks_pos
8690
91+
8792def calc_covariance (xEst , px ):
8893 cov = np .zeros ((3 , 3 ))
8994
9095 for i in range (px .shape [1 ]):
9196 dx = (px [:, i ] - xEst )[0 :3 ]
92- cov += dx .dot (dx .T )
97+ cov += dx .dot (dx .T )
9398
9499 return cov
95100
@@ -108,26 +113,26 @@ def enkf_localization(px, xEst, PEst, z, u):
108113 ud = np .array ([[ud1 , ud2 ]]).T
109114 x = motion_model (x , ud )
110115 px [:, ip ] = x [:, 0 ]
111- z_pos = calc_LM_Pos (x , z )
116+ z_pos = calc_LM_Pos (x , z )
112117 pz [:, ip ] = z_pos [:, 0 ]
113118
114- x_ave = np .mean (px , axis = 1 )
115- x_dif = px - np .tile (x_ave ,(NP ,1 )).T
119+ x_ave = np .mean (px , axis = 1 )
120+ x_dif = px - np .tile (x_ave , (NP , 1 )).T
116121
117- z_ave = np .mean (pz , axis = 1 )
118- z_dif = pz - np .tile (z_ave ,(NP ,1 )).T
122+ z_ave = np .mean (pz , axis = 1 )
123+ z_dif = pz - np .tile (z_ave , (NP , 1 )).T
119124
120- U = 1 / (NP - 1 )* x_dif @ z_dif .T
121- V = 1 / (NP - 1 )* z_dif @ z_dif .T
125+ U = 1 / (NP - 1 ) * x_dif @ z_dif .T
126+ V = 1 / (NP - 1 ) * z_dif @ z_dif .T
122127
123- K = U @ np .linalg .inv (V ) # Kalman Gain
128+ K = U @ np .linalg .inv (V ) # Kalman Gain
124129
125- z_lm_pos = z [:,[2 ,3 ]].reshape (- 1 ,)
130+ z_lm_pos = z [:, [2 , 3 ]].reshape (- 1 ,)
126131
127- px_hat = px + K @ (np .tile (z_lm_pos ,(NP ,1 )).T - pz )
132+ px_hat = px + K @ (np .tile (z_lm_pos , (NP , 1 )).T - pz )
128133
129- xEst = np .average (px_hat , axis = 1 ).reshape (4 ,1 )
130- PEst = calc_covariance (xEst , px_hat )
134+ xEst = np .average (px_hat , axis = 1 ).reshape (4 , 1 )
135+ PEst = calc_covariance (xEst , px_hat )
131136
132137 return xEst , PEst , px_hat
133138
@@ -171,6 +176,7 @@ def plot_covariance_ellipse(xEst, PEst): # pragma: no cover
171176def pi_2_pi (angle ):
172177 return (angle + math .pi ) % (2 * math .pi ) - math .pi
173178
179+
174180def main ():
175181 print (__file__ + " start!!" )
176182
@@ -181,7 +187,7 @@ def main():
181187 [10.0 , 10.0 ],
182188 [0.0 , 15.0 ],
183189 [- 5.0 , 20.0 ]])
184-
190+
185191 # State Vector [x y yaw v]'
186192 xEst = np .zeros ((4 , 1 ))
187193 xTrue = np .zeros ((4 , 1 ))
@@ -230,3 +236,4 @@ def main():
230236
231237if __name__ == '__main__' :
232238 main ()
239+
0 commit comments