ROS 2 - Python、XML 和 YAML 编写 Launch 文件

系列文章目录

ROS2 重要概念
ament_cmake_python 用户文档
ROS2 ament_cmake 用户文档
使用 rosdep 管理 ROS 2 依赖项


文章目录

  • 系列文章目录
  • 前言
  • 一、Launch 文件示例
    • 1.1 Python 版本
    • 1.2 XML 版本
    • 1.3 YAML 版本
  • 二、从命令行使用 Launch 文件
    • 1. Launching
    • 2. 设置参数
    • 3. 控制海龟
  • 三、Python、XML 或 YAML: 我应该使用哪种语言?
  • 四、为什么要使用 ROS 2 Launch


前言

ROS 2 launch 文件可以用 Python、XML 和 YAML 编写。本指南介绍了如何使用这些不同的格式来完成相同的任务,并对何时使用每种格式进行了讨论。


一、Launch 文件示例

下面是一个用 Python、XML 和 YAML 实现的 Launch 文件。每个 Launch 文件都执行以下操作:

  • 使用默认值设置命令行参数

  • 包含另一个 launch 文件

  • 在另一个命名空间中包含另一个启动文件

  • 启动节点并设置其名称空间

  • 启动一个节点,设置其名称空间,并在该节点中设置参数(使用参数)

  • 创建一个节点,将消息从一个话题重新映射到另一个话题

1.1 Python 版本

首先介绍涉及到的几个 方法

  1. ament_index_python.get_package_share_path(package_name, print_warning=True)
以 pathlib.Path 的形式返回给定软件包的共享目录。
例如,如果您将软件包 "foo "安装到"/home/user/ros2_ws/install",并以 "foo"作为参数调用此函数,那么它将返回一个代表"/home/user/ros2_ws/install/share/foo"的路径,然后您就可以用它来构建共享文件的路径,即 get_package_share_path('foo') /'urdf/robot.urdf'
  1. launch.LaunchDescription
基础: LaunchDescriptionEntity
可启动系统的描述。
该描述由一系列实体组成,这些实体代表了系统设计师的意图。
该描述还可能有参数,参数由该启动描述中的 launch.actions.DeclareLaunchArgument 操作声明。
该描述的参数可通过 get_launch_arguments() 方法访问。参数是通过搜索此启动描述中的实体和每个实体的描述(可能包括由这些实体产生的实体)收集的。
  1. launch.actions.declare_launch_argument.DeclareLaunchArgument(Action)
基础: Action
声明新启动参数的 Action。
启动参数存储在同名的 "启动配置 "中。请参阅 launch.actions.SetLaunchConfiguration 和 launch.substitutions.LaunchConfiguration。
在 launch.LaunchDescription 中声明的任何启动参数都会在包含该启动描述时作为参数显示出来,例如,在 launch.actions.IncludeLaunchDescription 动作中作为附加参数,或在使用 ros2 launch .... 启动时作为命令行参数。
除了名称(也是参数结果的存储位置)外,启动参数还可能有一个默认值、一个有效值选择列表和一个描述。如果给出了默认值,那么该参数就变成了可选参数,默认值将被放置在启动配置中。如果没有给出默认值,并且在包含启动说明时也没有给出值,则会发生错误。如果给出了一个选择列表,而给定值不在其中,则会发生错误。
默认值可以使用 Substitutions,但名称和描述只能是 Text,因为它们在启动前需要一个有意义的值,例如在列出命令行参数时。
需要注意的是,声明启动参数必须在启动描述的某个部分,而这个部分在不启动的情况下是可以描述的。例如,如果你在条件组中或作为事件处理程序的回调声明了一个启动参数,那么像 ros2 launch 这样的工具可能无法在启动描述之前知道该参数。在这种情况下,该参数在命令行上将不可见,但如果该参数在访问后未满足要求(且没有默认值),则可能引发异常。
换句话说,访问该操作的后置条件要么是同名的启动配置设置了值,要么是由于没有设置任何值且没有默认值而引发异常。但是,前置条件并不能保证在条件或情况夹杂后面的参数是可见的。
例如;
ld = LaunchDescription([
    DeclareLaunchArgument('simple_argument'),
    DeclareLaunchArgument('with_default_value', default_value='default'),
    DeclareLaunchArgument(
        'with_default_and_description',
        default_value='some_default',
        description='this argument is used to configure ...'),
    DeclareLaunchArgument(
        'mode',
        default_value='A',
        description='Choose between mode A and mode B',
        choices=['A', 'B']),
    # other actions here, ...
])
  1. launch.actions.GroupAction
