Skip to content

Commit

Permalink
Update for cleaner shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Sep 14, 2024
1 parent 87efb98 commit f3f43c6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
30 changes: 21 additions & 9 deletions launch_mvsim.launch.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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'))
]
)
)
])
14 changes: 9 additions & 5 deletions robot_commander.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()

Expand Down

0 comments on commit f3f43c6

Please sign in to comment.