【ROS】launch文件详解

2023-05-16

参考:https://www.cnblogs.com/fuzhuoxin/p/12588402.html

在节点少,程序小的情况下可以一个一个节点来启动,测试运行效果;但是当工程规模大,需要的节点多时就显得比较费劲,用.launch文件来启动可以将需要的节点同时启动,不用再一个一个进行。为工程搭建提高了效率,里面还有很多参数灵活使用会带来非常高效的调试。

一、创建launch文件

在上个例子ROS 消息通讯——发布者/订阅者的来龙去脉的基础上创建launch文件去启动节点看效果会如何。
在这里插入图片描述

<launch>
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher1"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber1"/>
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher2"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber2"/>
</launch>

pkg="ros_tutorials_topic"为对应的功能包的名称

type=“topic_publisher” 节点对应的可执行文件名一般为.cpp的文件名字:ros::init(argc,argv,“topic_publisher”); //初始化发布者节点名称对应

name="topic_publisher1"运行时显示的节点名称,也就是用命令rosnode list 所看到的节点列表里的名称这儿定义的名字优先会覆盖可执行程序(如.cpp里面init()赋予的节点名)当两者不一样是以name为准

查看运行效果:

roslaunch ros_tutorials_topic topic.launch --screen //--screen是将通信消息发送到屏幕端

在这里插入图片描述

roslaunch ros_tutorials_topic topic.launch //没有--screen屏幕上不显示通信的消息,但是会正常收发,只是不在终端显示

在这里插入图片描述
查看节点列表:
在这里插入图片描述
查看节点关系图:rqt_graph,从图中看到两个发布者同时向两个订阅者发布消息,原因是我们只改变了节点的名称,而没有改变要使用的消息名字,下面在launch文件里添加一个命名空间标记就可以解决。
在这里插入图片描述

  • respawn=“true”当roslaunch启动完所有该启动的节点之后,会监测每一个节点,保证它们正常的运行状态对于任意节点,当它终止时,roslaunch 会将该节点自动重启
  • required=“true”必要节点这个节点出问题了,整个launch结束注意此属性不可以与respawn="true"一起描述同一个节点

二、在launch文件中添加命名空间标记

修改后的launch文件如下;output="screen“,用于将话题信息打印到屏幕roslaunch ros_tutorials_topic topic.launch后面不用加 --screen也可以实现屏幕显示信息:

<launch>
 <group ns="ns1">
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher" output="screen"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber" output="screen"/>
 </group>
 <group ns="ns2">
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher" output="screen"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber" output="screen"/>
 </group>
</launch>

运行查看:roslaunch ros_tutorials_topic topic.launch
在这里插入图片描述
查看节点关系图:
在这里插入图片描述

  • group标签对node的批量管理,可以同时终止在同一个group中的节点
<group if="1-or-0">
……
……
……
</group>

<group unless="1-or-0">
……
……
……
</group>
  1. 第一种情况,当if属性的值为0的时候将会忽略掉<group> </group>之间的标签。
  2. 第二种恰好相反,当if属性的值为1的时候将会忽略掉<group> </group>之间的标签。

但是我们通常不会直接用10来定义if标签。因为这样不够灵活。通常会搭配$(arg arg_name)来使用。

三、<remap>重映射

在上图中我们看到topic_publisher发布的话题是ros_tutorial_msgtopic_subscriber接收的话题同样是ros_tutorial_msg,它们才能形成通讯:

ros::Publisher ros_tutorial_pub = nh.advertise<ros_tutorials_topic::MsgTutorial>("ros_tutorial_msg", 100); //(在topic_publisher.cpp中)
ros::Subscriber ros_tutorial_sub = nh.subscribe<ros_tutorials_topic::MsgTutorial>("ros_tutorial_msg", 100, msgCallback);
//(在topic_subscriber.cpp中),

只有这两者对应起来,接收和发布的话题相同才能成功通讯。

