-
Notifications
You must be signed in to change notification settings - Fork 0
/
birrt_example.py
98 lines (69 loc) · 2.3 KB
/
birrt_example.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
# Created on Sat Apr 14 02:52:22 2018
# Author: Chaitanya Pb
#%% Package imports
import time
import rrts
import pygame
from img_env import ImageEnv
#%% Define variables
RED = (255, 0, 0) # Red color
GREEN = (0, 255, 0) # Green color
BLUE = (0, 0, 255) # Blue color
START = (50, 150) # Start position
GOAL = (450, 350) # Goal position
step_size = 10 # RRT Step Size
IMG_NAME = "images/random.png" # Image file
#%% Start PyGame
pygame.init()
clock = pygame.time.Clock()
img = pygame.image.load(IMG_NAME)
pixacc = pygame.surfarray.array2d(img)
screen = pygame.display.set_mode(img.get_size())
screen.fill((0, 0, 0))
screen.blit(img, (0, 0))
pygame.draw.circle(screen, RED, START, 5)
pygame.draw.circle(screen, GREEN, GOAL, 5)
#%% Instantiate Image Environment
env = ImageEnv(pixacc)
start = time.time()
#%% Bidirectional RRT (Connect or Extend)
birrt = rrts.BiRRT(env, step_size, 'ce')
birrt.start_tree(START, GOAL)
samples = 0
done = False
goalFound = False
drawPath = True
# Until closed
while not done:
# Check for PyGame Quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# If goal not found, grow BiRRT
if not goalFound:
goalFound, meet_point, start_nodes, goal_nodes = birrt.extend_tree()
samples += 1
# Draw new growth in PyGame
for node in start_nodes + goal_nodes:
pygame.draw.line(screen, RED, node.state, node.parent.state)
# If goal found, find path and draw
if goalFound and drawPath:
time_taken = time.time() - start
path, cost = birrt.find_path(meet_point)
# Draw found path in PyGame
for node in path:
if node.parent != None:
pygame.draw.line(screen, GREEN, node.state, node.parent.state, 3)
drawPath = False
# Update PyGame display
pygame.display.update()
tree_size = len(birrt.rrt_goal.tree) + len(birrt.rrt_start.tree)
#%% Close PyGame
#pygame.image.save(screen, "imagerrt.png")
pygame.quit()
#%% Print statistics
print "Samples Drawn =", samples
print "Time Taken =", time_taken, "s"
print "Path Length =", len(path)-1
print "Path Cost =", cost
print "Total Tree Size =", tree_size