Skip to content

Commit d9508f6

Browse files
committed
fix rrt_star_dubins
1 parent 2bf23ad commit d9508f6

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

PathPlanning/DubinsPath/dubins_path_planning.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
77
"""
88
import math
9-
import numpy as np
9+
1010
import matplotlib.pyplot as plt
11+
import numpy as np
1112

1213
show_animation = True
1314

@@ -136,8 +137,8 @@ def LRL(alpha, beta, d):
136137
return t, p, q, mode
137138

138139

139-
def dubins_path_planning_from_origin(ex, ey, eyaw, c):
140-
# nomalize
140+
def dubins_path_planning_from_origin(ex, ey, eyaw, c, D_ANGLE):
141+
# normalize
141142
dx = ex
142143
dy = ey
143144
D = math.sqrt(dx ** 2.0 + dy ** 2.0)
@@ -165,12 +166,12 @@ def dubins_path_planning_from_origin(ex, ey, eyaw, c):
165166
bcost = cost
166167

167168
# print(bmode)
168-
px, py, pyaw = generate_course([bt, bp, bq], bmode, c)
169+
px, py, pyaw = generate_course([bt, bp, bq], bmode, c, D_ANGLE)
169170

170171
return px, py, pyaw, bmode, bcost
171172

172173

173-
def dubins_path_planning(sx, sy, syaw, ex, ey, eyaw, c):
174+
def dubins_path_planning(sx, sy, syaw, ex, ey, eyaw, c, D_ANGLE=np.deg2rad(10.0)):
174175
"""
175176
Dubins path plannner
176177
@@ -199,7 +200,7 @@ def dubins_path_planning(sx, sy, syaw, ex, ey, eyaw, c):
199200
leyaw = eyaw - syaw
200201

201202
lpx, lpy, lpyaw, mode, clen = dubins_path_planning_from_origin(
202-
lex, ley, leyaw, c)
203+
lex, ley, leyaw, c, D_ANGLE)
203204

204205
px = [math.cos(-syaw) * x + math.sin(-syaw)
205206
* y + sx for x, y in zip(lpx, lpy)]
@@ -210,7 +211,7 @@ def dubins_path_planning(sx, sy, syaw, ex, ey, eyaw, c):
210211
return px, py, pyaw, mode, clen
211212

212213

213-
def generate_course(length, mode, c):
214+
def generate_course(length, mode, c, D_ANGLE):
214215

215216
px = [0.0]
216217
py = [0.0]
@@ -221,7 +222,7 @@ def generate_course(length, mode, c):
221222
if m == "S":
222223
d = 1.0 * c
223224
else: # turning couse
224-
d = np.deg2rad(3.0)
225+
d = D_ANGLE
225226

226227
while pd < abs(l - d):
227228
# print(pd, l)

PathPlanning/RRT/rrt.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class RRT:
1919
Class for RRT planning
2020
"""
2121

22-
class Node():
22+
class Node:
2323
"""
2424
RRT Node
2525
"""
@@ -104,6 +104,13 @@ def steer(self, from_node, to_node, extend_length=float("inf")):
104104
new_node.path_x.append(new_node.x)
105105
new_node.path_y.append(new_node.y)
106106

107+
d, _ = self.calc_distance_and_angle(new_node, to_node)
108+
if d <= self.path_resolution:
109+
new_node.x = to_node.x
110+
new_node.y = to_node.y
111+
new_node.path_x[-1] = to_node.x
112+
new_node.path_y[-1] = to_node.y
113+
107114
new_node.parent = from_node
108115

109116
return new_node
@@ -137,9 +144,7 @@ def draw_graph(self, rnd=None):
137144
plt.plot(rnd.x, rnd.y, "^k")
138145
for node in self.node_list:
139146
if node.parent:
140-
plt.plot([node.x, node.parent.x],
141-
[node.y, node.parent.y],
142-
"-g")
147+
plt.plot(node.path_x, node.path_y, "-g")
143148

144149
for (ox, oy, size) in self.obstacle_list:
145150
plt.plot(ox, oy, "ok", ms=30 * size)

PathPlanning/RRTStar/rrt_star.py

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def __init__(self, start, goal, obstacle_list, rand_area,
5353
"""
5454
self.connect_circle_dist = connect_circle_dist
5555

