Note: This tutorial assumes you have a working knowledge of compiling programs in ROS. Check out the ROS tutorials to learn how to do this. |
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Introduction to tf
Description: This tutorial will give you a good idea of what tf can do for you. It shows off some of the tf power in a multi-robot example using turtlesim. This also introduces using tf_echo, view_frames, rqt_tf_tree, and rviz.Keywords: transforms, coordinate frames
Tutorial Level: BEGINNER
Next Tutorial: Writing a tf broadcaster (Python) (C++)
tf is deprecated in favor of tf2. tf2 provides a superset of the functionality of tf and is actually now the implementation under the hood. If you're just learning now it's strongly recommended to use the tf2/Tutorials instead.
Contents
Select which version of this tutorial you want: Show EOL distros:
Select which buildsystem you are using:
Set Up the Demo
The nodes for this tutorial are released for Ubuntu, so go ahead and install them:
$ sudo apt-get install ros-$ROS_DISTRO-ros-tutorials ros-$ROS_DISTRO-geometry-tutorials ros-$ROS_DISTRO-rviz ros-$ROS_DISTRO-rosbash ros-$ROS_DISTRO-rqt-tf-tree
Let's start by getting any necessary packages and dependencies and compiling the demo package.
$ sudo apt-get install ros-$ROS_DISTRO-geometry-tutorials $ rosdep install turtle_tf rviz $ rosmake turtle_tf rviz
Running the Demo
Now that we're done getting the turtle_tf tutorial package, let's run the demo.
$ roslaunch turtle_tf turtle_tf_demo.launch
You will see the turtlesim start with two turtles.
(in case you experience that a process dies, a workaround is available.)
Once the turtlesim is started you can drive the center turtle around in the turtlesim using the keyboard arrow keys, select the roslaunch terminal window so that your keystrokes will be captured to drive the turtle.
As you can see that one turtle will continuously move to follow the turtle you are driving around.
What is Happening
This demo is using the tf library to create three coordinate frames: a world frame, a turtle1 frame, and a turtle2 frame. This tutorial uses a tf broadcaster to publish the turtle coordinate frames and a tf listener to compute the difference in the turtle frames and move one turtle to follow the other.
tf Tools
Now let's look at how tf is being used to create this demo. We can use tf tools to look at what tf is doing behind the scenes.
Using view_frames
view_frames creates a diagram of the frames being broadcast by tf over ROS.
$ rosrun tf view_frames
You will see:
Transform Listener initing Listening to /tf for 5.000000 seconds Done Listening dot - Graphviz version 2.16 (Fri Feb 8 12:52:03 UTC 2008) Detected dot version 2.16 frames.pdf generated
Here a tf listener is listening to the frames that are being broadcast over ROS and drawing a tree of how the frames are connected. To view the tree:
$ evince frames.pdf
Here we can see the three frames that are broadcast by tf: the world, turtle1, and turtle2. We can also see that world is the parent of the turtle1 and turtle2 frames. For debugging purposes, view_frames also reports some diagnostic information about when the oldest and most recent frame transforms were received and how fast the tf frame is published to tf.
Using rqt_tf_tree
rqt_tf_tree is a runtime tool for visualizing the tree of frames being broadcast over ROS. You can refresh the tree simply by the refresh bottom in the top-left corner of the diagram.
Usage:
rosrun rqt_tf_tree rqt_tf_tree
Or simply:
rqt &
Then choose rqt_tf_tree from Plugins tab.
Using tf_echo
tf_echo reports the transform between any two frames broadcast over ROS.
Usage:
rosrun tf tf_echo [reference_frame] [target_frame]
Let's look at the transform of the turtle2 frame with respect to turtle1 frame which is equivalent to the product of the transform from turtle1 to the world multiplied by the transform from the world to turtle2 frame.
$ rosrun tf tf_echo turtle1 turtle2
You will see the transform displayed as the tf_echo listener receives the frames broadcast over ROS.
At time 1416409795.450 - Translation: [0.000, 0.000, 0.000] - Rotation: in Quaternion [0.000, 0.000, 0.914, 0.405] in RPY [0.000, -0.000, 2.308] At time 1416409796.441 - Translation: [0.000, 0.000, 0.000] - Rotation: in Quaternion [0.000, 0.000, 0.914, 0.405] in RPY [0.000, -0.000, 2.308] At time 1416409797.450 - Translation: [0.000, 0.000, 0.000] - Rotation: in Quaternion [0.000, 0.000, 0.914, 0.405] in RPY [0.000, -0.000, 2.308] At time 1416409798.441 - Translation: [0.000, 0.000, 0.000] - Rotation: in Quaternion [0.000, 0.000, 0.914, 0.405] in RPY [0.000, -0.000, 2.308] At time 1416409799.433 - Translation: [0.000, 0.000, 0.000] - Rotation: in Quaternion [0.000, 0.000, 0.691, 0.723] in RPY [0.000, -0.000, 1.526]
As you drive your turtle around you will see the transform change as the two turtles move relative to each other.
rviz and tf
rviz is a visualization tool that is useful for examining tf frames. Let's look at our turtle frames using rviz. Let's start rviz with the turtle_tf configuration file using the -d option for rviz:
$ rosrun rviz rviz -d `rospack find turtle_tf`/rviz/turtle_rviz.rviz
$ rosrun rviz rviz -d `rospack find turtle_tf`/rviz/turtle_rviz.vcg
In the side bar you will see the frames broadcast by tf. As you drive the turtle around you will see the frames move in rviz.
Now that we have examined the turtle_tf_demo, let's look at how to write the broadcaster (Python) (C++) for this demo.