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

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 image William  ( 2019-01-17 19:39:21 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
4

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

lucasw gravatar image

updated 2023-04-07 01:58:03 -0500

130s gravatar image

UPDATE by @130s: 4+ years later, there's an official tutorial (docs.ros.org/en/rolling) made that lays out what this answer is explaining.


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 image mkhansen  ( 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 image lucasw  ( 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 image lucasw  ( 2018-11-27 08:53:52 -0500 )edit

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

lucasw gravatar image lucasw  ( 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 image William  ( 2019-01-17 19:42:18 -0500 )edit

I don't quite understand this example. What is ThisLaunchFileDir()?

M@t gravatar image M@t  ( 2021-04-28 16:45:04 -0500 )edit

Were you ever able to get the value of the argument during the execution of the launch file?

morten gravatar image morten  ( 2021-09-30 05:06:42 -0500 )edit

Marked as answer. Years later I found a tutorial that essentially explains the same as this answer. To honor the answer I added a link to the body of the answer instead of adding a new answer.

130s gravatar image 130s  ( 2023-04-07 01:59:48 -0500 )edit

Question Tools

2 followers

Stats

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

Seen: 11,473 times

Last updated: Apr 07 '23