# ros2 launch: 替换错误：在启动配置中找不到参数'my_arg'

- **ID:** `ros2/ros2-launch-arg-substitution-not-found`
- **领域:** ros2
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

启动文件使用$(var my_arg)或类似替换，但该参数从未通过DeclareLaunchArgument声明。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ROS2 Humble (launch 1.0.0) | active | — | — |
| ROS2 Iron (launch 1.1.0) | active | — | — |
| ROS2 Rolling (launch 1.2.0) | active | — | — |

## 解决方案

1. ```
   在使用参数之前，在启动文件中添加DeclareLaunchArgument：from launch.actions import DeclareLaunchArgument; ld.add_action(DeclareLaunchArgument('my_arg', default_value='default'))
   ```
2. ```
   如果参数是可选的，使用条件替换：$(var my_arg 'default_value')来提供默认值。
   ```

## 无效尝试

- **Add the argument to the launch command line without declaring it in the file** — Arguments passed on the command line must match a declared argument; otherwise, ros2 launch ignores them. (80% 失败率)
- **Use $(env my_arg) instead of $(var my_arg)** — Environment variables are not the same as launch arguments; the substitution syntax is different. (90% 失败率)
