ROS 2边学边练(29)-- 使用替换机制

2024-04-20 03:12

本文主要是介绍ROS 2边学边练(29)-- 使用替换机制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

        启动文件用于启动节点、服务和执行流程。这组操作可能有影响其行为的参数。替换机制可以在参数中使用,以便在描述可重复使用的启动文件时提供更大的灵活性。替换是仅在执行启动描述期间评估的变量,可用于获取特定信息,如启动配置、环境变量或评估任意Python表达式。

        简而言之,ROS 2中的替换机制可以在启动描述运行过程中动态修改和管理修改某些参数而能改变运行状态和效果。

动动手

使用替换机制

创建功能包

        在launch_ws/src下创建一份功能包launch_tutorial(C++):

$ros2 pkg create --build-type ament_cmake --license Apache-2.0 launch_tutorial

        创建luanch文件夹,用来放置launch文件:

$mkdir launch_tutorial/launch

        另外CMakeLists.txt中加上下面的内容:

install(DIRECTORYlaunchDESTINATION share/${PROJECT_NAME}/
)
编写父类launch文件

        上面步骤都准备好后,我们开始此篇的主题重点部分。在launch文件夹下创建一份可以调用且能传参给其他launch文件的父类launch文件example_main_launch.py,格式可以python可以yaml,我们只演示下python版本,如下:

from launch_ros.substitutions import FindPackageSharefrom launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import PathJoinSubstitution, TextSubstitutiondef generate_launch_description():colors = {'background_r': '200'}return LaunchDescription([IncludeLaunchDescription(PythonLaunchDescriptionSource([PathJoinSubstitution([FindPackageShare('launch_tutorial'),'launch','example_substitutions_launch.py'])]),launch_arguments={'turtlesim_ns': 'turtlesim2','use_provided_red': 'True','new_background_r': TextSubstitution(text=str(colors['background_r']))}.items())])

简单说明一下:

PathJoinSubstitution([FindPackageShare('launch_tutorial'),'launch','example_substitutions_launch.py'
])

FindPackageShare用来查找launch_tutorial包的路径,PathJoinSubstitution用来连接到包路径下面example_substitutions_launch.py的路径。

launch_arguments={'turtlesim_ns': 'turtlesim2','use_provided_red': 'True','new_background_r': TextSubstitution(text=str(colors['background_r']))
}.items()

launch_arguments字典(含turtlesim_ns、use_provided_red参数)会传给IncludeLaunchDescription动作使用,另外TextSubstitution用于使用colors字典中的background_r键的值来定义new_background_r参数。

编写有替换机制功能的launch文件

        在功能包的根路径launch下创建一份example_substitutions_launch.py文件,内容如下:

from launch_ros.actions import Nodefrom launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess, TimerAction
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration, PythonExpressiondef generate_launch_description():turtlesim_ns = LaunchConfiguration('turtlesim_ns')use_provided_red = LaunchConfiguration('use_provided_red')new_background_r = LaunchConfiguration('new_background_r')turtlesim_ns_launch_arg = DeclareLaunchArgument('turtlesim_ns',default_value='turtlesim1')use_provided_red_launch_arg = DeclareLaunchArgument('use_provided_red',default_value='False')new_background_r_launch_arg = DeclareLaunchArgument('new_background_r',default_value='200')turtlesim_node = Node(package='turtlesim',namespace=turtlesim_ns,executable='turtlesim_node',name='sim')spawn_turtle = ExecuteProcess(cmd=[['ros2 service call ',turtlesim_ns,'/spawn ','turtlesim/srv/Spawn ','"{x: 2, y: 2, theta: 0.2}"']],shell=True)change_background_r = ExecuteProcess(cmd=[['ros2 param set ',turtlesim_ns,'/sim background_r ','120']],shell=True)change_background_r_conditioned = ExecuteProcess(condition=IfCondition(PythonExpression([new_background_r,' == 200',' and ',use_provided_red])),cmd=[['ros2 param set ',turtlesim_ns,'/sim background_r ',new_background_r]],shell=True)return LaunchDescription([turtlesim_ns_launch_arg,use_provided_red_launch_arg,new_background_r_launch_arg,turtlesim_node,spawn_turtle,change_background_r,TimerAction(period=2.0,actions=[change_background_r_conditioned],)])

