0
$\begingroup$

When I use the ros launch files I observed a phenomenon that, if I use launch file A to call launch file B, then the function of launchfile B "generate_launch_description" will be called two times, while the ros node is only executed one time.

So here I will provide my two launch files

Launch file A, named launch_example_A.py:

from pathlib import Path
import os
import launch
import launch_ros.actions
from launch import LaunchDescription
from ament_index_python.packages import get_package_share_directory
from launch.substitutions import LaunchConfiguration, PythonExpression, TextSubstitution

from launch.actions import (DeclareLaunchArgument, EmitEvent, ExecuteProcess,
                            LogInfo, RegisterEventHandler, TimerAction)



def generate_launch_description():

    ld = LaunchDescription()
    example_node_A = launch.actions.IncludeLaunchDescription(
        launch.launch_description_sources.PythonLaunchDescriptionSource([os.path.join(
            get_package_share_directory('example_nodes'), 'launch'),
            '/launch_example_B.py'])
        )

    ld.add_action( example_node_A )
    return ld

Launch file B, named launch_example_B.py:

from pathlib import Path
import os
import launch
import launch_ros.actions
from launch import LaunchDescription
from ament_index_python.packages import get_package_share_directory
from launch.substitutions import LaunchConfiguration, PythonExpression, TextSubstitution

from launch.actions import (DeclareLaunchArgument, EmitEvent, ExecuteProcess,
                            LogInfo, RegisterEventHandler, TimerAction)



def generate_launch_description():
    print("start launch file B")
    ld = LaunchDescription()
    example_node_B = launch.actions.IncludeLaunchDescription(
        launch.launch_description_sources.PythonLaunchDescriptionSource([os.path.join(
            get_package_share_directory('example_nodes'), 'launch'),
            '/launch_example_C.py'])
        )

    ld.add_action( example_node_B )
    return ld

When I use the command ros2 launch [package_name] lauch_example_A.py, Then in the terminal I get two prints, it shows like following:

start launch file B
start launch file B

Can someone explain why it looks like this? Thank you so much in advance!

$\endgroup$

2 Answers 2

0
$\begingroup$

Here is an easy, clean way to launch several other launch files, that I know works.

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription
from launch.substitutions import LaunchConfiguration, PathJoinSubstitution, PythonExpression
from launch.launch_description_sources import PythonLaunchDescriptionSource
# from launch_ros.actions import Node
from launch_ros.substitutions import FindPackageShare
# from launch.conditions import IfCondition, UnlessCondition


def generate_launch_description():
    lino2_launch_path = PathJoinSubstitution(
        [FindPackageShare('linorobot2_bringup'), 'launch', 'bringup.launch.py']
    )

    velodyne_launch_path = PathJoinSubstitution(
        [FindPackageShare('velodyne'), 'launch', 'velodyne-all-nodes-VLP16-composed-launch.py']
    )

    rtabmap_launch_path = '/home/rod/rtabmap_slam.launch.py'


    return LaunchDescription([

        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(lino2_launch_path),
            
        ),

        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(velodyne_launch_path),
                      
        ),
        
        IncludeLaunchDescription(
            PythonLaunchDescriptionSource(rtabmap_launch_path),
            
        )

    ])
```
$\endgroup$
1
  • $\begingroup$ Thank you so much for your answer, I have tried with your launch script and it has the same phenomenon. You can try it by yourself. Just print some stuff in bringup.launch.py after the def generate_launch_description():, you will see that it prints two times $\endgroup$
    – xiao yu ge
    Feb 1 at 13:25
0
$\begingroup$

I find one Issue that the ros developer explained it a little bit. It seems like it exits and they consider it as not a bug. https://github.com/ros2/ros2/issues/1283

So finally my solution looks like this: In the launch_example_B.py, I excute my command by using ExecuteProcess

def generate_launch_description():
  # print("start launch file B")
 # The my_cmd can be replaced with some commands which need to be executed.

    ld = LaunchDescription([
                     DeclareLaunchArgument(name='open_gui', default_value='False'),
                     ExecuteProcess(
                         cmd=['my_cmd'],
                         #condition=IfCondition(LaunchConfiguration('open_gui')),
                     ), 
                ])

    # > start example_node_C
    example_node_B = launch.actions.IncludeLaunchDescription(
    launch.launch_description_sources.PythonLaunchDescriptionSource([os.path.join(
        get_package_share_directory('example_nodes'), 'launch'),
        '/launch_example_C.py'])
    )

ld.add_action( example_node_B )

    return ld
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.