Skip to content

Commit afa185f

Browse files
Minor changes added
1 parent 9786fd4 commit afa185f

File tree

1 file changed

+109
-22
lines changed

1 file changed

+109
-22
lines changed

RRT.py

Lines changed: 109 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
parser.add_argument('-o', '--obstacles', type=bool, action=argparse.BooleanOptionalAction, metavar='', required=False, help='Obstacles on the map')
1212
parser.add_argument('-n', '--nodes', type=int, metavar='', required=False, help='Maximum number of nodes')
1313
parser.add_argument('-e', '--epsilon', type=float, metavar='', required=False, help='Step size')
14-
parser.add_argument('-init', '--x_init', nargs='+', type=int, metavar='', required=False, help='Initial node position in x and y respectively')
15-
parser.add_argument('-goal', '--x_goal', nargs='+', type=int, metavar='', required=False, help='Goal node position in x and y respectively')
14+
parser.add_argument('-init', '--x_init', nargs='+', type=int, metavar='', required=False, help='Initial node position in X and Y respectively')
15+
parser.add_argument('-goal', '--x_goal', nargs='+', type=int, metavar='', required=False, help='Goal node position in X and Y respectively')
1616
parser.add_argument('-srn', '--show_random_nodes', type=bool, action=argparse.BooleanOptionalAction, metavar='', required=False, help='Show random nodes on screen')
1717
parser.add_argument('-snn', '--show_new_nodes', type=bool, action=argparse.BooleanOptionalAction, metavar='', required=False, help='Show new nodes on screen')
1818
args = parser.parse_args()
@@ -21,7 +21,7 @@
2121
WIDTH, HEIGHT = 640, 480
2222
WINDOW = pygame.display.set_mode(size=(WIDTH, HEIGHT))
2323
pygame.display.set_caption('RRT')
24-
FPS = 60
24+
FPS = 120
2525

2626
# Colors
2727
WHITE = (255, 255, 255)
@@ -30,42 +30,118 @@
3030
GREEN = (0, 255, 0)
3131
BLUE = (0, 0, 255)
3232
BROWN = (189, 154, 122)
33+
GRAY = (105, 105, 105)
3334

3435
# RRT parameters
3536
MAX_NODES = args.nodes if not args.nodes is None else 5000 # Default maximum number of nodes/vertices
3637
EPSILON = args.epsilon if not args.epsilon is None else 7.0 # Default step size
38+
obstacles = []
3739

3840
def draw_window():
3941
"""Draw the window all white."""
4042
WINDOW.fill(WHITE)
4143

42-
def draw_obstacles():
43-
"""Draw obstacles on the window.
44+
# def draw_obstacles():
45+
# """Draw obstacles on the window.
46+
47+
# Parameters
48+
# ----------
49+
# None
50+
51+
# Returns
52+
# -------
53+
# list
54+
# All the rectangle obstacles.
55+
# """
56+
# obstacles = []
57+
# obstacle1 = pygame.Rect((WIDTH//2 + 90, HEIGHT//2 - 45, 90, 90))
58+
# obstacle2 = pygame.Rect((WIDTH//2 - 180, HEIGHT//2 - 45, 90, 90))
59+
# pygame.draw.rect(surface=WINDOW, color=RED, rect=obstacle1)
60+
# pygame.draw.rect(surface=WINDOW, color=RED, rect=obstacle2)
61+
62+
# # Just decoration borders in black
63+
# pygame.draw.rect(surface=WINDOW, color=BLACK, rect=obstacle1, width=2)
64+
# pygame.draw.rect(surface=WINDOW, color=BLACK, rect=obstacle2, width=2)
65+
66+
# obstacles.append(obstacle1)
67+
# obstacles.append(obstacle2)
68+
69+
# return obstacles
4470

