-
Notifications
You must be signed in to change notification settings - Fork 2
/
rrt.py
268 lines (220 loc) · 7.74 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
import cv2
import numpy as np
import copy
import random
import math
class Node(object):
def __init__(self, pos=[0, 0]):
self.pos = pos
self.parent = None
class RRT(object):
def __init__(self, map_path, qstart, qgoal, grid_size, step_size,
max_steps=1000, goal_prob=0.0):
'''
initialize RRT
'''
self.vertices = [] # 树的节点
self.edges = [] # 树的边
self.path = [] # 路径
self.qstart = Node(qstart) # 起点
self.qgoal = Node(qgoal) # 终点
self.step_size = step_size # 步长
self.max_steps = max_steps # 最大迭代次数
self.goal_prob = goal_prob # 随机趋向终点概率
self.grid_size = grid_size # 网格边长
self.MapPreProcess(map_path) # 初始化地图
def MapPreProcess(self, map_path):
'''
convert map image to binary image
'''
self.src_map = cv2.imread(map_path)
self.map = cv2.cvtColor(self.src_map, cv2.COLOR_BGR2GRAY)
_, self.map = cv2.threshold(
self.map, 0, 255, cv2.THRESH_BINARY_INV)
self.map_shape = np.shape(self.map)
cv2.imshow('RRT', self.src_map)
cv2.waitKey(50)
def GenerateRandomNode(self):
'''
qrand = a randomly chosen free configuration
'''
if random.random() > goal_prob:
row = random.randrange(0, self.map_shape[0])
col = random.randrange(0, self.map_shape[1])
return Node([row, col])
return self.qgoal
def Distance(self, p1, p2):
'''
calculate the distance between p1 and p2
'''
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
def FindNearestNode(self, q):
'''
qnear = closest neighbor of q in T
'''
min_distance = float('inf')
for node in self.vertices:
distance = self.Distance(node.pos, q.pos)
if distance < min_distance:
min_distance = distance
qnear = node
return min_distance, qnear
def ExtendTree(self, qnear, qrand):
'''
progress qnear by step_size along the straight line in Q(map) between qnear and qrand
if qrand is close to qnear, ignore qrand and return None
'''
vec = [qrand.pos[0] - qnear.pos[0], qrand.pos[1] - qnear.pos[1]]
norm_vec = math.sqrt(vec[0] ** 2 + vec[1] ** 2)
if norm_vec < 0.0001:
return None
vec = [vec[0] / norm_vec, vec[1] / norm_vec]
qnew_pos = [int(qnear.pos[0] + self.step_size * vec[0]),
int(qnear.pos[1] + self.step_size * vec[1])]
if qnew_pos[0] < 0 or qnew_pos[0] >= self.map_shape[0]:
return None
if qnew_pos[1] < 0 or qnew_pos[1] >= self.map_shape[1]:
return None
return Node(qnew_pos)
def IsObstacle(self, p):
'''
check the grid of pos p in map is obstacle or not
'''
half_grid_size = int(grid_size / 2)
area = self.map[p[1] - half_grid_size: p[1] + half_grid_size,
p[0] - half_grid_size: p[0] + half_grid_size]
if np.sum(area):
return True
return False
def CollsionFree(self, qnear, qnew):
'''
check qnear to qnew is collsion-free
'''
rows = qnew.pos[0] - qnear.pos[0]
cols = qnew.pos[1] - qnear.pos[1]
length = max(abs(rows), abs(cols))
for i in range(0, length, int(self.grid_size / 2)):
row = int(qnear.pos[0] + i * rows / length)
col = int(qnear.pos[1] + i * cols / length)
if self.IsObstacle([row, col]):
return False
# the qnew may not be contained above, confirm qnew check
return not self.IsObstacle(qnew.pos)
def AddVertices(self, qnew):
'''
add qnew to vertices
'''
self.vertices.append(qnew)
return
def AddEdges(self, qnear, qnew):
'''
here we use a pointer to point to qnear as qnew's parent
'''
qnew.parent = qnear
return
def DrawEdges(self, qnear, qnew, color=(0, 0, 255), thickness=1):
'''
draw the new edge
'''
cv2.line(self.src_map, tuple(qnear.pos), tuple(qnew.pos),
color, thickness)
cv2.imshow('RRT', self.src_map)
cv2.waitKey(50)
def IsArrival(self, qnew):
'''
if the distance between qnew and qgoal less than threshold,
and the path of qnew to qgoal is collsion-free,
the next vertices is qgoal, obviously
'''
if self.Distance(qnew.pos, self.qgoal.pos) > self.step_size:
return False
if self.CollsionFree(qnew, self.qgoal):
return True
return False
def FindPath(self):
'''
find the complete path with node in vertices propagate with parent pointer
'''
node = self.vertices[-1]
self.path.append(node)
while node.parent:
self.path.append(node.parent)
node = node.parent
self.path.reverse()
return self.path
def DrawPath(self, path, color=None, thickness=3):
'''
draw the complete path
'''
if color is None:
color = (random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255))
node = path[0]
for next_node in path:
cv2.line(self.src_map, tuple(node.pos), tuple(next_node.pos),
color, thickness)
cv2.imshow('RRT', self.src_map)
cv2.waitKey(50)
node = next_node
return
def SmoothPath(self, path):
'''
smooth path
'''
smooth_path = [path[0]]
pre_node = path[0]
cur_node = []
next_node = []
# pre_node(0) ...... cur_node(0), next_node(0)
# if pre_node -> next_node is collsion, then the new edge is pre_node -> cur_node.
# the next iterator is:
# pre_node(cur_node(0)) ...... cur_node(next_node(0)), next_node
for next_node in path:
if self.CollsionFree(pre_node, next_node) == False:
smooth_path.append(cur_node)
pre_node = cur_node
cur_node = next_node
# the last node
smooth_path.append(cur_node)
return smooth_path
def Planning(self):
'''
rrt planning
'''
self.AddVertices(self.qstart)
self.AddEdges(None, self.qstart)
k = -1
while k < self.max_steps:
k += 1
qrand = self.GenerateRandomNode()
_, qnear = self.FindNearestNode(qrand)
qnew = self.ExtendTree(qnear, qrand)
if qnew and self.CollsionFree(qnear, qnew):
self.AddVertices(qnew)
self.AddEdges(qnear, qnew)
self.DrawEdges(qnear, qnew)
if self.IsArrival(qnew):
print("Found")
self.AddVertices(self.qgoal)
self.AddEdges(qnew, self.qgoal)
path = self.FindPath()
self.DrawPath(path)
smooth_path = self.SmoothPath(path)
self.DrawPath(smooth_path)
return True
print('Not Found')
return False
if __name__ == "__main__":
map_path = 'map/area6.png'
qstart = [20, 20]
qgoal = [480, 480]
max_steps = 1000
step_size = 20
goal_prob = 0.01
grid_size = 10
rrt = RRT(map_path, qstart, qgoal, grid_size,
step_size, max_steps, goal_prob)
input('press any key to start planning:')
rrt.Planning()
input('press any key to quit:')