# Xacro

**Xacro**（XML Macros，XML宏）是[机器人操作系统](https://www.edgepedia.org/zh/robot-operating-system)（ROS）生态系统中使用的一种XML宏语言，用于生成以URDF（统一机器人描述格式）编写的机器人描述文件。借助xacro，作者可以通过使用展开为更大XML表达式的宏来构建更简短、更可读的XML文件，而`xacro`命令在启动时将该文件处理为纯URDF。<sup>[1](https://github.com/ros/xacro)</sup> Xacro并非URDF的替代品；URDF文件由Xacro源文件生成，仿真器和ROS工具所消费的正是展开后的结果。<sup>[2](https://atna-mam.utcluj.ro/index.php/Acta/article/download/2712/2123)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一种XML宏语言，通过宏、属性、简单算术和条件语句扩展URDF<sup>[1](https://github.com/ros/xacro)</sup><sup> • </sup><sup>[3](https://urdfbase.com/)</sup> |
| 输出 | 纯URDF文件，由`xacro`处理器在启动时或命令行上生成<sup>[2](https://atna-mam.utcluj.ro/index.php/Acta/article/download/2712/2123)</sup> |
| 软件包 | ROS软件包`xacro`，BSD-3-Clause许可证，作者为Stuart Glaser、William Woodall和Robert Haschke，由Robert Haschke维护<sup>[4](https://index.ros.org/p/xacro/)</sup> |
| 最新版本 | 2.1.1（2025年8月28日）<sup>[4](https://index.ros.org/p/xacro/)</sup> |
| 典型用法 | 由启动文件加载到`robot_description`参数中，再由`robot_state_publisher`广播为tf变换<sup>[5](https://www.clearpathrobotics.com/assets/guides/noetic/cpr-robot-customizer/RobotModel.html)</sup> |
| 已知痛点 | 一项对answers.ros.org上712个Xacro相关问题的研究发现，误解可归为10类，其中语法和解析器错误占比最大，达18%（441个中的78个）<sup>[6](https://arxiv.org/abs/2109.09694)</sup> |

## 目的及其与URDF的关系

在ROS中，机器人本体的结构通常以URDF记录，其中包括所涉及的组件、关节的位置、每个连杆的外观和碰撞几何形状，以及每个连杆的质量和惯性张量。<sup>[7](https://pouya-mansournia.github.io/ros2-zero-to-robot/04-archo-body-urdf-xacro.html)</sup> 原生URDF冗长繁琐，而机器人会多次重复相同的结构：四条相同的腿、六个相似的关节。Xacro就是带有宏、属性和简单算术的URDF，因此重复的结构只需编写一次。<sup>[3](https://urdfbase.com/)</sup> `.urdf.xacro`文件的用途与普通的`.urdf`文件相同，都是描述机器人模型，但额外启用了xacro宏。<sup>[5](https://www.clearpathrobotics.com/assets/guides/noetic/cpr-robot-customizer/RobotModel.html)</sup>

该语言处于一条关键工具链之中，这条工具链从几何表示贯穿仿真、可视化和系统执行。<sup>[6](https://arxiv.org/abs/2109.09694)</sup> 在[ROS 2](https://www.edgepedia.org/zh/ros-2)软件包中，`package.xml`通常会在`robot_state_publisher`和`rviz2`之外声明对`xacro`的`<exec_depend>`依赖，而启动文件使用`xacro`命令在启动时将描述转换为规范的URDF字符串。<sup>[8](https://ros-industrial.github.io/ros2_i_training/_source/manipulation/urdf/02_urdf_xacro_exercice_cartesian_robot.html)</sup>

## 语言特性

**属性与表达式。** `<xacro:property>`定义一个具名值，可在文件其他位置替换使用；`${...}`表达式支持算术运算和Python求值，因此连杆长度等尺寸可以参数化，而不必硬编码。<sup>[3](https://urdfbase.com/)</sup><sup> • </sup><sup>[8](https://ros-industrial.github.io/ros2_i_training/_source/manipulation/urdf/02_urdf_xacro_exercice_cartesian_robot.html)</sup> 一个教程示例构建了一个带移动关节的[笛卡尔机器人](https://www.edgepedia.org/zh/cartesian-robot)，其连杆长度通过xacro参数定制，以此教授模块化、可适配的机器人描述方法。<sup>[8](https://ros-industrial.github.io/ros2_i_training/_source/manipulation/urdf/02_urdf_xacro_exercice_cartesian_robot.html)</sup> 属性还可以携带默认值，这一能力在1.11.3版（2018年）中加入。<sup>[4](https://index.ros.org/p/xacro/)</sup>

**宏。** `<xacro:macro>`定义一个带参数的可复用模板，参数包括块参数（`*param`）和内容参数（`**param`），通过`<xacro:insert_block>`插入输出中。<sup>[9](https://github.com/gkjohnson/xacro-parser/blob/master/README.md)</sup> 宏可以带命名空间，包含指令可以携带`ns`属性来限定其所定义宏的作用域。<sup>[10](https://github.com/ros/xacro/issues/305)</sup>

**包含与条件语句。** `<xacro:include>`引入其他xacro文件，从而可以将描述拆分为基座、轮子、传感器和Gazebo专用标签等独立文件。<sup>[5](https://www.clearpathrobotics.com/assets/guides/noetic/cpr-robot-customizer/RobotModel.html)</sup><sup> • </sup><sup>[11](https://github.com/openAMRobot/openamr-platform-sw/blob/main/docs/ROS2_COMPLETE_COURSE.md)</sup> `<xacro:if>`条件语句使一个文件能够描述多种机器人变体。<sup>[12](https://github.com/robomotic/sim-benchmarks/blob/main/FORMATS.md)</sup> 条件表达式的求值结果为“0”、“1”、“true”或“false”。<sup>[13](https://github.com/ros/xacro/issues/145)</sup>

**YAML与替换参数。** 处理器可以用`load_yaml`加载YAML文件，这些文件在计算依赖时会被考虑；形如`$(arg name)`的替换参数将启动时的值传入文件。<sup>[4](https://index.ros.org/p/xacro/)</sup><sup> • </sup><sup>[9](https://github.com/gkjohnson/xacro-parser/blob/master/README.md)</sup>

## 处理流程

`xacro`命令展开源文件并打印生成的URDF，通常重定向到文件：`rosrun xacro xacro scene.urdf.xacro > scene.urdf`。<sup>[10](https://github.com/ros/xacro/issues/305)</sup> 在ROS 1启动文件中，转换通常嵌入在`robot_description`参数定义中，使用`command="$(find xacro)/xacro ..."`一行；在ROS 2中，启动系统调用处理器来生成URDF字符串。<sup>[8](https://ros-industrial.github.io/ros2_i_training/_source/manipulation/urdf/02_urdf_xacro_exercice_cartesian_robot.html)</sup> 处理后的描述随后加载到`robot_state_publisher`中，由后者广播机器人的tf变换；当机器人具有轮子等非固定关节时，还会加入`joint_state_publisher`。<sup>[5](https://www.clearpathrobotics.com/assets/guides/noetic/cpr-robot-customizer/RobotModel.html)</sup>

由于`${...}`表达式是任意Python代码，展开步骤相当于一个小型编程环境，错误会以解析失败或求值失败的形式出现，而非URDF问题。2021年，机器人学研究者Nicholas Albergo和Vivek Rathi，以及[北卡罗来纳州立大学](https://www.edgepedia.org/zh/north-carolina-state-university)软件工程研究者John-Paul Ore，对answers.ros.org上的712个问题进行了研究，使用开放式编码对误解进行分类，发现Xacro问题在广泛的语境中都有表现，阻碍了新手上手。<sup>[6](https://arxiv.org/abs/2109.09694)</sup>

## 软件包历史与生态

`xacro`软件包发布在ROS软件包索引中，状态为受维护，作者署名为Stuart Glaser、William Woodall和Robert Haschke。<sup>[4](https://index.ros.org/p/xacro/)</sup> 其变更日志记录了功能的稳步增长：1.11.3版（2018年）为属性增加了默认值，并将允许的Python内置函数扩展到包括`min`、`max`和`round`；2.0.6版（2021年）将`YamlDictWrapper`以`dotify()`形式暴露，用于对字典进行点号访问；2.0.11版（2024年）通过`pyproject.toml`增加了pip安装方式，并允许在无ROS环境下使用替换参数；2.1.1版（2025年）在`process_file()`中处理`str`和`pathlib.Path`参数。<sup>[4](https://index.ros.org/p/xacro/)</sup>

该格式已经超出了其最初的Python实现。独立处理器包括xacro-parser，一个该格式的[JavaScript](https://www.edgepedia.org/zh/javascript)解析器和加载器<sup>[9](https://github.com/gkjohnson/xacro-parser/blob/master/README.md)</sup>；xacrodoc，一个从软件包文件或包含指令加载文档的Python库<sup>[14](https://github.com/adamheins/xacrodoc/blob/main/README.md)</sup>；以及xacro-pure，一个无需ROS或Python安装即可运行的Rust处理器，它通过以rustpython-vm解释器作为求值器后端，实现了与`${...}` Python表达式的完全对等，支持带参数、默认值、`^`转发、块参数和`**content`参数的宏、命名空间宏，以及`ns=`、`optional=`和通配符包含。<sup>[15](https://github.com/CogniPilot/xacro-pure)</sup>

## 在机器人描述格式中的位置

Xacro的采用随URDF而来。在2023年一项对510名机器人开发者的调查中，96.8%的参与者此前仿真过机器人，其中95.5%使用过URDF；53.5%认为URDF未来会更常用，而12.2%认为其他标准或工具会使其过时。<sup>[16](https://arxiv.org/abs/2302.13442)</sup> 同一调查指出了URDF的局限性，而xacro并不能消除这些局限，例如无法对并联连杆机构和闭链系统建模，以及动力学参数数量有限。<sup>[16](https://arxiv.org/abs/2302.13442)</sup> 从业者也注意到这种方法的局限；ROSCon 2022的一场演讲题为“Xacro的能力有限：借助EmPy和ERB更进一步”，指向其他模板工具以应对xacro无法覆盖的情况。<sup>[17](https://download.ros.org/downloads/roscon/2022/Xacro%20can%20only%20take%20you%20so%20far%20go%20further%20with%20EmPy%20and%20ERB!.pdf)</sup>

## References

1. [ros/xacro repository README, GitHub](https://github.com/ros/xacro)
2. [Modelisation and programing of a mobile robot on ROS and Gazebo, Robotix Academy](https://atna-mam.utcluj.ro/index.php/Acta/article/download/2712/2123)
3. [URDF — the format robots are described in, urdfbase.com](https://urdfbase.com/)
4. [xacro — ROS Package Overview, ROS Index](https://index.ros.org/p/xacro/)
5. [Setting Up Your Robot's Model, Clearpath Robotics Robot Customizer Tutorials](https://www.clearpathrobotics.com/assets/guides/noetic/cpr-robot-customizer/RobotModel.html)
6. [Nicholas Albergo, Vivek Rathi, John-Paul Ore (2021). Understanding Xacro Misunderstandings. arXiv:2109.09694](https://arxiv.org/abs/2109.09694)
7. [Chapter 4 — The ARCHO Body with URDF and Xacro, ROS2 Zero to Robot](https://pouya-mansournia.github.io/ros2-zero-to-robot/04-archo-body-urdf-xacro.html)
8. [URDF-XACRO exercise: build a cartesian robot description, ROS-Industrial ROS 2 training](https://ros-industrial.github.io/ros2_i_training/_source/manipulation/urdf/02_urdf_xacro_exercice_cartesian_robot.html)
9. [xacro-parser README, GitHub](https://github.com/gkjohnson/xacro-parser/blob/master/README.md)
10. [ros/xacro issue #305: Include macro with namespace propagates across all subsequent includes](https://github.com/ros/xacro/issues/305)
11. [ROS2 Complete Course, openAMRobot](https://github.com/openAMRobot/openamr-platform-sw/blob/main/docs/ROS2_COMPLETE_COURSE.md)
12. [sim-benchmarks FORMATS.md, robomotic](https://github.com/robomotic/sim-benchmarks/blob/main/FORMATS.md)
13. [ros/xacro issue #145: True/False vs. true/false when using conditional blocks with --inorder](https://github.com/ros/xacro/issues/145)
14. [xacrodoc README, GitHub](https://github.com/adamheins/xacrodoc/blob/main/README.md)
15. [CogniPilot/xacro-pure repository, GitHub](https://github.com/CogniPilot/xacro-pure)
16. [Daniella Tola and colleague (2023). Understanding URDF: A Survey Based on User Experience. arXiv:2302.13442](https://arxiv.org/abs/2302.13442)
17. [Xacro can only take you so far: go further with EmPy and ERB!, ROSCon 2022](https://download.ros.org/downloads/roscon/2022/Xacro%20can%20only%20take%20you%20so%20far%20go%20further%20with%20EmPy%20and%20ERB!.pdf)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 工程与制造 › 机器人技术与自动化 › Robot software, AI, and methods › Robot middleware, frameworks, and behavior architectures

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/xacro

English original: https://www.edgepedia.org/xacro
