ROS launch 启动文件的使用方法

ROS launch 启动文件的使用方法


参考:古月21讲


launch文件介绍

launch文件:通过XML文件实现多节点的配置和启动(可自启动ROS Master)。
正常一套程序运行时节点是很多的,不可能每次运行都要一个一个手动打开。所以需要launch来启动所有节点和功能。
launch文件内的语法为xml

<launch>\ <machine>\ <node>\ …,这些叫做标签。

name\ address\ type\ …,这些叫做属性。

每一个node都相当是一个rosrun 命令

<launch>
    <arg name="config_path" default = "$(find feature_tracker)/../config/euroc/euroc_config.yaml" />
	  <arg name="vins_path" default = "$(find feature_tracker)/../config/../" />
    
    <node name="feature_tracker" pkg="feature_tracker" type="feature_tracker" output="log">
        <param name="config_file" type="string" value="$(arg config_path)" />
        <param name="vins_folder" type="string" value="$(arg vins_path)" />
    </node>
 
    <node name="vins_estimator" pkg="vins_estimator" type="vins_estimator" output="screen">
       <param name="config_file" type="string" value="$(arg config_path)" />
       <param name="vins_folder" type="string" value="$(arg vins_path)" />
    </node>
 
    <node name="pose_graph" pkg="pose_graph" type="pose_graph" output="screen">
        <param name="config_file" type="string" value="$(arg config_path)" />
        <param name="visualization_shift_x" type="int" value="0" />
        <param name="visualization_shift_y" type="int" value="0" />
        <param name="skip_cnt" type="int" value="0" />
        <param name="skip_dis" type="double" value="0" />
    </node>
 
</launch>

launch 文件语法

<launch>
	<node pkg="turtlesim" name="sim1" type="turtlesim_node"/>
	<node pkg="turtlesim" name="sim2" type="turtlesim_node"/>
</launch>

<launch> launch文件中的根元素采用 标签来定义。

<node> 表示 启动节点
<node pkg="package-name" type="executable-name" name="node-name" />
其中:

  • pkg:节点所在的功能包名称;
  • type: 节点的可执行文件名称;
  • name:节点运行时的名称;
  • output, respawn, required, ns, args

可以结合下面的语句来理解上面的node节点语句

	$ rosrun turtlesim turtlesim_node

turtlesim 是功能包名称,turtlesim_node是可执行文件名称
name 是ros系统中节点运行后节点的名字,name后面跟的字符串 跟之前学过的ros::init(argc,argv,"node_name") 中的node_name 是同一个东西
就像是上面的代码中,sim1 会取代turtle_node中初始化的节点名。

  • 参数设置

<param> /<rosparam>
设置ROS系统运行中的参数,存储在参数服务器中。
<param name="output_frame" value="odom"/>
其中:

  • name:参数名;
  • value:参数值;

param 指令可以设置单个参数,而rosparam 指令可以一次性操作多个文件。
加载参数文件中的多个参数:

<rosparam file="params.yaml" command="load" ns="params"/>

参数存放在.yaml文件中,ns 是namespace的简写。

除了param可以设置ros参数,arg指令也是可以的
但是arg只能操作launch文件内部的局部变量,仅限于launch文件使用

<arg name="arg-name" default="arg-value"/>

其中:

  • name:参数名
  • default: 参数值

arg可以作为launch文件内部的参数,可以作为node初始化时使用的参数
调用:

<param name="foo" value="$(arg arg-name)"/>
<node name="node" pkg="package" type" args="$(arg arg-name)"/>
  • 重映射
    重映射 作用主要是对ROS中某些计算图的资源做重新命名。
<remap from ="/turtlebot/cmd_vel" to="/cmd_vel"/>

比如说控制海龟速度的话题名字为"/turtle1/cmd_vel", 但是如果我们想用其他的机器人,不用turtle1这个前缀,就可以用<remap> 这个标签把他的名字重新换一个,也就是把"/turtle1/cmd_vel"重映射成"/cmd_vel",可以把这个标签理解为对资源重命名的工具,它把ros原来某个命名的资源重新换一个名字,有个问题就是,换完名字之后,原来的名字就没有了,类似于,word里面的查找->全部替换

  • 嵌套
    <include>
    <include>有点像程序里面的#include<头文件.h> 一样,因为launch文件有的时候里面的内容会比较多,我们可以把launch中的文件做一个拆分模块化,每一个文件可以保存一部分内容,然后就可以用<include> 互相去包含,下面的事例就是用include去包含另外一个launch文件:
<include file="$(dirname)/other.launch"/>

其中,dirname是路径文本。

程序实践-1

  • 回到工作空间下面新建一个功能包
	$ ~/catkin_ws/src$ catkin_create_pkg learning_launch

后面不需要任何的依赖。因为launch文件本身是一个系统文件,它是用来链接其他库里面的功能,不需要自己的文件里面有什么可编程的东西。
可以看到,创建完的功能包里面只有CMakeList.txtpackage.xml (这两个文件是功能包必备的)

  • 创建文件夹
	$ cd ~/catkin_ws/src/learning_launch
	$ mkdir launch
	$ cd launch

一般在ros里面存放launch文件的文件夹都命名为launch

  • 创建文件
    直接用touch创建一个.launch 后缀的文件
    用文本编辑器打开
<launch>
    <node pkg="learning_topic" type="person_subscriber" name="talker" output="screen" />
    <node pkg="learning_topic" type="person_publisher" name="listener" output="screen" />
</launch>

加上output 的目的是让两个节点运行成功之后将日志信息打印到终端中,为了输出运行效果,让其在终端中做出显示。

  • 不用修改CMakeList.txt文件
  • 编译
  • 启动launch文件
    回到工作空间根目录,
	$ roslaunch learning_launch simple.launch

roslaunch 指令是用来启动ros里面的launch文件的,后面的语法一样,先跟功能包名字,然后是功能包里面的launch文件名字,带后缀的

程序实践-2

  • 编辑launch文件
<launch>
    <param name="/turtle_number" value="2" />
    <node> pkg="turtlesim" type="turtlesim_node" name="turtlesim_node" >
        <param name="turtle_name1" value="Tom" />
        <param name="turtle_name2" value="Jerry" />
        <rosparam file="$(find learning_launch/config/param.yaml" cammand="load" />
    </node>
    <node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" output="screen" />
</launch>

其中 <param name="/turtle_number" value="2" /> 的意思是:海龟的数量2个,"/turtle_number"是个变量的名字,他的值是2 ,这一句的目的是把这个变量和他的值存到参数服务器中。
接下来是运行turtlesim海龟仿真器节点,他的节点名字叫做turtlesim_node,紧接着创建了两个变量名与值。
接下来用rosparam这个标签加载了一个参数文件,注意$(find 功能包名)/config/file_name.yaml 中的find+功能报名 会输出一个完整的路径。

  • 在learning_launch 文件夹下创建新文件夹config/
  • 新建param.yaml文件,编辑
	A:123
	B:"hello"
	
	group:
	    C:456
	    D:"hello"
  • 编译,其实也不用编译
  • 运行
    运行之后可以发现不必打开多个终端,就可以运行rosmaster,海龟节点,和控制节点了,只需要一个终端就能完成以上工作。
  • 查看参数服务器
	$ rosparam list

在这里插入图片描述
可以根据launch文件的语法和yaml文件的语法体会出参数的从属关系(标签与缩进)。
在这里插入图片描述


在这里插入图片描述

  • 6
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值