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

ROS2 python launch: Using argument to create file name for a launch_argument

asked 2022-07-21 11:05:47 -0500

Markus Bader gravatar image

updated 2022-07-21 11:19:52 -0500

I like to launch gazebo with different environments (world files). But I only want to give as argument the world file name and not the full path. The full path should be created by launch file.

How can I use a LaunchArgument to create a full file name. The following statement in the IncludeLaunchDescription is not working.

launch_arguments={'world': pkg_tuw_gazebo + '/worlds/' + LaunchConfiguration('room') + '.world'}.items(),

Thanks in advance

There's the full code python launch file.

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import SetEnvironmentVariable
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.actions import SetEnvironmentVariable
from launch.substitutions import LaunchConfiguration, TextSubstitution 

def generate_launch_description():

    room_arg    = DeclareLaunchArgument('room', default_value=TextSubstitution(text='roblab'))
    pkg_gazebo_ros = get_package_share_directory('gazebo_ros')
    pkg_tuw_gazebo = get_package_share_directory('tuw_gazebo')
    pkg_tuw_gazebo_models = get_package_share_directory('tuw_gazebo_models')

    if 'GAZEBO_MODEL_PATH' in os.environ:
        model_path =  os.environ['GAZEBO_MODEL_PATH'] \
            + ':' + pkg_tuw_gazebo_models + '/models'
        model_path =  pkg_tuw_gazebo_models + '/models'

    return LaunchDescription([
        SetEnvironmentVariable(name='GAZEBO_MODEL_PATH', value=model_path),
            PythonLaunchDescriptionSource( os.path.join(pkg_gazebo_ros, 'launch', '')),
            # This is working, but I like to substitute cave in with my argument
            #launch_arguments={'world': os.path.join(pkg_tuw_gazebo, 'worlds', '')}.items(),  
            # This is NOT working, I don't know how to access the room argument?
            launch_arguments={'world': pkg_tuw_gazebo + '/worlds/' \
                + LaunchConfiguration('room') + '.world'}.items(),

I am getting the Error:

launch.invalid_launch_file_error.InvalidLaunchFileError: Caught exception when trying to load file of format [py]: can only concatenate str (not "LaunchConfiguration") to str
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-07-21 12:33:10 -0500

ljaniec gravatar image

You can use OpaqueFunction for this, nice example is there:

Or in this accepted answer:

edit flag offensive delete link more



Thanks, I found now a way by using OpaqueFunction.

My solution looks now like:

def generate_launch_description():


    def world_file_file_name(context):
        file = os.path.join(pkg_tuw_gazebo, 'worlds', context.launch_configurations['room'] + '.world')
        return [SetLaunchConfiguration('world', file)]

    world_file_path_arg = OpaqueFunction(function=world_file_file_name)

    return LaunchDescription([
        DeclareLaunchArgument('room', default_value=TextSubstitution(text='empty')),
        SetEnvironmentVariable(name='GAZEBO_MODEL_PATH', value=model_path),
            PythonLaunchDescriptionSource( os.path.join(pkg_gazebo_ros, 'launch', '')),
Markus Bader gravatar image Markus Bader  ( 2022-07-22 03:32:13 -0500 )edit

Nice, please accept the answer, if it explains everything, so it is properly marked in the queue :)

ljaniec gravatar image ljaniec  ( 2022-07-22 03:51:19 -0500 )edit

Question Tools

1 follower


Asked: 2022-07-21 11:05:47 -0500

Seen: 167 times

Last updated: Jul 21 '22