A ROS2 package that implements a multi-node control system for turtlesim. This project demonstrates process management, custom service calls, subscriber/publisher patterns, and safety monitoring within the ROS2 ecosystem.
This package allows users to interactively control two turtles in a simulation while a background monitoring process ensures safety. It features:
- ✅ Automatic Setup: Spawns a second turtle immediately upon launch.
- ✅ Interactive UI: A dedicated terminal interface for sending velocity commands.
- ✅ Safety Monitor: A high-priority node that overrides user commands to prevent collisions or boundary breaches.
assignment1_rt/
├── launch/
│ └── assignment1.launch.py # Main launch file (Orchestrator)
├── assignment1_rt/
│ ├── __init__.py
│ ├── ui_node.py # User Interface (Input)
│ ├── distance_monitor.py # Safety Controller (Logic)
│ ├── distance_checks.py # Helper Class (Calculations)
│ └── turtle_spawn.py # Service Client (Setup)
├── package.xml
├── setup.py
└── setup.cfg
Assuming ROS2 Humble and its dependencies are already installed:
- Navigate to your ROS2 workspace
srcfolder:cd ~/ros2_ws/src
- Clone the repository from GitHub:
git clone https://github.com/Belal-Elmikkawy/Assignment1_RT1.git
- Go back to the workspace root and install dependencies:
cd ~/ros2_ws rosdep install --from-paths src --ignore-src -r -y
- Build the package:
colcon build --packages-select assignment1_rt
- Source the setup file:
source install/setup.bash
Run the entire project with:
ros2 launch assignment1_rt assignment1.launch.pyA new terminal window titled ui_node will appear. Use it to control the robots:
- Select Robot:
turtle1orturtle2 - Linear X: Speed forward (+) or backward (-)
- Angular Z: Rotation speed (Clockwise/Counter-Clockwise)
Note: The turtle moves for 1.0 second and then stops automatically.
- Purpose: Provides an interactive terminal interface for the user.
- Functionality:
- Captures user input for selecting the turtle (
turtle1orturtle2). - Accepts velocity commands: Linear X and Angular Z.
- Publishes
geometry_msgs/Twistmessages to the corresponding topic (/turtleX/cmd_vel).
- Captures user input for selecting the turtle (
- Interaction:
- Works independently but sends commands that can be overridden by the safety monitor.
- Purpose: Ensures safe operation of both turtles.
- Functionality:
- Subscribes to
/turtle1/poseand/turtle2/posetopics. - Calculates the Euclidean distance between turtles using helper functions.
- Checks for collisions and boundary violations.
- Publishes zero velocity to turtles if safety conditions are breached.
- Subscribes to
- Interaction:
- Overrides commands from
ui_nodewhen safety limits are exceeded.
- Overrides commands from
- Purpose: Provides reusable functions for distance and boundary calculations.
- Functionality:
- Computes Euclidean distance between two points.
- Validates if a turtle is within safe boundaries.
- Interaction:
- Used internally by
distance_monitor.pyfor logic processing.
- Used internally by
- Purpose: Spawns the second turtle in the simulation.
- Functionality:
- Calls the
/spawnservice provided byturtlesim. - Places
turtle2at coordinates(7.0, 7.0)with a default orientation.
- Calls the
- Interaction:
- Runs once at launch to set up the environment.
- Purpose: The standard ROS2 turtlesim node.
- Functionality:
- Provides the simulation environment for the turtles.
- Offers topics like
/turtleX/cmd_veland/turtleX/posefor control and monitoring.
- Interaction:
- Acts as the base system that other nodes interact with.
The distance_monitor node runs at 10Hz and performs:
- Collision Detection:
- Condition: Distance between
turtle1andturtle2<2.0. - Action: Publishes
0.0velocity to both turtles immediately.
- Condition: Distance between
- Boundary Constraints:
- Condition: Any turtle gets closer than
1.0unit to the wall (0or11). - Action: Stops the specific turtle approaching the wall.
- Condition: Any turtle gets closer than
When a safety breach occurs, a warning is logged:
[WARN] [distance_monitor]: Turtles too close! Dist: 1.45