同样简单说明一下:

turtlesim_ns = LaunchConfiguration('turtlesim_ns')
use_provided_red = LaunchConfiguration('use_provided_red')
new_background_r = LaunchConfiguration('new_background_r')turtlesim_ns_launch_arg = DeclareLaunchArgument('turtlesim_ns',default_value='turtlesim1'
)
use_provided_red_launch_arg = DeclareLaunchArgument('use_provided_red',default_value='False'
)
new_background_r_launch_arg = DeclareLaunchArgument('new_background_r',default_value='200'
)

        定义了turtlesim_ns、use_provided_red和new_background_r这三个启动配置。它们被用来存储上述变量中的启动参数值,并传递给所需的动作。这些LaunchConfiguration替换允许我们在启动描述的任何部分获取启动参数的值。

   DeclareLaunchArgument 用于定义启动参数,这些参数可以从上述启动文件或控制台中传递。

turtlesim_node = Node(package='turtlesim',namespace=turtlesim_ns,executable='turtlesim_node',name='sim'
)

        定义了名为turtlesim_node的节点,并将命名空间设置为turtlesim_ns的LaunchConfiguration替换。

spawn_turtle = ExecuteProcess(cmd=[['ros2 service call ',turtlesim_ns,'/spawn ','turtlesim/srv/Spawn ','"{x: 2, y: 2, theta: 0.2}"']],shell=True
)

        定义了一个名为spawn_turtle的ExecuteProcess动作,并带有相应的cmd参数。这个命令调用turtlesim节点的spawn服务。

        此外,LaunchConfiguration替换用于获取turtlesim_ns启动参数的值,以构建命令字符串。

change_background_r = ExecuteProcess(cmd=[['ros2 param set ',turtlesim_ns,'/sim background_r ','120']],shell=True
)
change_background_r_conditioned = ExecuteProcess(condition=IfCondition(PythonExpression([new_background_r,' == 200',' and ',use_provided_red])),cmd=[['ros2 param set ',turtlesim_ns,'/sim background_r ',new_background_r]],shell=True
)

        对于改变turtlesim背景红色参数颜色的change_background_r和change_background_r_conditioned动作,采用了相同的方法。不同的是,只有当提供的new_background_r参数等于200,且use_provided_red启动参数设置为True时,change_background_r_conditioned动作才会执行。在IfCondition内部的评估是通过PythonExpression替换来完成的。

        这些内容是不是有够复杂的。

构建功能包

        进入工作空间的根路径下,构建完成后source一下环境(source install/setup.bash):

$colcon build

跑跑例子

        下面我们就可以运行下launch文件了,先跑一下example_main_launch.py:

$ros2 launch launch_tutorial example_main_launch.py

这个launch文件将会做如下操作:

  1. 启动一个海龟仿真节点(蓝色背景);
  2. 孵化第二只海龟;
  3. 改变背景颜色为紫色;
  4. 2秒后改变颜色为粉色(如果提供的参数background_r值为200且参数use_provided值为True);

 

修改launch文件参数

        如果要修改启动参数,除了可以直接在example_main_launch.py文件里修改launch_arguments字典内容外,还可以使用首选参数启动example_substitutions_launch.py。

要查看可能传递给启动文件的参数有哪些,运行以下命令即可:

$ros2 launch launch_tutorial example_substitutions_launch.py --show-args

它会返回可能传递给launch文件的参数和其对应的默认值:

我们试试利用example_substitutions_launch.py来修改参数:

$ros2 launch launch_tutorial example_substitutions_launch.py turtlesim_ns:='turtlesim3' use_provided_red:='True' new_background_r:=200

可以看到通过命令行里面的参数(命名空间等参数值)已经修改成功。

本篇完。 

这篇关于ROS 2边学边练(29)-- 使用替换机制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/919156

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互