From 79dbf8c1d4e1397eae48aa86233d66d1afe6096a Mon Sep 17 00:00:00 2001 From: Chris Lalancette Date: Mon, 24 Jun 2024 19:01:09 +0000 Subject: [PATCH] Switch to using a context manager for rclpy initialization. This ensures that everything will be properly cleaned up when we leave the context manager. Signed-off-by: Chris Lalancette --- .../turtle_tf2_py/dynamic_frame_tf2_broadcaster.py | 11 +++++------ .../turtle_tf2_py/fixed_frame_tf2_broadcaster.py | 11 +++++------ .../turtle_tf2_py/static_turtle_tf2_broadcaster.py | 13 ++++++------- .../turtle_tf2_py/turtle_tf2_broadcaster.py | 11 +++++------ turtle_tf2_py/turtle_tf2_py/turtle_tf2_listener.py | 11 +++++------ .../turtle_tf2_py/turtle_tf2_message_broadcaster.py | 11 +++++------ 6 files changed, 31 insertions(+), 37 deletions(-) diff --git a/turtle_tf2_py/turtle_tf2_py/dynamic_frame_tf2_broadcaster.py b/turtle_tf2_py/turtle_tf2_py/dynamic_frame_tf2_broadcaster.py index 44030c4..64b542e 100644 --- a/turtle_tf2_py/turtle_tf2_py/dynamic_frame_tf2_broadcaster.py +++ b/turtle_tf2_py/turtle_tf2_py/dynamic_frame_tf2_broadcaster.py @@ -17,6 +17,7 @@ from geometry_msgs.msg import TransformStamped import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from tf2_ros import TransformBroadcaster @@ -49,11 +50,9 @@ def broadcast_timer_callback(self): def main(): - rclpy.init() - node = DynamicFrameBroadcaster() try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(): + node = DynamicFrameBroadcaster() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - - rclpy.shutdown() diff --git a/turtle_tf2_py/turtle_tf2_py/fixed_frame_tf2_broadcaster.py b/turtle_tf2_py/turtle_tf2_py/fixed_frame_tf2_broadcaster.py index b8f5cc0..21a39e3 100644 --- a/turtle_tf2_py/turtle_tf2_py/fixed_frame_tf2_broadcaster.py +++ b/turtle_tf2_py/turtle_tf2_py/fixed_frame_tf2_broadcaster.py @@ -15,6 +15,7 @@ from geometry_msgs.msg import TransformStamped import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from tf2_ros import TransformBroadcaster @@ -45,11 +46,9 @@ def broadcast_timer_callback(self): def main(): - rclpy.init() - node = FixedFrameBroadcaster() try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(): + node = FixedFrameBroadcaster() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - - rclpy.shutdown() diff --git a/turtle_tf2_py/turtle_tf2_py/static_turtle_tf2_broadcaster.py b/turtle_tf2_py/turtle_tf2_py/static_turtle_tf2_broadcaster.py index c5fd9b6..5619b13 100644 --- a/turtle_tf2_py/turtle_tf2_py/static_turtle_tf2_broadcaster.py +++ b/turtle_tf2_py/turtle_tf2_py/static_turtle_tf2_broadcaster.py @@ -20,6 +20,7 @@ import numpy as np import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from tf2_ros.static_transform_broadcaster import StaticTransformBroadcaster @@ -104,12 +105,10 @@ def main(): logger.info('Your static turtle name cannot be "world"') sys.exit(2) - # pass parameters and initialize node - rclpy.init() - node = StaticFramePublisher(sys.argv) try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(): + # pass parameters and initialize node + node = StaticFramePublisher(sys.argv) + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - - rclpy.shutdown() diff --git a/turtle_tf2_py/turtle_tf2_py/turtle_tf2_broadcaster.py b/turtle_tf2_py/turtle_tf2_py/turtle_tf2_broadcaster.py index 797cc2b..0c95d1a 100644 --- a/turtle_tf2_py/turtle_tf2_py/turtle_tf2_broadcaster.py +++ b/turtle_tf2_py/turtle_tf2_py/turtle_tf2_broadcaster.py @@ -19,6 +19,7 @@ import numpy as np import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from tf2_ros import TransformBroadcaster @@ -104,11 +105,9 @@ def handle_turtle_pose(self, msg): def main(): - rclpy.init() - node = FramePublisher() try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(): + node = FramePublisher() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - - rclpy.shutdown() diff --git a/turtle_tf2_py/turtle_tf2_py/turtle_tf2_listener.py b/turtle_tf2_py/turtle_tf2_py/turtle_tf2_listener.py index 14e57e8..bb835bc 100644 --- a/turtle_tf2_py/turtle_tf2_py/turtle_tf2_listener.py +++ b/turtle_tf2_py/turtle_tf2_py/turtle_tf2_listener.py @@ -17,6 +17,7 @@ from geometry_msgs.msg import Twist import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from tf2_ros import TransformException @@ -109,11 +110,9 @@ def on_timer(self): def main(): - rclpy.init() - node = FrameListener() try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(): + node = FrameListener() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - - rclpy.shutdown() diff --git a/turtle_tf2_py/turtle_tf2_py/turtle_tf2_message_broadcaster.py b/turtle_tf2_py/turtle_tf2_py/turtle_tf2_message_broadcaster.py index 594c7d3..d82154f 100644 --- a/turtle_tf2_py/turtle_tf2_py/turtle_tf2_message_broadcaster.py +++ b/turtle_tf2_py/turtle_tf2_py/turtle_tf2_message_broadcaster.py @@ -16,6 +16,7 @@ from geometry_msgs.msg import Twist import rclpy +from rclpy.executors import ExternalShutdownException from rclpy.node import Node from turtlesim_msgs.msg import Pose @@ -87,11 +88,9 @@ def handle_turtle_pose(self, msg): def main(): - rclpy.init() - node = PointPublisher() try: - rclpy.spin(node) - except KeyboardInterrupt: + with rclpy.init(): + node = PointPublisher() + rclpy.spin(node) + except (KeyboardInterrupt, ExternalShutdownException): pass - - rclpy.shutdown()