56-
def planning(self, animation=True, search_until_maxiter=True):
56+
def planning(self, animation=True, search_until_max_iter=True):
5757
"""
5858
rrt star path planning
5959
@@ -63,7 +63,7 @@ def planning(self, animation=True, search_until_maxiter=True):
6363

6464
self.node_list = [self.start]
6565
for i in range(self.max_iter):
66-
print(i, len(self.node_list))
66+
print("Iter:", i, ", number of nodes:", len(self.node_list))
6767
rnd = self.get_random_node()
6868
nearest_ind = self.get_nearest_list_index(self.node_list, rnd)
6969
new_node = self.steer(self.node_list[nearest_ind], rnd, self.expand_dis)
@@ -78,10 +78,10 @@ def planning(self, animation=True, search_until_maxiter=True):
7878
if animation and i % 5 == 0:
7979
self.draw_graph(rnd)
8080

81-
if (not search_until_maxiter) and new_node: # check reaching the goal
82-
d, _ = self.calc_distance_and_angle(new_node, self.end)
83-
if d <= self.expand_dis:
84-
return self.generate_final_course(len(self.node_list) - 1)
81+
if (not search_until_max_iter) and new_node: # check reaching the goal
82+
last_index = self.search_best_goal_node()
83+
if last_index:
84+
return self.generate_final_course(last_index)
8585

8686
print("reached max iteration")
8787

@@ -91,25 +91,6 @@ def planning(self, animation=True, search_until_maxiter=True):
9191

9292
return None
9393

94-
def connect_nodes(self, from_node, to_node):
95-
new_node = self.Node(from_node.x, from_node.y)
96-
d, theta = self.calc_distance_and_angle(new_node, to_node)
97-
98-
new_node.path_x = [new_node.x]
99-
new_node.path_y = [new_node.y]
100-
101-
n_expand = math.floor(d / self.path_resolution)
102-
103-
for _ in range(n_expand):
104-
new_node.x += self.path_resolution * math.cos(theta)
105-
new_node.y += self.path_resolution * math.sin(theta)
106-
new_node.path_x.append(new_node.x)
107-
new_node.path_y.append(new_node.y)
108-
109-
new_node.parent = from_node
110-
111-
return new_node
112-
11394
def choose_parent(self, new_node, near_inds):
11495
if not near_inds:
11596
return None
@@ -130,8 +111,8 @@ def choose_parent(self, new_node, near_inds):
130111
return None
131112

132113
min_ind = near_inds[costs.index(min_cost)]
114+
new_node = self.steer(self.node_list[min_ind], new_node)
133115
new_node.parent = self.node_list[min_ind]
134-
new_node = self.steer(new_node.parent, new_node)
135116
new_node.cost = min_cost
136117

137118
return new_node
@@ -162,21 +143,22 @@ def rewire(self, new_node, near_inds):
162143
for i in near_inds:
163144
near_node = self.node_list[i]
164145
edge_node = self.steer(new_node, near_node)
165-
edge_node.cost = self.calc_new_cost(near_node, new_node)
146+
edge_node.cost = self.calc_new_cost(new_node, near_node)
166147

167148
no_collision = self.check_collision(edge_node, self.obstacle_list)
168149
improved_cost = near_node.cost > edge_node.cost
169150

170151
if no_collision and improved_cost:
152+
near_node = edge_node
171153
near_node.parent = new_node
172-
near_node.cost = edge_node.cost
173154
self.propagate_cost_to_leaves(new_node)
174155

175156
def calc_new_cost(self, from_node, to_node):
176157
d, _ = self.calc_distance_and_angle(from_node, to_node)
177158
return from_node.cost + d
178159

179160
def propagate_cost_to_leaves(self, parent_node):
161+
180162
for node in self.node_list:
181163
if node.parent == parent_node:
182164
node.cost = self.calc_new_cost(parent_node, node)

0 commit comments

Comments
 (0)