# ros2 launch：参数'my_arg'：期望类型'int'但收到'string'

- **ID:** `ros2/launch-argument-type-mismatch`
- **领域:** ros2
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

启动文件声明了一个具有特定类型的启动参数（如LaunchConfiguration('my_arg', data_type='int')），但用户通过命令行或另一个启动文件传递了无法隐式转换的字符串值。

## 版本兼容性

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

## 解决方案

1. ```
   确保参数不加引号传递：'ros2 launch my_pkg my_launch.py my_arg:=5'（而不是'my_arg:="5"'）。如果使用YAML文件，将值指定为整数（如'my_arg: 5'）。
   ```
2. ```
   修改启动文件以接受字符串，并在启动文件内部使用Python的int()函数转换为整数：'int(LaunchConfiguration('my_arg'))'。
   ```

## 无效尝试

- **Adding quotes around the integer value (e.g., 'ros2 launch my_pkg my_launch.py my_arg:=5' becomes 'my_arg:="5"')** — Quotes force the value to be a string, which is exactly what causes the type mismatch. (90% 失败率)
- **Changing the launch file to remove type checking entirely** — Type checking is a safety feature; removing it may cause runtime errors if downstream nodes expect a specific type. (40% 失败率)
