< >
Home » ROS2与launch入门教程 » ROS2与launch入门教程-使用Python、XML 和 YAML格式编写ROS2 launch文件

ROS2与launch入门教程-使用Python、XML 和 YAML格式编写ROS2 launch文件

ROS2与launch入门教程-使用Python、XML 和 YAML格式编写ROS2 launch文件

说明:

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

launch文件

  • 下面是一个用 Python、XML 和 YAML 实现的启动文件。

  • 每个启动文件执行以下操作:

    • 使用默认值设置命令行参数
    • 包含另一个启动文件
    • 在另一个命名空间中包含另一个启动文件
    • 启动一个节点并设置它的命名空间
    • 启动一个节点,设置其命名空间,并在该节点中设置参数(使用 args)
    • 创建一个节点以将消息从一个主题重新映射到另一个主题
  • 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 IncludeLaunchDescription
from launch.actions import GroupAction
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


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_ns_launch_arg = DeclareLaunchArgument(
        "chatter_ns", default_value=TextSubstitution(text="my/chatter/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 another launch file in the chatter_ns namespace
    launch_include_with_namespace = GroupAction(
        actions=[
            # push_ros_namespace to set namespace of included nodes
            PushRosNamespace(LaunchConfiguration('chatter_ns')),
            IncludeLaunchDescription(
                PythonLaunchDescriptionSource(
                    os.path.join(
                        get_package_share_directory('demo_nodes_cpp'),
                        'launch/topics/talker_listener.launch.py'))
            ),
        ]
    )

    # 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_ns_launch_arg,
        launch_include,
        launch_include_with_namespace,
        turtlesim_node,
        turtlesim_node_with_parameters,
        forward_turtlesim_commands_to_second_turtlesim_node,
    ])
  • 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_ns" default="my/chatter/ns"/>

  <!-- include another launch file -->
  <include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener.launch.py"/>
  <!-- include another launch file in the chatter_ns namespace-->
  <group>
    <!-- push_ros_namespace to set namespace of included nodes -->
    <push_ros_namespace namespace="$(var chatter_ns)"/>
    <include file="$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener.launch.py"/>
  </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>
  • 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_ns"
    default: "my/chatter/ns"


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

# include another launch file in the chatter_ns namespace
- group:
    - push_ros_namespace:
        namespace: "$(var chatter_ns)"
    - include:
        file: "$(find-pkg-share demo_nodes_cpp)/launch/topics/talker_listener.launch.py"

# 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. 启动launch文件
  • 上面的任何启动文件都可以使用 ros2 启动运行。
  • 要在本地尝试它们,您可以创建一个新包并使用
ros2 launch <package_name> <launch_file_name>
  • 或通过指定启动文件的路径直接运行文件
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

如何选择格式

  • ROS1 中的启动文件是用 XML 编写的,因此来自 ROS 1 的人可能最熟悉 XML。

  • 要查看发生了什么变化,您可以访问将启动文件从 ROS 1 迁移到 ROS 2

  • 对于大多数应用程序,选择哪种 ROS 2 启动格式取决于开发人员的偏好。

  • 但是,如果您的启动文件需要 XML 或 YAML 无法实现的灵活性,您可以使用 Python 编写启动文件。

  • 由于以下两个原因,使用 Python 启动 ROS 2 更加灵活:

    • Python 是一种脚本语言,因此您可以在启动文件中利用该语言及其库。
    • ros2/launch(一般启动功能)和 ros2/launch_ros(ROS 2 特定启动功能)是用 Python 编写的,因此您对 XML 和 YAML 可能未公开的启动功能具有较低级别的访问权限。
  • 但是,用 Python 编写的启动文件可能比用 XML 或 YAML 编写的启动文件更复杂和冗长。

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros2与launch入门教程