Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions sound_play/scripts/soundplay_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@

from ament_index_python.packages import get_package_share_directory
import rclpy.action
import rclpy.callback_groups
import rclpy.duration
import rclpy.executors
import rclpy.logging
import rclpy.node

Expand Down Expand Up @@ -253,7 +255,9 @@ def __init__(self):
self._as = rclpy.action.ActionServer(
self, SoundRequestAction, 'sound_play',
execute_callback=self.execute_cb,
handle_accepted_callback=self.handle_accepted_cb)
cancel_callback=self.cancel_cb,
handle_accepted_callback=self.handle_accepted_cb,
callback_group=rclpy.callback_groups.ReentrantCallbackGroup())

# For ros startup race condition
self.sleep(0.5)
Expand Down Expand Up @@ -522,6 +526,9 @@ def diagnostics(self, state):
self.get_logger().error(
'Exception in diagnostics: %s' % str(e))

def cancel_cb(self, goal_handle):
return rclpy.action.CancelResponse.ACCEPT

def execute_cb(self, goal_handle):
data = goal_handle.request.sound_request
if not self.initialized:
Expand Down Expand Up @@ -549,18 +556,20 @@ def execute_cb(self, goal_handle):
feedback.stamp = (
self.get_clock().now() - start_time).to_msg()
goal_handle.publish_feedback(feedback)
if not goal_handle.is_active:
if goal_handle.is_cancel_requested:
self.get_logger().info(
'sound_play action: Preempted')
sound.stop()
success = False
break
self.sleep(1.0 / self.loop_rate)
result.playing = feedback.playing
result.stamp = feedback.stamp
if success:
result.playing = feedback.playing
result.stamp = feedback.stamp
self.get_logger().info('sound_play action: Succeeded')
goal_handle.succeed()
else:
goal_handle.canceled()
except Exception as e:
goal_handle.abort()
self.get_logger().error(
Expand Down Expand Up @@ -608,7 +617,9 @@ def idle_loop(self):
if __name__ == '__main__':
rclpy.init()
soundplay_node = SoundPlayNode()
rclpy.spin(soundplay_node)
executor = rclpy.executors.MultiThreadedExecutor()
executor.add_node(soundplay_node)
executor.spin()
soundplay_node.destroy_node()
del soundplay_node
rclpy.shutdown()
Loading