Skip to content

Commit 299140f

Browse files
Robot movement added
1 parent 7844ba1 commit 299140f

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

RRT.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
metavar='', required=False, help='Show new nodes on screen')
2222
parser.add_argument('-bp', '--bias_percentage', type=int, metavar='', required=False, default=30,
2323
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')
2428
args = parser.parse_args()
2529

2630
# Constants
@@ -56,18 +60,24 @@ def main():
5660
iteration = 0
5761
bias_percentage = 10 - args.bias_percentage//10 if args.bias_percentage != 100 else 1
5862

63+
nears = [] # To store the generated x_near nodes
64+
5965
while run and k < MAX_NODES:
6066
# Make sure the loop runs at 60 FPS
6167
clock.tick(environment_.FPS)
6268
for event in pygame.event.get():
6369
if event.type == pygame.QUIT:
6470
run = False
6571

72+
# Draw points and lines for visualization
73+
graph_.draw_initial_node(map_=environment_.map)
74+
graph_.draw_goal_node(map_=environment_.map)
75+
6676
if not is_simulation_finished:
6777
# Sample free space and check x_rand node collision
6878
x_rand = graph_.generate_random_node(obstacles=obstacles) # Random node
6979
rand_collision_free = graph_.is_free(point=x_rand, obstacles=obstacles)
70-
80+
7181
if rand_collision_free:
7282
x_near = graph_.nearest_neighbor(tree, x_rand) # Nearest neighbor to the random node
7383
x_new = graph_.new_state(x_rand, x_near, x_goal) # New node
@@ -78,15 +88,12 @@ def main():
7888

7989
iteration += 1
8090

81-
# Draw points and lines for visualization
82-
graph_.draw_initial_node(map_=environment_.map)
83-
graph_.draw_goal_node(map_=environment_.map)
84-
8591
# Check x_new node collision
8692
new_collision_free = graph_.is_free(point=x_new, obstacles=obstacles)
8793

8894
if new_collision_free:
8995
tree.append(x_new) # Append to the tree the x_new node
96+
nears.append(x_near) # Append nearest neighbor of x_new
9097

9198
# Append current node value and place it in the parent list
9299
values.append(node_value)
@@ -108,9 +115,15 @@ def main():
108115
graph_.tree = tree
109116
graph_.path_to_goal()
110117
graph_.get_path_coordinates()
111-
graph_.draw_path_to_goal(map_=environment_.map)
112118

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+
114127
pygame.display.update()
115128

116129
pygame.quit()

__pycache__/graph.cpython-310.pyc

927 Bytes
Binary file not shown.

graph.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,34 @@ def draw_path_to_goal(self, map_):
274274
"""Draws the path from the x_goal node to the x_init node."""
275275
for i in range(len(self.path_coordinates)-1):
276276
pygame.draw.line(surface=map_, color=self.RED, start_pos=self.path_coordinates[i],
277-
end_pos=self.path_coordinates[i+1], width=4)
277+
end_pos=self.path_coordinates[i+1], width=4)
278+
279+
def move_robot(self, position, map_):
280+
"""Draws the robot moving."""
281+
pygame.draw.circle(surface=map_, color=(0, 0, 255),
282+
center=position, radius=4)
283+
284+
def draw_tree(self, nears, news, map_):
285+
"""Draws the tree constantly. Used to display it in an infinite loop."""
286+
for i in range(len(nears)):
287+
self.draw_local_planner(p1=nears[i], p2=news[i+1], map_=map_)
288+
289+
def draw_trajectory(self, nears, news, environment_):
290+
"""Draws the robot moving in the map."""
291+
for i in range(len(self.path_coordinates)-1):
292+
robot_position = self.path_coordinates[::-1][i]
293+
294+
# Draw inital and final robot configuration
295+
self.draw_initial_node(map_=environment_.map)
296+
self.draw_goal_node(map_=environment_.map)
297+
environment_.draw_obstacles()
298+
299+
# Draw path to goal, and the robot movement
300+
self.draw_path_to_goal(map_=environment_.map)
301+
self.move_robot(position=robot_position, map_=environment_.map)
302+
self.draw_tree(nears=nears, news=news, map_=environment_.map)
303+
304+
# Refresh the screen
305+
pygame.display.update()
306+
pygame.time.delay(50) # Wait 0.1 seconds
307+
environment_.map.fill(self.WHITE)

0 commit comments

Comments
 (0)