-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrrt.py
More file actions
150 lines (126 loc) · 4.1 KB
/
rrt.py
File metadata and controls
150 lines (126 loc) · 4.1 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
#!/usr/bin/env python
# rrtstar.py
# This program generates a
# asymptotically optimal rapidly exploring random tree (RRT* proposed by Sertac Keraman, MIT) in a rectangular region.
#
# Originally written by Steve LaValle, UIUC for simple RRT in
# May 2011
# Modified by Md Mahbubur Rahman, FIU for RRT* in
# January 2016
import sys, random, math, pygame
from pygame.locals import *
from math import sqrt,cos,sin,atan2
from lineIntersect import *
import time
#constants
XDIM = 640
YDIM = 480
WINSIZE = [XDIM, YDIM]
EPSILON = 7.0
NUMNODES = 2000
RADIUS=15
OBS=[(500,150,100,50),(300,80,100,50),(150,220,100,50)]
def obsDraw(pygame,screen):
blue=(0,0,255)
for o in OBS:
pygame.draw.rect(screen,blue,o)
def dist(p1,p2):
return sqrt((p1[0]-p2[0])*(p1[0]-p2[0])+(p1[1]-p2[1])*(p1[1]-p2[1]))
def step_from_to(p1,p2):
if dist(p1,p2) < EPSILON:
return p2
else:
theta = atan2(p2[1]-p1[1],p2[0]-p1[0])
return p1[0] + EPSILON*cos(theta), p1[1] + EPSILON*sin(theta)
def chooseParent(nn,newnode,nodes):
for p in nodes:
if checkIntersect(p,newnode,OBS) and dist([p.x,p.y],[newnode.x,newnode.y]) <dist([nn.x,nn.y],[newnode.x,newnode.y]):
nn = p
newnode.cost=nn.cost+dist([nn.x,nn.y],[newnode.x,newnode.y])
newnode.parent=nn
return newnode,nn
def path_length(nodes):
length = 0
for i in range(len(nodes)-1):
length+=dist([nodes[i].x,nodes[i].y],[nodes[i+1].x,nodes[i+1].y])
return length
def drawSolutionPath(start,goal,nodes,pygame,screen):
pink = 200, 20, 240
nn = nodes[0]
for p in nodes:
if dist([p.x,p.y],[goal.x,goal.y]) < dist([nn.x,nn.y],[goal.x,goal.y]):
nn = p
while nn!=start:
pygame.draw.line(screen,pink,[nn.x,nn.y],[nn.parent.x,nn.parent.y],5)
nn=nn.parent
def get_path(start,goal,nodes):
ret_nodes = []
nn = nodes[0]
for p in nodes:
if dist([p.x,p.y],[goal.x,goal.y]) < dist([nn.x,nn.y],[goal.x,goal.y]):
nn = p
while nn!=start:
ret_nodes.append(nn)
nn=nn.parent
return ret_nodes
class Node:
x = 0
y = 0
cost=0
parent=None
def __init__(self,xcoord, ycoord):
self.x = xcoord
self.y = ycoord
def main(imgno):
#initialize and prepare screen
#a=checkIntersect()
#print(a)
pygame.init()
screen = pygame.display.set_mode(WINSIZE)
pygame.display.set_caption('RRTstar')
white = 255, 255, 255
black = 20, 20, 40
screen.fill(white)
obsDraw(pygame,screen)
t = time.time()
nodes = []
#nodes.append(Node(XDIM/2.0,YDIM/2.0)) # Start in the center
nodes.append(Node(0.0,0.0)) # Start in the corner
start=nodes[0]
goal=Node(630.0,470.0)
for i in range(NUMNODES):
rand = Node(random.random()*XDIM, random.random()*YDIM)
nn = nodes[0]
for p in nodes:
if dist([p.x,p.y],[rand.x,rand.y]) < dist([nn.x,nn.y],[rand.x,rand.y]):
nn = p
interpolatedNode= step_from_to([nn.x,nn.y],[rand.x,rand.y])
newnode = Node(interpolatedNode[0],interpolatedNode[1])
if checkIntersect(nn,rand,OBS):
[newnode,nn]=chooseParent(nn,newnode,nodes);
nodes.append(newnode)
pygame.draw.line(screen,black,[nn.x,nn.y],[newnode.x,newnode.y])
# nodes=reWire(nodes,newnode,pygame,screen)
pygame.display.update()
#print i, " ", nodes
for e in pygame.event.get():
if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
sys.exit("Leaving because you requested it.")
elapsed = time.time()-t
expanded = len(nodes)
path = get_path(start,goal,nodes)
path_len = path_length(path)
drawSolutionPath(start,goal,nodes,pygame,screen)
pygame.display.update()
print expanded,elapsed,path_len
pygame.image.save(screen, "rrt_stats/image"+str(imgno)+".jpg")
pygame.quit()
# if python says run, then we should run
if __name__ == '__main__':
for i in range(10):
main(i)
# running = True
# while running:
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# running = False