基础: Action
可产生其他操作的 Action。
此操作用于嵌套其他操作,而无需包含单独的启动说明,同时还可选择具有一个条件(与所有其他操作一样)、扩展和转发启动配置和环境变量,以及/或仅为组及其产生的操作声明启动配置。
Scoped=True 时,对启动配置和环境变量的更改仅限于组操作中的操作范围。
当 scopeed=True 和 forwarding=True 时,所有现有的启动配置和环境变量都可在作用域上下文中使用。
当 scope=True 和 forwarding=False 时,所有现有的启动配置和环境变量都会从作用域上下文中移除。
launch_configurations 字典中定义的任何启动配置都将在当前上下文中设置。当 scopeed=False 时,即使 GroupAction 已完成,这些配置也将持续存在。当 scoped=True 时,这些配置将仅对 GroupAction 中的动作可用。当 scope=True 和 forwarding=False 时,launch_configurations 字典将在清除前进行评估,然后在清除的 scope 上下文中重新设置。
  1. launch.actions.IncludeLaunchDescription
基础: Action
包含启动描述源并在访问时生成其实体的动作。
可以向启动描述传递参数,这些参数是通过 launch.actions.DeclareLaunchArgument 动作声明的。
如果给定的参数与已声明的启动参数名称不匹配,则仍会使用 launch.actions.SetLaunchConfiguration 动作将其设置为启动配置。这样做的原因是,在给定的启动描述中,并非总能检测到所有已声明启动参数类的实例。
另一方面,如果给定的启动描述声明了启动参数,但未向此操作提供其值,有时会引发错误。不过,只有当声明的启动参数是无条件的(有时声明启动参数的操作只有在特定情况下才会被访问),并且没有默认值可供选择时,才会产生这种错误。
有条件包含的启动参数如果没有默认值,在尽力进行参数检查后仍无法提前发现未满足的参数时,最终将引发错误。
  1. launch.launch_description_sources.PythonLaunchDescriptionSource(LaunchDescriptionSource)
基础: LaunchDescriptionSource
Python 启动文件的封装,可在启动过程中加载。
  1. launch.substitutions.LaunchConfiguration(Substitution)
可访问启动配置变量的替代变量。
  1. launch.substitutions.TextSubstitution(Substitution)
可对单个字符串文本进行替换。
  1. PushROSNamespace(Action)
推送 ros 命名空间的动作。
在有作用域的 `GroupAction` 中使用时,它会自动弹出。没有其他方法可以弹出它。 
  1. XMLLaunchDescriptionSource(FrontendLaunchDescriptionSource)
封装 XML 启动文件,可在启动过程中加载。
  1. YAMLLaunchDescriptionSource(FrontendLaunchDescriptionSource)
封装 YAML 启动文件,可在启动过程中加载。
  1. Node(ExecuteProcess)
执行一个 ROS 节点的操作。

Python 代码如下

# example_launch.py

import os

from ament_index_python import get_package_share_directory

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import GroupAction
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import LaunchConfiguration
from launch.substitutions import TextSubstitution
from launch_ros.actions import Node
from launch_ros.actions import PushRosNamespace
from launch_xml.launch_description_sources import XMLLaunchDescriptionSource
from launch_yaml.launch_description_sources import YAMLLaunchDescriptionSource


