# SMACH（机器人学）

**SMACH**（“State MACHine”，即状态机）是一个用 Python 编写的任务级执行框架，用于将复杂的机器人行为组合为分层且并发的状态机，并与[机器人操作系统](https://www.edgepedia.org/zh/robot-operating-system)（ROS）集成。<sup>[1](https://github.com/ros/executive_smach)</sup> 它是一个不依赖 ROS 的库，可以构建分层和并发的状态机，以及任何其他符合其接口的任务状态容器，其目的是让机器人的执行系统由更高层的任务规划系统控制。<sup>[2](https://www.researchgate.net/publication/224204236_The_SMACH_High-Level_Executive_ROS_News)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 一个过程式的、基于 Python 的任务级执行框架，带 ROS 集成，以 `executive_smach` 元包形式分发，包含 `smach`、`smach_ros` 和 `smach_msgs` 三个包<sup>[1](https://github.com/ros/executive_smach)</sup><sup> • </sup><sup>[3](https://index.ros.org/p/executive_smach/)</sup> |
| 核心模型 | 分层并发状态机；状态表示执行的状态，带有已声明的结果和一个阻塞式 `execute` 函数，该函数运行至返回某个结果为止<sup>[2](https://www.researchgate.net/publication/224204236_The_SMACH_High-Level_Executive_ROS_News)</sup> |
| 容器 | StateMachine、Concurrence、Sequence 和 Iterator，每一种都可嵌套在其他容器内<sup>[4](https://mozdev.oregonstate.edu/pub/ros/ros_wiki_mirror/smach%282f%29Documentation.html)</sup> |
| 数据流 | 每个容器持有一个局部作用域的用户数据字典，其子状态可以访问，因此状态可以读取先前已执行状态写入的数据<sup>[2](https://www.researchgate.net/publication/224204236_The_SMACH_High-Level_Executive_ROS_News)</sup> |
| 当前版本 | 版本 3.0.3，BSD 许可，使用 ament_cmake 构建，标记为 MAINTAINED 和 RELEASED；2023 年 6 月 12 日发布的 3.0.0 版使代码兼容 ROS 2<sup>[3](https://index.ros.org/p/executive_smach/)</sup> |
| 作者 | Jonathan Bohren、Wim Meeussen 和 Charles Lesire；原始库由 Bohren 和 Steve Cousins 于 2010 年在《IEEE 机器人与自动化杂志》上描述<sup>[3](https://index.ros.org/p/executive_smach/)</sup><sup> • </sup><sup>[2](https://www.researchgate.net/publication/224204236_The_SMACH_High-Level_Executive_ROS_News)</sup> |
| 预期用途 | 任务级编排；由于用 Python 编写，它不是快速、底层状态转换的好选择<sup>[5](https://webweb.ams3.cdn.digitaloceanspaces.com/data/simmcdev.webweb.ai.in/MCAdigitalbook/Operating-Systems-20240629T072015Z-001/Programming%20Robots%20with%20ROS_%20A%20Practical%20Introduction%20to%20the%20Robot%20Operating%20System%20%28%20PDFDrive%20%29%20%282%29.pdf)</sup> |

## 状态与结果

在 SMACH 中，“状态”是执行的局部状态，对应于系统正在执行某项任务，而非形式状态机理论中系统的某种配置。这让开发者可以专注于系统正在执行什么及其结果；SMACH 中的状态更接近结构化编程中的状态。<sup>[4](https://mozdev.oregonstate.edu/pub/ros/ros_wiki_mirror/smach%282f%29Documentation.html)</sup>

状态与其容器之间的接口通过结果来定义。一个状态的潜在结果是该状态实例的属性，必须在状态执行之前声明，这意味着手写的 SMACH 计划可以在不运行的情况下检查其转换的一致性。结果的作用域限于发出该结果的状态：在状态机中，诸如 `succeeded`、`aborted` 或 `preempted` 这样的结果会与另一个状态关联以形成转换，而在另一种容器类型中，同样的结果可能被区别对待。<sup>[4](https://mozdev.oregonstate.edu/pub/ros/ros_wiki_mirror/smach%282f%29Documentation.html)</sup>

该库提供了现成的状态类：`State` 基础接口；`SPAState`，带有 `succeeded`、`preempted` 和 `aborted` 三个常见结果；`MonitorState`，订阅一个 ROS 话题并在条件成立时阻塞；`ConditionState`，运行一个条件回调；以及 `SimpleActionState`，一个指向简单 actionlib 动作的代理。<sup>[4](https://mozdev.oregonstate.edu/pub/ros/ros_wiki_mirror/smach%282f%29Documentation.html)</sup>

## 容器与并发

所有 SMACH 容器都持有一个以字符串标签为键的状态字典，以及一个其子级可访问的用户数据结构。要修改容器的结构，必须先打开它，可以用 Python 的 `with` 关键字，或调用 `open()` 和 `close()`；随后容器提供静态方法，将状态添加到当前打开的容器中。<sup>[4](https://mozdev.oregonstate.edu/pub/ros/ros_wiki_mirror/smach%282f%29Documentation.html)</sup>

**Concurrence。** `Concurrence` 容器实现简单的分叉-汇合并发：所有包含的状态同时执行，容器只有在所有状态都准备好转换时才能转换。终止发生在所有子状态终止时、至少一个子状态终止时，或用户定义的回调发出终止信号时。容器结果由结果回调、要求所有子状态终止的结果映射、要求一个子状态终止的结果映射或默认结果决定；当提供多种策略时，优先级依次为结果回调、首个触发的结果映射、最后触发的结果映射，最后是默认结果。子状态终止回调还可以抢占一部分状态、全部状态或都不抢占。<sup>[6](https://docs.ros.org/jade/api/smach/html/python/smach.concurrence.Concurrence-class.html)</sup>

容器可以嵌套，从而产生分层状态机，有一个教程系列涵盖嵌套状态机、在状态之间传递用户数据，以及从状态机内部调用动作服务器。<sup>[7](https://mirror.umd.edu/roswiki/smach%282f%29Tutorials.html)</sup>

## 内省与可视化

SMACH 容器可以通过 ROS 暴露一个调试接口，让开发者设置 SMACH 树的完整初始状态，包括每个容器的初始状态标签以及每一层用户数据结构的内容；这通过 SMACH Viewer 进行可视化。<sup>[4](https://mozdev.oregonstate.edu/pub/ros/ros_wiki_mirror/smach%282f%29Documentation.html)</sup> `IntrospectionClient` 类可以列出广播 SMACH 状态的服务器，并在远程内省服务器上设置目标容器的初始状态和用户数据。<sup>[8](https://docs.ros.org/en/kinetic/api/smach_ros/html/python/smach_ros.introspection.IntrospectionClient-class.html)</sup>

## 历史与 ROS 2 支持

该库由约翰斯·霍普金斯大学的 Jonathan Bohren 和 Steve Cousins 于 2010 年在《IEEE 机器人与自动化杂志》上提出，是一个基于分层并发状态机的 Python API。<sup>[2](https://www.researchgate.net/publication/224204236_The_SMACH_High-Level_Executive_ROS_News)</sup> 代码位于 `ros/executive_smach` 仓库，据最近报告有 241 次提交、184 个星标和 151 个复刻。<sup>[1](https://github.com/ros/executive_smach)</sup> 版本 2.5.0（2020 年 5 月）增加了 Python 3 兼容性，版本 3.0.0（2023 年 6 月）使该包兼容 [ROS 2](https://www.edgepedia.org/zh/ros-2)；当前版本是 3.0.3（2023 年 7 月），仓库最后更新于 2024 年 9 月 23 日，开发状态为 MAINTAINED。<sup>[3](https://index.ros.org/p/executive_smach/)</sup>

**扩展。** FlexBE（Flexible Behavior Engine，灵活行为引擎）由 [ViGIR 团队](https://www.edgepedia.org/zh/team-vigir)为其参加 2015 年 DARPA 机器人挑战赛决赛而开发，是 SMACH 的扩展，并作为其 Atlas 人形机器人的高层执行系统，由操作员通过图形界面进行监控和交互。<sup>[9](https://discourse.ros.org/t/state-machine-visualization/1691)</sup> SMACHA 是一个单独的工具，从 YAML 脚本和模板生成 SMACH Python 代码。<sup>[10](https://reconcell.gitlab.io/smacha/smacha/smacha.html)</sup>

## 使用与替代方案

SMACH 已被用作多个研究系统中的任务级执行系统，包括 DE NIRO 移动操作平台，其中每个状态共享一个共同结构，并可以在状态进入或退出时调用 ROS 参数服务器、激活 ROS 模块以及在组件之间传递数据。<sup>[11](https://pmc.ncbi.nlm.nih.gov/articles/PMC7805901/)</sup> 它还被用作 RoboCup@Work 移动操作和多传感器工业机器人架构中状态机的基础。<sup>[12](https://www.researchgate.net/publication/308925409_A_Failure-Tolerant_Approach_for_Autonomous_Mobile_Manipulation_in_RoboCupWork)</sup><sup> • </sup><sup>[13](https://www.mdpi.com/1424-8220/17/6/1249)</sup>

[行为树](https://www.edgepedia.org/zh/behavior-trees)已成为基于 ROS 的机器人学中一种竞争性的行为建模语言，对行为树领域特定语言的研究将它们与该领域的标准行为模型语言——状态机——直接进行比较。<sup>[14](https://arxiv.org/abs/2208.04211)</sup> 特别是对于 ROS 2，出现了较新的状态机库，如 YASMIN，其指出 SMACH 和 SMACC 在 ROS 1 中简化了状态机的实现，而 ROS 2 的替代方案较少。<sup>[15](https://arxiv.org/abs/2205.13284)</sup> 一本实用指南总结了 SMACH 的定位：它适合可以明确画成状态图的行为，但不适合快速的底层控制循环，因为它用 Python 编写。<sup>[5](https://webweb.ams3.cdn.digitaloceanspaces.com/data/simmcdev.webweb.ai.in/MCAdigitalbook/Operating-Systems-20240629T072015Z-001/Programming%20Robots%20with%20ROS_%20A%20Practical%20Introduction%20to%20the%20Robot%20Operating%20System%20%28%20PDFDrive%20%29%20%282%29.pdf)</sup>

## References

1. [ros/executive_smach: A procedural python-based task execution framework with ROS integration, GitHub](https://github.com/ros/executive_smach)
2. [Jonathan Bohren, Steve Cousins (2010). The SMACH High-Level Executive [ROS News]. IEEE Robotics & Automation Magazine 17(4):18–20](https://www.researchgate.net/publication/224204236_The_SMACH_High-Level_Executive_ROS_News)
3. [executive_smach, ROS Package Overview (index.ros.org)](https://index.ros.org/p/executive_smach/)
4. [SMACH Documentation (ROS wiki mirror)](https://mozdev.oregonstate.edu/pub/ros/ros_wiki_mirror/smach%282f%29Documentation.html)
5. [Programming Robots with ROS, chapter "Defining State Machines with smach"](https://webweb.ams3.cdn.digitaloceanspaces.com/data/simmcdev.webweb.ai.in/MCAdigitalbook/Operating-Systems-20240629T072015Z-001/Programming%20Robots%20with%20ROS_%20A%20Practical%20Introduction%20to%20the%20Robot%20Operating%20System%20%28%20PDFDrive%20%29%20%282%29.pdf)
6. [smach.concurrence.Concurrence class documentation, ROS API](https://docs.ros.org/jade/api/smach/html/python/smach.concurrence.Concurrence-class.html)
7. [SMACH Tutorials (ROS wiki mirror)](https://mirror.umd.edu/roswiki/smach%282f%29Tutorials.html)
8. [smach_ros.introspection.IntrospectionClient class documentation, ROS API](https://docs.ros.org/en/kinetic/api/smach_ros/html/python/smach_ros.introspection.IntrospectionClient-class.html)
9. [State Machine Visualization, Open Robotics Discourse (FlexBE discussion)](https://discourse.ros.org/t/state-machine-visualization/1691)
10. [SMACHA Documentation](https://reconcell.gitlab.io/smacha/smacha/smacha.html)
11. [Robot DE NIRO: A Human-Centered, Autonomous, Mobile Research Platform, PMC](https://pmc.ncbi.nlm.nih.gov/articles/PMC7805901/)
12. [A Failure-Tolerant Approach for Autonomous Mobile Manipulation in RoboCup@Work, ResearchGate](https://www.researchgate.net/publication/308925409_A_Failure-Tolerant_Approach_for_Autonomous_Mobile_Manipulation_in_RoboCupWork)
13. [Enhanced Flexibility and Reusability through State Machine-Based Architectures for Multisensor Intelligent Robotics, Sensors](https://www.mdpi.com/1424-8220/17/6/1249)
14. [Behavior Trees and State Machines in Robotics Applications, arXiv:2208.04211](https://arxiv.org/abs/2208.04211)
15. [YASMIN: Yet Another State MachINe library for ROS 2, arXiv:2205.13284](https://arxiv.org/abs/2205.13284)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 工程与制造 › 机器人技术与自动化 › 机器人软件、人工智能与方法 › 机器人中间件、框架与行为架构

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/smach-robotics

English original: https://www.edgepedia.org/smach-robotics
