ros2 config_error ai_generated true

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

ros2 launch: argument 'my_arg': expected type 'int' but got 'string'

ID: ros2/launch-argument-type-mismatch

其他格式: JSON · Markdown 中文 · English
90%修复率
88%置信度
1证据数
2023-11-20首次发现

版本兼容性

版本状态引入弃用备注
ROS2 Humble active
ROS2 Iron active
ROS2 Jazzy active

根因分析

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

English

Launch file declares a launch argument with a specific type (e.g., LaunchConfiguration('my_arg', data_type='int')) but the user passes a string value on the command line or via a launch file that cannot be implicitly converted.

generic

官方文档

https://docs.ros.org/en/humble/Tutorials/Intermediate/Launch/Using-Launch-Arguments.html

解决方案

  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'))'。

无效尝试

常见但无效的做法:

  1. Adding quotes around the integer value (e.g., 'ros2 launch my_pkg my_launch.py my_arg:=5' becomes 'my_arg:="5"') 90% 失败

    Quotes force the value to be a string, which is exactly what causes the type mismatch.

  2. Changing the launch file to remove type checking entirely 40% 失败

    Type checking is a safety feature; removing it may cause runtime errors if downstream nodes expect a specific type.