# tf2：等待从'base_link'到'odom'的变换时，变换缓存超时

- **ID:** `ros2/tf2-timeout-on-startup`
- **领域:** ros2
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

变换广播器（如robot_state_publisher或自定义节点）未足够频繁地发布所需变换，或者存在启动延迟导致变换尚未可用，导致tf2的缓存查找在默认持续时间（如1秒）后超时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ROS2 Humble | active | — | — |
| ROS2 Iron | active | — | — |
| ROS2 Rolling | active | — | — |

## 解决方案

1. ```
   确保变换广播器以足够高的频率（如50 Hz）发布，并且节点的定时器设置正确。对于robot_state_publisher，检查URDF是否已加载并且关节状态正在发布。
   ```
2. ```
   在订阅者节点中添加带指数退避的重试循环：'while not tf_buffer.can_transform('odom', 'base_link', rclpy.time.Time(), timeout=rclpy.duration.Duration(seconds=0.1)): rclpy.spin_once(node)'
   ```
3. ```
   如果使用静态变换，在启动文件中使用static_transform_publisher发布它，以确保它始终可用。
   ```

## 无效尝试

- **Increasing the tf2 buffer timeout to a very large value (e.g., 10 seconds) in the node's code** — This masks the problem but does not fix the underlying publishing issue; the transform may never arrive if the broadcaster is not running. (70% 失败率)
- **Adding a sleep(5) at the beginning of the node's main function to wait for transforms** — Sleeping does not guarantee the transform will be available; it only delays the error, and the transform may still timeout. (80% 失败率)
