【linux】如何写一个launch文件

编写一个ROS(Robot Operating System)的launch文件是为了方便地启动一组相关的节点(nodes)、参数服务器(parameter server)参数、消息发布者/订阅者(publishers/subscribers)、服务(services)以及动作服务器(action servers)。

以下是一个编写ROS launch文件的基本步骤和示例:


  1. 创建launch文件 : 在项目的launch目录下创建一个新的.launch文件,例如my_launch_file.launch

  2. 声明XML根元素及命名空间 : 所有launch文件都以XML格式编写,需包含根元素 <launch>,并声明必要的命名空间。通常,ROS的launch文件会包含如下声明:

XML 复制代码
   <launch>
     <!-- ROS launch文件内容 -->
   </launch>

3 .设置ROS环境变量 : 可以通过<env>标签来设置环境变量,如设置ROS主目录:

XML 复制代码
   <launch>
     <env name="ROS_PACKAGE_PATH" value="$(find your_package)/..:${env{ROS_PACKAGE_PATH}}" />
     <!-- 其他ROS环境变量设置 -->
   </launch>

4. 加载参数 : 使用<param>标签将参数加载到参数服务器。可以指定参数的值或从文件中读取:

XML 复制代码
   <launch>
     <param name="my_node/topic_name" type="string" value="/example_topic" />
     <param name="my_node/config.yaml" command="$(find my_package)/scripts/load_config.py" />
   </launch>

5. 启动节点 : 使用<node>标签启动一个ROS节点。需要指定节点的可执行文件路径、名称(可选,默认与可执行文件同名)、参数(如果有的话)以及额外的属性(如 respawn、remap、output等):

XML 复制代码
   <launch>
     <node pkg="your_package" type="your_node_executable" name="your_node_name" output="screen">
       <param name="param1" value="value1" />
       <param name="param2" value="value2" />
     </node>
   </launch>

6. 节点间通信设置 : 使用<remap>标签重映射话题、服务或动作的名称:

XML 复制代码
   <launch>
     <node pkg="your_package" type="your_node_executable" name="your_node_name">
       <remap from="old_topic_name" to="new_topic_name" />
     </node>
   </launch>

7. 条件语句与循环 : 使用<if><unless>标签实现条件判断,<include>标签结合$(eval)进行循环操作:

XML 复制代码
   <launch>
     <group ns="$(arg robot_name)">
       <!-- 为每个机器人启动相同节点 -->
       <include file="$(find your_package)/launch/node.launch">
         <arg name="robot_id" value="$(eval robot_name + '_node')" />
       </include>
     </group>
   </launch>

8. 包含其他launch文件 : 使用<include>标签将多个相关但独立的launch文件组合在一起:

XML 复制代码
   <launch>
     <include file="$(find other_package)/launch/other_launch_file.launch" />
     <!-- 其他内容 -->
   </launch>

9. 定义和使用参数 : 使用<arg>标签定义参数,通过$(arg parameter_name)引用:

XML 复制代码
   <launch>
     <arg name="input_topic" default="/default_topic" />
     <node pkg="your_package" type="your_node_executable" name="your_node_name">
       <remap from="input" to="$(arg input_topic)" />
     </node>
   </launch>

完整示例

XML 复制代码
<launch>
  <!-- 设置环境变量 -->
  <env name="ROS_PACKAGE_PATH" value="$(find your_package)/..:${env{ROS_PACKAGE_PATH}}" />

  <!-- 加载参数 -->
  <param name="my_node/topic_name" type="string" value="/example_topic" />
  <param name="my_node/config.yaml" command="$(find my_package)/scripts/load_config.py" />

  <!-- 启动节点 -->
  <node pkg="your_package" type="your_node_executable" name="your_node_name" output="screen">
    <param name="param1" value="value1" />
    <param name="param2" value="value2" />
    <remap from="old_topic_name" to="new_topic_name" />
  </node>

  <!-- 条件语句与循环 -->
  <group ns="$(arg robot_name)">
    <include file="$(find other_package)/launch/other_launch_file.launch">
      <arg name="robot_id" value="$(eval robot_name + '_node')" />
    </include>
  </group>

  <!-- 包含其他launch文件 -->
  <include file="$(find other_package)/launch/other_launch_file.launch" />
</launch>
相关推荐
DYWorker0011 小时前
Linux驱动子系统:DMA-Buf —— Consumer和Provider(008)
linux·驱动开发
雯宝2 小时前
Linux 内核函数集
linux
GeW3 小时前
从挂科到高分通过:RHCE快速拿证的10个提分技巧
linux
灵途科技3 小时前
超远距离测距新品领衔 灵途科技多系列光电新品齐亮相CIOE2026
机器人
鸠摩智首席音效师3 小时前
Linux /etc/nsswitch.conf 文件有什么作用 ?
linux
东城居士4 小时前
理解pinctrl和gpio子系统驱动
linux·嵌入式系统
H_oRIZoN_4 小时前
Linux入门DAY43 ARM 架构基础、寄存器、大小端、异常向量、交叉编译
linux·arm·linux应用编程
ι:4 小时前
NUC12 Pro 安装 Ubuntu 24.04 LTS 详细教程
linux·windows·ubuntu·nuc
代码村新手5 小时前
Linux-进程概念
linux
ShineWinsu5 小时前
对于Git:基础操作的超详细保姆级解析
linux·c++·git·面试·备份·管理·版本控制器