ROS2 Tutorial: Moving to ROS2 Galactic
If you want to thank the author, please use icons on the right next time you shop on Amazon

Making sure Gazebo and RViz are closed

Often, when you close Gazebo, RViz doesn't close, or when you close RViz, Gazebo (either client or VERY often, server) keeps running, or when you press CTRL-C... same thing. Let's fix it.

We are going to add event handlers that, when Gazebo dies, kills RViz, and when RViz dies, kills Gazebo.

            from launch.actions import DeclareLaunchArgument, EmitEvent, RegisterEventHandler
            from launch.event_handlers import OnProcessExit
            from launch.events import Shutdown
            ...

            gazebo_exit_event_handler = RegisterEventHandler(
                event_handler=OnProcessExit(
                    target_action=gazebo,
                    on_exit=EmitEvent(event=Shutdown(reason='gazebo exited'))))
        
            rviz_exit_event_handler = RegisterEventHandler(
                event_handler=OnProcessExit(
                    target_action=rviz_cmd,
                    on_exit=EmitEvent(event=Shutdown(reason='rviz exited'))))
                    
            ...

            ld = LaunchDescription()
            ...

            ld.add_action(gazebo_exit_event_handler)
            ld.add_action(rviz_exit_event_handler)

            ...
        

Very simple.

(C) snowcron.com, all rights reserved

Please read the disclaimer