21
21
metavar = '' , required = False , help = 'Show new nodes on screen' )
22
22
parser .add_argument ('-bp' , '--bias_percentage' , type = int , metavar = '' , required = False , default = 30 ,
23
23
help = 'Amount of bias the RRT from 1 to 100' )
24
+ parser .add_argument ('-ptg' , '--path_to_goal' , type = bool , action = argparse .BooleanOptionalAction ,
25
+ metavar = '' , required = False , help = 'Draws a red line indicating the path to goal' )
26
+ parser .add_argument ('-mr' , '--move_robot' , type = bool , action = argparse .BooleanOptionalAction ,
27
+ metavar = '' , required = False , help = 'Shows the movements of the robot from the start to the end' )
24
28
args = parser .parse_args ()
25
29
26
30
# Constants
@@ -56,18 +60,24 @@ def main():
56
60
iteration = 0
57
61
bias_percentage = 10 - args .bias_percentage // 10 if args .bias_percentage != 100 else 1
58
62
63
+ nears = [] # To store the generated x_near nodes
64
+
59
65
while run and k < MAX_NODES :
60
66
# Make sure the loop runs at 60 FPS
61
67
clock .tick (environment_ .FPS )
62
68
for event in pygame .event .get ():
63
69
if event .type == pygame .QUIT :
64
70
run = False
65
71
72
+ # Draw points and lines for visualization
73
+ graph_ .draw_initial_node (map_ = environment_ .map )
74
+ graph_ .draw_goal_node (map_ = environment_ .map )
75
+
66
76
if not is_simulation_finished :
67
77
# Sample free space and check x_rand node collision
68
78
x_rand = graph_ .generate_random_node (obstacles = obstacles ) # Random node
69
79
rand_collision_free = graph_ .is_free (point = x_rand , obstacles = obstacles )
70
-
80
+
71
81
if rand_collision_free :
72
82
x_near = graph_ .nearest_neighbor (tree , x_rand ) # Nearest neighbor to the random node
73
83
x_new = graph_ .new_state (x_rand , x_near , x_goal ) # New node
@@ -78,15 +88,12 @@ def main():
78
88
79
89
iteration += 1
80
90
81
- # Draw points and lines for visualization
82
- graph_ .draw_initial_node (map_ = environment_ .map )
83
- graph_ .draw_goal_node (map_ = environment_ .map )
84
-
85
91
# Check x_new node collision
86
92
new_collision_free = graph_ .is_free (point = x_new , obstacles = obstacles )
87
93
88
94
if new_collision_free :
89
95
tree .append (x_new ) # Append to the tree the x_new node
96
+ nears .append (x_near ) # Append nearest neighbor of x_new
90
97
91
98
# Append current node value and place it in the parent list
92
99
values .append (node_value )
@@ -108,9 +115,15 @@ def main():
108
115
graph_ .tree = tree
109
116
graph_ .path_to_goal ()
110
117
graph_ .get_path_coordinates ()
111
- graph_ .draw_path_to_goal (map_ = environment_ .map )
112
118
113
- k += 1
119
+ if args .path_to_goal :
120
+ graph_ .draw_path_to_goal (map_ = environment_ .map )
121
+
122
+ k += 1 # One more node sampled
123
+
124
+ if graph_ .is_goal_reached and args .move_robot :
125
+ graph_ .draw_trajectory (nears = nears , news = tree , environment_ = environment_ )
126
+
114
127
pygame .display .update ()
115
128
116
129
pygame .quit ()
0 commit comments