diff --git a/Dockerfile b/Dockerfile index bf9122b..a52c2ff 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,10 +21,9 @@ WORKDIR /ros2_ws # Build the workspace RUN colcon build -# Source ROS 2 upon starting the container +# Source ROS 2 and setup the environment for the launch file RUN echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc RUN echo "source /ros2_ws/install/setup.bash" >> ~/.bashrc -RUN echo "ros2 launch src/launch_mvsim.launch.py" >> ~/.bashrc -# Default command when starting the container -CMD ["/bin/bash"] +# Default command to launch the ROS 2 simulation +CMD ["bash", "-c", "source /opt/ros/humble/setup.bash && source /ros2_ws/install/setup.bash && ros2 launch src/launch_mvsim.launch.py"] \ No newline at end of file diff --git a/launch_mvsim.launch.py b/launch_mvsim.launch.py index b662611..da35195 100644 --- a/launch_mvsim.launch.py +++ b/launch_mvsim.launch.py @@ -1,12 +1,13 @@ # launch_mvsim.launch.py from launch import LaunchDescription from launch.substitutions import TextSubstitution -from launch.actions import DeclareLaunchArgument -from launch.actions import IncludeLaunchDescription -from launch.launch_description_sources import PythonLaunchDescriptionSource from launch.substitutions import LaunchConfiguration from launch_ros.actions import Node from ament_index_python.packages import get_package_share_directory +from launch.actions import (DeclareLaunchArgument, + EmitEvent, LogInfo, RegisterEventHandler) +from launch.event_handlers import OnProcessExit +from launch.events import Shutdown import os @@ -40,16 +41,27 @@ def generate_launch_description(): }] ) + robot_commander_node = Node( + # package='ros2_ws', # Change this if the script is in another package + executable='/ros2_ws/src/robot_commander.py', + name='robot_commander', + output='screen' + ) + return LaunchDescription([ world_file_launch_arg, headless_launch_arg, do_fake_localization_arg, mvsim_node, - # Node to execute the Python script - Node( - # package='ros2_ws', # Change this if the script is in another package - executable='/ros2_ws/src/robot_commander.py', - name='robot_commander', - output='screen' + robot_commander_node, + RegisterEventHandler( + OnProcessExit( + target_action=robot_commander_node, + on_exit=[ + LogInfo(msg=('robot_commander_node ended')), + EmitEvent(event=Shutdown( + reason='robot_commander_node ended')) + ] + ) ) ]) diff --git a/robot_commander.py b/robot_commander.py index 273f127..3d511b6 100755 --- a/robot_commander.py +++ b/robot_commander.py @@ -78,10 +78,8 @@ def on_timer(self): f"Robot's orientation: {trans.transform.rotation}") elif self.iter == 4: - print("Cleaning up...") - rclpy.shutdown() - else: - print("Run", self.iter) + self.get_logger().info("SHUTTING DOWN") + raise SystemExit except Exception as e: self.get_logger().error( @@ -102,8 +100,14 @@ def scan_callback(self, msg): def main(args=None): rclpy.init(args=args) + node = RobotCommander() - rclpy.spin(node) + + try: + rclpy.spin(node) + except SystemExit: + rclpy.logging.get_logger("Quitting").info('Done') + node.destroy_node() rclpy.shutdown()