☆重点
ROS1和ROS2其中一个很大区别之一就是launch的编写方式。在ROS1中采用xml格式编写launch,而ROS2保留了XML 格式launch,还另外引入了Python和YAML 编写方式。选择哪种编写取决于每位开发人员的爱好,但是ROS2官方推荐使用Python方式编写,理由如下:Python 是一种脚本语言,使用灵活,Python库强大,可以直接引入:ROS2launch框架本身是用Python编写,由于亲和力高Python可以直接调用一些高级特性 ,而XML 和 YAML 可能无法调用。
当然用 Python 编写的启动文件可能比 XML 或 YAML 编写的启动文件更复杂、更冗长。
☆不过建议能使用Python编写尽量使用Python,不要问为什么,问就是官方推荐!
在使用Python版的launch文件时,涉及的API众多,为了提高编码效率,可以在VScode中设置launch文件的代码模板,将VScode的配置文件python.json修改为为如下内容:
python
"ros2 launch py": {
"prefix": "ros2_launch_py",
"body": [
"from launch import LaunchDescription",
"from launch_ros.actions import Node",
"# 封装终端指令相关类--------------",
"# from launch.actions import ExecuteProcess",
"# from launch.substitutions import FindExecutable",
"# 参数声明与获取-----------------",
"# from launch.actions import DeclareLaunchArgument",
"# from launch.substitutions import LaunchConfiguration",
"# 文件包含相关-------------------",
"# from launch.actions import IncludeLaunchDescription",
"# from launch.launch_description_sources import PythonLaunchDescriptionSource",
"# 分组相关----------------------",
"# from launch_ros.actions import PushRosNamespace",
"# from launch.actions import GroupAction",
"# 事件相关----------------------",
"# from launch.event_handlers import OnProcessStart, OnProcessExit",
"# from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo",
"# 获取功能包下share目录路径-------",
"# from ament_index_python.packages import get_package_share_directory",
"",
"def generate_launch_description():",
" ",
" return LaunchDescription([])"
],
"description": "ros2 launch"
}
一、节点设置
launch 中需要执行的节点被封装为了 launch_ros.actions.Node 对象。
**示例:**新建 py01_node.launch.py 文件,输入如下内容:
python
from launch import LaunchDescription
from launch_ros.actions import Node
import os
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
turtle1 = Node(package="turtlesim",
executable="turtlesim_node",
namespace="ns_1",
name="t1",
exec_name="turtle_label", # 表示流程的标签
respawn=True)
turtle2 = Node(package="turtlesim",
executable="turtlesim_node",
name="t2",
# 参数设置方式1
# parameters=[{"background_r": 0,"background_g": 0,"background_b": 0}],
# 参数设置方式2: 从 yaml 文件加载参数,yaml 文件所属目录需要在配置文件中安装。
parameters=[os.path.join(get_package_share_directory("cpp01_launch"),"config","t2.yaml")],
)
turtle3 = Node(package="turtlesim",
executable="turtlesim_node",
name="t3",
remappings=[("/turtle1/cmd_vel","/cmd_vel")] #话题重映射
)
rviz = Node(package="rviz2",
executable="rviz2",
# 节点启动时传参
arguments=["-d", os.path.join(get_package_share_directory("cpp01_launch"),"config","my.rviz")]
)
turtle4 = Node(package="turtlesim",
executable="turtlesim_node",
# 节点启动时传参,相当于 arguments 传参时添加前缀 --ros-args
ros_arguments=["--remap", "__ns:=/t4_ns", "--remap", "__node:=t4"]
)
return LaunchDescription([turtle1, turtle2, turtle3, rviz, turtle4])
代码解释
1.1 Node使用语法1
python
turtle1 = Node(package="turtlesim",
executable="turtlesim_node",
namespace="group_1",
name="t1",
exec_name="turtle_label", # 表示流程的标签
respawn=True)
上述代码会创建一个 turtlesim_node 节点,设置了若干节点属性,并且节点关闭后会自动重启。
- package:功能包;
- executable:可执行文件;
- namespace:命名空间;
- name:节点名称;
- exe_name:流程标签;
- respawn:设置为True时,关闭节点后,可以自动重启。
1.2 Node使用语法2
python
turtle2 = Node(package="turtlesim",
executable="turtlesim_node",
name="t2",
# 参数设置方式1
# parameters=[{"background_r": 0,"background_g": 0,"background_b": 0}],
# 参数设置方式2: 从 yaml 文件加载参数,yaml 文件所属目录需要在配置文件中安装。
parameters=[os.path.join(get_package_share_directory("cpp01_launch"),"config","t2.yaml")],
)
上述代码会创建一个 turtlesim_node 节点,并导入背景色相关参数。
- parameters:导入参数。
parameter 用于设置被导入的参数,如果是从 yaml 文件加载参数,那么需要先准备 yaml 文件,在功能包下新建 config 目录,config目录下新建 t2.yaml 文件,并输入如下内容:
python
/t2:
ros__parameters:
background_b: 0
background_g: 0
background_r: 50
qos_overrides:
/parameter_events:
publisher:
depth: 1000
durability: volatile
history: keep_last
reliability: reliable
use_sim_time: false
注意,还需要在 CMakeLists.txt 中安装 config:
install(DIRECTORY
launch
config
DESTINATION share/${PROJECT_NAME})
1.3 Node使用语法3
python
turtle3 = Node(package="turtlesim",
executable="turtlesim_node",
name="t3",
remappings=[("/turtle1/cmd_vel","/cmd_vel")] #话题重映射
)
上述代码会创建一个 turtlesim_node 节点,并将话题名称从 /turtle1/cmd_vel 重映射到 /cmd_vel。
- remappings:话题重映射。
1.4 Node使用语法4
python
rviz = Node(package="rviz2",
executable="rviz2",
# 节点启动时传参
arguments=["-d", os.path.join(get_package_share_directory("cpp01_launch"),"config","my.rviz")]
)
上述代码会创建一个 rviz2 节点,并加载了 rviz2 相关的配置文件。
该配置文件可以先启动 rviz2 ,配置完毕后,保存到 config 目录并命名为 my.rviz。
- arguments:调用指令时的参数列表。
1.5 Node使用语法5
python
turtle4 = Node(package="turtlesim",
executable="turtlesim_node",
# 节点启动时传参,相当于 arguments 传参时添加前缀 --ros-args
ros_arguments=["--remap", "__ns:=/t4_ns", "--remap", "__node:=t4"]
)
上述代码会创建一个 turtlesim_node 节点,并在指令调用时传入参数列表。
- ros_arguments:相当于 arguments 前缀 --ros-args。
二、执行指令
launch 中需要执行的命令被封装为了 launch.actions.ExecuteProcess 对象。
**示例:**新建 py02_cmd.launch.py 文件,输入如下内容:
python
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutable
def generate_launch_description():
turtle = Node(package="turtlesim", executable="turtlesim_node")
spawn = ExecuteProcess(
# cmd=["ros2 service call /spawn turtlesim/srv/Spawn \"{x: 8.0, y: 9.0,theta: 0.0, name: 'turtle2'}\""],
# 或
cmd = [
FindExecutable(name = "ros2"), # 不可以有空格
" service call",
" /spawn turtlesim/srv/Spawn",
" \"{x: 8.0, y: 9.0,theta: 1.0, name: 'turtle2'}\""
],
output="both",
shell=True)
return LaunchDescription([turtle,spawn])
代码解释
python
spawn = ExecuteProcess(
# cmd=["ros2 service call /spawn turtlesim/srv/Spawn \"{x: 8.0, y: 9.0,theta: 0.0, name: 'turtle2'}\""],
# 或
cmd = [
FindExecutable(name = "ros2"), # 不可以有空格
" service call",
" /spawn turtlesim/srv/Spawn",
" \"{x: 8.0, y: 9.0,theta: 1.0, name: 'turtle2'}\""
],
output="both",
shell=True)
上述代码用于执行 cmd 参数中的命令,该命令会在 turtlesim_node 中生成一只新的小乌龟。
- cmd:被执行的命令;
- output:设置为 both 时,日志会被输出到日志文件和终端,默认为 log,日志只输出到日志文件。
- shell:如果为 True,则以 shell 的方式执行命令。
三、参数设置
参数设置主要涉及到参数的声明与调用两部分,其中声明被封装为 launch.actions.DeclareLaunchArgument,调用则被封装为 launch.substitutions import LaunchConfiguration。
**需求:**启动turtlesim_node节点时,可以动态设置背景色。
**示例:**新建 py03_args.launch.py 文件,输入如下内容:
python
from pkg_resources import declare_namespace
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration
def generate_launch_description():
decl_bg_r = DeclareLaunchArgument(name="background_r",default_value="255")
decl_bg_g = DeclareLaunchArgument(name="background_g",default_value="255")
decl_bg_b = DeclareLaunchArgument(name="background_b",default_value="255")
turtle = Node(package="turtlesim",
executable="turtlesim_node",
parameters=[{"background_r": LaunchConfiguration("background_r"), "background_g": LaunchConfiguration("background_g"), "background_b": LaunchConfiguration("background_b")}]
)
return LaunchDescription([decl_bg_r,decl_bg_g,decl_bg_b,turtle])
代码解释
python
decl_bg_r = DeclareLaunchArgument(name="background_r",default_value="255")
decl_bg_g = DeclareLaunchArgument(name="background_g",default_value="255")
decl_bg_b = DeclareLaunchArgument(name="background_b",default_value="255")
上述代码会使用DeclareLaunchArgument对象声明三个参数,且每个参数都有参数名称以及默认值。
-
name:参数名称;
-
default_value:默认值。
parameters=[{"background_r": LaunchConfiguration(variable_name="background_r"), "background_g": LaunchConfiguration("background_g"), "background_b": LaunchConfiguration("background_b")}]
上述代码会使用LaunchConfiguration对象获取参数值。
- variable_name:被解析的参数名称。
launch文件执行时,可以动态传入参数,示例如下:
ros2 launch cpp01_launch py03_args.launch.py background_r:=200 background_g:=80 background_b:=30
如果执行launch文件时不手动传入参数,那么解析到的参数值是声明时设置的默认值。
四、文件包含
在 launch 文件中可以包含其他launch文件,需要使用的API为:launch.actions.IncludeLaunchDescription 和 launch.launch_description_sources.PythonLaunchDescriptionSource。
**需求:**新建 launch 文件,包含 4.2.3 中的 launch 文件并为之传入设置背景色相关的参数。
**示例:**新建 py04_include.launch.py 文件,输入如下内容:
python
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
import os
from ament_index_python import get_package_share_directory
def generate_launch_description():
include_launch = IncludeLaunchDescription(
launch_description_source= PythonLaunchDescriptionSource(
launch_file_path=os.path.join(
get_package_share_directory("cpp01_launch"),
"launch/py",
"py03_args.launch.py"
)
),
launch_arguments={
"background_r": "200",
"background_g": "100",
"background_b": "70",
}.items()
)
return LaunchDescription([include_launch])
代码解释
include_launch = IncludeLaunchDescription(
launch_description_source= PythonLaunchDescriptionSource(
launch_file_path=os.path.join(
get_package_share_directory("cpp01_launch"),
"launch/py",
"py03_args.launch.py"
)
),
launch_arguments={
"background_r": "200",
"background_g": "100",
"background_b": "70",
}.items()
)
上述代码将包含一个launch文件并为launch文件传参。
在 IncludeLaunchDescription 对象中:
- launch_description_source:用于设置被包含的 launch 文件;
- launch_arguments:元组列表,每个元组中都包含参数的键和值。
在 PythonLaunchDescriptionSource 对象中:
- launch_file_path:被包含的 launch 文件路径。
五、分组设置
在 launch 文件中,为了方便管理可以对节点分组,分组相关API为:launch.actions.GroupAction和launch_ros.actions.PushRosNamespace。
**需求:**对 launch 文件中的多个 Node 进行分组。
**示例:**新建 py05_group.launch.py 文件,输入如下内容:
python
from launch import LaunchDescription
from launch_ros.actions import Node
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupAction
def generate_launch_description():
turtle1 = Node(package="turtlesim",executable="turtlesim_node",name="t1")
turtle2 = Node(package="turtlesim",executable="turtlesim_node",name="t2")
turtle3 = Node(package="turtlesim",executable="turtlesim_node",name="t3")
g1 = GroupAction(actions=[PushRosNamespace(namespace="g1"),turtle1, turtle2])
g2 = GroupAction(actions=[PushRosNamespace(namespace="g2"),turtle3])
return LaunchDescription([g1,g2])
代码解释
g1 = GroupAction(actions=[PushRosNamespace(namespace="g1"),turtle1, turtle2])
g2 = GroupAction(actions=[PushRosNamespace(namespace="g2"),turtle3])
上述代码将创建两个组,两个组使用了不同的命名空间,每个组下包含了不同的节点。
在 GroupAction 对象中,使用的参数为:
- actions:action列表,比如被包含到组内的命名空间、节点等。
在 PushRosNamespace 对象中,使用的参数为:
- namespace:当前组使用的命名空间。
六、添加事件
节点在运行过程中会触发不同的事件,当事件触发时可以为之注册一定的处理逻辑。事件使用相关的 API 为:launch.actions.RegisterEventHandler、launch.event_handlers.OnProcessStart、launch.event_handlers.OnProcessExit。
**需求:**为 turtlesim_node 节点添加事件,事件1:节点启动时调用spawn服务生成新乌龟;事件2:节点关闭时,输出日志信息。
**示例:**新建 py06_event.launch.py 文件,输入如下内容:
python
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
from launch.substitutions import FindExecutable
from launch.event_handlers import OnProcessStart, OnProcessExit
def generate_launch_description():
turtle = Node(package="turtlesim", executable="turtlesim_node")
spawn = ExecuteProcess(
cmd = [
FindExecutable(name = "ros2"), # 不可以有空格
" service call",
" /spawn turtlesim/srv/Spawn",
" \"{x: 8.0, y: 1.0,theta: 1.0, name: 'turtle2'}\""
],
output="both",
shell=True)
start_event = RegisterEventHandler(
event_handler=OnProcessStart(
target_action = turtle,
on_start = spawn
)
)
exit_event = RegisterEventHandler(
event_handler=OnProcessExit(
target_action = turtle,
on_exit = [LogInfo(msg = "turtlesim_node退出!")]
)
)
return LaunchDescription([turtle,start_event,exit_event])
代码解释
start_event = RegisterEventHandler(
event_handler=OnProcessStart(
target_action = turtle,
on_start = spawn
)
)
exit_event = RegisterEventHandler(
event_handler=OnProcessExit(
target_action = turtle,
on_exit = [LogInfo(msg = "turtlesim_node退出!")]
)
)
上述代码为 turtle 节点注册启动事件和退出事件,当 turtle 节点启动后会执行 spwn 节点,当 turtle 节点退出时,会输出日志文本:"turtlesim_node退出!"。
对象 RegisterEventHandler 负责注册事件,其参数为:
- event_handler:注册的事件对象。
OnProcessStart 是启动事件对象,其参数为:
- target_action:被注册事件的目标对象;
- on_start:事件触发时的执行逻辑。
OnProcessExit 是退出事件对象,其参数为:
- target_action:被注册事件的目标对象;
- on_exit:事件触发时的执行逻辑。
LogInfo 是日志输出对象,其参数为:
- msg:被输出的日志信息。