-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRRTbasePy.py
215 lines (175 loc) · 5.66 KB
/
RRTbasePy.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
import random
import pygame
import math
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((255,255,255))
self.nodeRad=3
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.Green = (0,255,0)
self.Red = (255,0,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.Green,self.goal,self.nodeRad+20,1)
self.drawObs(obstacles)
def drawPath(self):
pass
def drawObs(self,obstacles):
obstaclesList=obstacles[:]
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=[]
# initializing the tree
self.x.append(x)
self.y.append(y)
self.parent.append(0)
# 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[:]
return obs
def add_node(self,n,x,y):
self.x.insert(n,x)
self.y.append(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[:]
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[:]
while(len(obs)>0):
rectangle=obs.pop(0)
if rectangle.clipline(x1, y1, x2, y2):
return True
return False
def connect(self,n1,n2):
(x1,y1) = (self.x[n1],self.y[n1])
(x2,y2) = (self.x[n2],self.y[n2])
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):
pass
def getPathCoords(self):
pass
# go towards goal
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
# explore
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 cost(self):
pass