ROS1的launch文件

July 02, 2025 作者: funnywii 分类: 编程操作系统 浏览: 18 评论: 0

之前一直是 rosrun方式启动ros节点的, 一是因为懒, 二是因为我只负责一个节点, 用不到launch方式.

这次遇到了不能保存ROS日志的bug, 换成launch方式启动就能成功保存日志了...

Launch文件

ROS中 .launch 文件可以同时启动多个 node, 包括 roscoremaster节点.

基本格式是XML.

用launch方式启动

roslaunch pkg_name xxxxx.launch

launch文件的创建

launch文件应创建在功能包launch文件夹下, 后缀应为 *.launch.

下面是官方demo中给出的最简launch示例, 其中的 name 为节点名称, pkg 为功能包名称, type 为可执行文件的名称.

<launch>
  <node name="node_name" pkg="ros_pkg" type="ros_exe" />
</launch>

注意Attributes是 name而不是 node name, node是一个tag

常用tag

tag就是一个launch文件中的一个根元素, 其中可以包含很多元素.

node

node指定希望启动的ROS节点, 包含很多Attributes:

  • pkg: 功能包
  • type: 可执行文件名称
  • name: node的名称, 也就是 ros::init()中设置的信息, 如果在这里再次设置name, init()方法中的节点名会被覆盖.
  • args: 向节点传递参数.
  • respawn: 如果节点崩了, 重启节点.
  • machine: 在指定设备上启动ROS节点
<node name="bar1" pkg="foo_pkg" type="bar" args="$(find baz_pkg)/resources/map.pgm" />
<node name="listener1" pkg="rospy_tutorials" type="listener.py" args="--test" respawn="true" />

第一行示例的 args 使用 $ 搜索 ROS_PACKAGE_PATH环境变量找到 baz_pkg包所在的目录, 通过 $(find 包名)可以查找包的路径; 通过 $(env 环境变量名)可以获取环境变量的值; 通过 $(arg 参数名)引用定义的参数.

第二行示例的 args可以用于传递命令行参数.

还有一些其他的Attributes 可以参见: roslaunch/XML/node - ROS Wiki

env

env标签可以为启动的Node设置环境变量.

  • name: 环境变量名
  • value: 环境变量值

include

可以导入其他 .launch文件

  • file: 指定导入的launch文件, <include file="$(find pkg-name)/path/filename.launch"/>

arg

arg标签可以声明可复用的参数.

  • name: 参数名
  • default: 参数默认值, 不能和value共存
  • value: 参数值, 不能和default共存
  • doc: 可选参数, arg的描述
$ roslaunch my_file.launch hoge:=my_value   // 当前包.launch文件中的某个arg的参数
$ roslaunch %YOUR_ROS_PKG% my_file.launch hoge:=my_value  // 某个ros pkg的某个.launch中的arg的参数

// 传递参数给被包含的.launch文件
<include file="included.launch">
  <!-- all vars that included.launch requires must be set -->
  <arg name="hoge" value="fuga" />
</include>

// 在当前.launch文件中声明并使用参数
<launch>
  <!-- declare arg to be passed in -->
  <arg name="hoge" /> 

  <!-- read value of arg -->
  <param name="param" value="$(arg hoge)"/>
</launch>

rosparam

把参数写入 .yaml文件中, 然后一次性将多个参数加载到Parameter Server中.

  • command: 有 load(加载), dump(转储), delete(删除) 三种命令, 默认为load.
  • file: rosparam文件名称. file="$(find pkg-name)/path/foo.yaml"
  • param: 参数名.
<rosparam command="load" file="$(find rosparam)/example.yaml" />
<rosparam command="delete" param="my/param" />

param

param标签可以在Parameter Server上设置一些参数, 如果放在node标签下, 被视为该node的私有参数.

  • name: 参数名称
  • value(可选): 参数值
  • type(可选): 参数类型
  • textfile: 读取文件内容, 并作为string存储 textfile="$(find pkg-name)/path/file.txt"
  • binfile: 读取文件并存储为base64编码的XML-RPC二进制对象 binfile="$(find pkg-name)/path/file"
  • command: 指令输出被保存为字符串 command="$(find pkg-name)/exe '$(find pkg-name)/arg.txt'"
