-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRRTbasePy.py
More file actions
220 lines (188 loc) · 6.76 KB
/
RRTbasePy.py
File metadata and controls
220 lines (188 loc) · 6.76 KB
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
import random
import math
import pygame
class RRTMap:
def __init__(self, start, goal, MapDimensions, obsdim, obsnum):
self.start = start
self.goal = goal
self.MapDimensions = MapDimensions
self.Maph, self.Mapw = self.MapDimensions
# window settings
self.MapWindowName = 'RRT path Planning'
pygame.display.set_caption(self.MapWindowName)
self.map = pygame.display.set_mode((self.Mapw, self.Maph))
self.map.fill((225, 225, 225))
self.nodeRad = 2
self.nodeThickness = 0
self.edgeThickness = 1
self.obstacles = []
self.obsdim = obsdim
self.obsNumber = obsnum
#Colors
self.grey = (70, 70, 70)
self.Blue = (0, 0, 255)
self.Red = (255, 0, 0)
self.Green = (0, 255, 0)
self.white = (255, 255, 255)
def drawMap(self, obstacles):
pygame.draw.circle(self.map, self.Green, self.start, self.nodeRad + 5, 0)
pygame.draw.circle(self.map, self.Red, self.goal, self.nodeRad + 20, 1)
self.drawObs(obstacles)
def drawPath(self, path):
for node in path:
pygame.draw.circle(self.map, self.Red, node, self.nodeRad + 3, 0)
def drawObs(self, obstacles):
obstaclesList = obstacles.copy()
while (len(obstaclesList) > 0):
obstacle = obstaclesList.pop(0)
pygame.draw.rect(self.map, self.grey, obstacle)
class RRTGraph:
def __init__(self, start, goal, MapDimensions, obsdim, obsnum):
(x, y) = start
self.start = start
self.goal = goal
self.goalFlag = False
self.maph, self.mapw = MapDimensions
self.x = []
self.y = []
self.parent = []
# initialize the tree
self.x.append(x)
self.y.append(y)
self.parent.append(0)
# the obstacles
self.obstacles = []
self.obsDim = obsdim
self.obsNum = obsnum
# path
self.goalstate = None
self.path = []
def makeRandomRect(self):
uppercornerx = int(random.uniform(0, self.mapw - self.obsDim))
uppercornery = int(random.uniform(0, self.maph - self.obsDim))
return (uppercornerx, uppercornery)
def makeobs(self):
obs = []
for i in range(0, self.obsNum):
rectang = None
startgoalcol = True
while startgoalcol:
upper = self.makeRandomRect()
rectang = pygame.Rect(upper, (self.obsDim, self.obsDim))
if rectang.collidepoint(self.start) or rectang.collidepoint(self.goal):
startgoalcol = True
else:
startgoalcol = False
obs.append(rectang)
self.obstacles = obs.copy()
return obs
def add_node(self, n, x, y):
self.x.insert(n, x)
self.y.insert(n, y)
def remove_node(self, n):
self.x.pop(n)
self.y.pop(n)
def add_edge(self, parent, child):
self.parent.insert(child, parent)
def remove_edge(self, n):
self.parent.pop(n)
def number_of_nodes(self):
return len(self.x)
def distance(self, n1, n2):
(x1, y1) = (self.x[n1], self.y[n1])
(x2, y2) = (self.x[n2], self.y[n2])
px = (float(x1) - float(x2))**2
py = (float(y1) - float(y2))**2
return (px + py)**(0.5)
def sample_envir(self):
x = int(random.uniform(0, self.mapw))
y = int(random.uniform(0, self.maph))
return x, y
def nearest(self, n):
dmin = self.distance(0, n)
nnear = 0
for i in range(0, n):
if self.distance(i, n) < dmin:
dmin = self.distance(i, n)
nnear = i
return nnear
def isFree(self):
n = self.number_of_nodes() - 1
(x, y) = (self.x[n], self.y[n])
obs = self.obstacles.copy()
while len(obs) > 0:
rectang = obs.pop(0)
if rectang.collidepoint(x, y):
self.remove_node(n)
return False
return True
def crossObstacle(self, x1, x2, y1, y2):
obs = self.obstacles.copy()
while (len(obs) > 0):
rectang = obs.pop(0)
for i in range(0, 100):
u = i/100
x = x1*u + x2*(1-u)
y = y1*u + y2*(1-u)
if rectang.collidepoint(x, y):
return True
return False
def connect(self, n1, n2):
(x1, y1) = (self.x[n1], self.y[n1])
(x2, y2) = (self.x[n2], self.y[n1])
if self.crossObstacle(x1, x2, y1, y2):
self.remove_node(n2)
return False
else:
self.add_edge(n1, n2)
return True
def step(self, nnear, nrand, dmax = 35):
d = self.distance(nnear, nrand)
if d > dmax:
u = dmax/d
(xnear, ynear) = (self.x[nnear], self.y[nnear])
(xrand, yrand) = (self.x[nrand], self.y[nrand])
(px, py) = (xrand - xnear, yrand - ynear)
theta = math.atan2(py, px)
(x, y) = (int(xnear + dmax * math.cos(theta)), int(ynear + dmax * math.sin(theta)))
self.remove_node(nrand)
if abs(x - self.goal[0]) <= dmax and abs(y - self.goal[1]) <= dmax:
self.add_node(nrand, self.goal[0], self.goal[1])
self.goalstate = nrand
self.goalFlag = True
else:
self.add_node(nrand, x, y)
def path_to_goal(self):
if self.goalFlag:
self.path = []
self.path.append(self.goalstate)
newpos = self.parent[self.goalstate]
while (newpos != 0):
self.path.append(newpos)
newpos = self.parent[newpos]
self.path.append(0)
return self.goalFlag
def getPathCoords(self):
pathCoords = []
for node in self.path:
x,y = (self.x[node], self.y[node])
pathCoords.append((x, y))
return pathCoords
def bias(self, ngoal):
n = self.number_of_nodes()
self.add_node(n, ngoal[0], ngoal[1])
nnear = self.nearest(n)
self.step(nnear, n)
self.connect(nnear, n)
return self.x, self.y, self.parent
def expand(self):
n= self.number_of_nodes()
x, y = self.sample_envir()
self.add_node(n, x, y)
if self.isFree():
xnearest = self.nearest(n)
self.step(xnearest, n)
self.connect(xnearest, n)
return self.x, self.y, self.parent
def const(self):
pass