然而在使用别人给的功能包的时候,自己发送的话题和它接收的可能名称不同,但是内容、格式相同,这时候我们就可以在launch中进行重映射。

ros::Publisher ros_tutorial_pub = nh.advertise<ros_tutorials_topic::MsgTutorial>("ros_tutorial_msg", 100);//(在topic_publisher.cpp中)
ros::Subscriber ros_tutorial_sub = nh.subscribe<ros_tutorials_topic::MsgTutorial>("remap/ros_tutorial_msg", 100, msgCallback);
//(在topic_subscriber.cpp中)

这时候订阅者订阅的话题为"remap/ros_tutorial_msg",发布者发布的话题为“ros_tutorial_msg",这时就不能进行通讯,需要进行重映射。

在没有加重映射之前,启动节点查看话题会看到如下结果:
在这里插入图片描述
在这里插入图片描述
如果把launch文件的话题进行一个重映射,如下(最好将映射放到node标签的前面,这样后面所有使用这个话题的节点都可以更新):

<launch> 
 <remap from="ros_tutorial_msg" to="remap/ros_tutorial_msg"/>
 <group ns="ns1">
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher" output="screen"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber" output="screen"/>
 </group>
 <group ns="ns2">
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher" output="screen"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber" output="screen"/>
 </group>
</launch>
  • <remap from="ros_tutorial_msg" to="remap/ros_tutorial_msg"/>这样写意味着将ros_tutorial_msg变为remap/ros_tutorial_msg,其实是将发布者发布的话题进行修改:
    在这里插入图片描述
    在这里插入图片描述
  • <remap from="remap/ros_tutorial_msg" to="ros_tutorial_msg"/>这样写意味着将remap/ros_tutorial_msg变为ros_tutorial_msg,其实是将订阅者订阅的话题进行修改一般用这种比较多也容易理解意思是,我订阅者想订阅一个话题但是你的跟我想要的不同,那么我就将自己的话题映射成与你发布的一样就可以啦。
    在这里插入图片描述
    在这里插入图片描述
  • 当然也可以将<remap>作为节点的子类包含在节点中(只更新该节点的订阅消息):
<launch>
 <group ns="ns1">
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher" output="screen"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber" output="screen"/>
  <remap from="remap/ros_tutorial_msg" to="ros_tutorial_msg"/>  </node>
 </group>
 <group ns="ns2">
  <node pkg="ros_tutorials_topic" type="topic_publisher" name="topic_publisher" output="screen"/>
  <node pkg="ros_tutorials_topic" type="topic_subscriber" name="topic_subscriber" output="screen"/>
 </group>
</launch>

四、<param>参数的用法

在参数的用法——利用参数创建节点中我们利用命令(rosparam set /calculation_method 2)改变参数“calculation_method“来实现加减乘除运算,那么现在我们用launch参数命令来改变程序中的参数,看看会是什么效果:

sevice.launch文件如下:(顺便启动服务器节点去等待客户端请求)

<launch>

  <param name="calculation_method" type="int" value="3" />
  <node pkg="ros_tutorials_service" type="service_server" name="service_server" output="screen"/>
  
</launch>

同时将这句屏蔽://nh.setParam("calculation_method", PLUS);(在service_server.cpp中),我们从launch文件中给定参数,然后编译运行:
在这里插入图片描述

五、<arg>参数的用法

  • arg虽然也是参数命令,但不储存在参数服务器中,不能提供给节点使用,只能在launch文件中使用,用来在运行中或直接在文件中修改launch文件中被arg定义的变量
  • param则是储存在参数服务器中,可以被节点使用,这是最本质的区别

还以现在这个例子来说,<param name="calculation_method" type="int" value="3" />将节点中参数的值赋值为3是进行乘法运算,但是当要进行除法运算时要求将参数变为4,但是在运行时,在launch中变为4,还得编译重新运行,那么用arg来定义一个变量,用这个变量去改变calculation_method的值就实现在运行过程中的改变。launch文件内容变为如下:

