|
| 1 | +Using ROS 2 launch to launch composable nodes |
| 2 | +============================================= |
| 3 | + |
| 4 | +.. contents:: Table of Contents |
| 5 | + :depth: 1 |
| 6 | + :local: |
| 7 | + |
| 8 | +In the :doc:`Composition tutorial <../Tutorials/Composition>`, you learned about composable nodes and how to use them from the command-line. |
| 9 | +In the :doc:`Launch tutorials <../Tutorials/Launch/Launch-Main>`, you learned about launch files and how to use them to manage multiple nodes. |
| 10 | + |
| 11 | +This guide will combine the above two topics and teach you how to write launch files for composable nodes. |
| 12 | + |
| 13 | +Setup |
| 14 | +----- |
| 15 | + |
| 16 | +See the :doc:`installation instructions <../Installation>` for details on installing ROS 2. |
| 17 | + |
| 18 | +If you've installed ROS 2 from packages, ensure that you have ``ros-{DISTRO}-image-tools`` installed. |
| 19 | +If you downloaded the archive or built ROS 2 from source, it will already be part of the installation. |
| 20 | + |
| 21 | +Launch file examples |
| 22 | +-------------------- |
| 23 | + |
| 24 | +Below is a launch file that launches composable nodes in Python, XML, and YAML. |
| 25 | +The launch files all do the following: |
| 26 | + |
| 27 | +* Instantiate a cam2image composable node with remappings, custom parameters, and extra arguments |
| 28 | +* Instantiate a showimage composable node with remappings, custom parameters, and extra arguments |
| 29 | + |
| 30 | +.. tabs:: |
| 31 | + |
| 32 | + .. group-tab:: Python |
| 33 | + |
| 34 | + .. code-block:: python |
| 35 | +
|
| 36 | + import launch |
| 37 | + from launch_ros.actions import ComposableNodeContainer |
| 38 | + from launch_ros.descriptions import ComposableNode |
| 39 | +
|
| 40 | +
|
| 41 | + def generate_launch_description(): |
| 42 | + """Generate launch description with multiple components.""" |
| 43 | + container = ComposableNodeContainer( |
| 44 | + name='image_container', |
| 45 | + namespace='', |
| 46 | + package='rclcpp_components', |
| 47 | + executable='component_container', |
| 48 | + composable_node_descriptions=[ |
| 49 | + ComposableNode( |
| 50 | + package='image_tools', |
| 51 | + plugin='image_tools::Cam2Image', |
| 52 | + name='cam2image', |
| 53 | + remappings=[('/image', '/burgerimage')], |
| 54 | + parameters=[{'width': 320, 'height': 240, 'burger_mode': True, 'history': 'keep_last'}], |
| 55 | + extra_arguments=[{'use_intra_process_comms': True}]), |
| 56 | + ComposableNode( |
| 57 | + package='image_tools', |
| 58 | + plugin='image_tools::ShowImage', |
| 59 | + name='showimage', |
| 60 | + remappings=[('/image', '/burgerimage')], |
| 61 | + parameters=[{'history': 'keep_last'}], |
| 62 | + extra_arguments=[{'use_intra_process_comms': True}]) |
| 63 | + ], |
| 64 | + output='both', |
| 65 | + ) |
| 66 | +
|
| 67 | + return launch.LaunchDescription([container]) |
| 68 | +
|
| 69 | + .. group-tab:: XML |
| 70 | + |
| 71 | + .. code-block:: xml |
| 72 | +
|
| 73 | + <launch> |
| 74 | + <node_container pkg="rclcpp_components" exec="component_container" name="image_container" namespace=""> |
| 75 | + <composable_node pkg="image_tools" plugin="image_tools::Cam2Image" name="cam2image" namespace=""> |
| 76 | + <remap from="/image" to="/burgerimage" /> |
| 77 | + <param name="width" value="320" /> |
| 78 | + <param name="height" value="240" /> |
| 79 | + <param name="burger_mode" value="true" /> |
| 80 | + <param name="history" value="keep_last" /> |
| 81 | + <extra_arg name="use_intra_process_comms" value="true" /> |
| 82 | + </composable_node> |
| 83 | + <composable_node pkg="image_tools" plugin="image_tools::ShowImage" name="showimage" namespace=""> |
| 84 | + <remap from="/image" to="/burgerimage" /> |
| 85 | + <param name="history" value="keep_last" /> |
| 86 | + <extra_arg name="use_intra_process_comms" value="true" /> |
| 87 | + </composable_node> |
| 88 | + </node_container> |
| 89 | + </launch> |
| 90 | +
|
| 91 | + .. group-tab:: YAML |
| 92 | + |
| 93 | + .. code-block:: yaml |
| 94 | +
|
| 95 | + launch: |
| 96 | +
|
| 97 | + - node_container: |
| 98 | + pkg: rclcpp_components |
| 99 | + exec: component_container |
| 100 | + name: image_container |
| 101 | + namespace: '' |
| 102 | + composable_node: |
| 103 | + - pkg: image_tools |
| 104 | + plugin: image_tools::Cam2Image |
| 105 | + name: cam2image |
| 106 | + namespace: '' |
| 107 | + remap: |
| 108 | + - from: /image |
| 109 | + to: /burgerimage |
| 110 | + param: |
| 111 | + - name: width |
| 112 | + value: 320 |
| 113 | + - name: height |
| 114 | + value: 240 |
| 115 | + - name: burger_mode |
| 116 | + value: true |
| 117 | + - name: history |
| 118 | + value: keep_last |
| 119 | + extra_arg: |
| 120 | + - name: use_intra_process_comms |
| 121 | + value: 'true' |
| 122 | +
|
| 123 | + - pkg: image_tools |
| 124 | + plugin: image_tools::ShowImage |
| 125 | + name: showimage |
| 126 | + namespace: '' |
| 127 | + remap: |
| 128 | + - from: /image |
| 129 | + to: /burgerimage |
| 130 | + param: |
| 131 | + - name: history |
| 132 | + value: keep_last |
| 133 | + extra_arg: |
| 134 | + - name: use_intra_process_comms |
| 135 | + value: 'true' |
| 136 | +
|
| 137 | +Using the Launch files from the command-line |
| 138 | +-------------------------------------------- |
| 139 | + |
| 140 | +Any of the launch files above can be run with ``ros2 launch``. |
| 141 | +Copy the data into a local file, and then run: |
| 142 | + |
| 143 | +.. code-block:: console |
| 144 | +
|
| 145 | + ros2 launch <path_to_launch_file> |
| 146 | +
|
| 147 | +Intra-process communications |
| 148 | +---------------------------- |
| 149 | + |
| 150 | +All of the above examples use an extra argument to setup intra-process communication between the nodes. |
| 151 | +For more information on what intra-process communications are, see the :doc:`intra-process comms tutorial <../Tutorials/Intra-Process-Communication>`. |
| 152 | + |
| 153 | +Python, XML, or YAML: Which should I use? |
| 154 | +----------------------------------------- |
| 155 | + |
| 156 | +See the discussion in :doc:`Launch-file-different-formats` for more information. |
0 commit comments