1212import numpy as np
1313
1414# Estimation parameter of PF
15- Q = np .diag ([0.1 ]) ** 2 # range error
16- R = np .diag ([1 .0 , np .deg2rad (40.0 )]) ** 2 # input error
15+ Q = np .diag ([0.2 ]) ** 2 # range error
16+ R = np .diag ([2 .0 , np .deg2rad (40.0 )]) ** 2 # input error
1717
1818# Simulation parameter
19- Qsim = np .diag ([0.2 ]) ** 2
20- Rsim = np .diag ([1.0 , np .deg2rad (30.0 )]) ** 2
19+ Q_sim = np .diag ([0.2 ]) ** 2
20+ R_sim = np .diag ([1.0 , np .deg2rad (30.0 )]) ** 2
2121
2222DT = 0.1 # time tick [s]
2323SIM_TIME = 50.0 # simulation time [s]
3232
3333def calc_input ():
3434 v = 1.0 # [m/s]
35- yawrate = 0.1 # [rad/s]
36- u = np .array ([[v , yawrate ]]).T
35+ yaw_rate = 0.1 # [rad/s]
36+ u = np .array ([[v , yaw_rate ]]).T
3737 return u
3838
3939
40- def observation (xTrue , xd , u , RFID ):
40+ def observation (xTrue , xd , u , RF_ID ):
4141 xTrue = motion_model (xTrue , u )
4242
4343 # add noise to gps x-y
4444 z = np .zeros ((0 , 3 ))
4545
46- for i in range (len (RFID [:, 0 ])):
46+ for i in range (len (RF_ID [:, 0 ])):
4747
48- dx = xTrue [0 , 0 ] - RFID [i , 0 ]
49- dy = xTrue [1 , 0 ] - RFID [i , 1 ]
48+ dx = xTrue [0 , 0 ] - RF_ID [i , 0 ]
49+ dy = xTrue [1 , 0 ] - RF_ID [i , 1 ]
5050 d = math .sqrt (dx ** 2 + dy ** 2 )
5151 if d <= MAX_RANGE :
52- dn = d + np .random .randn () * Qsim [0 , 0 ] # add noise
53- zi = np .array ([[dn , RFID [i , 0 ], RFID [i , 1 ]]])
52+ dn = d + np .random .randn () * Q_sim [0 , 0 ] ** 0.5 # add noise
53+ zi = np .array ([[dn , RF_ID [i , 0 ], RF_ID [i , 1 ]]])
5454 z = np .vstack ((z , zi ))
5555
5656 # add noise to input
57- ud1 = u [0 , 0 ] + np .random .randn () * Rsim [0 , 0 ]
58- ud2 = u [1 , 0 ] + np .random .randn () * Rsim [1 , 1 ]
57+ ud1 = u [0 , 0 ] + np .random .randn () * R_sim [0 , 0 ] ** 0.5
58+ ud2 = u [1 , 0 ] + np .random .randn () * R_sim [1 , 1 ] ** 0.5
5959 ud = np .array ([[ud1 , ud2 ]]).T
6060
6161 xd = motion_model (xd , ud )
@@ -92,6 +92,7 @@ def calc_covariance(xEst, px, pw):
9292 for i in range (px .shape [1 ]):
9393 dx = (px [:, i ] - xEst )[0 :3 ]
9494 cov += pw [0 , i ] * dx .dot (dx .T )
95+ cov /= NP
9596
9697 return cov
9798
@@ -106,8 +107,8 @@ def pf_localization(px, pw, z, u):
106107 w = pw [0 , ip ]
107108
108109 # Predict with random input sampling
109- ud1 = u [0 , 0 ] + np .random .randn () * Rsim [0 , 0 ]
110- ud2 = u [1 , 0 ] + np .random .randn () * Rsim [1 , 1 ]
110+ ud1 = u [0 , 0 ] + np .random .randn () * R [0 , 0 ] ** 0.5
111+ ud2 = u [1 , 0 ] + np .random .randn () * R [1 , 1 ] ** 0.5
111112 ud = np .array ([[ud1 , ud2 ]]).T
112113 x = motion_model (x , ud )
113114
@@ -127,66 +128,66 @@ def pf_localization(px, pw, z, u):
127128 xEst = px .dot (pw .T )
128129 PEst = calc_covariance (xEst , px , pw )
129130
130- px , pw = resampling (px , pw )
131+ px , pw = re_sampling (px , pw )
131132
132133 return xEst , PEst , px , pw
133134
134135
135- def resampling (px , pw ):
136+ def re_sampling (px , pw ):
136137 """
137138 low variance re-sampling
138139 """
139140
140- Neff = 1.0 / (pw .dot (pw .T ))[0 , 0 ] # Effective particle number
141- if Neff < NTh :
142- wcum = np .cumsum (pw )
141+ N_eff = 1.0 / (pw .dot (pw .T ))[0 , 0 ] # Effective particle number
142+ if N_eff < NTh :
143+ w_cum = np .cumsum (pw )
143144 base = np .cumsum (pw * 0.0 + 1 / NP ) - 1 / NP
144- resampleid = base + np .random .rand (base .shape [0 ]) / NP
145+ re_sample_id = base + np .random .rand (base .shape [0 ]) / NP
145146
146- inds = []
147+ indexes = []
147148 ind = 0
148149 for ip in range (NP ):
149- while resampleid [ip ] > wcum [ind ]:
150+ while re_sample_id [ip ] > w_cum [ind ]:
150151 ind += 1
151- inds .append (ind )
152+ indexes .append (ind )
152153
153- px = px [:, inds ]
154+ px = px [:, indexes ]
154155 pw = np .zeros ((1 , NP )) + 1.0 / NP # init weight
155156
156157 return px , pw
157158
158159
159160def plot_covariance_ellipse (xEst , PEst ): # pragma: no cover
160161 Pxy = PEst [0 :2 , 0 :2 ]
161- eigval , eigvec = np .linalg .eig (Pxy )
162+ eig_val , eig_vec = np .linalg .eig (Pxy )
162163
163- if eigval [0 ] >= eigval [1 ]:
164- bigind = 0
165- smallind = 1
164+ if eig_val [0 ] >= eig_val [1 ]:
165+ big_ind = 0
166+ small_ind = 1
166167 else :
167- bigind = 1
168- smallind = 0
168+ big_ind = 1
169+ small_ind = 0
169170
170171 t = np .arange (0 , 2 * math .pi + 0.1 , 0.1 )
171172
172- # eigval[bigind ] or eiqval[smallind ] were occassionally negative numbers extremely
173+ # eig_val[big_ind ] or eiq_val[small_ind ] were occasionally negative numbers extremely
173174 # close to 0 (~10^-20), catch these cases and set the respective variable to 0
174175 try :
175- a = math .sqrt (eigval [ bigind ])
176+ a = math .sqrt (eig_val [ big_ind ])
176177 except ValueError :
177178 a = 0
178179
179180 try :
180- b = math .sqrt (eigval [ smallind ])
181+ b = math .sqrt (eig_val [ small_ind ])
181182 except ValueError :
182183 b = 0
183184
184185 x = [a * math .cos (it ) for it in t ]
185186 y = [b * math .sin (it ) for it in t ]
186- angle = math .atan2 (eigvec [ bigind , 1 ], eigvec [ bigind , 0 ])
187- R = np .array ([[math .cos (angle ), math .sin (angle )],
188- [ - math .sin (angle ), math .cos (angle )]])
189- fx = R .dot (np .array ([[x , y ]]))
187+ angle = math .atan2 (eig_vec [ big_ind , 1 ], eig_vec [ big_ind , 0 ])
188+ Rot = np .array ([[math .cos (angle ), - math .sin (angle )],
189+ [ math .sin (angle ), math .cos (angle )]])
190+ fx = Rot .dot (np .array ([[x , y ]]))
190191 px = np .array (fx [0 , :] + xEst [0 , 0 ]).flatten ()
191192 py = np .array (fx [1 , :] + xEst [1 , 0 ]).flatten ()
192193 plt .plot (px , py , "--r" )
@@ -197,16 +198,15 @@ def main():
197198
198199 time = 0.0
199200
200- # RFID positions [x, y]
201- RFID = np .array ([[10.0 , 0.0 ],
202- [10.0 , 10.0 ],
203- [0.0 , 15.0 ],
204- [- 5.0 , 20.0 ]])
201+ # RF_ID positions [x, y]
202+ RFi_ID = np .array ([[10.0 , 0.0 ],
203+ [10.0 , 10.0 ],
204+ [0.0 , 15.0 ],
205+ [- 5.0 , 20.0 ]])
205206
206207 # State Vector [x y yaw v]'
207208 xEst = np .zeros ((4 , 1 ))
208209 xTrue = np .zeros ((4 , 1 ))
209- PEst = np .eye (4 )
210210
211211 px = np .zeros ((4 , NP )) # Particle store
212212 pw = np .zeros ((1 , NP )) + 1.0 / NP # Particle weight
@@ -221,7 +221,7 @@ def main():
221221 time += DT
222222 u = calc_input ()
223223
224- xTrue , z , xDR , ud = observation (xTrue , xDR , u , RFID )
224+ xTrue , z , xDR , ud = observation (xTrue , xDR , u , RFi_ID )
225225
226226 xEst , PEst , px , pw = pf_localization (px , pw , z , ud )
227227
@@ -235,7 +235,7 @@ def main():
235235
236236 for i in range (len (z [:, 0 ])):
237237 plt .plot ([xTrue [0 , 0 ], z [i , 1 ]], [xTrue [1 , 0 ], z [i , 2 ]], "-k" )
238- plt .plot (RFID [:, 0 ], RFID [:, 1 ], "*k" )
238+ plt .plot (RFi_ID [:, 0 ], RFi_ID [:, 1 ], "*k" )
239239 plt .plot (px [0 , :], px [1 , :], ".r" )
240240 plt .plot (np .array (hxTrue [0 , :]).flatten (),
241241 np .array (hxTrue [1 , :]).flatten (), "-b" )
0 commit comments