Ask Your Question
0

ros2 include a launch file from a launch file

asked 2018-10-28 08:21:27 -0500

lucasw gravatar image

updated 2018-10-28 08:53:12 -0500

I see https://github.com/ros2/launch/blob/m...

:class:launch.actions.IncludeLaunchDescription This action will include another launch description as if it had been copy-pasted to the location of the include action.

Can this be used within a launch.py something like this:

def generate_launch_description():
    included_launch = launch_ros.actions.IncludeLaunchDescription(package='foo', launch='my_launch.py', arguments=[...])  
    regular_node = launch_ros.actions.Node( package='bar', node_executable='my_node', output='screen')

    return launch.LaunchDescription([
        included_launch,
        regular_node,
    ])
edit retag flag offensive close merge delete

Comments

Yes, that should work.

William gravatar imageWilliam ( 2019-01-17 19:39:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-10-28 08:41:48 -0500

lucasw gravatar image

updated 2019-01-17 15:46:31 -0500

This works but I don't see a way to get arguments in:

from ament_index_python.packages import get_package_share_directory
...
foo_dir = get_package_share_directory('foo')
included_launch = launch.actions.IncludeLaunchDescription(
        launch.launch_description_sources.PythonLaunchDescriptionSource(
                foo_dir + '/launch/my_launch.py'))
...
return launch.LaunchDescription([
    included_launch,
    regular_node,
])

Crystal updates

launch file arguments

I found an example in https://github.com/ros2/launch of including a launch file and passing in arguments https://github.com/ros2/launch/tree/m...

Here is getting a launch file argument from https://github.com/lucasw/ros2_cpp_py which is adapted from the launch example:

print("{}".format(launch.substitutions.LaunchConfiguration('test')))

return LaunchDescription([
    launch.actions.DeclareLaunchArgument(
        'test',
        default_value='default_value',
        description='test'),
    launch_ros.actions.Node(
        package='ros2_cpp_py', node_executable='cpp_test', output='screen',
        node_name=launch.substitutions.LaunchConfiguration('test'),
        ),
])

Then this results in a node named 'default_value':

ros2 launch ros2_cpp_py example.launch.py

And this names the node 'foo':

ros2 launch ros2_cpp_py example.launch.py test:=foo

What I need to be able to do is get the value of the argument during the execution of the launch file, but it comes out as:

print("{}".format(launch.substitutions.LaunchConfiguration('test')))

Results in:

<launch.substitutions.launch_configuration.LaunchConfiguration object at 0x7f0db4b44a58>

Maybe it has a method that returns the value- the perform() method wants a LaunchContext but I'm not sure where to get it.

including another launch file

def generate_launch_description():
    """Launch the example.launch.py launch file."""
    return LaunchDescription([
        launch.actions.DeclareLaunchArgument(
            'test',
            default_value='different_default_value',
            description='test arg that overlaps arg in included file',
            ),
        LogInfo(msg=[
            'Including launch file located at: ', ThisLaunchFileDir(), '/example.launch.py'
        ]),
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource([ThisLaunchFileDir(), '/example.launch.py']),
            launch_arguments={'node_name': 'bar'}.items(),
        ),
    ])

I've made a new launch argument with the same name as in the included launch file, it appears the top level one supersedes it. It is also possible to set an arg in an included launch file regardless of whether the top level launch exposes it or not. Large hierarchies of of includes have to be careful not to have same named but different meaning arguments, they'll get overwritten.

ros2 launch ros2_cpp_py includes_example.launch.py
edit flag offensive delete link more

Comments

@lucasw - did you get arguments to work? I can't seem to get them passed through to an included launch file either

mkhansen gravatar imagemkhansen ( 2018-11-27 08:22:22 -0500 )edit

I haven't tried it but sys.argv should still be valid in the included file, so argparse would work- so independent arguments could be passed in as long as the two launch files don't have same named args that need different values.

lucasw gravatar imagelucasw ( 2018-11-27 08:50:14 -0500 )edit

That method wouldn't allow the first launch script setting the args for the included files (except by modifying sys.argv, which is starting to get really hacky), which is something I did very frequently in ros1 launch files.

lucasw gravatar imagelucasw ( 2018-11-27 08:53:52 -0500 )edit

Updated answer with crystal specific launch file including and argument setting.

lucasw gravatar imagelucasw ( 2019-01-17 15:45:29 -0500 )edit
1

Let me see if I can put together a simple example. It's possible there's a feature missing somewhere that prevent this, though my recollection is that it should be possible.

William gravatar imageWilliam ( 2019-01-17 19:42:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2018-10-28 08:21:27 -0500

Seen: 435 times

Last updated: Jan 17