launch文件详解

概述

roslaunch指令提供了一次开启多个ros节点的方法。

roslaunch命令行指令

一般用法:

# 在对应包的根文件夹下面:无需指定包名
roscd roslaunch
roslaunch example.launch

# 在任意目录:需指定包名
roslaunch roslaunch example.launch

# 指定路径
roslaunch pr2_robot/pr2_bringup/pr2.launch

传参:

roslaunch my_file.launch arg:=value

注:roscore会在roslaunch的时候自动运行,不需要单独启动。

.launch/xml文件编写

读取顺序:从上到下顺序读取,后定义的参数会覆盖前面定义的相同名称的参数。

变量代换:

$(env ENVIRONMENT_VARIABLE)
# 读取某个环境变量。若当前没有该环境变量,则会报错挂掉。注意该变量为只读。

$(optenv ENVIRONMENT_VARIABLE)
$(optenv ENVIRONMENT_VARIABLE default_value)
# 读取某个环境变量。若当前没有该环境变量,且定义了默认值,则使用默认值。若未定义默认值,则使用空字符串。

<!-- wp:code -->
<pre class="wp-block-code"><code class="">$(env ENVIRONMENT_VARIABLE)
# 读取某个环境变量。若当前没有该环境变量,则会报错挂掉。注意该变量为只读。

$(optenv ENVIRONMENT_VARIABLE)
$(optenv ENVIRONMENT_VARIABLE default_value)
# 读取某个环境变量。若当前没有该环境变量,且定义了默认值,则使用默认值。若未定义默认值,则使用空字符串。

$(find pkg)
# 读取某个软件包的根目录,进而指定包里面的文件路径。注意该包应该在当前PATH中,需要source。

$(anon name)
生成一个匿名id。注意该id是基于name生成的,故当name相同时,生成的id也是相同的。

$(arg foo)
定义一个叫foo的变量,可从外部传入。

$(eval <expression>)
用于表达任意python表达式。注:一个字符串中eval与其他变量不可共存,需要作为eval语句的函数出现。

$(dirname)
读取当前文件夹路径。

条件语句

<group if="$(arg foo)">
  <!-- stuff that will only be evaluated if foo is true -->
</group>

<param name="foo" value="bar" unless="$(arg foo)" />  <!-- This param won't be set when "unless" condition is met -->

其他标签

node:

<node name="listener1" pkg="rospy_tutorials" type="listener.py" args="--test" respawn="true" />

# pkg="mypackage":包名
# type="nodetype":节点种类,即包中的可执行文件
# name="nodename":节点名,任取。如果节点需要在指定命名空间下运行,需指定ns,而不能在name前面加。
# args="arg1 arg2 arg3":传入参数
# machine="machine-name":在指定机器运行节点
# respawn="true":节点挂了之后会重新启动
# ns="foo":在指定命名空间下启动节点

param:

# 定义一个double类型的publish_frequency值为10.0
<param name="publish_frequency" type="double" value="10.0" />

rosparam:

# 导入一个yaml文件中的参数
<rosparam command="load" file="$(find rosparam)/example.yaml" />

# 删除一个参数
<rosparam command="delete" param="my/param" />

# 定义一个参数
<rosparam param="a_list">[1, 2, 3, 4]</rosparam>

# 定义两个参数
<rosparam>
  a: 1
  b: 2
</rosparam>

launch文件示例

<launch>
  <!-- local machine already has a definition by default.
       This tag overrides the default definition with
       specific ROS_ROOT and ROS_PACKAGE_PATH values -->
  <machine name="local_alt" address="localhost" default="true" ros-root="/u/user/ros/ros/" ros-package-path="/u/user/ros/ros-pkg" />
  <!-- a basic listener node -->
  <node name="listener-1" pkg="rospy_tutorials" type="listener" />
  <!-- pass args to the listener node -->
  <node name="listener-2" pkg="rospy_tutorials" type="listener" args="-foo arg2" />
  <!-- a respawn-able listener node -->
  <node name="listener-3" pkg="rospy_tutorials" type="listener" respawn="true" />
  <!-- start listener node in the 'wg1' namespace -->
  <node ns="wg1" name="listener-wg1" pkg="rospy_tutorials" type="listener" respawn="true" />
  <!-- start a group of nodes in the 'wg2' namespace -->
  <group ns="wg2">
    <!-- remap applies to all future statements in this scope. -->
    <remap from="chatter" to="hello"/>
    <node pkg="rospy_tutorials" type="listener" name="listener" args="--test" respawn="true" />
    <node pkg="rospy_tutorials" type="talker" name="talker">
      <!-- set a private parameter for the node -->
      <param name="talker_1_param" value="a value" />
      <!-- nodes can have their own remap args -->
      <remap from="chatter" to="hello-1"/>
      <!-- you can set environment variables for a node -->
      <env name="ENV_EXAMPLE" value="some value" />
    </node>
  </group>
</launch>
<launch>
  <param name="somestring1" value="bar" />
  <!-- force to string instead of integer -->
  <param name="somestring2" value="10" type="str" />

  <param name="someinteger1" value="1" type="int" />
  <param name="someinteger2" value="2" />

  <param name="somefloat1" value="3.14159" type="double" />
  <param name="somefloat2" value="3.0" />

  <!-- you can set parameters in child namespaces -->
  <param name="wg/childparam" value="a child namespace parameter" />

  <!-- upload the contents of a file to the server -->
  <param name="configfile" textfile="$(find roslaunch)/example.xml" />
  <!-- upload the contents of a file as base64 binary to the server -->
  <param name="binaryfile" binfile="$(find roslaunch)/example.xml" />

</launch>

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据