launch文件基础

来源:互联网 发布:美国最新经济数据今晚 编辑:程序博客网 时间:2024/06/01 10:24

在ROS应用中,每个节点通常有许多参数需要设置,为了方便高效操作多个节点,可以编写launch文件,然后用roslaunch命令运行;

启动launch文件命令格式:
•roslaunch: roslaunch [options] [package] [arg_name:=value…]
roslaunch [options] […] [arg_name:=value…]
launch文件的一般格式,参数:

<launch>  <node .../>  <rosparam ..../>  <param .../>  <include .../>  <env .../>  <remap .../>  <arg.../></launch>

•参数说明

•<node >要启动的node参数  pkg=''mypackage''  type=''nodetype''  name=''nodename''  arg=''arg1 ....''(可选)  respawn=''ture''(可选)如果节点停止,自动重启节点  ns=''foo''(可选)在foo命名空间启动节点  output=''log|screen''(可选)
<rosparam>操作yaml文件参数                //一次可操作多个参数;  command=''load|dump|delete''(默认load)  file=''$(find pkg-name)/path/foo.yaml''(load或dump命令)yaml文件的名字  param=''param-name''参数名
•<param>定义一个设置在参数服务器的参数,它可以添加到<node>中    //一次只能指定一个标准类型的参数,如 int等  name=''namespace/name''  value=''value''(可选)如果省略这个参数,则应指定一个文件(binfile/textfile)或命令  type=''str|int|double|boot''(可选)指定参数的类型  textfile=''$(find pkg-name)/path/file''(可选)   binfile=''$(find pkg-name)/path/file''()  command=''(find pkg-name)/exe '$(find pkg-name)/arg.txt' ''(可选)exe是可执行文件(cpppy),arg.txt是参  数文件
•<include>在当前launch文件中调用另一个launch文件  file=''$(find pkg-name)/path/launch-file.launch'' 
<env>设置节点的环境变量  name=''environment-variable-name''  value=''environment-variable-value'' 
<remap>将一个参数名映射为另一个名字  from=''original-name''  to=''new-name''
•<arg>定义一个局部参数,该参数只能在一个launch文件中使用  <arg name=''foo''/>声明一个参数foo,后面需要给它赋值  <arg name=''foo'' default=''1''/>声明一个参数foo,如不赋值取默认值  <arg name=''foo'' value=''bar''/>声明一常量foo,它的值不能修改

示例:

1. 新建基本launch文件

创建一个包:

& cd beginer_ws/src& catkin_create_pkg turtlesim_launch roscpp rospy & cd .. & catkin_make

launch :

& cd src& gedit turtlesim_launch.launch

将该程序写入新建的launch文件中。

<launch><node pkg="turtle" type="turtle_node" name="turtle_node" respawn="true" output="screen" /><node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" respawn="false" output="screen" /></launch>

启动launch文件:

roslaunch turtlesim_launch turtlesim_launch.launch

即可通过键盘 的上下左右箭头键盘控制小海龟运动:

这里写图片描述

2. rosparam操作yaml文件参数, 使用示例

现在 文件夹中新建一个 color.yaml文件,并在其中写入下列语句

background_r: 0background_g: 0background_b: 0

再修改launch文件在其后添加

<rosparam command="dump" file="$(find turtlesim_launch)/color.yaml" />

添加后如下:

<launch><node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node" respawn="true" output="screen" /><node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" respawn="false" output="screen" /><rosparam command="dump" file="$(find turtlesim_launch)/color.yaml" /></launch>

再次启动launch文件,可以发现,程序已经执行了这条语句

这里写图片描述

3. param 参数使用

在launch中中添加如下语句:
指定参数为 turtle

<param name="turtle" value="1" />

运行后:
可看到,在参数中出现、turtle:1
这里写图片描述

也可以在node中写,如放在turtlesim_node 这个node里,调换两个node的语句顺序, 去掉turtlesim_node 语句中 >前的/, 下面添加

<param name="turtle" value="1" />

将launch改写如下:

<launch><node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" respawn="false" output="screen" /><node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node" respawn="true" output="screen" ><param name="turtle" value="1" /><rosparam command="dump" file="$(find turtlesim_launch)/color.yaml" /></node></launch>运行后,可以看到参数中,已是/turtle_teleop_key/turtle:1

这里写图片描述

4. include使用示例

格式为:
•在当前launch文件中调用另一个launch文件
file=”$(find pkg-name)/path/launch-file.launch”

将turtlesim_launch中其中一个节点,拿出来单独建立一个teleop.launch文件,如下:

<launch><node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" respawn="false" output="screen" /></launch>

将turtlesim_launch 改为:
添加include

<launch><include file="$(find turtlesim_launch)/teleop.launch" /><node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node" respawn="true" output="screen" ><param name="turtle" value="1" /><rosparam command="dump" file="$(find turtlesim_launch)/color.yaml" /></node></launch>

重新启动即可;

5.arg 定义一个局部参数, 使用示例:

格式:

<arg>定义一个局部参数,该参数只能在一个launch文件中使用  <arg name=''foo''/>声明一个参数foo,后面需要给它赋值  <arg name=''foo'' default=''1''/>声明一个参数foo,如不赋值取默认值  <arg name=''foo'' value=''bar''/>声明一常量foo,它的值不能修改

定义在节点外:
如:

<launch><include file="$(find turtlesim_launch)/teleop.launch" /><node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node" respawn="true" output="screen" ><param name="turtle" value="1" /><rosparam command="dump" file="$(find turtlesim_launch)/color.yaml" /></node><arg name="foo"  /></launch>

如不赋值,,则需要在启动launch时赋值:
如:

 roslaunch turtlesim_launch turtlesim_launch.launch foo:=2

或者,指定常数:

<arg name="foo"  value="2"/>

则启动时 不需要额外赋值。


注: 其中参考了某ROS课程中的讲解资料。

原创粉丝点击