使用Launch运行/监控多节点 [小鱼@7614]

ROS 2launch系统

ROS 2中的launch系统负责帮助用户描述其系统的配置,然后按照所述执行。系统的配置包括运行哪些程序、在哪里运行它们、传递它们的参数,和ROS特定的约定,通过为组件提供不同的配置,可以轻松地在整个系统中重用组件。它还负责监控过程launched的状态,并报告和/或对这些过程的状态变化作出反应。 [待校准@7616]

用Python编写的Launch文件可以启动和停止不同的节点,也可以触发和作用于各种事件。提供此帧工作的包是 launch_ros ,它在下面使用非ROS特定的 launch 帧工作。 [待校准@7617]

[需手动修复的语法] design document 详述了ROS 2的launch系统设计的目标 (目前并非所有功能都可用)。 [待校准@7618]

编写ROS 2launch文件 [待校准@7619]

如果你还没有,一定要完成关于如何 create a ROS 2 package 的教程。在ROS 2中创建launch文件的一种方法是使用Python文件,该文件由ROS 2 CLI工具 ros2 launch 执行。我们首先在工作区中使用 ros2 pkg create <pkg-name> --dependencies [deps] 创建一个ROS 2包,并创建一个新的 launch 目录。 [待校准@7620]

Python功能包 [小鱼@7621]

对于Python包,您的目录应如下所示:

src/
    my_package/
        launch/
        setup.py
        setup.cfg
        package.xml

为了让colcon找到launch文件,我们需要使用 setupdata_files 参数通知Python的设置工具我们的launch文件。 [待校准@7623]

在我们的 setup.py 文件中: [待校准@7624]

import os
from glob import glob
from setuptools import setup

package_name = 'my_package'

setup(
    # Other parameters ...
    data_files=[
        # ... Other data files
        # Include all launch files. This is the most important line here!
        (os.path.join('share', package_name), glob('launch/*.launch.py'))
    ]
)

Cpackages包 [待校准@7625]

如果您正在创建一个C++ 包,我们将只通过添加以下内容来调整 CMakeLists.txt 文件: [待校准@7626]

# Install launch files.
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)

到文件末尾 (但在 ament_package() 之前)。 [待校准@7627]

写launch文件 [待校准@7628]

在你的launch目录内,用 .launch.py 后缀创建一个新的launch文件。例如 my_script.launch.py[待校准@7629]

因此``.launch.py`` is not specifically required as the file suffix for launch files. Another popular option is _launch.py, used in the beginner level launch files tutorial. If you do change the suffix, make sure to adjust the glob() argument in your setup.py file。 [待校准@7630]

你的launch文件应该定义 generate_launch_description() ,它返回 ros2 launch 动词使用的 launch.LaunchDescription()[待校准@7631]

import launch
import launch.actions
import launch.substitutions
import launch_ros.actions


def generate_launch_description():
    return launch.LaunchDescription([
        launch.actions.DeclareLaunchArgument(
            'node_prefix',
            default_value=[launch.substitutions.EnvironmentVariable('USER'), '_'],
            description='Prefix for node names'),
        launch_ros.actions.Node(
            package='demo_nodes_cpp', executable='talker', output='screen',
            name=[launch.substitutions.LaunchConfiguration('node_prefix'), 'talker']),
    ])

用法 [Alyssa@7632]

虽然launch文件可以作为独立脚本编写,但ROS中的典型用法是由ROS 2工具调用launch文件。 [待校准@7633]

后运行 colcon build 和采购工作区,你应该能够launch的launch文件如下: [待校准@7634]

ros2 launch my_package script.launch.py

ROS 2launch概念示例

的launch文件 this example launches两个节点,节点 managed lifecycle (a "lifecycle node" )。生命周期节点launch通过 launch_ros 自动调用y在状态之间转换时发出 * 事件 *。然后,这些事件可以通过launch帧的工作来执行。例如,通过发出其他事件 (例如请求另一个状态转换,生命周期节点launch通过 launch_ros 自动调用y具有事件处理程序) 或触发其他 * 动作 * (例如g.启动另一个节点)。 [待校准@7636]

在上述示例中,向 talker 生命周期节点请求各种转换请求,并且其转换事件响应于,例如,launch成为 listener 节点生命周期健谈达到适当状态。 [待校准@7637]

文档 [待校准@7638]

[需手动修复的语法] The launch documentation 提供了更多关于同样用于 launch_ros 的概念的细节。 [待校准@7639]

其他文件/功能示例即将发布。同时见 the source code[待校准@7640]