In this post, we will see how to make a robot follow another robot. We’ll make the iRobot follow the big turtle all around the world when it moves, using ROS TF broadcaster and listener nodes.
PS: This ROS project is part of our ROS Mini Challenge series, which gives you an opportunity to win an amazing ROS Developers T-shirt! This challenge is already solved. For updates on future challenges, please stay tuned to our Twitter channel.
Step 1: Grab a copy of the ROS Project containing the code for the challenge
Click here to get your own copy of the project. If you don’t have an account on the ROS Development Studio, you will need to create one. Once you create an account or log in, we will copy the project to your workspace. That done, open your ROSject using the Open button. This might take a few moments, please be patient.
You should now see a notebook with detailed instructions about the challenge. This post includes a summary of these instructions as well as the solution to the challenge.
PS: Please ignore the Claim your Prize! section because…well…you are late the party 🙂
Step 2: Start the Simulation and get the robots moving
- Click on the Simulations menu and then Choose launch file… . In the dialog that appears, select rotw7.launch under turtle_tf_3d package. Then click the Launch button. You should see a Gazebo window popup showing the simulation.
- Get the robots moving. Pick a Shell from the Tools menu and run the following commands:
user:~$ source ~/catkin_ws/devel/setup.bash user:~$ roslaunch rotw7_pkg irobot_follow_turtle.launch
At this point, you should already see the iRobot moving towards the big turtle.
Nothing happened? Heck, we gotta fix this! Let’s do that in the next section.
Step 3: Let’s find the problem
So the robots didn’t move as we expected. And we had this error message:
[INFO] [1580892397.791963, 77.216000]: Retrieveing Model indexes [INFO] [1580892397.860043, 77.241000]: Robot Name=irobot, is NOT in model_state, trying again
The error message above says it cannot find the model name specified in the code, so let’s check that up. Fire up the IDE from the Tools menu and browse to the directory catkin_ws/src/rotw7_pkg/scripts
. We have two Python scripts in there:
turtle_tf_broadcaster.py
turtle_tf_listener.py
The robot model names are specified on line 19 of turtle_tf_broadcaster.py:
, in the publisher_of_tf
function:
robot_name_list = ["irobot","turtle"]
Let’s check if we can find these robots in the simulation, using a Gazebo service:
user:~$ rosservice call /gazebo/get_world_properties "{}" sim_time: 424.862 model_names: [ground_plane, coke_can, turtle1, turtle2] rendering_enabled: True success: True status_message: "GetWorldProperties: got properties"
So we see that the names we specified are not in the simulation! The robots we need are turtle2
and turtle1
.
Also, on line 19 of turtle_tf_listener.py
, the code is publishing the “cmd_vel” (the topic that moves the robot) of the following robot:
turtle_vel = rospy.Publisher('/cmd_vel', geometry_msgs.msg.Twist,queue_size=1)
But, which of the turtles is the follower, and what is the correct topic for its “cmd_vel”? We have a hint from the launch file irobot_follow_turtle.launch
:
<?xml version="1.0" encoding="UTF-8"?> <launch> <include file="$(find rotw7_pkg)/launch/run_turtle_tf_broadcaster.launch"/> <include file="$(find rotw7_pkg)/launch/run_turtle_tf_listener.launch"> <arg name="model_to_be_followed_name" value="turtle1" /> <arg name="follower_model_name" value="turtle2" /> </include> </launch>
So the follower is turtle2
. Now, let’s check what it’s “cmd_vel” topic is. It’s specified as /cmd_vel
in the code, but is this true? Let’s check the list of topics:
user:~$ rostopic list #... /turtle1/cmd_vel /turtle2/cmd_vel
Probably, it’s /turtle2/cmd_vel
. How do we know? Let’s publish to both /cmd_vel
and /turtle2/cmd_vel
and see which works.
ser:~$ rostopic pub /cmd_vel Display all 152 possibilities? (y or n) user:~$ rostopic pub /turtle2/cmd_vel geometry_msgs/Twist "linear: x: 0.2 y: 0.0 z: 0.0 angular: x: 0.0 y: 0.0 z: 0.0" publishing and latching message. Press ctrl-C to terminate ^Cuser:~$ rostopic pub /turtle2/cmd_vel geometry_msgs/Twist "linr:r x: 0.0 y: 0.0 z: 0.0 angular: x: 0.0 y: 0.0 z: 0.0" publishing and latching message. Press ctrl-C to terminate
Publishing to /turtle2/cmd_vel
works. /cmd_vel
didn’t work.
Step 4: Let’s fix the problem
We saw the problems in Step 3, now let’s implement the fix and test again.
On line 19 of turtle_tf_broadcaster.py
, change the list to reflect the real turtle names:
robot_name_list = ["turtle1","turtle2"]
Also, on line 19 of turtle_tf_listener.py
, change /cmd_vel
to /turtle2/cmd_vel
:
turtle_vel = rospy.Publisher('/turtle2/cmd_vel', geometry_msgs.msg.Twist,queue_size=1)
Now rerun the commands to move the robots:
user:~$ source ~/catkin_ws/devel/setup.bash user:~$ roslaunch rotw7_pkg irobot_follow_turtle.launch
You should now see the iRobot moving towards the big turtle. Now you can start moving the Turtle using the keyboard. Pick another Shell from the Tools menu and run the following command:
user:~$ roslaunch turtle_tf_3d turtle_keyboard_move.launch
Move the big turtle around with the keyboard, and you should see that the iRobot follows it. Done, that’s an example of how to make a robot follow another robot.
Extra: Video of this post
We made a video showing how we solved this challenge and made the iRobot follow another robot. If you prefer “sights and sounds” to “black and white”, here you go:
Related Resources
Feedback
Did you like this post? Do you have any questions about the explanations? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.
If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it.
Hi dear developer,
I have tried to launch the first step ‘rotw7.launch’. However, there is no robot and turtle appear in Gazebo.
Can you give me some suggestion ?
Thanks.