ros2
config_error
ai_generated
true
ros2 launch: argument 'my_arg': expected type 'int' but got 'string'
ID: ros2/launch-argument-type-mismatch
90%Fix Rate
88%Confidence
1Evidence
2023-11-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| ROS2 Humble | active | — | — | — |
| ROS2 Iron | active | — | — | — |
| ROS2 Jazzy | active | — | — | — |
Root Cause
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中文
启动文件声明了一个具有特定类型的启动参数(如LaunchConfiguration('my_arg', data_type='int')),但用户通过命令行或另一个启动文件传递了无法隐式转换的字符串值。
Official Documentation
https://docs.ros.org/en/humble/Tutorials/Intermediate/Launch/Using-Launch-Arguments.htmlWorkarounds
-
95% success Ensure the argument is passed without quotes: 'ros2 launch my_pkg my_launch.py my_arg:=5' (not 'my_arg:="5"'). If using a YAML file, specify the value as an integer (e.g., 'my_arg: 5').
Ensure the argument is passed without quotes: 'ros2 launch my_pkg my_launch.py my_arg:=5' (not 'my_arg:="5"'). If using a YAML file, specify the value as an integer (e.g., 'my_arg: 5').
-
90% success Modify the launch file to accept a string and convert it to int inside the launch file using Python's int() function: 'int(LaunchConfiguration('my_arg'))'.
Modify the launch file to accept a string and convert it to int inside the launch file using Python's int() function: 'int(LaunchConfiguration('my_arg'))'.
中文步骤
确保参数不加引号传递:'ros2 launch my_pkg my_launch.py my_arg:=5'(而不是'my_arg:="5"')。如果使用YAML文件,将值指定为整数(如'my_arg: 5')。
修改启动文件以接受字符串,并在启动文件内部使用Python的int()函数转换为整数:'int(LaunchConfiguration('my_arg'))'。
Dead Ends
Common approaches that don't work:
-
Adding quotes around the integer value (e.g., 'ros2 launch my_pkg my_launch.py my_arg:=5' becomes 'my_arg:="5"')
90% fail
Quotes force the value to be a string, which is exactly what causes the type mismatch.
-
Changing the launch file to remove type checking entirely
40% fail
Type checking is a safety feature; removing it may cause runtime errors if downstream nodes expect a specific type.