<param name="publish_frequency" type="double" value="10.0" />

其他Attributes见: roslaunch/XML/param - ROS Wiki

param和rosparam的区别主要在于, param只能对1个参数进行操作, 而rosparam可以在把参数放到 .yaml文件后对多个参数进行操作, 但是这两者都是把参数设置到ROS master; 而arg则是在 .launch内部设置参数.

machine

声明了所有可以运行ROS节点的计算机, 如果是在本地启动节点, 不需要此tag.

  • name: 给远程机器分配的设备名
  • address: 远程机器的地址
  • env-loader: 指定远程计算机的环境文件, 必须是一个.sh脚本

更多Attributes参见: roslaunch/XML/machine - ROS Wiki

示例ROS Launch 文件解读

<launch>
    <arg name="port" default="$(optenv HUSKY_PORT /dev/prolific)" />

    <node pkg="clearpath_base" type="kinematic_node" name="husky_kinematic" ns="husky">
        <param name="port" value="$(arg port)" />
        <rosparam>
               cmd_fill: True
               data:
                        system_status: 10
                        safety_status: 10
                        encoders: 10
                        differential_speed: 10
                        differential_output: 10
                        power_status: 1
         </rosparam>
    </node>

    <!-- Publish diagnostics information from low-level MCU outputs -->
    <node pkg="husky_base" name="husky_base_diagnostics" type="diagnostics_publisher" />

    <!-- Publish wheel odometry from MCU encoder data -->
    <node pkg="husky_base" name="husky_basic_odom" type="basic_odom_publisher" />

    <!-- Diagnostic Aggregator -->
    <node pkg="diagnostic_aggregator" type="aggregator_node" name="diagnostic_aggregator">
    <rosparam command="load" file="$(find husky_base)/config/diagnostics.yaml"/>
    </node>
</launch>

参数定义, 定义一个名为 port的参数, 若 HUSKY_PORT环境变量不存在,就会使用 /dev/prolific作为默认值.

其中 optenv 的用法为 $(optenv ENVIRONMENT_VARIABLE default_value), 若设置环境变量, 则使用环境变量, 否则使用默认值.

    <arg name="port" default="$(optenv HUSKY_PORT /dev/prolific)" />

启动指定包名 clearpath_base,可执行文件为 kinematic_node, 节点名为 husky_kinematic, 命名空间 husky.

设置参数 port, 值为上面的arg值, 传递到Parameter Server中.

同时设置 cmd_fill等参数.

所有参数都被添加到 husky命名空间下面.

<node pkg="clearpath_base" type="kinematic_node" name="husky_kinematic" ns="husky">
  <param name="port" value="$(arg port)" />
  <rosparam>
    cmd_fill: True
    data:
      system_status: 10
      safety_status: 10
      encoders: 10
    differential_speed: 10
    differential_output: 10
    power_status: 1
  </rosparam>
</node>

同样是节点, 包, 可执行文件设置

<!-- Publish diagnostics information from low-level MCU outputs -->
<node pkg="husky_base" name="husky_base_diagnostics" type="diagnostics_publisher" />
<!-- Publish wheel odometry from MCU encoder data -->
<node pkg="husky_base" name="husky_basic_odom" type="basic_odom_publisher" />

启动对应包下的可执行文件的指定节点后, 定位 husky_base ros包, 查找 diagnostics.yaml文件, 并加载其内容至Parameter Server.

<node pkg="diagnostic_aggregator" type="aggregator_node" name="diagnostic_aggregator">
  <rosparam command="load" file="$(find husky_base)/config/diagnostics.yaml"/>
</node>

参考文章

[1] roslaunch/XML - ROS Wiki

[2] roslaunch/XML/launch - ROS Wiki

[3] launch文件中param、rosparam以及arg之间的区别

[4] Launch Files — ROS Tutorials 0.5.2 documentation

#ROS(8)#C++(27)

评论