Want to launch another launch file when the first launch file is closed

asked 2023-06-01 09:39:16 -0500

hydro gravatar image

Hi everyone, this is my first time posting on an online forum for answer, so please bear with me.

As the title suggests, when a node is killed, I want the launch file to launch (possibly) the same launch file again, using the RegisterEventHandler action, here is my code for now.

lifecycle_launch.py

def generate_launch_description():

life_node = LifecycleNode(package='lifecycle', executable='lifecycle_talker',
                  name='lc_talker', namespace='', output='screen')

def launch_setup(context, *args, **kwargs):
    return [ ExecuteProcess(
        cmd=[[
            'ros2 launch lifecycle_py test_launch.py'
        ]], shell=True
    ),
    ]

return LaunchDescription([
    life_node,
    RegisterEventHandler(
        OnShutdown(
            on_shutdown=[
                LogInfo(msg='reaches here'),
                OpaqueFunction(function= launch_setup)
            ]
        )
    ), ])

when the program is running and I interrupt using a Ctrl+C, here is the terminal output that I get

 [INFO] [launch]: All log files can be found below /home/amongsus/.ros/log/2023-06-01-10-10-27-092501-computer0-96952
 [INFO] [launch]: Default logging verbosity is set to INFO
 [INFO] [lifecycle_talker-1]: process started with pid [96966]
 ^C[WARNING] [launch]: user interrupted with ctrl-c (SIGINT)
 [INFO] [launch.user]: reaches here
[lifecycle_talker-1] [INFO] [1685628630.993959613] [rclcpp]: signal_handler(signum=2)
[INFO] [lifecycle_talker-1]: process has finished cleanly [pid 96966]

when I do a

ros2 node list

I don't get any output meaning the lauch file wasn't launched. When I replace OnShutdown with OnProcessExit the results are on different, ie. I don't get another launch file. However OnProcessStartdoes indeed launch the file, but it closes as soon as I do a Ctrl+C, on the window meant for lifecycle_launch.py.

Here is the file test_launch.py if that is relevant.

from launch import LaunchDescription
from launch_ros.actions import LifecycleNode
from launch_ros.actions import Node
from launch.event_handlers import (OnExecutionComplete, OnProcessExit,
                                OnProcessIO, OnProcessStart, OnShutdown)
from launch.events import Shutdown
from launch.actions import (DeclareLaunchArgument, EmitEvent, ExecuteProcess,
                            LogInfo, RegisterEventHandler, TimerAction, OpaqueFunction)




def generate_launch_description():

    life_node = LifecycleNode(package='lifecycle', executable='lifecycle_talker',
                    name='lc_talker_1', namespace='', output='screen')

    return LaunchDescription([
        life_node
])

System specs:- Ubuntu 20.04

edit retag flag offensive close merge delete