ros2 config_error ai_generated true

ros2 launch: Substitution error: argument 'my_arg' not found in launch configuration

ID: ros2/ros2-launch-arg-substitution-not-found

Also available as: JSON · Markdown · 中文
90%Fix Rate
88%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ROS2 Humble (launch 1.0.0) active
ROS2 Iron (launch 1.1.0) active
ROS2 Rolling (launch 1.2.0) active

Root Cause

A launch file uses $(var my_arg) or similar substitution for an argument that was never declared with DeclareLaunchArgument.

generic

中文

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

Official Documentation

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

Workarounds

  1. 95% success Add a DeclareLaunchArgument for 'my_arg' in the launch file before using it: from launch.actions import DeclareLaunchArgument; ld.add_action(DeclareLaunchArgument('my_arg', default_value='default'))
    Add a DeclareLaunchArgument for 'my_arg' in the launch file before using it: from launch.actions import DeclareLaunchArgument; ld.add_action(DeclareLaunchArgument('my_arg', default_value='default'))
  2. 85% success If the argument is optional, use a conditional substitution: $(var my_arg 'default_value') to provide a fallback.
    If the argument is optional, use a conditional substitution: $(var my_arg 'default_value') to provide a fallback.

中文步骤

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

Dead Ends

Common approaches that don't work:

  1. Add the argument to the launch command line without declaring it in the file 80% fail

    Arguments passed on the command line must match a declared argument; otherwise, ros2 launch ignores them.

  2. Use $(env my_arg) instead of $(var my_arg) 90% fail

    Environment variables are not the same as launch arguments; the substitution syntax is different.