66
77""" 
88
9- import  numpy  as  np 
109import  math 
10+ 
1111import  matplotlib .pyplot  as  plt 
12+ import  numpy  as  np 
1213
1314# Estimation parameter of PF 
14- Q  =  np .diag ([0.1  ]) ** 2   # range error 
15- R  =  np .diag ([1 .0np .deg2rad (40.0 )])** 2   # input error 
15+ Q  =  np .diag ([0.2  ])  **   2   # range error 
16+ R  =  np .diag ([2 .0np .deg2rad (40.0 )])  **   2   # input error 
1617
1718#  Simulation parameter 
18- Qsim  =  np .diag ([0.2 ])** 2 
19- 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 
2021
2122DT  =  0.1   # time tick [s] 
2223SIM_TIME  =  50.0   # simulation time [s] 
3132
3233def  calc_input ():
3334    v  =  1.0   # [m/s] 
34-     yawrate  =  0.1   # [rad/s] 
35-     u  =  np .array ([[v , yawrate ]]).T 
35+     yaw_rate  =  0.1   # [rad/s] 
36+     u  =  np .array ([[v , yaw_rate ]]).T 
3637    return  u 
3738
3839
39- def  observation (xTrue , xd , u , RFID ):
40- 
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 ]
50-         d  =  math .sqrt (dx ** 2  +  dy ** 2 )
48+         dx  =  xTrue [0 , 0 ] -  RF_ID [i , 0 ]
49+         dy  =  xTrue [1 , 0 ] -  RF_ID [i , 1 ]
50+         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 )
@@ -64,7 +64,6 @@ def observation(xTrue, xd, u, RFID):
6464
6565
6666def  motion_model (x , u ):
67- 
6867    F  =  np .array ([[1.0 , 0 , 0 , 0 ],
6968                  [0 , 1.0 , 0 , 0 ],
7069                  [0 , 0 , 1.0 , 0 ],
@@ -93,30 +92,32 @@ def calc_covariance(xEst, px, pw):
9392    for  i  in  range (px .shape [1 ]):
9493        dx  =  (px [:, i ] -  xEst )[0 :3 ]
9594        cov  +=  pw [0 , i ] *  dx .dot (dx .T )
95+     cov  /=  NP 
9696
9797    return  cov 
9898
9999
100- def  pf_localization (px , pw , xEst ,  PEst ,  z , u ):
100+ def  pf_localization (px , pw , z , u ):
101101    """ 
102102    Localization with Particle filter 
103103    """ 
104104
105105    for  ip  in  range (NP ):
106106        x  =  np .array ([px [:, ip ]]).T 
107107        w  =  pw [0 , ip ]
108+ 
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
114115        #  Calc Importance Weight 
115116        for  i  in  range (len (z [:, 0 ])):
116117            dx  =  x [0 , 0 ] -  z [i , 1 ]
117118            dy  =  x [1 , 0 ] -  z [i , 2 ]
118-             prez  =  math .sqrt (dx ** 2  +  dy ** 2 )
119-             dz  =  prez  -  z [i , 0 ]
119+             pre_z  =  math .sqrt (dx   **   2  +  dy   **   2 )
120+             dz  =  pre_z  -  z [i , 0 ]
120121            w  =  w  *  gauss_likelihood (dz , math .sqrt (Q [0 , 0 ]))
121122
122123        px [:, ip ] =  x [:, 0 ]
@@ -127,66 +128,66 @@ def pf_localization(px, pw, xEst, PEst, 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,9 +221,9 @@ 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
226-         xEst , PEst , px , pw  =  pf_localization (px , pw , xEst ,  PEst ,  z , ud )
226+         xEst , PEst , px , pw  =  pf_localization (px , pw , z , ud )
227227
228228        # store data history 
229229        hxEst  =  np .hstack ((hxEst , xEst ))
@@ -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