def generate_launch_description():

    # args that can be set from the command line or a default will be used
    background_r_launch_arg = DeclareLaunchArgument(
        "background_r", default_value=TextSubstitution(text="0")
    )
    background_g_launch_arg = DeclareLaunchArgument(
        "background_g", default_value=TextSubstitution(text="255")
    )
    background_b_launch_arg = DeclareLaunchArgument(
        "background_b", default_value=TextSubstitution(text="0")
    )
    chatter_py_ns_launch_arg = DeclareLaunchArgument(
        "chatter_py_ns", default_value=TextSubstitution(text="chatter/py/ns")
    )
    chatter_xml_ns_launch_arg = DeclareLaunchArgument(
        "chatter_xml_ns", default_value=TextSubstitution(text="chatter/xml/ns")
    )
    chatter_yaml_ns_launch_arg = DeclareLaunchArgument(
        "chatter_yaml_ns", default_value=TextSubstitution(text="chatter/yaml/ns")
    )

    # include another launch file
    launch_include = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            os.path.join(
                get_package_share_directory('demo_nodes_cpp'),
                'launch/topics/talker_listener_launch.py'))
    )
    # include a Python launch file in the chatter_py_ns namespace
    launch_py_include_with_namespace = GroupAction(
        actions=[
            # push_ros_namespace to set namespace of included nodes
            PushRosNamespace('chatter_py_ns'),
            IncludeLaunchDescription(
                PythonLaunchDescriptionSource(
                    os.path.join(
                        get_package_share_directory('demo_nodes_cpp'),
                        'launch/topics/talker_listener_launch.py'))
            ),
        ]
    )

    # include a xml launch file in the chatter_xml_ns namespace
    launch_xml_include_with_namespace = GroupAction(
        actions=[
            # push_ros_namespace to set namespace of included nodes
            PushRosNamespace('chatter_xml_ns'),
            IncludeLaunchDescription(
                XMLLaunchDescriptionSource(
                    os.path.join(
                        get_package_share_directory('demo_nodes_cpp'),
                        'launch/topics/talker_listener_launch.xml'))
            ),
        ]
    )

    # include a yaml launch file in the chatter_yaml_ns namespace
    launch_yaml_include_with_namespace = GroupAction(
        actions=[
            # push_ros_namespace to set namespace of included nodes
            PushRosNamespace('chatter_yaml_ns'),
            IncludeLaunchDescription(
                YAMLLaunchDescriptionSource(
                    os.path.join(
                        get_package_share_directory('demo_nodes_cpp'),
                        'launch/topics/talker_listener_launch.yaml'))
            ),
        ]
    )

    # start a turtlesim_node in the turtlesim1 namespace
    turtlesim_node = Node(
        package='turtlesim',
        namespace='turtlesim1',
        executable='turtlesim_node',
        name='sim'
    )

    # start another turtlesim_node in the turtlesim2 namespace
    # and use args to set parameters
    turtlesim_node_with_parameters = Node(
        package='turtlesim',
        namespace='turtlesim2',
        executable='turtlesim_node',
        name='sim',
        parameters=[{
            "background_r": LaunchConfiguration('background_r'),
            "background_g": LaunchConfiguration('background_g'),
            "background_b": LaunchConfiguration('background_b'),
        }]
    )

    # perform remap so both turtles listen to the same command topic
    forward_turtlesim_commands_to_second_turtlesim_node = Node(
        package='turtlesim',
        executable='mimic',
        name='mimic',
        remappings=[
            ('/input/pose', '/turtlesim1/turtle1/pose'),
            ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
        ]
    )

    return LaunchDescription([
        background_r_launch_arg,
        background_g_launch_arg,
        background_b_launch_arg,
        chatter_py_ns_launch_arg,
        chatter_xml_ns_launch_arg,
        chatter_yaml_ns_launch_arg,
        launch_include,
        launch_py_include_with_namespace,
        launch_xml_include_with_namespace,
        launch_yaml_include_with_namespace,
        turtlesim_node,
        turtlesim_node_with_parameters,
        forward_turtlesim_commands_to_second_turtlesim_node,
    ])

1.2 XML 版本

<!-- example_launch.xml -->

<launch>

    <!-- args that can be set from the command line or a default will be used -->
    <arg name="background_r" default="0" />
    <arg name="background_g" default="255" />
    <arg name="background_b" default="0" />
    <arg name="chatter_py_ns" default="chatter/py/ns" />
    <arg name="chatter_xml_ns" default="chatter/xml/ns" />
    <arg name="chatter_yaml_ns" default="chatter/yaml/ns" />

    <!-- include another launch file -->
    <include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py" />
    <!-- include a Python launch file in the chatter_py_ns namespace-->
    <group>
        <!-- push_ros_namespace to set namespace of included nodes -->
        <push_ros_namespace namespace="$(var chatter_py_ns)" />
        <include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py" />
    </group>
    <!-- include a xml launch file in the chatter_xml_ns namespace-->
    <group>
        <!-- push_ros_namespace to set namespace of included nodes -->
        <push_ros_namespace namespace="$(var chatter_xml_ns)" />
        <include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.xml" />
    </group>
    <!-- include a yaml launch file in the chatter_yaml_ns namespace-->
    <group>
        <!-- push_ros_namespace to set namespace of included nodes -->
        <push_ros_namespace namespace="$(var chatter_yaml_ns)" />
        <include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.yaml" />
    </group>

    <!-- start a turtlesim_node in the turtlesim1 namespace -->
    <node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim1" />
    <!-- start another turtlesim_node in the turtlesim2 namespace
        and use args to set parameters -->
    <node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim2">
        <param name="background_r" value="$(var background_r)" />
        <param name="background_g" value="$(var background_g)" />
        <param name="background_b" value="$(var background_b)" />
    </node>
    <!-- perform remap so both turtles listen to the same command topic -->
    <node pkg="turtlesim" exec="mimic" name="mimic">
        <remap from="/input/pose" to="/turtlesim1/turtle1/pose" />
        <remap from="/output/cmd_vel" to="/turtlesim2/turtle1/cmd_vel" />
    </node>