<launch>
  <arg name="updata_value" default="1"/>
  <param name="calculation_method" type="int" value="$(arg updata_value)" />
  <node pkg="ros_tutorials_service" type="service_server" name="service_server" output="screen"/>
</launch> 

在这里插入图片描述
开始默认为1,运行加法运算,那么在运行是我们将其值变为4,执行除法运算:roslaunch ros_tutorials_service service.launch updata_value:=4
在这里插入图片描述
注意 argdefaultvalue的区别

<arg name="arg-name" default="arg-value" />  注意default定义的值在roslaunch时才可以改变
<arg name="arg-name" value="arg-value" />    注意value定义的值在roslaunch时不可以改变

六、<rosparam>加载.yaml的配置文件

在上面我们实现了在launch文件中修改程序的参数问题。但是想象一下如果有成千上万个参数的时候我们该怎么改,也这样一句句定义是不现实的。那就用rosparam加载一个配置文件来实现

在功能包下面建一个config文件夹,放入参数配置文件service.yaml,内容为:calculation_method: 3(执行乘法运算):
在这里插入图片描述
在这里插入图片描述
launch文件内容变为:

<launch>

  <node pkg="ros_tutorials_service" type="service_server" name="service_server" output="screen"/>

  <rosparam command="load" file="$(find ros_tutorials_service)/config/service.yaml"/>
  
</launch>

编译执行效果为:
在这里插入图片描述

七、<include>的用法

<include>用于包含其它launch文件,被包含的launch文件将会被一同启动

<include file="$(find pkg_name)/launch/demo.launch"/>
  • 如果要给<include >包含进来的launch文件中的元素赋值,由于arg是局部的,只能改变本launch文件的参数值,因此需要建立一个包含在<include >标签之间的arg,格式如下:
<include file="path-to-launch-file">
<arg name="arg-name" value="arg-value"/>
. . .
</include>
  • 如果改变<include >包含进来的launch文件中元素用的arg名称与本launch文件一样的话,需写为如下格式:位置还放在上面<include >标签之间:
<arg name="arg-name" value="$(arg arg-name)" />
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【ROS】launch文件详解 的相关文章

