11"""
22
3- Graph based SLAM example
3+ Graph based SLAM example
44
55author: Atsushi Sakai (@Atsushi_twi)
66
1212import itertools
1313import matplotlib .pyplot as plt
1414
15-
1615# Simulation parameter
17- Qsim = np .diag ([0.1 , math .radians (1.0 )])** 2
16+ Qsim = np .diag ([0.2 , math .radians (1.0 )])** 2
1817Rsim = np .diag ([0.1 , math .radians (10.0 )])** 2
1918
20- DT = 1 .0 # time tick [s]
21- SIM_TIME = 40 .0 # simulation time [s]
22- MAX_RANGE = 20 .0 # maximum observation range
19+ DT = 2 .0 # time tick [s]
20+ SIM_TIME = 100 .0 # simulation time [s]
21+ MAX_RANGE = 30 .0 # maximum observation range
2322STATE_SIZE = 3 # State size [x,y,yaw]
2423
2524# Covariance parameter of Graph Based SLAM
26- C_SIGMA1 = 0.01
27- C_SIGMA2 = 0.01
28- C_SIGMA3 = math .radians (5 .0 )
25+ C_SIGMA1 = 0.1
26+ C_SIGMA2 = 0.1
27+ C_SIGMA3 = math .radians (1 .0 )
2928
30- MAX_ITR = 20 # Maximuma iteration
29+ MAX_ITR = 20 # Maximum iteration
3130
31+ show_graph_dtime = 20.0 # [s]
3232show_animation = True
3333
3434
@@ -59,8 +59,8 @@ def cal_observation_sigma(d):
5959
6060def calc_rotational_matrix (angle ):
6161
62- Rt = np .matrix ([[math .cos (angle ), math .sin (angle ), 0 ],
63- [- math .sin (angle ), math .cos (angle ), 0 ],
62+ Rt = np .matrix ([[math .cos (angle ), - math .sin (angle ), 0 ],
63+ [math .sin (angle ), math .cos (angle ), 0 ],
6464 [0 , 0 , 1.0 ]])
6565 return Rt
6666
@@ -107,6 +107,9 @@ def calc_edges(xlist, zlist):
107107 x1 , y1 , yaw1 = xlist [0 , t1 ], xlist [1 , t1 ], xlist [2 , t1 ]
108108 x2 , y2 , yaw2 = xlist [0 , t2 ], xlist [1 , t2 ], xlist [2 , t2 ]
109109
110+ if zlist [t1 ] is None or zlist [t2 ] is None :
111+ continue # No observation
112+
110113 for iz1 in range (len (zlist [t1 ][:, 0 ])):
111114 for iz2 in range (len (zlist [t2 ][:, 0 ])):
112115 if zlist [t1 ][iz1 , 3 ] == zlist [t2 ][iz2 , 3 ]:
@@ -121,7 +124,7 @@ def calc_edges(xlist, zlist):
121124 edges .append (edge )
122125 cost += (edge .e .T * edge .omega * edge .e )[0 , 0 ]
123126
124- print ("cost:" , cost )
127+ print ("cost:" , cost , ",nedge:" , len ( edges ) )
125128 return edges
126129
127130
@@ -160,13 +163,17 @@ def fill_H_and_b(H, b, edge):
160163def graph_based_slam (x_init , hz ):
161164 print ("start graph based slam" )
162165
166+ zlist = copy .deepcopy (hz )
167+ zlist .insert (1 , zlist [0 ])
168+ # zlist.append(zlist[-1])
169+ # zlist.append(None)
170+
163171 x_opt = copy .deepcopy (x_init )
164- # n = len(hz) * STATE_SIZE
165- n = x_opt . shape [ 1 ] * STATE_SIZE
172+ nt = x_opt . shape [ 1 ]
173+ n = nt * STATE_SIZE
166174
167175 for itr in range (MAX_ITR ):
168- edges = calc_edges (x_opt , hz )
169- # print("n edges:", len(edges))
176+ edges = calc_edges (x_opt , zlist )
170177
171178 H = np .matrix (np .zeros ((n , n )))
172179 b = np .matrix (np .zeros ((n , 1 )))
@@ -175,20 +182,18 @@ def graph_based_slam(x_init, hz):
175182 H , b = fill_H_and_b (H , b , edge )
176183
177184 # to fix origin
178- H [0 :STATE_SIZE , 0 :STATE_SIZE ] += np .identity (STATE_SIZE ) * 1000.0
185+ H [0 :STATE_SIZE , 0 :STATE_SIZE ] += np .identity (STATE_SIZE )
179186
180187 dx = - np .linalg .inv (H ).dot (b )
181188
182- for i in range (( x_opt . shape [ 1 ]) ):
189+ for i in range (nt ):
183190 x_opt [0 :3 , i ] += dx [i * 3 :i * 3 + 3 , 0 ]
184191
185192 diff = dx .T .dot (dx )
186193 print ("iteration: %d, diff: %f" % (itr + 1 , diff ))
187194 if diff < 1.0e-5 :
188195 break
189196
190- # print(x_opt)
191-
192197 return x_opt
193198
194199
@@ -263,7 +268,9 @@ def main():
263268 RFID = np .array ([[10.0 , - 2.0 , 0.0 ],
264269 [15.0 , 10.0 , 0.0 ],
265270 [3.0 , 15.0 , 0.0 ],
266- [- 5.0 , 20.0 , 0.0 ]])
271+ [- 5.0 , 20.0 , 0.0 ],
272+ [- 5.0 , 5.0 , 0.0 ]
273+ ])
267274
268275 # State Vector [x y yaw v]'
269276 xTrue = np .matrix (np .zeros ((STATE_SIZE , 1 )))
@@ -272,12 +279,12 @@ def main():
272279 # history
273280 hxTrue = xTrue
274281 hxDR = xTrue
275- # hz = [np.matrix(np.zeros((1, 4)))]
276- # hz[0][0, 3] = -1
277282 hz = []
283+ dtime = 0.0
278284
279285 while SIM_TIME >= time :
280286 time += DT
287+ dtime += DT
281288 u = calc_input ()
282289
283290 xTrue , z , xDR , ud = observation (xTrue , xDR , u , RFID )
@@ -286,24 +293,24 @@ def main():
286293 hxTrue = np .hstack ((hxTrue , xTrue ))
287294 hz .append (z )
288295
289- hz . append ( hz [ - 1 ])
290-
291- x_opt = graph_based_slam ( hxDR , hz )
296+ if dtime >= show_graph_dtime :
297+ x_opt = graph_based_slam ( hxDR , hz )
298+ dtime = 0.0
292299
293- if show_animation :
294- plt .cla ()
300+ if show_animation :
301+ plt .cla ()
295302
296- plt .plot (RFID [:, 0 ], RFID [:, 1 ], "*k" )
303+ plt .plot (RFID [:, 0 ], RFID [:, 1 ], "*k" )
297304
298- plt .plot (np .array (hxTrue [0 , :]).flatten (),
299- np .array (hxTrue [1 , :]).flatten (), "xb " )
300- plt .plot (np .array (hxDR [0 , :]).flatten (),
301- np .array (hxDR [1 , :]).flatten (), "-k" )
302- plt .plot (np .array (x_opt [0 , :]).flatten (),
303- np .array (x_opt [1 , :]).flatten (), "-r" )
304- plt .axis ("equal" )
305- plt .grid (True )
306- plt .show ( )
305+ plt .plot (np .array (hxTrue [0 , :]).flatten (),
306+ np .array (hxTrue [1 , :]).flatten (), "-b " )
307+ plt .plot (np .array (hxDR [0 , :]).flatten (),
308+ np .array (hxDR [1 , :]).flatten (), "-k" )
309+ plt .plot (np .array (x_opt [0 , :]).flatten (),
310+ np .array (x_opt [1 , :]).flatten (), "-r" )
311+ plt .axis ("equal" )
312+ plt .grid (True )
313+ plt .pause ( 1.0 )
307314
308315
309316if __name__ == '__main__' :
0 commit comments