Skip to content

Commit 3e344b4

Browse files
committed
improve code
1 parent a5c81da commit 3e344b4

File tree

2 files changed

+273
-79
lines changed

2 files changed

+273
-79
lines changed

PathPlanning/CRRRTStar/cr_rrt_star_car.py

Lines changed: 42 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
import random
1313
import math
1414
import copy
15-
import pandas as pd
1615
import numpy as np
1716
import reeds_shepp_path_planning
1817
import pure_pursuit
19-
import unicycle_model
18+
import pandas as pd
19+
20+
21+
target_speed = 10.0 / 3.6
22+
accel = 0.1
23+
curvature = 10.0
2024

2125

2226
class RRT():
@@ -82,7 +86,7 @@ def Planning(self, animation=True):
8286
print("feasible path is found")
8387
break
8488

85-
return x, y, yaw, v, t, a, d
89+
return flag, x, y, yaw, v, t, a, d
8690

8791
def calc_tracking_path(self, path):
8892
path = np.matrix(path[::-1])
@@ -106,67 +110,43 @@ def calc_tracking_path(self, path):
106110

107111
def check_tracking_path_is_feasible(self, path):
108112
print("check_tracking_path_is_feasible")
113+
path = np.matrix(path[::-1])
109114

110-
init_speed = 0.0
111-
max_speed = 10.0 / 3.6
112-
113-
path = self.calc_tracking_path(path)
114-
# speed_profile = self.calc_speed_profile(path, max_speed)
115-
116-
# plt.plot(path[:, 0], path[:, 1], '-xg')
117115
# save csv
118116
df = pd.DataFrame()
119117
df["x"] = np.array(path[:, 0]).flatten()
120118
df["y"] = np.array(path[:, 1]).flatten()
121119
df["yaw"] = np.array(path[:, 2]).flatten()
122120
df.to_csv("rrt_course.csv", index=None)
123121

124-
state = unicycle_model.State(
125-
x=self.start.x, y=self.start.y, yaw=self.start.yaw, v=init_speed)
126-
127-
target_ind = pure_pursuit.calc_nearest_index(
128-
state, path[:, 0], path[:, 1])
129-
130-
lastIndex = len(path[:, 0]) - 2
131-
132-
x = [state.x]
133-
y = [state.y]
134-
yaw = [state.yaw]
135-
v = [state.v]
136-
t = [0.0]
137-
a = [0.0]
138-
d = [0.0]
139-
time = 0.0
140-
141-
while lastIndex > target_ind:
142-
# print(lastIndex, target_ind)
143-
ai = pure_pursuit.PIDControl(max_speed, state.v)
144-
di, target_ind = pure_pursuit.pure_pursuit_control(
145-
state, path[:, 0], path[:, 1], target_ind)
146-
state = unicycle_model.update(state, ai, di)
147-
148-
time = time + unicycle_model.dt
149-
150-
x.append(state.x)
151-
y.append(state.y)
152-
yaw.append(state.yaw)
153-
v.append(state.v)
154-
t.append(time)
155-
a.append(ai)
156-
d.append(di)
157-
158-
if self.CollisionCheckWithXY(path[:, 0], path[:, 1], self.obstacleList):
159-
# print("OK")
160-
return True, x, y, yaw, v, t, a, d
161-
else:
162-
# print("NG")
163-
return False, x, y, yaw, v, t, a, d
164-
165-
# plt.plot(x, y, '-r')
166-
# plt.plot(path[:, 0], path[:, 1], '-g')
167-
# plt.show()
122+
cx = np.array(path[:, 0])
123+
cy = np.array(path[:, 1])
124+
cyaw = np.array(path[:, 2])
168125

169-
# return True
126+
goal = [cx[-1], cy[-1], cyaw[-1]]
127+
128+
cx, cy, cyaw = pure_pursuit.extend_path(cx, cy, cyaw)
129+
130+
speed_profile = pure_pursuit.calc_speed_profile(
131+
cx, cy, cyaw, target_speed, accel)
132+
133+
t, x, y, yaw, v, a, d, find_goal = pure_pursuit.closed_loop_prediction(
134+
cx, cy, cyaw, speed_profile, goal)
135+
136+
if abs(yaw[-1] - goal[2]) >= math.pi / 2.0:
137+
print(yaw[-1], goal[2])
138+
find_goal = False
139+
if not find_goal:
140+
print("This path is bad")
141+
142+
plt.clf
143+
plt.plot(x, y, '-r')
144+
plt.plot(path[:, 0], path[:, 1], '-g')
145+
plt.grid(True)
146+
plt.axis("equal")
147+
plt.show()
148+
149+
return find_goal, x, y, yaw, v, t, a, d
170150

171151
def choose_parent(self, newNode, nearinds):
172152
if len(nearinds) == 0:
@@ -202,7 +182,6 @@ def pi_2_pi(self, angle):
202182

203183
def steer(self, rnd, nind):
204184
# print(rnd)
205-
curvature = 5.0
206185

207186
nearestNode = self.nodeList[nind]
208187

@@ -313,6 +292,7 @@ def DrawGraph(self, rnd=None):
313292
for node in self.nodeList:
314293
if node.parent is not None:
315294
plt.plot(node.path_x, node.path_y, "-g")
295+
pass
316296
# plt.plot([node.x, self.nodeList[node.parent].x], [
317297
# node.y, self.nodeList[node.parent].y], "-g")
318298

@@ -401,7 +381,11 @@ def __init__(self, x, y, yaw):
401381
goal = [10.0, 10.0, math.radians(0.0)]
402382

403383
rrt = RRT(start, goal, randArea=[-2.0, 15.0], obstacleList=obstacleList)
404-
x, y, yaw, v, t, a, d = rrt.Planning(animation=False)
384+
flag, x, y, yaw, v, t, a, d = rrt.Planning(animation=False)
385+
386+
if not flag:
387+
print("cannot find feasible path")
388+
exit()
405389

406390
# flg, ax = plt.subplots(1)
407391
# Draw final path
@@ -429,7 +413,7 @@ def __init__(self, x, y, yaw):
429413
flg, ax = plt.subplots(1)
430414
plt.plot(t, a, '-r')
431415
plt.xlabel("time[s]")
432-
plt.ylabel("input[m/s]")
416+
plt.ylabel("accel[m/ss]")
433417
plt.grid(True)
434418

435419
flg, ax = plt.subplots(1)

0 commit comments

Comments
 (0)