Skip to content

启动可组合节点

在组合教程中,你学习了可组合节点的概念,以及如何从命令行使用它们。 在 Launch 教程中,你学习了 launch 文件以及如何用它来管理多个节点。

本指南将结合这两个主题,教你如何为可组合节点编写 launch 文件。

有关安装 ROS 2 的详细信息,请参阅安装说明。

如果你是通过包安装的 ROS 2,请确保已安装 ros-{DISTRO}-image-tools。 如果你下载了归档文件或从源码构建 ROS 2,它应该已经包含在安装中。

下面是用 XML、YAML 和 Python 编写的启动可组合节点的 launch 文件。 这些 launch 文件都执行以下操作:

  • 实例化一个 cam2image 可组合节点,并配置重映射、自定义参数和额外参数
  • 实例化一个 showimage 可组合节点,并配置重映射、自定义参数和额外参数
<?xml version="1.0" encoding="UTF-8"?>
<launch>
<node_container pkg="rclcpp_components" exec="component_container" name="image_container" namespace="">
<composable_node pkg="image_tools" plugin="image_tools::Cam2Image" name="cam2image">
<remap from="/image" to="/burgerimage" />
<param name="width" value="320" />
<param name="height" value="240" />
<param name="burger_mode" value="true" />
<param name="history" value="keep_last" />
<extra_arg name="use_intra_process_comms" value="true" />
</composable_node>
<composable_node pkg="image_tools" plugin="image_tools::ShowImage" name="showimage">
<remap from="/image" to="/burgerimage" />
<param name="history" value="keep_last" />
<extra_arg name="use_intra_process_comms" value="true" />
</composable_node>
</node_container>
</launch>
%YAML 1.2
---
launch:
- node_container:
pkg: rclcpp_components
exec: component_container
name: image_container
composable_node:
- pkg: image_tools
plugin: image_tools::Cam2Image
name: cam2image
remap:
- from: /image
to: /burgerimage
param:
- name: width
value: 320
- name: height
value: 240
- name: burger_mode
value: true
- name: history
value: keep_last
extra_arg:
- name: use_intra_process_comms
value: true
- pkg: image_tools
plugin: image_tools::ShowImage
name: showimage
remap:
- from: /image
to: /burgerimage
param:
- name: history
value: keep_last
extra_arg:
- name: use_intra_process_comms
value: true
import launch
from launch_ros.actions import ComposableNodeContainer
from launch_ros.descriptions import ComposableNode
def generate_launch_description():
"""Generate launch description with multiple components."""
return launch.LaunchDescription([
ComposableNodeContainer(
name='image_container',
namespace='',
package='rclcpp_components',
executable='component_container',
composable_node_descriptions=[
ComposableNode(
package='image_tools',
plugin='image_tools::Cam2Image',
name='cam2image',
remappings=[('/image', '/burgerimage')],
parameters=[{
'width': 320,
'height': 240,
'burger_mode': True,
'history': 'keep_last'}],
extra_arguments=[{'use_intra_process_comms': True}]),
ComposableNode(
package='image_tools',
plugin='image_tools::ShowImage',
name='showimage',
remappings=[('/image', '/burgerimage')],
parameters=[{'history': 'keep_last'}],
extra_arguments=[{'use_intra_process_comms': True}])
],
output='both',
),
])

将可组合节点加载到已有容器中

Section titled “将可组合节点加载到已有容器中”

容器有时可能由其他 launch 文件启动,或直接从命令行启动。 此时,你需要将组件添加到已有容器中。 为此,可以使用 LoadComposableNodes 将组件加载到指定容器中。 下面的示例启动了与上面相同的节点。

<?xml version="1.0" encoding="UTF-8"?>
<launch>
<node pkg="rclcpp_components" exec="component_container" name="image_container" />
<load_composable_node target="image_container">
<composable_node pkg="image_tools" plugin="image_tools::Cam2Image" name="cam2image">
<remap from="/image" to="/burgerimage" />
<param name="width" value="320" />
<param name="height" value="240" />
<param name="burger_mode" value="true" />
<param name="history" value="keep_last" />
<extra_arg name="use_intra_process_comms" value="true" />
</composable_node>
<composable_node pkg="image_tools" plugin="image_tools::ShowImage" name="showimage" namespace="">
<remap from="/image" to="/burgerimage" />
<param name="history" value="keep_last" />
<extra_arg name="use_intra_process_comms" value="true" />
</composable_node>
</load_composable_node>
</launch>
%YAML 1.2
---
launch:
- node_container:
pkg: rclcpp_components
exec: component_container
name: image_container
- load_composable_node:
target: "image_container"
composable_node:
- pkg: image_tools
plugin: image_tools::Cam2Image
name: cam2image
remap:
- from: /image
to: /burgerimage
param:
- name: width
value: 320
- name: height
value: 240
- name: burger_mode
value: true
- name: history
value: keep_last
extra_arg:
- name: use_intra_process_comms
value: true
- pkg: image_tools
plugin: image_tools::ShowImage
name: showimage
remap:
- from: /image
to: /burgerimage
param:
- name: history
value: keep_last
extra_arg:
- name: use_intra_process_comms
value: true
from launch import LaunchDescription
from launch_ros.actions import LoadComposableNodes, Node
from launch_ros.descriptions import ComposableNode
def generate_launch_description():
return LaunchDescription([
Node(
name='image_container',
package='rclcpp_components',
executable='component_container',
output='both',
),
LoadComposableNodes(
target_container='image_container',
composable_node_descriptions=[
ComposableNode(
package='image_tools',
plugin='image_tools::Cam2Image',
name='cam2image',
remappings=[('/image', '/burgerimage')],
parameters=[
{'width': 320, 'height': 240, 'burger_mode': True, 'history': 'keep_last'}
],
extra_arguments=[{'use_intra_process_comms': True}],
),
ComposableNode(
package='image_tools',
plugin='image_tools::ShowImage',
name='showimage',
remappings=[('/image', '/burgerimage')],
parameters=[{'history': 'keep_last'}],
extra_arguments=[{'use_intra_process_comms': True}]
),
],
)
])

上面的任何 launch 文件都可以使用 ros2 launch 运行。 将上述内容保存到本地文件中,然后运行:

Terminal window
ros2 launch <path_to_launch_file>

以上所有示例都通过额外参数启用了节点之间的进程内通信。 有关进程内通信的更多信息,请参阅组合教程。

XML、YAML 还是 Python:应该使用哪种?

Section titled “XML、YAML 还是 Python:应该使用哪种?”

有关更多信息,请参阅 Launch 文件不同格式中的讨论。