</launch>

1.3 YAML 版本

# example_launch.yaml

launch:

# args that can be set from the command line or a default will be used
- arg:
    name: "background_r"
    default: "0"
- arg:
    name: "background_g"
    default: "255"
- arg:
    name: "background_b"
    default: "0"
- arg:
    name: "chatter_py_ns"
    default: "chatter/py/ns"
- arg:
    name: "chatter_xml_ns"
    default: "chatter/xml/ns"
- arg:
    name: "chatter_yaml_ns"
    default: "chatter/yaml/ns"


# include another launch file
- include:
    file: "$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py"

# include a Python launch file in the chatter_py_ns namespace
- group:
    - push_ros_namespace:
        namespace: "$(var chatter_py_ns)"
    - include:
        file: "$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.py"

# include a xml launch file in the chatter_xml_ns namespace
- group:
    - push_ros_namespace:
        namespace: "$(var chatter_xml_ns)"
    - include:
        file: "$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.xml"

# include a yaml launch file in the chatter_yaml_ns namespace
- group:
    - push_ros_namespace:
        namespace: "$(var chatter_yaml_ns)"
    - include:
        file: "$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener_launch.yaml"

# start a turtlesim_node in the turtlesim1 namespace
- node:
    pkg: "turtlesim"
    exec: "turtlesim_node"
    name: "sim"
    namespace: "turtlesim1"

# start another turtlesim_node in the turtlesim2 namespace and use args to set parameters
- node:
    pkg: "turtlesim"
    exec: "turtlesim_node"
    name: "sim"
    namespace: "turtlesim2"
    param:
    -
      name: "background_r"
      value: "$(var background_r)"
    -
      name: "background_g"
      value: "$(var background_g)"
    -
      name: "background_b"
      value: "$(var background_b)"

# perform remap so both turtles listen to the same command topic
- node:
    pkg: "turtlesim"
    exec: "mimic"
    name: "mimic"
    remap:
    -
        from: "/input/pose"
        to: "/turtlesim1/turtle1/pose"
    -
        from: "/output/cmd_vel"
        to: "/turtlesim2/turtle1/cmd_vel"

二、从命令行使用 Launch 文件

1. Launching

上述任何启动文件都可以通过 ros2 launch 运行。要在本地试用它们,可以创建一个新软件包,然后使用

ros2 launch <package_name> <launch_file_name>

或通过指定 launch 文件的路径直接运行该文件

ros2 launch <path_to_launch_file>

2. 设置参数

要设置传递给启动文件的参数,应使用 key:=value 语法。例如,可以用以下方式设置 background_r 的值:

ros2 launch <package_name> <launch_file_name> background_r:=255
ros2 launch <path_to_launch_file> background_r:=255

3. 控制海龟

要测试重映射是否有效,可以在另一个终端运行以下命令来控制海龟:

ros2 run turtlesim turtle_teleop_key --ros-args --remap __ns:=/turtlesim1

三、Python、XML 或 YAML: 我应该使用哪种语言?

ROS 1 中的启动文件是用 XML 编写的,因此对于来自 ROS 1 的用户来说,XML 可能是最熟悉的。

对于大多数应用程序来说,选择哪种 ROS 2 启动格式取决于开发人员的偏好。不过,如果你的启动文件需要有 XML 或 YAML 无法实现的灵活性,你可以使用 Python 来编写启动文件。由于以下两个原因,使用 Python 编写 ROS 2 启动文件更为灵活:

  • Python 是一种脚本语言,因此您可以在启动文件中使用该语言及其库。

  • ros2/launch(一般启动功能)和 ros2/launch_ros(ROS 2 特定启动功能)都是用 Python 编写的,因此你可以访问 XML 和 YAML 可能无法提供的较低级别的启动功能。

尽管如此,用 Python 编写的启动文件可能比 XML 或 YAML 编写的文件更复杂、更冗长。

四、为什么要使用 ROS 2 Launch

ROS 2 系统通常由运行在多个不同进程(甚至不同机器)上的多个节点组成。虽然每个节点都可以单独运行,但很快就会变得非常麻烦。

ROS 2 中的启动系统旨在通过一条命令自动运行多个节点。它可以帮助用户描述系统配置,然后按描述执行。系统配置包括运行哪些程序、在哪里运行、传递哪些参数,以及 ROS 特有的约定,通过为每个组件提供不同的配置,可以方便地在整个系统中重复使用组件。它还负责监控已启动进程的状态,并对这些进程的状态变化做出报告和/或反应。

