yashar_turtle_chaseο
Dynamic leader-follower turtlesim demo with leader switching service
READMEο
This package and README file is written for the Robotics-Lab HW2. Yashar Zafari - Student Number: 99106209, Sharif University of Technology. email
π’ yashar_turtle_chaseο
A dynamic leader-follower demo using ROS2βs turtlesim
, where multiple turtles can follow a designated leader with real-time leader switching capability.
π¦ Overviewο
This package demonstrates a multi-turtle simulation where:
Multiple turtles are spawned in the
turtlesim
environment.One turtle acts as the leader, controllable via keyboard.
Other turtles follow the leader using proportional control.
The leader can be switched dynamically at runtime.
π§° Featuresο
Dynamic Turtle Spawning: Automatically spawns a specified number of turtles at random, non-overlapping positions.
Leader-Follower Mechanism: Implements a simple proportional controller for followers to track the leaderβs position.
Real-Time Leader Switching: Change the leader turtle during runtime using a ROS 2 topic.
Launch File: Convenient launch file to start the entire setup, including
turtlesim
, turtle spawner, follower logic, and teleoperation.
π Installationο
Clone the Repository:
cd ~/ros2_ws/src git clone https://github.com/your_username/yashar_turtle_chase.git
Install Dependencies:
cd ~/ros2_ws rosdep install --from-paths src --ignore-src -r -y
Build the Package:
colcon build --packages-select yashar_turtle_chase source install/setup.bash
π§ͺ Usageο
Launch the Simulationο
ros2 launch yashar_turtle_chase dynamic_chase.launch.py
This will:
Start the
turtlesim_node
.Spawn the specified number of turtles (default is 2).
Initiate the dynamic follower node.
Teleoperate the leader in the command window opened.
Switch the leader in another terminal.
Control the Leader Turtleο
Use the keyboard in the teleoperation terminal to move the leader turtle.
Switch the Leader Turtleο
Publish a new leader name to the /switch_leader
topic:
ros2 topic pub /switch_leader std_msgs/String "{data: 'turtle2'}"
Replace 'turtle2'
with the desired turtle name (e.g., 'turtle1'
, 'turtle3'
, etc.).
π§ Implementation Detailsο
Follower Logicο
The dynamic_follower.py
node:
Subscribes to the
/turtleX/pose
topics to get the positions of all turtles.Publishes velocity commands to
/turtleY/cmd_vel
for follower turtles.Calculates the distance and angle to the leader and applies proportional control to follow.
Leader Detectionο
The current leader is stored as a variable within the node. It can be updated at runtime by publishing to the /switch_leader
topic.
Challenges Facedο
Dynamic Spawning: Ensuring turtles spawn at non-overlapping positions required implementing a check against existing positions.
Real-Time Leader Switching: Maintaining smooth transitions when changing leaders without disrupting follower behavior.
πΉ Demonstrationο
https://github.com/user-attachments/assets/0013b428-5731-40c6-85f9-7b0369217965
In the demo, observe how the follower turtle tracks the leader and how switching the leader affects the behavior.
π Licenseο
This project is licensed under the Apache License 2.0.
For more information on ROS 2 and turtlesim
, refer to the official ROS 2 tutorials.