ROS2 respawn
Hello ROS2 community,
I'm currently playing around with the best way to respawn a node (ROS2 foxy). My current solution exists of two files (see below). The first file ("test_package_launch.py") defines the generate_launch_description() method. This file is invoked with "ros2 launch test_package test_package_launch.py". Within the launch file the ExecuteProcess invokes the second file ("test_package_respawn.py").
So far everything works fine and also it's working as expected. I just don't like the fact to have two files. Is there a way to do this in a single file?
Thanks Jeremy
test_package_launch.py:
import sys
import os
from launch import LaunchDescription
import launch
from launch.actions.execute_process import ExecuteProcess
from launch_ros.actions import Node
from launch import LaunchService
from ament_index_python.packages import get_package_share_directory
def callback_respawn(event, context):
print("---------------------------------- on exit callback -------------------------------")
respawn_delay = 3
def generate_launch_description():
return LaunchDescription([
ExecuteProcess(
cmd=[sys.executable, "test_package_respawn.py"],
respawn=True, respawn_delay=respawn_delay, on_exit=callback_respawn,
emulate_tty=True,
output="screen"
)
])
test_package_respawn.py:
#!/usr/bin/env python3
import os
import sys
from launch import LaunchDescription
from launch.actions.execute_process import ExecuteProcess
from launch.actions.shutdown_action import Shutdown
from launch.actions.timer_action import TimerAction
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
from launch import LaunchService
config = os.path.join(
get_package_share_directory('test_package'),
'config',
'config.yaml'
)
def launch_node():
return LaunchDescription(
[Node(
package="test_package",
executable="test_package_node",
name="test_package",
output="screen",
emulate_tty=True,
parameters=[config]
)
]
)
def main():
print("------------------------- started node in respawn process -----------------------------")
desc = launch_node()
service = LaunchService()
service.include_launch_description(desc)
service.run()
if __name__ == '__main__':
main()