上述所有内容都在一个启动文件中指定,该文件可以用 Python、XML 或 YAML 编写。使用 ros2 launch 命令运行该启动文件后,所有指定的节点都将运行。

设计文档详细介绍了 ROS 2 启动系统的设计目标(目前尚未提供所有功能)。


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我不太懂写launch文件,但我可以提供一些建议:1.确保将ORB-SLAM2的先验设置包含在launch文件中;2.确保launch文件正确地配置ORB-SLAM2,使其可以正确处理数据;3.确保launch文件正确配置ORB-SLAM2,使其可以从摄像头接收数据。 ### 回答2: ORB-SLAM2是一个开源的视觉SLAM系统,它使用了特征点提取和匹配的方法进行定位和建图。在ROS中,可以通过编写一个launch文件来运行ORB-SLAM2系统。 下面是一个示例ORB-SLAM2的launch文件: ``` <launch> <!-- 设置ORB-SLAM2节点 --> <node pkg="orb_slam2" type="orb_slam2_mono" name="orb_slam2" output="screen"> <!-- 设置相机的参数 --> <param name="camera_topic" value="/camera/image_raw"/> <param name="camera_info_topic" value="/camera/camera_info"/> <param name="camera_calibration_file" value="/path/to/camera_calibration.yaml"/> <!-- 设置ORB-SLAM2的参数 --> <param name="orb_vocabulary" value="/path/to/orb_vocabulary_path"/> <param name="orb_settings" value="/path/to/orb_settings.yaml"/> </node> </launch> ``` 上述launch文件使用`<node>`标签定义了一个ORB-SLAM2节点,并且设置了一些参数。其中,`camera_topic`参数指定了相机图像的ROS话题,`camera_info_topic`参数指定了相机的参数信息的话题,`camera_calibration_file`参数指定了相机的标定文件的路径。 另外,`orb_vocabulary`参数指定了ORB-SLAM2的词袋文件的路径,`orb_settings`参数指定了ORB-SLAM2的配置文件的路径。 以上是一个基本的launch文件示例,可以根据具体的需求对参数进行调整。当执行这个launch文件时,ORB-SLAM2节点会开始运行,订阅相机图像和参数信息,并且进行视觉SLAM定位和建图的处理。 ### 回答3: ORB-SLAM2是一个基于特征点的单目、双目和RGB-D相机的实时定位与建图系统。为了使用ORB-SLAM2,我们可以编写一个launch文件来启动相机节点和ORB-SLAM2节点。 在launch文件中,首先我们需要定义用于启动相机和ORB-SLAM2节点的参数。我们可以定义相机的类型(单目、双目或RGB-D)、相机的配置文件路径、ORB-SLAM2的配置文件路径等。 接下来,我们可以定义一个相机节点,使用相机的类型和配置文件路径来启动相机。这可以使用roslaunch中的node标签来完成。 然后,我们可以定义一个ORB-SLAM2节点,使用ORB-SLAM2的配置文件路径来启动。我们可以通过将ORB-SLAM2节点的类型设置为可执行文件,并使用ORB-SLAM2的配置文件路径作为参数来完成这一步骤。 最后,我们可以使用remap标签来将相机节点的图像话题与ORB-SLAM2节点的输入图像话题连接起来。这将确保相机节点的输出可以传递给ORB-SLAM2节点进行定位与建图。 这是一个简单的示例launch文件来启动相机节点和ORB-SLAM2节点: <?xml version="1.0" encoding="UTF-8"?> <launch> <!-- Define camera parameters --> <arg name="camera_type" default="monocular" /> <arg name="camera_config_file" default="$(find orb_slam2)/config/camera.cfg" /> <!-- Define ORB-SLAM2 parameters --> <arg name="orb_slam2_config_file" default="$(find orb_slam2)/config/ORBvoc.txt" /> <!-- Start camera node --> <node name="camera_node" pkg="camera_package" type="camera_node" output="screen" > <param name="camera_type" value="$(arg camera_type)" /> <param name="camera_config_file" value="$(arg camera_config_file)" /> </node> <!-- Start ORB-SLAM2 node --> <node name="orb_slam2_node" pkg="orb_slam2" type="orb_slam2_node" output="screen" > <param name="orb_slam2_config_file" value="$(arg orb_slam2_config_file)" /> </node> <!-- Connect camera output to ORB-SLAM2 input --> <remap from="camera_node/image" to="orb_slam2_node/image" /> </launch> 这个launch文件可以根据实际情况进行修改和调整,以适应不同的相机和ORB-SLAM2的配置。希望这个回答能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值