Skip to content

Commit 0983395

Browse files
committed
first release CLRRT simulation
1 parent aa26af0 commit 0983395

File tree

3 files changed

+56
-72
lines changed

3 files changed

+56
-72
lines changed

PathPlanning/CRRRTStar/cr_rrt_star_car.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33
"""
4-
@brief: Path Planning Sample Code with Closed roop RRT for car like robot.
4+
@brief: Path Planning Sample Code with Closed loop RRT for car like robot.
55
66
@author: AtsushiSakai(@Atsushi_twi)
77
@@ -20,10 +20,6 @@
2020

2121

2222
target_speed = 10.0 / 3.6
23-
accel = 0.1
24-
25-
# TODO
26-
# 制約条件をいれる
2723

2824

2925
class RRT():
@@ -32,7 +28,7 @@ class RRT():
3228
"""
3329

3430
def __init__(self, start, goal, obstacleList, randArea,
35-
maxIter=100):
31+
maxIter=200):
3632
u"""
3733
Setting Parameter
3834
@@ -109,9 +105,6 @@ def search_best_feasible_path(self, path_indexs):
109105
flag, x, y, yaw, v, t, a, d = self.check_tracking_path_is_feasible(
110106
path)
111107

112-
# print(t[-1])
113-
# print(ind)
114-
115108
if flag and best_time >= t[-1]:
116109
print("feasible path is found")
117110
best_time = t[-1]
@@ -169,13 +162,15 @@ def check_tracking_path_is_feasible(self, path):
169162
cx, cy, cyaw = pure_pursuit.extend_path(cx, cy, cyaw)
170163

171164
speed_profile = pure_pursuit.calc_speed_profile(
172-
cx, cy, cyaw, target_speed, accel)
165+
cx, cy, cyaw, target_speed)
173166

174167
t, x, y, yaw, v, a, d, find_goal = pure_pursuit.closed_loop_prediction(
175168
cx, cy, cyaw, speed_profile, goal)
176-
177169
yaw = [self.pi_2_pi(iyaw) for iyaw in yaw]
178170

171+
if not find_goal:
172+
print("cannot reach goal")
173+
179174
if abs(yaw[-1] - goal[2]) >= math.pi / 4.0:
180175
print("final angle is bad")
181176
find_goal = False
@@ -194,12 +189,12 @@ def check_tracking_path_is_feasible(self, path):
194189
print("This path is collision")
195190
find_goal = False
196191

197-
plt.clf
198-
plt.plot(x, y, '-r')
199-
plt.plot(path[:, 0], path[:, 1], '-g')
200-
plt.grid(True)
201-
plt.axis("equal")
202-
plt.show()
192+
# plt.clf
193+
# plt.plot(x, y, '-r')
194+
# plt.plot(path[:, 0], path[:, 1], '-g')
195+
# plt.grid(True)
196+
# plt.axis("equal")
197+
# plt.show()
203198

204199
return find_goal, x, y, yaw, v, t, a, d
205200

@@ -430,12 +425,24 @@ def __init__(self, x, y, yaw):
430425
(7, 5, 2),
431426
(9, 5, 2)
432427
] # [x,y,size(radius)]
428+
obstacleList = [
429+
(5, 5, 1),
430+
(4, 6, 1),
431+
(4, 8, 1),
432+
(4, 10, 1),
433+
(6, 5, 1),
434+
(7, 5, 1),
435+
(8, 6, 1),
436+
(8, 8, 1),
437+
(8, 10, 1)
438+
] # [x,y,size(radius)]
433439

434440
# Set Initial parameters
435441
start = [0.0, 0.0, math.radians(0.0)]
436-
goal = [10.0, 10.0, math.radians(0.0)]
442+
# goal = [10.0, 10.0, math.radians(0.0)]
443+
goal = [6.0, 7.0, math.radians(90.0)]
437444

438-
rrt = RRT(start, goal, randArea=[-2.0, 15.0], obstacleList=obstacleList)
445+
rrt = RRT(start, goal, randArea=[-2.0, 20.0], obstacleList=obstacleList)
439446
flag, x, y, yaw, v, t, a, d = rrt.Planning(animation=False)
440447

441448
if not flag:

PathPlanning/CRRRTStar/pure_pursuit.py

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Lf = 0.5 # look-ahead distance
1717
T = 100.0 # max simulation time
1818
goal_dis = 0.5
19-
stop_speed = 0.1
19+
stop_speed = 0.5
2020

2121
# animation = True
2222
animation = False
@@ -35,7 +35,7 @@ def PIDControl(target, current):
3535

3636
def 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

7167
def 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

9088
def 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

228207
def 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)

PathPlanning/CRRRTStar/unicycle_model.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
L = 2.9 # [m]
1313
steer_max = math.radians(40.0)
1414
curvature_max = math.tan(steer_max) / L
15-
curvature_max = 1.0 / curvature_max
16-
# print(curvature_max)
15+
curvature_max = 1.0 / curvature_max + 1.0
1716

1817
accel_max = 5.0
1918

0 commit comments

Comments
 (0)