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

ros2 launch: How to select parameters yaml file depending on parameter?

asked 2022-02-07 12:52:44 -0500

tompe17 gravatar image

updated 2022-02-07 12:53:25 -0500

I want to use a parameter (ns) to select the yaml file to load for parameters. I have a hard time finding how to do it... Is there a better way to do it?

I want to do something like:


def generate_launch_description():
    ns_arg = DeclareLaunchArgument("ns", default_value=TextSubstitution(text="/dji0"))
    location_arg = DeclareLaunchArgument("location", default_value=TextSubstitution(text="granso"))

    location_config = os.path.join(
        get_package_share_directory('lrs_wdb'),
        'config',
        f'location_{LaunchConfiguration('location')}.yaml'
    )

    return LaunchDescription([
        ns_arg,        
        Node(
            package='lrs_wdb',
            output='screen',
            emulate_tty=True,            
            namespace=LaunchConfiguration('ns'),
            executable='wdb',
            name='wdb',
            parameters=[location_config]
        )
    ])

But this is not working since "LaunchConfiguration('ns')" is an object. And googling it I did not find a strightforward way to get the value. So any suggestions how to solve this? I could use an environment variable I suppose but I do not think that is a good solution since for example ns is specified with a parameter.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-02-09 19:48:22 -0500

ChuiV gravatar image

With launch substitutions, you can build strings like this with tuples. Sure there's more elegant ways to create the tuple, but this works.

location_config = (get_package_share_directory('lrs_wdb'),
                   '/config/location_',
                   LaunchConfiguration('location'), '.yaml')

return LaunchDescription([
    Node(package='lrs_wdb',
         executable='wdb',
         parameters=[location_config]
    )
])
edit flag offensive delete link more

Comments

Thanks, it worked. Is this documented somewhere that you can use a tuple? Or is it some Python standard?

tompe17 gravatar image tompe17  ( 2022-02-11 11:40:36 -0500 )edit

If it is documented, I don't know where. I can say that this feature comes from the launch.utilities.perform_substitutions_impl.py file.

ChuiV gravatar image ChuiV  ( 2022-02-11 13:41:51 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-02-07 12:52:44 -0500

Seen: 544 times

Last updated: Feb 09 '22