# ros2 launch：替换错误：参数 'my_param' 期望类型 'double' 但得到 'integer'

- **ID:** `ros2/ros2-launch-parameter-substitution-type-mismatch`
- **领域:** ros2
- **类别:** config_error
- **错误码:** `ROS2-2001`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

启动文件使用的替换结果与声明的参数类型不兼容（例如整数 vs 双精度浮点数），因为 LaunchConfiguration 或 Python 替换返回了 int 而非 float。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ros2:humble | active | — | — |
| ros2:iron | active | — | — |
| ros2:rolling | active | — | — |
| launch:2.0.0 | active | — | — |

## 解决方案

1. ```
   使用 PythonExpression 显式将替换结果转换为期望类型。示例：
from launch.substitutions import PythonExpression

param_value = PythonExpression(['float(', LaunchConfiguration('my_param'), ')'])

这将强制替换结果为浮点数。
   ```
2. ```
   在启动文件中使用类型化替换（如 FloatSubstitution，如果可用），或确保 LaunchConfiguration 默认值为浮点数：
from launch.substitutions import LaunchConfiguration

param = LaunchConfiguration('my_param', default=0.0)  # 默认值为浮点数
   ```

## 无效尝试

- **Changing the parameter type in the node's declaration to match the substitution type** — The node may expect a specific type for correct operation; changing the declaration can cause runtime errors in the node. (50% 失败率)
- **Wrapping the substitution in str() without converting to the correct numeric type** — String substitution will be interpreted as a string, not a number, causing a different type mismatch error. (90% 失败率)
