1414
1515Kp = 1.0 # speed propotional gain
1616Lf = 1.0 # look-ahead distance
17+ # animation = True
18+ animation = False
1719
1820
1921def PIDControl (target , current ):
@@ -29,16 +31,23 @@ def pure_pursuit_control(state, cx, cy, pind):
2931 if pind >= ind :
3032 ind = pind
3133
32- tx = cx [ind ]
33- ty = cy [ind ]
34+ # print(pind, ind)
35+ if ind < len (cx ):
36+ tx = cx [ind ]
37+ ty = cy [ind ]
38+ else :
39+ tx = cx [- 1 ]
40+ ty = cy [- 1 ]
41+ ind = len (cx ) - 1
3442
3543 alpha = math .atan2 (ty - state .y , tx - state .x ) - state .yaw
3644
3745 if state .v < 0 : # back
38- if alpha > 0 :
39- alpha = math .pi - alpha
40- else :
41- alpha = math .pi + alpha
46+ alpha = math .pi - alpha
47+ # if alpha > 0:
48+ # alpha = math.pi - alpha
49+ # else:
50+ # alpha = math.pi + alpha
4251
4352 delta = math .atan2 (2.0 * unicycle_model .L * math .sin (alpha ) / Lf , 1.0 )
4453
@@ -64,48 +73,56 @@ def calc_target_index(state, cx, cy):
6473 return ind
6574
6675
67- def closed_loop_prediction (cx , cy , cyaw , speed_profile ):
76+ def closed_loop_prediction (cx , cy , cyaw , speed_profile , goal ):
6877
69- T = 100.0 # max simulation time
78+ T = 500.0 # max simulation time
79+ goal_dis = 0.3
80+ stop_speed = 0.05
7081
7182 state = unicycle_model .State (x = - 0.0 , y = - 0.0 , yaw = 0.0 , v = 0.0 )
7283
73- lastIndex = len (cx ) - 1
84+ # lastIndex = len(cx) - 1
7485 time = 0.0
7586 x = [state .x ]
7687 y = [state .y ]
7788 yaw = [state .yaw ]
7889 v = [state .v ]
7990 t = [0.0 ]
8091 target_ind = calc_target_index (state , cx , cy )
81- # print(target_ind)
8292
83- while T >= time and lastIndex > target_ind :
93+ while T >= time :
8494 di , target_ind = pure_pursuit_control (state , cx , cy , target_ind )
8595 ai = PIDControl (speed_profile [target_ind ], state .v )
8696 state = unicycle_model .update (state , ai , di )
8797
88- if abs (state .v ) <= 0.05 :
98+ if abs (state .v ) <= stop_speed :
8999 target_ind += 1
90100
91101 time = time + unicycle_model .dt
92102
103+ # check goal
104+ dx = state .x - goal [0 ]
105+ dy = state .y - goal [1 ]
106+ if math .sqrt (dx ** 2 + dy ** 2 ) <= goal_dis :
107+ print ("Goal" )
108+ break
109+
93110 x .append (state .x )
94111 y .append (state .y )
95112 yaw .append (state .yaw )
96113 v .append (state .v )
97114 t .append (time )
98115
99- plt . cla ()
100- plt . plot ( cx , cy , "-r" , label = "course" )
101- plt .plot (x , y , "ob " , label = "trajectory " )
102- plt .plot (cx [ target_ind ], cy [ target_ind ] , "xg " , label = "target " )
103- plt .axis ( "equal " )
104- plt .grid ( True )
105- plt . title ( "speed:" + str ( round ( state . v , 2 )) +
106- "tind :" + str (target_ind ))
107- plt . pause ( 0.0001 )
108- # input( )
116+ if target_ind % 20 == 0 and animation :
117+ plt . cla ( )
118+ plt .plot (cx , cy , "-r " , label = "course " )
119+ plt .plot (x , y , "ob " , label = "trajectory " )
120+ plt .plot ( cx [ target_ind ], cy [ target_ind ], "xg" , label = "target " )
121+ plt .axis ( "equal" )
122+ plt . grid ( True )
123+ plt . title ( "speed :" + str (round ( state . v , 2 )) +
124+ "tind:" + str ( target_ind ) )
125+ plt . pause ( 0.0001 )
109126
110127 return t , x , y , yaw , v
111128
@@ -185,45 +202,31 @@ def calc_speed_profile(cx, cy, cyaw, target_speed, a):
185202 speed_profile [- i - 1 ] = tspeed
186203
187204 # flg, ax = plt.subplots(1)
188- plt .plot (speed_profile , "-r" )
189- # plt.plot(cx, cy, "-r")
190- plt .show ()
205+ # plt.plot(speed_profile, "-r")
206+ # plt.show()
191207
192208 return speed_profile
193209
194210
195- def main ():
196- import pandas as pd
197- data = pd .read_csv ("rrt_course.csv" )
198- cx = np .array (data ["x" ])
199- cy = np .array (data ["y" ])
200- cyaw = np .array (data ["yaw" ])
201-
202- target_speed = 10.0 / 3.6
203- a = 0.1
211+ def extend_path (cx , cy , cyaw ):
204212
205- speed_profile = calc_speed_profile (cx , cy , cyaw , target_speed , a )
213+ dl = 0.1
214+ dl_list = [dl ] * (int (Lf / dl ) + 0 )
206215
207- t , x , y , yaw , v = closed_loop_prediction (cx , cy , cyaw , speed_profile )
216+ move_direction = math .atan2 (cy [- 1 ] - cy [- 2 ], cx [- 1 ] - cx [- 2 ])
217+ is_back = abs (move_direction - cyaw [- 1 ]) >= math .pi / 2.0
208218
209- flg , ax = plt .subplots (1 )
210- plt .plot (cx , cy , ".r" , label = "course" )
211- plt .plot (x , y , "-b" , label = "trajectory" )
212- plt .legend ()
213- plt .xlabel ("x[m]" )
214- plt .ylabel ("y[m]" )
215- plt .axis ("equal" )
216- plt .grid (True )
219+ for idl in dl_list :
220+ if is_back :
221+ idl *= - 1
222+ cx = np .append (cx , cx [- 1 ] + idl * math .cos (cyaw [- 1 ]))
223+ cy = np .append (cy , cy [- 1 ] + idl * math .sin (cyaw [- 1 ]))
224+ cyaw = np .append (cyaw , cyaw [- 1 ])
217225
218- flg , ax = plt .subplots (1 )
219- plt .plot (t , [iv * 3.6 for iv in v ], "-r" )
220- plt .xlabel ("Time[s]" )
221- plt .ylabel ("Speed[km/h]" )
222- plt .grid (True )
223- plt .show ()
226+ return cx , cy , cyaw
224227
225228
226- def main2 ():
229+ def main ():
227230 # target course
228231 import numpy as np
229232 cx = np .arange (0 , 50 , 0.1 )
@@ -233,8 +236,8 @@ def main2():
233236
234237 T = 15.0 # max simulation time
235238
236- # state = unicycle_model.State(x=-0.0, y=-0 .0, yaw=0.0, v=0.0)
237- state = unicycle_model .State (x = - 1.0 , y = - 5.0 , yaw = 0.0 , v = - 30.0 / 3.6 )
239+ state = unicycle_model .State (x = - 0.0 , y = - 3 .0 , yaw = 0.0 , v = 0.0 )
240+ # state = unicycle_model.State(x=-1.0, y=-5.0, yaw=0.0, v=-30.0 / 3.6)
238241 # state = unicycle_model.State(x=10.0, y=5.0, yaw=0.0, v=-30.0 / 3.6)
239242 # state = unicycle_model.State(
240243 # x=3.0, y=5.0, yaw=math.radians(-40.0), v=-10.0 / 3.6)
@@ -289,6 +292,43 @@ def main2():
289292 plt .show ()
290293
291294
295+ def main2 ():
296+ import pandas as pd
297+ data = pd .read_csv ("rrt_course.csv" )
298+ cx = np .array (data ["x" ])
299+ cy = np .array (data ["y" ])
300+ cyaw = np .array (data ["yaw" ])
301+
302+ target_speed = 10.0 / 3.6
303+ a = 0.1
304+
305+ goal = [cx [- 1 ], cy [- 1 ]]
306+
307+ cx , cy , cyaw = extend_path (cx , cy , cyaw )
308+
309+ speed_profile = calc_speed_profile (cx , cy , cyaw , target_speed , a )
310+
311+ t , x , y , yaw , v = closed_loop_prediction (cx , cy , cyaw , speed_profile , goal )
312+
313+ flg , ax = plt .subplots (1 )
314+ plt .plot (cx , cy , ".r" , label = "course" )
315+ plt .plot (x , y , "-b" , label = "trajectory" )
316+ plt .plot (goal [0 ], goal [1 ], "xg" , label = "goal" )
317+ plt .legend ()
318+ plt .xlabel ("x[m]" )
319+ plt .ylabel ("y[m]" )
320+ plt .axis ("equal" )
321+ plt .grid (True )
322+
323+ flg , ax = plt .subplots (1 )
324+ plt .plot (t , [iv * 3.6 for iv in v ], "-r" )
325+ plt .xlabel ("Time[s]" )
326+ plt .ylabel ("Speed[km/h]" )
327+ plt .grid (True )
328+ plt .show ()
329+
330+
292331if __name__ == '__main__' :
293332 print ("Pure pursuit path tracking simulation start" )
294- main ()
333+ # main()
334+ main2 ()
0 commit comments