ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

After some time I think that my current approach is best, which I will explain here for anyone who else may need it!

on the node side, the code is as followed:

import rclpy
import traceback
from .example_node import NodeClass
from rclpy.node import Node

def main(args=None):

    rclpy.init(args=args)

    try:
        node_class = NodeClass()
        rclpy.spin(node_class)

    except Exception as exception:  
        traceback_logger_node = Node('node_class_traceback_logger')
        traceback_logger_node.get_logger().error(traceback.format_exc())  
        raise exception

    else:
        # Destroy the node explicitly
        # (optional - otherwise it will be done automatically
        # when the garbage collector destroys the node object)
        node_class.destroy_node()
        rclpy.shutdown()

An import thing to note with this approach is that if you are using a launch file that you need to omit the name field, as shown below. Otherwise both nodes will be given the same name!

from launch_ros.actions import Node

from launch import LaunchDescription


def generate_launch_description():
    return LaunchDescription(
        [
            Node(
                package="system_monitor",
                executable="system_monitor_node",
                output={'both': {'screen', 'log', 'own_log'}},
            ),
        ]
    )