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

As of ROS2 Foxy, there are still ongoing issues/discussions regarding this. See the following Github threads if you're interested:

TL;DR: Composable Nodes are NOT treated the same as ordinary Nodes in ROS2. The only code I've found to work is the following:

import os
import yaml
import launch
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import ComposableNodeContainer
from launch_ros.descriptions import ComposableNode

def generate_launch_description():    

    # Get the filepath to your config file
    configFilepath = os.path.join(
        get_package_share_directory("my_package_name"), 
        'my_params.yaml')                       

    # Load the parameters specific to your ComposableNode
    with open(configFilepath, 'r') as file:
        configParams = yaml.safe_load(file)['my_node_name']['ros__parameters']   

    # Create your ComposableNode
    container = ComposableNodeContainer(
        name='my_container_name',
        namespace='',
        package='rclcpp_components',
        executable='component_executable',
        composable_node_descriptions=[
            ComposableNode(
                package='my_package_name',
                plugin='my_namespace::my_node_class', 
                name='my_node_name',
                parameters=[configParams]
            )
        ],
        output='screen',
    )

    return launch.LaunchDescription([container])

It is honestly a real pain that they're not treated the same way, but that's hopefully something that will change in future ROS2 distros.

Also note that there are a lot of edits you need to make to your CMakelists file to get your node to even compile in a way that allows the executable to be composed. But for that you should look at the composition demo code.

As of ROS2 Foxy, there are still ongoing issues/discussions regarding this. See the following Github threads if you're interested:

TL;DR: Composable Nodes are NOT treated the same as ordinary Nodes in ROS2. The only code I've found to work is the following:

import os
import yaml
import launch
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import ComposableNodeContainer
from launch_ros.descriptions import ComposableNode

def generate_launch_description():    

    # Get the filepath to your config file
    configFilepath = os.path.join(
        get_package_share_directory("my_package_name"), 
        'my_params.yaml')                       

    # Load the parameters specific to your ComposableNode
    with open(configFilepath, 'r') as file:
        configParams = yaml.safe_load(file)['my_node_name']['ros__parameters']   

    # Create your ComposableNode
    container = ComposableNodeContainer(
        name='my_container_name',
        namespace='',
        package='rclcpp_components',
        executable='component_executable',
executable='component_executable',         # This name must match your CMakelist file
        composable_node_descriptions=[
            ComposableNode(
                package='my_package_name',
                plugin='my_namespace::my_node_class', 
                name='my_node_name',
                parameters=[configParams]
            )
        ],
        output='screen',
    )

    return launch.LaunchDescription([container])

It is honestly a real pain that they're not treated the same way, but that's hopefully something that will change in future ROS2 distros.

Also note that there are a lot of edits you need to make to your CMakelists file to get your node to even compile in a way that allows the executable to be composed. But for that you should look at the composition demo code.

As of ROS2 Foxy, there are still ongoing issues/discussions regarding this. See the following Github threads if you're interested:

TL;DR: Composable Nodes are NOT treated the same as ordinary Nodes in ROS2. The only code I've found to work is the following:

import os
import yaml
import launch
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import ComposableNodeContainer
from launch_ros.descriptions import ComposableNode

def generate_launch_description():    

    # Get the filepath to your config file
    configFilepath = os.path.join(
        get_package_share_directory("my_package_name"), 
        'my_params.yaml') 'my_params.yaml'
    )                       

    # Load the parameters specific to your ComposableNode
    with open(configFilepath, 'r') as file:
        configParams = yaml.safe_load(file)['my_node_name']['ros__parameters']   

    # Create your ComposableNode
    container = ComposableNodeContainer(
        name='my_container_name',
        namespace='',
        package='rclcpp_components',
        executable='component_executable',         # This name must match your CMakelist file
        composable_node_descriptions=[
            ComposableNode(
                package='my_package_name',
                plugin='my_namespace::my_node_class', 
                name='my_node_name',
                parameters=[configParams]
            )
        ],
        output='screen',
    )

    return launch.LaunchDescription([container])

It is honestly a real pain that they're not treated the same way, but that's hopefully something that will change in future ROS2 distros.

Also note that there are a lot of edits you need to make to your CMakelists file to get your node to even compile in a way that allows the executable to be composed. But for that you should look at the composition demo code.