71+
def make_obstacles_T(initial_point):
72+
"""
73+
Given a initial point, it makes a obstacle with shape of T.
74+
4575
Parameters
4676
----------
47-
None
48-
77+
initial_point : tuple
78+
X and Y coordinates, starting from the top-left most part where
79+
the obstacle will be placed.
80+
4981
Returns
5082
-------
5183
list
52-
All the rectangle obstacles.
84+
A collection of sides composing the T obstacle.
5385
"""
54-
obstacles = []
55-
obstacle1 = pygame.Rect((WIDTH//2 + 90, HEIGHT//2 - 45, 90, 90))
56-
obstacle2 = pygame.Rect((WIDTH//2 - 180, HEIGHT//2 - 45, 90, 90))
57-
pygame.draw.rect(surface=WINDOW, color=RED, rect=obstacle1)
58-
pygame.draw.rect(surface=WINDOW, color=RED, rect=obstacle2)
86+
x, y = initial_point[0], initial_point[1]
87+
width, height = 50, 150
88+
89+
side1 = pygame.Rect(x, y, height, width)
90+
side2 = pygame.Rect((x+height//2) - width//2, y, width, height)
91+
92+
obstacle = [side1, side2]
93+
94+
return obstacle
95+
96+
def make_obstacles_L(initial_point):
97+
"""
98+
Given a initial point, it makes a obstacle with shape of L.
99+
100+
Parameters
101+
----------
102+
initial_point : tuple
103+
X and Y coordinates, starting from the top-left most part where
104+
the obstacle will be placed.
105+
106+
Returns
107+
-------
108+
list
109+
A collection of sides composing the L obstacle.
110+
"""
111+
x, y = initial_point[0], initial_point[1]
112+
width, height = 50, 150
113+
114+
side1 = pygame.Rect(x, y, width, height)
115+
side2 = pygame.Rect(x, y+height-width, height, width)
116+
117+
obstacle = [side1, side2]
59118

60-
# Just decoration borders in black
61-
pygame.draw.rect(surface=WINDOW, color=BLACK, rect=obstacle1, width=2)
62-
pygame.draw.rect(surface=WINDOW, color=BLACK, rect=obstacle2, width=2)
119+
return obstacle
120+
121+
def make_obstacles():
122+
"""Generate the obstacles to be placed on the final map."""
123+
124+
obstacle1 = make_obstacles_T(initial_point=(350, 200))
125+
obstacle2 = make_obstacles_L(initial_point=(150, 20))
63126

64127
obstacles.append(obstacle1)
65128
obstacles.append(obstacle2)
66129

67130
return obstacles
68131

132+
def draw_obstacles():
133+
"""Draw each side of the obstacles."""
134+
obstacles = []
135+
obs = make_obstacles()
136+
137+
for obstacle in obs:
138+
for side in obstacle:
139+
pygame.draw.rect(surface=WINDOW, color=GRAY,
140+
rect=side)
141+
obstacles.append(side)
142+
143+
return obstacles
144+
69145
def is_free(point, obstacles, tree):
70146
"""Checks whether a node is colliding with an obstacle or not.
71147
@@ -149,10 +225,11 @@ def nearest_neighbor(tree, x_rand):
149225
global min_distance
150226

151227
distances = []
228+
152229
for state in tree:
153230
distance = euclidean_distance(state, x_rand)
154231
distances.append(distance)
155-
232+
156233
# Index of the minimum distance to the generated random node
157234
min_distance = np.argmin(distances)
158235
x_near = tree[min_distance]
@@ -217,7 +294,7 @@ def generate_parents(values, parent):
217294
218295
Returns
219296
-------
220-
parent : list
297+
list
221298
Ordered collection of the parents.
222299
"""
223300
parent_value = values[min_distance] # Value nearest node
@@ -230,7 +307,6 @@ def generate_parents(values, parent):
230307

231308
return parent
232309

233-
234310
def main(has_obstacles, show_random_nodes, show_new_nodes):
235311
global is_goal_reached
236312

@@ -251,25 +327,33 @@ def main(has_obstacles, show_random_nodes, show_new_nodes):
251327

252328
k = 0
253329
node_value = 0
330+
iteration = 0
331+
254332
while run and k < MAX_NODES:
255333
# Make sure the loop runs at 60 FPS
256334
clock.tick(FPS)
257335
for event in pygame.event.get():
258336
if event.type == pygame.QUIT:
259337
run = False
260338

261-
262339
x_rand = generate_random_node() # Random node
263340
x_near = nearest_neighbor(tree, x_rand) # Nearest neighbor to the random node
264341
x_new = new_state(x_rand, x_near, x_goal) # New node
265342
tree.append(x_new)
266-
267-
# Draw points and lines to the visualization
343+
344+
# Every n iterations bias the RRT
345+
if iteration%10 == 0:
346+
x_rand = x_goal
347+
348+
iteration += 1
349+
350+
# Draw points and lines to visualization
268351
pygame.draw.circle(surface=WINDOW, color=BLUE, center=x_init, radius=3)
269352
pygame.draw.circle(surface=WINDOW, color=RED, center=x_goal, radius=3)
270353

271354
if has_obstacles and not is_simulation_finished:
272355
collision_free = is_free(point=x_new, obstacles=obstacles, tree=tree) # Check collision
356+
273357
if collision_free:
274358
# Append current node value and place it in the parent list
275359
values.append(node_value)
@@ -286,11 +370,13 @@ def main(has_obstacles, show_random_nodes, show_new_nodes):
286370
if is_goal_reached:
287371
pygame.draw.line(surface=WINDOW, color=BLACK, start_pos=x_new, end_pos=x_goal)
288372
is_simulation_finished = True
373+
print(iteration)
289374
elif not is_simulation_finished:
290375
# Append current node value and place it in the parent list
291376
values.append(node_value)
292377
parent = generate_parents(values, parent)
293378

379+
294380
if show_random_nodes:
295381
pygame.draw.circle(surface=WINDOW, color=GREEN, center=x_rand, radius=3)
296382
if show_new_nodes:
@@ -302,6 +388,7 @@ def main(has_obstacles, show_random_nodes, show_new_nodes):
302388
if is_goal_reached:
303389
pygame.draw.line(surface=WINDOW, color=BLACK, start_pos=x_new, end_pos=x_goal)
304390
is_simulation_finished = True
391+
print(iteration)
305392

306393
pygame.display.update()
307394
k += 1

0 commit comments

Comments
 (0)