ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You might be able to use launch.substitutions.LaunchConfiguration to achieve what you want.

For example, the following will substitute the value of the launch argument "my_arg" if it is provided or an empty string if it is not:

 n = launch_ros.actions.Node(
     package='my_pkg',
     executable='my_node',
     prefix=launch.substitutions.LaunchConfiguration('my_arg', default='')
 )

"my_arg" can be set from the command line like so:

ros2 launch my.launch.py my_arg:="some_prefix"

Alternatively, you can set the argument inside a launch file with launch.actions.SetLaunchConfiguration. Here's a complete example based on your question:

def generate_launch_description():
    set_launch_config = launch.actions.SetLaunchConfiguration(
        'my_arg', 'some_prefix'
    )
    n = launch_ros.actions.Node(
        package='my_pkg',
        executable='my_node',
        prefix='some_prefix'
    )

    return launch.LaunchDescription([set_launch_config, n])

Note, I think using SetlaunchConfiguration precludes setting the value from the command-line.