对ROS 2Launch文件使用Python、XML和YAML [待校准@6162]
ROS 2launch文件可以用Python、XML和YAML编写。本指南展示了如何使用这些不同的格式来完成相同的任务,并讨论了何时使用每种格式。 [待校准@6163]
Launch文件示例 [待校准@6164]
下面是用Python、XML和YAML实现的launch文件。每个launch文件执行以下动作: [待校准@6165]
使用默认值设置命令行参数 [待校准@6166]
包括另一个launch文件 [待校准@6167]
在另一个命名空间中包含另一个launch文件 [待校准@6168]
启动节点并设置其命名空间 [待校准@6169]
启动节点,设置其命名空间,并在该节点中设置参数 (使用args) [待校准@6170]
创建一个节点,将消息从一个话题重新映射到另一个话题 [待校准@6171]
# 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,
])
<!-- 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>
# 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文件 [待校准@6174]
Launch [待校准@6175]
上述任何launch文件都可以与 ros2 launch
一起运行。要尝试调用y,您可以创建一个新包并使用 [待校准@6176]
ros2 launch <package_name> <launch_file_name>
或者通过指定launch文件的路径直接运行文件 [待校准@6177]
ros2 launch <path_to_launch_file>
设置参数 [待校准@6178]
要设置传递给launch文件的参数,应使用 “key:= value” 语法。例如,您可以通过以下方式设置 background_r
的值: [待校准@6179]
ros2 launch <package_name> <launch_file_name> background_r:=255
ros2 launch <path_to_launch_file> background_r:=255
控制海龟 [待校准@6181]
要测试重新映射是否有效,您可以通过在另一个终端中运行以下命令来控制海龟: [待校准@6182]
ros2 run turtlesim turtle_teleop_key --ros-args --remap __ns:=/turtlesim1
Python、XML或YAML: 我应该使用哪个? [待校准@6183]
注解
[需手动修复的语法]Launch文件ROS 1写XML,XML最熟悉的可能来自ROS 1。要了解发生了什么变化,您可以访问 迁移launch文件ROS 1ROS 2 [待校准@6189] 。 [待校准@6184]
对于大多数应用,选择哪种ROS 2launch格式取决于开发人员的偏好。但是,如果您的launch文件需要XML或YAML无法实现的灵活性,则可以使用Python编写launch文件。对ROS 2launch使用Python更加灵活,原因有以下两个: [待校准@6185]
Python是一种脚本语言,因此您可以在launch文件中利用该语言及其库。 [待校准@6186]
[需手动修复的语法] ros2/launch (general launch features) and ros2/launch_ros (ROS 2特定的launch特征) 是用Python编写的,因此您可以更低级地访问可能不会被XML和YAML公开的launch特征。 [待校准@6187]
也就是说,用Python编写的launch文件可能比用XML或YAML编写的文件更复杂、更冗长。 [待校准@6188]