-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrrt.py
296 lines (228 loc) · 9.35 KB
/
rrt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import math
import random
import matplotlib.pyplot as plt
import numpy as np
from math import sin, cos, pi
class RRT:
class Node:
def __init__(self, x, y):
self.x = x
self.y = y
self.path_x = []
self.path_y = []
self.parent = None
class Edge:
def __init__(self, fnode, tnode):
self.fromx = fnode.x
self.fromy = fnode.y
self.tox = tnode.x
self.toy = tnode.y
self.from_node = fnode
self.to_node = tnode
def __init__(self, start, goal, obstacle_list, rand_area,
expand_dis=0.5, path_resolution=0.5, goal_sample_rate=5, max_iter=500):
self.start = self.Node(start[0], start[1])
self.end = self.Node(goal[0], goal[1])
self.min_randx = rand_area[0]
self.max_randx = rand_area[1]
self.min_randy = rand_area[2]
self.max_randy = rand_area[3]
self.expand_dis = expand_dis
self.path_resolution = path_resolution
self.goal_sample_rate = goal_sample_rate
self.max_iter = max_iter
self.obstacle_list = obstacle_list
self.node_list = []
self.edge_list = []
def planning(self):
self.node_list = [self.start]
for i in range(self.max_iter):
rnd_node = self.get_random_node()
nearest_ind = self.get_nearest_node_index(self.node_list, rnd_node)
nearest_node = self.node_list[nearest_ind]
new_node = self.steer(nearest_node, rnd_node, self.expand_dis)
if self.check_node_collision(new_node, self.obstacle_list):
self.node_list.append(new_node)
if i % 5 == 0:
self.draw_graph(rnd_node)
if self.calc_dist_to_goal(self.node_list[-1].x, self.node_list[-1].y) <= self.expand_dis:
final_node = self.steer(self.node_list[-1], self.end, self.expand_dis)
if self.check_node_collision(final_node, self.obstacle_list):
return self.generate_final_course(len(self.node_list) - 1)
if i % 5:
self.draw_graph(rnd_node)
return None
def steer(self, from_node, to_node, extend_length=float("inf")):
new_node = self.Node(from_node.x, from_node.y)
d, theta = self.calc_distance_and_angle(new_node, to_node)
new_node.path_x = [new_node.x]
new_node.path_y = [new_node.y]
if extend_length > d:
extend_length = d
n_expand = math.floor(extend_length / self.path_resolution)
for _ in range(n_expand):
new_node.x += self.path_resolution * math.cos(theta)
new_node.y += self.path_resolution * math.sin(theta)
new_node.path_x.append(new_node.x)
new_node.path_y.append(new_node.y)
d, _ = self.calc_distance_and_angle(new_node, to_node)
if d <= self.path_resolution:
new_node.path_x.append(to_node.x)
new_node.path_y.append(to_node.y)
new_node.parent = from_node
return new_node
def generate_final_course(self, goal_ind):
path = [[self.end.x, self.end.y]]
node = self.node_list[goal_ind]
while node.parent is not None:
path.append([node.x, node.y])
node = node.parent
path.append([node.x, node.y])
return path
def calc_dist_to_goal(self, x, y):
dx = x - self.end.x
dy = y - self.end.y
# Euclidean norm
return math.hypot(dx, dy)
def get_random_node(self):
if random.randint(0, 100) >= self.goal_sample_rate:
rnd = self.Node(random.uniform(self.min_randx, self.max_randx),
random.uniform(self.min_randy, self.max_randy))
else:
rnd = self.Node(self.end.x, self.end.y)
return rnd
def draw_graph(self, rnd=None):
plt.clf()
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
if rnd is not None:
plt.plot(rnd.x, rnd.y, "^k")
for node in self.node_list:
if node.parent:
plt.plot(node.path_x, node.path_y, "-g")
for (ox, oy, size) in self.obstacle_list:
self.plot_circle(ox, oy, size)
plt.plot(self.start.x, self.start.y, "xr")
plt.plot(self.end.x, self.end.y, "xr")
plt.axis("equal")
plt.axis([-3, 3, -1, 1])
plt.grid(True)
plt.pause(0.01)
@staticmethod
def check_collision(node, obstacleList, area):
if node is None:
return False
if node.x < area[0] or node.x > area[1] or node.y < area[2] or node.y > area[3]:
return False
for (ox, oy, size) in obstacleList:
dx_list = [ox - x for x in node.path_x]
dy_list = [oy - y for y in node.path_y]
d_list = [dx * dx + dy * dy for (dx, dy) in zip(dx_list, dy_list)]
if min(d_list) <= size ** 2:
return False
return True
@staticmethod
def plot_circle(x, y, size, color="-k"):
deg = list(range(0, 360, 5))
deg.append(0)
xl = [x + size * math.cos(np.deg2rad(d)) for d in deg]
yl = [y + size * math.sin(np.deg2rad(d)) for d in deg]
plt.plot(xl, yl, color)
def getAngle(self, a, b, c):
ang = math.degrees(abs(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0])))
if ang < 0:
ang = ang + 180
if ang > 180:
ang = ang - 180
return ang
def get_nearest_point_on_edge(self, edge, node):
angle = (edge.toy - edge.fromy) / (edge.tox - edge.fromx)
a = edge.fromy - edge.toy
b = -(edge.fromx - edge.tox)
c = edge.fromx*edge.toy + edge.tox*edge.fromy
if self.getAngle((edge.fromx,edge.fromy),(edge.tox,edge.toy),(node.x,node.y)) < 90 and self.getAngle((edge.tox,edge.toy),(edge.fromx,edge.fromy),(node.x,node.y)) < 90:
temp = -1 * (a * node.x + b * node.y + c) / (a * a + b * b)
x = temp * a + node.x
y = temp * b + node.y
new_node = self.Node(x, y)
new_node.path_x = edge.from_node.path_x
new_node.path_y = edge.from_node.path_y
new_node.parent = edge.from_node
edge.to_node.parent = new_node
return new_node
elif math.hypot((edge.fromx - node.x),(edge.fromy - node.y)) > math.hypot((edge.tox - node.x),(edge.toy - node.y)):
return edge.to_node
return edge.from_node
def get_edge_dist(self, edge, node):
a = edge.fromy - edge.toy
b = -(edge.fromx - edge.tox)
c = edge.fromx*edge.toy + edge.tox*edge.fromy
if self.getAngle((edge.fromx,edge.fromy),(edge.tox,edge.toy),(node.x,node.y)) < 90 and self.getAngle((edge.tox,edge.toy),(edge.fromx,edge.fromy),(node.x,node.y)) < 90:
return abs(a*node.x+b*node.y+c)/math.hypot(a,b)
return min(math.hypot(node.x-edge.fromx,node.y-edge.fromy), math.hypot(node.x-edge.tox,node.y-edge.toy))
@staticmethod
def get_nearest_node_index(node_list, rnd_node):
dlist = [math.sqrt((node.x - rnd_node.x) ** 2 + (node.y - rnd_node.y)
** 2) for node in node_list]
minind = dlist.index(min(dlist))
return minind
def get_nearest_edge_index(self, edge_list, rnd_node):
dlist = [self.get_edge_dist(edge, rnd_node) for edge in edge_list]
minind = dlist.index(min(dlist))
return minind
@staticmethod
def check_node_collision(node, obstacleList):
if node is None:
return False
for (ox, oy, size) in obstacleList:
dx_list = [ox - x for x in node.path_x]
dy_list = [oy - y for y in node.path_y]
d_list = [dx * dx + dy * dy for (dx, dy) in zip(dx_list, dy_list)]
if min(d_list) <= size ** 2:
return False # collision
return True
def check_edge_collision(self, edge, obstacleList):
if edge is None:
return False
for (ox, oy, size) in obstacleList:
if self.get_edge_dist(edge, self.Node(ox,oy)) < size:
return False # collision
return True
@staticmethod
def calc_distance_and_angle(from_node, to_node):
dx = to_node.x - from_node.x
dy = to_node.y - from_node.y
d = math.hypot(dx, dy)
theta = math.atan2(dy, dx)
return d, theta
def plot_circle(x, y, size, color="-k"):
deg = list(range(0, 360, 5))
deg.append(0)
xl = [x + size * math.cos(np.deg2rad(d)) for d in deg]
yl = [y + size * math.sin(np.deg2rad(d)) for d in deg]
plt.plot(xl, yl, color)
def main():
sx=-2.0
sy=-0.5
gx=2.0
gy=-0.5
obstacleList = [
(0, 1, 0.98),
(0, -1, 0.98)
]
rrt = RRT(start=[sx, sy],
goal=[gx, gy],
rand_area=[-3, 3, -1, 1],
obstacle_list=obstacleList)
path = rrt.planning()
if path is None:
print("Cannot find path")
else:
print("found path")
rrt.draw_graph()
plt.plot([x for (x, y) in path], [y for (x, y) in path], '-r')
plt.grid(True)
plt.pause(0.01)
plt.show()
if __name__ == '__main__':
main()