1616Lf = 0.5 # look-ahead distance
1717T = 100.0 # max simulation time
1818goal_dis = 0.5
19- stop_speed = 0.1
19+ stop_speed = 0.5
2020
2121# animation = True
2222animation = False
@@ -35,7 +35,7 @@ def PIDControl(target, current):
3535
3636def pure_pursuit_control (state , cx , cy , pind ):
3737
38- ind = calc_target_index (state , cx , cy )
38+ ind , dis = calc_target_index (state , cx , cy )
3939
4040 if pind >= ind :
4141 ind = pind
@@ -53,10 +53,6 @@ def pure_pursuit_control(state, cx, cy, pind):
5353
5454 if state .v <= 0.0 : # back
5555 alpha = math .pi - alpha
56- # if alpha > 0:
57- # alpha = math.pi - alpha
58- # else:
59- # alpha = math.pi + alpha
6056
6157 delta = math .atan2 (2.0 * unicycle_model .L * math .sin (alpha ) / Lf , 1.0 )
6258
@@ -65,16 +61,17 @@ def pure_pursuit_control(state, cx, cy, pind):
6561 elif delta < - unicycle_model .steer_max :
6662 delta = - unicycle_model .steer_max
6763
68- return delta , ind
64+ return delta , ind , dis
6965
7066
7167def calc_target_index (state , cx , cy ):
7268 dx = [state .x - icx for icx in cx ]
7369 dy = [state .y - icy for icy in cy ]
7470
7571 d = [abs (math .sqrt (idx ** 2 + idy ** 2 )) for (idx , idy ) in zip (dx , dy )]
72+ mindis = min (d )
7673
77- ind = d .index (min ( d ) )
74+ ind = d .index (mindis )
7875
7976 L = 0.0
8077
@@ -84,7 +81,8 @@ def calc_target_index(state, cx, cy):
8481 L += math .sqrt (dx ** 2 + dy ** 2 )
8582 ind += 1
8683
87- return ind
84+ # print(mindis)
85+ return ind , mindis
8886
8987
9088def closed_loop_prediction (cx , cy , cyaw , speed_profile , goal ):
@@ -100,15 +98,22 @@ def closed_loop_prediction(cx, cy, cyaw, speed_profile, goal):
10098 t = [0.0 ]
10199 a = [0.0 ]
102100 d = [0.0 ]
103- target_ind = calc_target_index (state , cx , cy )
101+ target_ind , mindis = calc_target_index (state , cx , cy )
104102 find_goal = False
105103
104+ maxdis = 0.5
105+
106106 while T >= time :
107- di , target_ind = pure_pursuit_control (state , cx , cy , target_ind )
108- ai = PIDControl (speed_profile [target_ind ], state .v )
107+ di , target_ind , dis = pure_pursuit_control (state , cx , cy , target_ind )
108+
109+ target_speed = speed_profile [target_ind ]
110+ target_speed = target_speed * \
111+ (maxdis - min (dis , maxdis - 0.1 )) / maxdis
112+
113+ ai = PIDControl (target_speed , state .v )
109114 state = unicycle_model .update (state , ai , di )
110115
111- if abs (state .v ) <= stop_speed :
116+ if abs (state .v ) <= stop_speed and target_ind <= len ( cx ) - 2 :
112117 target_ind += 1
113118
114119 time = time + unicycle_model .dt
@@ -139,6 +144,9 @@ def closed_loop_prediction(cx, cy, cyaw, speed_profile, goal):
139144 "tind:" + str (target_ind ))
140145 plt .pause (0.0001 )
141146
147+ else :
148+ print ("Time out!!" )
149+
142150 return t , x , y , yaw , v , a , d , find_goal
143151
144152
@@ -176,61 +184,32 @@ def set_stop_point(target_speed, cx, cy, cyaw):
176184 # plt.plot(cx[i], cy[i], "xb")
177185 # print(iyaw, move_direction, dx, dy)
178186 speed_profile [0 ] = 0.0
179- speed_profile [- 1 ] = 0.0
187+ if is_back :
188+ speed_profile [- 1 ] = - stop_speed
189+ else :
190+ speed_profile [- 1 ] = stop_speed
180191
181192 d .append (d [- 1 ])
182193
183194 return speed_profile , d
184195
185196
186- def calc_speed_profile (cx , cy , cyaw , target_speed , a ):
197+ def calc_speed_profile (cx , cy , cyaw , target_speed ):
187198
188199 speed_profile , d = set_stop_point (target_speed , cx , cy , cyaw )
189200
190- # nsp = len(speed_profile)
191-
192201 if animation :
193202 plt .plot (speed_profile , "xb" )
194203
195- # forward integration
196- # for i in range(nsp - 1):
197-
198- # if speed_profile[i + 1] >= 0: # forward
199- # tspeed = speed_profile[i] + a * d[i]
200- # if tspeed <= speed_profile[i + 1]:
201- # speed_profile[i + 1] = tspeed
202- # else:
203- # tspeed = speed_profile[i] - a * d[i]
204- # if tspeed >= speed_profile[i + 1]:
205- # speed_profile[i + 1] = tspeed
206-
207- # if animation:
208- # plt.plot(speed_profile, "ok")
209-
210- # # back integration
211- # for i in range(nsp - 1):
212- # if speed_profile[- i - 1] >= 0: # forward
213- # tspeed = speed_profile[-i] + a * d[-i]
214- # if tspeed <= speed_profile[-i - 1]:
215- # speed_profile[-i - 1] = tspeed
216- # else:
217- # tspeed = speed_profile[-i] - a * d[-i]
218- # if tspeed >= speed_profile[-i - 1]:
219- # speed_profile[-i - 1] = tspeed
220-
221- # if animation:
222- # plt.plot(speed_profile, "-r")
223- # plt.show()
224-
225204 return speed_profile
226205
227206
228207def extend_path (cx , cy , cyaw ):
229208
230209 dl = 0.1
231- dl_list = [dl ] * (int (Lf / dl ) + 10 )
210+ dl_list = [dl ] * (int (Lf / dl ) + 1 )
232211
233- move_direction = math .atan2 (cy [- 1 ] - cy [- 2 ], cx [- 1 ] - cx [- 2 ])
212+ move_direction = math .atan2 (cy [- 1 ] - cy [- 3 ], cx [- 1 ] - cx [- 3 ])
234213 is_back = abs (move_direction - cyaw [- 1 ]) >= math .pi / 2.0
235214
236215 for idl in dl_list :
@@ -317,13 +296,12 @@ def main2():
317296 cyaw = np .array (data ["yaw" ])
318297
319298 target_speed = 10.0 / 3.6
320- a = 2.0
321299
322300 goal = [cx [- 1 ], cy [- 1 ]]
323301
324302 cx , cy , cyaw = extend_path (cx , cy , cyaw )
325303
326- speed_profile = calc_speed_profile (cx , cy , cyaw , target_speed , a )
304+ speed_profile = calc_speed_profile (cx , cy , cyaw , target_speed )
327305
328306 t , x , y , yaw , v , a , d , flag = closed_loop_prediction (
329307 cx , cy , cyaw , speed_profile , goal )
0 commit comments