> For the complete documentation index, see [llms.txt](https://hanxu.gitbook.io/blogs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hanxu.gitbook.io/blogs/ros/ros-jiao-cheng-bian-xie-launch-wen-jian.md).

# 【ROS教程】编写launch文件

@[TOC](https://github.com/UnderTurrets/notes/blob/master/ROS/文章目录/README.md)

***

## 1.launch文件有哪些标签

| 标签       | 含义                                      |
| -------- | --------------------------------------- |
| node     | 指定 ROS 节点，是最常见的标签                       |
| include  | 用于将另一个 xml 格式的 launch 文件导入到当前文件         |
| remap    | 话题重命名                                   |
| param    | 在参数服务器上设置参数                             |
| rosparam | 从 YAML 文件导入参数，或将参数导出到 YAML 文件，也可以用来删除参数 |
| group    | 可以对节点分组，具有 ns 属性，可以让节点归属某个命名空间          |
| arg      | 用于动态设置参数，可以增强launch文件的灵活性               |
| env      | 用于设置环境变量                                |

## 2.node标签

### 2.1 必选属性

| 属性                 | 含义                     |
| ------------------ | ---------------------- |
| name="node-name"   | 节点名称(在 ROS 网络拓扑中节点的名称) |
| pkg="package-name" | 节点所属的包                 |
| type="node-type"   | 节点类型(与之相同名称的可执行文件)     |

### 2.2 可选属性

| 属性                           | 含义                                         |
| ---------------------------- | ------------------------------------------ |
| args="arg1 arg2 arg3"        | 将参数传递给节点                                   |
| machine="machine-name"       | 在指定机器上启动节点                                 |
| respawn="true\| false"       | 如果节点退出，是否自动重启                              |
| respawn\_delay="n"           | 如果 respawn 为 true, 那么延迟 N 秒后启动节点           |
| required="true\| false"      | 该节点是否必须，如果为 true,那么如果该节点退出，将杀死整个 roslaunch |
| ns="namespace"               | 在指定命名空间中启动节点                               |
| clear\_params="true\| false" | 在启动前，删除节点的私有空间的所有参数                        |
| output="log\| screen"        | 日志发送目标，可以设置为 log 日志文件，或 screen 屏幕,默认是 log  |

### 2.3 可选子级标签

1. \<env>
2. \<remap>
3. \<rosparam>
4. \<param>

## 3.include标签

### 3.1 必选属性

| 属性                                           | 含义       |
| -------------------------------------------- | -------- |
| file="$(find pkg-name)/path/filename.launch" | 要包含的文件路径 |

### 3.2 可选属性

| 属性             | 含义          |
| -------------- | ----------- |
| ns="namespace" | 在指定命名空间导入文件 |

### 3.3 可选子级标签

1. \<env>
2. \<arg>

## 4.remap标签

### 4.1 必选属性

| 属性                   | 含义     |
| -------------------- | ------ |
| from="original-name" | 原始话题名称 |
| to="new-name"        | 目标名称   |

## 5.param标签

* 作为`\<node>`子级标签时，相当于私有命名空间。

### 5.1 必选属性

| 属性                    | 含义            |
| --------------------- | ------------- |
| name="namespace/name" | 参数名称，可以包含命名空间 |

### 5.2 可选属性

| 属性                                  | 含义                               |
| ----------------------------------- | -------------------------------- |
| value="value"                       | 定义参数值，如果此处省略，必须指定外部文件作为参数源       |
| type="str\|int\|double\|bool\|yaml" | 指定参数类型，如果未指定，roslaunch 会尝试确定参数类型 |

roslaunch 确定参数类型的规则如下：

* 如果包含 '.' 的数字解析未浮点型，否则为整型
* "true" 和 "false" 是 bool 值(不区分大小写)
* 其他是字符串

## 6.rosparam标签

### 6.1 必选属性

| 属性                                    | 含义              |
| ------------------------------------- | --------------- |
| file="$(find pkg-name)/path/foo.yaml" | 加载或导出到的 yaml 文件 |
| param="param-name"                    | 参数名称            |

### 6.2 可选属性

| 属性                              | 含义               |
| ------------------------------- | ---------------- |
| command="load\| dump \| delete" | 加载、导出或删除参数，默认为加载 |
| ns="namespace"                  | 将参数指定到命名空间       |

## 7.group标签

### 7.1 可选属性

| 属性                          | 含义                                               |
| --------------------------- | ------------------------------------------------ |
| ns="namespace"              | 将该组节点分配到指定的命名空间。命名空间可以是全局的，也可以是相对的，但不鼓励使用全局命名空间。 |
| clear\_params="true\|false" | 在启动前删除组的命名空间中的所有参数。这个功能非常危险，应该谨慎使用。              |

### 7.2 可选子级标签

* **其他所有标签都是其子级标签**

## 8.arg标签

### 8.1 必选属性

| 属性              | 含义       |
| --------------- | -------- |
| name="arg-name" | 欲设置的参数名称 |

### 8.2 可选属性

| 属性                             | 含义                     |
| ------------------------------ | ---------------------- |
| default="default value"        | 设置参数的默认值。不能与value属性结合。 |
| value="value"                  | 设置参数的值。不能与default属性结合。 |
| doc="description for this arg" | 加载、导出或删除参数，默认为加载       |

### 8.3 示例

```xml
<launch>
  <!-- declare arg to be passed in -->
  <arg name="hoge" /> 

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

## 9.env标签

### 9.1 必选属性

| 属性                                 | 含义          |
| ---------------------------------- | ----------- |
| name="environment-variable-name"   | 所设置的环境变量的名字 |
| value="environment-variable-value" | 参数说明        |

## 10.全局示例

```xml
<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>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hanxu.gitbook.io/blogs/ros/ros-jiao-cheng-bian-xie-launch-wen-jian.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
