Skip to content

Commit

Permalink
Fix path following cancelation logic (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-bass authored Sep 27, 2024
1 parent 824664b commit 9157eed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
23 changes: 13 additions & 10 deletions pyrobosim/pyrobosim/gui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,19 @@ def set_buttons_during_action(self, state):
:param state: Desired button state (True to enable, False to disable)
:type state: bool
"""
self.nav_button.setEnabled(state)
self.pick_button.setEnabled(state)
self.place_button.setEnabled(state)
self.detect_button.setEnabled(state)
self.open_button.setEnabled(state)
self.close_button.setEnabled(state)
self.rand_pose_button.setEnabled(state)
self.cancel_action_button.setEnabled(not state)
self.reset_world_button.setEnabled(state and self.world.source_file is not None)
self.reset_path_planner_button.setEnabled(state)
if self.get_current_robot():
self.nav_button.setEnabled(state)
self.pick_button.setEnabled(state)
self.place_button.setEnabled(state)
self.detect_button.setEnabled(state)
self.open_button.setEnabled(state)
self.close_button.setEnabled(state)
self.rand_pose_button.setEnabled(state)
self.cancel_action_button.setEnabled(not state)
self.reset_world_button.setEnabled(
state and self.world.source_file is not None
)
self.reset_path_planner_button.setEnabled(state)

####################
# Button Callbacks #
Expand Down
11 changes: 5 additions & 6 deletions pyrobosim/pyrobosim/gui/world_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class WorldCanvas(FigureCanvasQTAgg):
robot_dir_line_factor = 3.0
""" Multiplier of robot radius for plotting robot orientation lines. """

draw_lock = threading.Lock()
draw_lock = threading.RLock()
""" Lock for drawing on the canvas in a thread-safe manner. """

draw_signal = Signal()
Expand Down Expand Up @@ -363,11 +363,10 @@ def show(self):

def draw_and_sleep(self):
"""Redraws the figure and waits a small amount of time."""
if self.draw_lock.locked():
return
self.fig.canvas.draw()
self.fig.canvas.flush_events()
time.sleep(0.005)
with self.draw_lock:
self.fig.canvas.draw()
self.fig.canvas.flush_events()
time.sleep(0.005)

def show_planner_and_path(self, robot=None, show_graphs=True, path=None):
"""
Expand Down
11 changes: 8 additions & 3 deletions pyrobosim/pyrobosim/navigation/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ def __init__(
self.validation_step_dist = validation_step_dist

# Execution state
self.reset_state()

def reset_state(self):
"""
Resets all the states for tracking the status of path execution.
"""
self.current_traj_time = 0.0
self.following_path = False # Flag to track path following
self.abort_execution = False # Flag to abort internally
Expand Down Expand Up @@ -86,8 +92,7 @@ def execute(self, path, realtime_factor=1.0, battery_usage=0.0):
message=message,
)

self.current_traj_time = 0.0
self.abort_execution = False
self.reset_state()
self.following_path = True

# Convert the path to an interpolated trajectory.
Expand Down Expand Up @@ -148,7 +153,7 @@ def execute(self, path, realtime_factor=1.0, battery_usage=0.0):
time.sleep(max(0, sleep_time - (time.time() - start_time)))

# Finalize path execution.
self.following_path = False
self.reset_state()
time.sleep(0.1) # To ensure background threads get the end of the path.
self.robot.last_nav_result = ExecutionResult(status=status, message=message)
return self.robot.last_nav_result
Expand Down
3 changes: 2 additions & 1 deletion pyrobosim_ros/pyrobosim_ros/ros_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,8 @@ def robot_path_follow_callback(self, goal_handle, robot=None):
if goal_handle.is_cancel_requested:
robot.cancel_actions()
goal_handle.canceled()
time.sleep(0.5)
break
time.sleep(0.1)

if self.world.has_gui:
self.world.gui.set_buttons_during_action(True)
Expand Down

0 comments on commit 9157eed

Please sign in to comment.