随机推荐

  • Matplotlib 可视化必备神书,附pdf下载

    出品 xff1a Python数据之道 大家好 xff0c 我是阳哥 大家知道 xff0c 在利用Python进行数据可视化过程中 xff0c 基本上是很难绕开 Matplotlib 的 xff0c 因为 不少其他的可视化库多多少少是建立在
  • ubuntu学习笔记02

    1 sudo sh 与sudo bash sh区别 以超级用户身份运行 34 sh 34 xff0c sh实用程序是一个命令语言解释器 以超级用户身份运行 34 bash 34 xff0c Bash是shell或命令语言解释器 xff0c
  • 史上最浅显易懂的Git教程!

    从零起步的Git教程 xff0c 让你无痛苦上手世界上最流行的分布式版本控制系统Git xff01 既然号称史上最浅显易懂的Git教程 xff0c 那这个教程有什么让你怦然心动的特点呢 xff1f 首先 xff0c 本教程绝对面向初学者 x
  • 数据库死锁原因及解决办法

    死锁 xff08 Deadlock xff09 所谓死锁 xff1a 是指两个或两个以上的进程在执行过程中 xff0c 因争夺资源而造成的一种互相等待的现象 xff0c 若无外力作用 xff0c 它们都将无法推进下去 此时称系统处于死锁状态
  • spring和springmvc父子容器的关系

    大家都知道 xff0c 在spring的配置中要分开配置service层的注解扫描 xff0c 以及springmvc变现层的注解扫描 xff0c 如下 xff1a lt 扫描加载Service实现类 gt lt context compo
  • pageHelper分页插件实现原理及使用方法

    插件官方网站 xff1a https github com pagehelper Mybatis PageHelper tree master src main java com github pagehelper 实现原理 xff1a 使
  • 虚拟机Linux系统安装nginx服务器并启动的步骤

    工作前的准备 xff1a 1 装有Linux的虚拟机 2 nginx安装包 xff0c 注意是gz结尾的压缩文件 具体步骤1 xff1a 1 nginx安装环境 nginx是 C 语言开发 xff0c 建议在 linux 上运行 xff0c
  • 什么是反射机制,有什么作用

    1 反射机制定义 反射的概念是由Smith在1982年首次提出的 xff0c 主要是指程序可以访问 检测和修改其本身状态或行为的一种能力 在Java环境中 xff0c 反射机制允许程序在执行时获取某个类自身的定义信息 xff0c 例如熟悉和
  • 写给2016

    你不能期待着遇见怎样的自己 xff0c 但你可以选择成为怎样的自己 转眼16年就迎来了它的落幕 xff0c 不论怎样华丽的开场 xff0c 总有归于平静散场的结束 xff0c 不早不晚 xff0c 于清晨到傍晚 xff0c 于四季的轮回 x
  • 模块化建立项目流程(Maven聚合模块)

    先说项目使用Maven的好处 1 项目构建 Maven定义了软件开发的整套流程体系 xff0c 并进行了封装 xff0c 开发人员只需要指定项目的构建流程 xff0c 无需针对每个流程编写自己的构建脚本 2 依赖管理 除了项目构建 xff0
  • 如何在linux下判断web服务是否开启?

    对于web服务的开启的判断有以下几种常用方法 xff1a 1 端口查看 xff1a 本地 xff1a ss xff0c netstat xff0c lsof 1 2 3 4 5 6 7 8 9 10
  • git基本命令

    最近再写一些项目上传到github xff0c 所以要用到git命令 本地需要先安装git客户端 xff0c 然后指定一个git地址为本地仓库 然后右键git bash here打开git命令界面 首先服务端需要创建一个项目以便clone到
  • jps查看Java线程,jstack查看具体线程堆状态

    想要使用jps需要配置环境变量 xff0c 在classpath后在加一个指定Java bin目录 具体命令如下 t2挂起了 xff0c 堆里面显示t2为RUNNABLE xff0c suspend xff0c resume废弃使用 IBM
  • heap_1.c详解--------FreeRTOS内存管理

    heap 1源码分析 include lt stdlib h gt Defining MPU WRAPPERS INCLUDED FROM API FILE prevents task h from redefining all the A
  • 记录一个类加载变量引发的问题

    类加载变量导致的问题 类加载变量导致的问题 类加载变量导致的问题 因为项目需要 xff0c 银行要求使用weblogic部署并且启动所有项目 xff0c 不允许项目单独开服务启动一般都有这样的要求 xff0c 我所在的项目组有两个单独mai
  • Ubuntu16.04安装intel RealSense D435i驱动并在ROS中使用

    参考 xff1a https blog csdn net qq 43265072 article details 106437287https blog csdn net zhangfenger article details 849980
  • 【ROS】的单线程Spinning和多线程Spinning

    参考 xff1a https www cnblogs com feixiao5566 p 5288206 htmlhttps www freesion com article 9499126134 https blog csdn net y
  • Intel RealSense D435i 深度相机介绍

    参考 xff1a https www sohu com a 340984033 715754https www chiphell com thread 1945054 1 1 htmlhttps blog csdn net cherry y
  • 【ROS】rosnode信息命令

    参考 xff1a https www cnblogs com kay2018 p 10314741 html 一 概述 ROS信息命令用于识别话题 服务 节点和参数等信息 尤其是rostopic rosservice rosnode和ros
  • 【ROS】launch文件详解

    参考 xff1a https www cnblogs com fuzhuoxin p 12588402 html 在节点少 xff0c 程序小的情况下可以一个一个节点来启动 xff0c 测试运行效果 xff1b 但是当工程规模大 xff0c