11
11
parser .add_argument ('-o' , '--obstacles' , type = bool , action = argparse .BooleanOptionalAction , metavar = '' , required = False , help = 'Obstacles on the map' )
12
12
parser .add_argument ('-n' , '--nodes' , type = int , metavar = '' , required = False , help = 'Maximum number of nodes' )
13
13
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' )
16
16
parser .add_argument ('-srn' , '--show_random_nodes' , type = bool , action = argparse .BooleanOptionalAction , metavar = '' , required = False , help = 'Show random nodes on screen' )
17
17
parser .add_argument ('-snn' , '--show_new_nodes' , type = bool , action = argparse .BooleanOptionalAction , metavar = '' , required = False , help = 'Show new nodes on screen' )
18
18
args = parser .parse_args ()
21
21
WIDTH , HEIGHT = 640 , 480
22
22
WINDOW = pygame .display .set_mode (size = (WIDTH , HEIGHT ))
23
23
pygame .display .set_caption ('RRT' )
24
- FPS = 60
24
+ FPS = 120
25
25
26
26
# Colors
27
27
WHITE = (255 , 255 , 255 )
30
30
GREEN = (0 , 255 , 0 )
31
31
BLUE = (0 , 0 , 255 )
32
32
BROWN = (189 , 154 , 122 )
33
+ GRAY = (105 , 105 , 105 )
33
34
34
35
# RRT parameters
35
36
MAX_NODES = args .nodes if not args .nodes is None else 5000 # Default maximum number of nodes/vertices
36
37
EPSILON = args .epsilon if not args .epsilon is None else 7.0 # Default step size
38
+ obstacles = []
37
39
38
40
def draw_window ():
39
41
"""Draw the window all white."""
40
42
WINDOW .fill (WHITE )
41
43
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
44
70
71
+ def make_obstacles_T (initial_point ):
72
+ """
73
+ Given a initial point, it makes a obstacle with shape of T.
74
+
45
75
Parameters
46
76
----------
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
+
49
81
Returns
50
82
-------
51
83
list
52
- All the rectangle obstacles.
84
+ A collection of sides composing the T obstacle.
53
85
"""
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 ]
59
118
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 ))
63
126
64
127
obstacles .append (obstacle1 )
65
128
obstacles .append (obstacle2 )
66
129
67
130
return obstacles
68
131
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
+
69
145
def is_free (point , obstacles , tree ):
70
146
"""Checks whether a node is colliding with an obstacle or not.
71
147
@@ -149,10 +225,11 @@ def nearest_neighbor(tree, x_rand):
149
225
global min_distance
150
226
151
227
distances = []
228
+
152
229
for state in tree :
153
230
distance = euclidean_distance (state , x_rand )
154
231
distances .append (distance )
155
-
232
+
156
233
# Index of the minimum distance to the generated random node
157
234
min_distance = np .argmin (distances )
158
235
x_near = tree [min_distance ]
@@ -217,7 +294,7 @@ def generate_parents(values, parent):
217
294
218
295
Returns
219
296
-------
220
- parent : list
297
+ list
221
298
Ordered collection of the parents.
222
299
"""
223
300
parent_value = values [min_distance ] # Value nearest node
@@ -230,7 +307,6 @@ def generate_parents(values, parent):
230
307
231
308
return parent
232
309
233
-
234
310
def main (has_obstacles , show_random_nodes , show_new_nodes ):
235
311
global is_goal_reached
236
312
@@ -251,25 +327,33 @@ def main(has_obstacles, show_random_nodes, show_new_nodes):
251
327
252
328
k = 0
253
329
node_value = 0
330
+ iteration = 0
331
+
254
332
while run and k < MAX_NODES :
255
333
# Make sure the loop runs at 60 FPS
256
334
clock .tick (FPS )
257
335
for event in pygame .event .get ():
258
336
if event .type == pygame .QUIT :
259
337
run = False
260
338
261
-
262
339
x_rand = generate_random_node () # Random node
263
340
x_near = nearest_neighbor (tree , x_rand ) # Nearest neighbor to the random node
264
341
x_new = new_state (x_rand , x_near , x_goal ) # New node
265
342
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
268
351
pygame .draw .circle (surface = WINDOW , color = BLUE , center = x_init , radius = 3 )
269
352
pygame .draw .circle (surface = WINDOW , color = RED , center = x_goal , radius = 3 )
270
353
271
354
if has_obstacles and not is_simulation_finished :
272
355
collision_free = is_free (point = x_new , obstacles = obstacles , tree = tree ) # Check collision
356
+
273
357
if collision_free :
274
358
# Append current node value and place it in the parent list
275
359
values .append (node_value )
@@ -286,11 +370,13 @@ def main(has_obstacles, show_random_nodes, show_new_nodes):
286
370
if is_goal_reached :
287
371
pygame .draw .line (surface = WINDOW , color = BLACK , start_pos = x_new , end_pos = x_goal )
288
372
is_simulation_finished = True
373
+ print (iteration )
289
374
elif not is_simulation_finished :
290
375
# Append current node value and place it in the parent list
291
376
values .append (node_value )
292
377
parent = generate_parents (values , parent )
293
378
379
+
294
380
if show_random_nodes :
295
381
pygame .draw .circle (surface = WINDOW , color = GREEN , center = x_rand , radius = 3 )
296
382
if show_new_nodes :
@@ -302,6 +388,7 @@ def main(has_obstacles, show_random_nodes, show_new_nodes):
302
388
if is_goal_reached :
303
389
pygame .draw .line (surface = WINDOW , color = BLACK , start_pos = x_new , end_pos = x_goal )
304
390
is_simulation_finished = True
391
+ print (iteration )
305
392
306
393
pygame .display .update ()
307
394
k += 1
0 commit comments