网站不在首页显示出来吗,WordPress添加前台注册功能,在手机上开发app,论坛网站建设用工具软件☆重点 ROS1和ROS2其中一个很大区别之一就是launch的编写方式。在ROS1中采用xml格式编写launch#xff0c;而ROS2保留了XML 格式launch#xff0c;还另外引入了Python和YAML 编写方式。选择哪种编写取决于每位开发人员的爱好#xff0c;但是ROS2官方推荐使用Python方式编写… ☆重点 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修改为为如下内容 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 文件输入如下内容
from launch import LaunchDescription
from launch_ros.actions import Node
import os
from ament_index_python.packages import get_package_share_directorydef generate_launch_description():turtle1 Node(packageturtlesim, executableturtlesim_node, namespacens_1,namet1, exec_nameturtle_label, # 表示流程的标签respawnTrue)turtle2 Node(packageturtlesim, executableturtlesim_node, namet2,# 参数设置方式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(packageturtlesim, executableturtlesim_node, namet3, remappings[(/turtle1/cmd_vel,/cmd_vel)] #话题重映射)rviz Node(packagerviz2,executablerviz2,# 节点启动时传参arguments[-d, os.path.join(get_package_share_directory(cpp01_launch),config,my.rviz)])turtle4 Node(packageturtlesim, executableturtlesim_node,# 节点启动时传参相当于 arguments 传参时添加前缀 --ros-args ros_arguments[--remap, __ns:/t4_ns, --remap, __node:t4])return LaunchDescription([turtle1, turtle2, turtle3, rviz, turtle4])代码解释
1.1 Node使用语法1
turtle1 Node(packageturtlesim, executableturtlesim_node, namespacegroup_1, namet1, exec_nameturtle_label, # 表示流程的标签respawnTrue)上述代码会创建一个 turtlesim_node 节点设置了若干节点属性并且节点关闭后会自动重启。
package功能包executable可执行文件namespace命名空间name节点名称exe_name流程标签respawn设置为True时关闭节点后可以自动重启。
1.2 Node使用语法2
turtle2 Node(packageturtlesim, executableturtlesim_node, namet2,# 参数设置方式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 文件并输入如下内容
/t2:ros__parameters:background_b: 0background_g: 0background_r: 50qos_overrides:/parameter_events:publisher:depth: 1000durability: volatilehistory: keep_lastreliability: reliableuse_sim_time: false注意还需要在 CMakeLists.txt 中安装 config
install(DIRECTORY launchconfigDESTINATION share/${PROJECT_NAME})1.3 Node使用语法3
turtle3 Node(packageturtlesim, executableturtlesim_node, namet3, remappings[(/turtle1/cmd_vel,/cmd_vel)] #话题重映射)上述代码会创建一个 turtlesim_node 节点并将话题名称从 /turtle1/cmd_vel 重映射到 /cmd_vel。
remappings话题重映射。
1.4 Node使用语法4
rviz Node(packagerviz2,executablerviz2,# 节点启动时传参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
turtle4 Node(packageturtlesim, executableturtlesim_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 文件输入如下内容
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import ExecuteProcess
from launch.substitutions import FindExecutabledef generate_launch_description():turtle Node(packageturtlesim, executableturtlesim_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}\],outputboth,shellTrue)return LaunchDescription([turtle,spawn])代码解释
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}\],outputboth,shellTrue)上述代码用于执行 cmd 参数中的命令该命令会在 turtlesim_node 中生成一只新的小乌龟。
cmd被执行的命令output设置为 both 时日志会被输出到日志文件和终端默认为 log日志只输出到日志文件。shell如果为 True则以 shell 的方式执行命令。
三、参数设置
参数设置主要涉及到参数的声明与调用两部分其中声明被封装为 launch.actions.DeclareLaunchArgument调用则被封装为 launch.substitutions import LaunchConfiguration。
需求启动turtlesim_node节点时可以动态设置背景色。
示例新建 py03_args.launch.py 文件输入如下内容
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 LaunchConfigurationdef generate_launch_description():decl_bg_r DeclareLaunchArgument(namebackground_r,default_value255)decl_bg_g DeclareLaunchArgument(namebackground_g,default_value255)decl_bg_b DeclareLaunchArgument(namebackground_b,default_value255)turtle Node(packageturtlesim, executableturtlesim_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])代码解释
decl_bg_r DeclareLaunchArgument(namebackground_r,default_value255)
decl_bg_g DeclareLaunchArgument(namebackground_g,default_value255)
decl_bg_b DeclareLaunchArgument(namebackground_b,default_value255)上述代码会使用DeclareLaunchArgument对象声明三个参数且每个参数都有参数名称以及默认值。
name参数名称default_value默认值。
parameters[{background_r: LaunchConfiguration(variable_namebackground_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 文件输入如下内容
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSourceimport os
from ament_index_python import get_package_share_directorydef generate_launch_description():include_launch IncludeLaunchDescription(launch_description_source PythonLaunchDescriptionSource(launch_file_pathos.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_pathos.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 文件输入如下内容
from launch import LaunchDescription
from launch_ros.actions import Node
from launch_ros.actions import PushRosNamespace
from launch.actions import GroupActiondef generate_launch_description():turtle1 Node(packageturtlesim,executableturtlesim_node,namet1)turtle2 Node(packageturtlesim,executableturtlesim_node,namet2)turtle3 Node(packageturtlesim,executableturtlesim_node,namet3)g1 GroupAction(actions[PushRosNamespace(namespaceg1),turtle1, turtle2])g2 GroupAction(actions[PushRosNamespace(namespaceg2),turtle3])return LaunchDescription([g1,g2])代码解释
g1 GroupAction(actions[PushRosNamespace(namespaceg1),turtle1, turtle2])
g2 GroupAction(actions[PushRosNamespace(namespaceg2),turtle3])上述代码将创建两个组两个组使用了不同的命名空间每个组下包含了不同的节点。
在 GroupAction 对象中使用的参数为
actionsaction列表比如被包含到组内的命名空间、节点等。
在 PushRosNamespace 对象中使用的参数为
namespace当前组使用的命名空间。 六、添加事件
节点在运行过程中会触发不同的事件当事件触发时可以为之注册一定的处理逻辑。事件使用相关的 API 为launch.actions.RegisterEventHandler、launch.event_handlers.OnProcessStart、launch.event_handlers.OnProcessExit。
需求为 turtlesim_node 节点添加事件事件1节点启动时调用spawn服务生成新乌龟事件2节点关闭时输出日志信息。
示例新建 py06_event.launch.py 文件输入如下内容
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(packageturtlesim, executableturtlesim_node)spawn ExecuteProcess(cmd [FindExecutable(name ros2), # 不可以有空格 service call, /spawn turtlesim/srv/Spawn, \{x: 8.0, y: 1.0,theta: 1.0, name: turtle2}\],outputboth,shellTrue)start_event RegisterEventHandler(event_handlerOnProcessStart(target_action turtle,on_start spawn))exit_event RegisterEventHandler(event_handlerOnProcessExit(target_action turtle,on_exit [LogInfo(msg turtlesim_node退出!)]))return LaunchDescription([turtle,start_event,exit_event])代码解释
start_event RegisterEventHandler(event_handlerOnProcessStart(target_action turtle,on_start spawn))
exit_event RegisterEventHandler(event_handlerOnProcessExit(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被输出的日志信息。