Using ROS 2? Check out the ROS 2 tf2 tutorials.

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 tf2

Description: This tutorial will give you a good idea of what tf2 can do for you. It shows off some of the tf2 power in a multi-robot example using turtlesim. This also introduces using tf2_echo, view_frames, and rviz.

Keywords: transforms, coordinate frames

Tutorial Level: BEGINNER

Next Tutorial: Writing a tf2 broadcaster (Python) (C++)

Installing the Demo

Let's start by getting any dependencies and compiling the demo package.

$ sudo apt-get install ros-${ROS_DISTRO}-turtle-tf2 ros-${ROS_DISTRO}-tf2-tools ros-${ROS_DISTRO}-tf

Running the Demo

Now that we're done compiling the turtle_tf2 tutorial package let's run the demo.

$ roslaunch turtle_tf2 turtle_tf2_demo.launch

You will see the turtlesim start with two turtles.

  • turtle_tf_start.png

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.

  • turtle_tf_drive.png

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 tf2 library to create three coordinate frames: a world frame, a turtle1 frame, and a turtle2 frame. This tutorial uses a tf2 broadcaster to publish the turtle coordinate frames and a tf2 listener to compute the difference in the turtle frames and move one turtle to follow the other.

tf2 Tools

Now let's look at how tf2 is being used to create this demo. We can use tf2 tools to look at what tf2 is doing behind the scenes.

Using view_frames

view_frames creates a diagram of the frames being broadcast by tf2 over ROS.

$ rosrun tf2_tools view_frames.py

You will see:

  • Listening to tf data during 5 seconds...
    Generating graph in frames.pdf file...

Here a tf2 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
  • view_frames_2.png

Here we can see the three frames that are broadcast by tf2 the world, turtle1, and turtle2 and that world is the parent of the turtle1 and turtle2 frames. view_frames also report some diagnostic information about when the oldest and most recent frame transforms were received and how fast the tf2 frame is published to tf2 for debugging purposes.

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

latex error! exitcode was 2 (signal 0), transscript follows:

[Fri Apr 19 18:41:13.788517 2024] [:error] [pid 4893] failed to exec() latex
:

$ rosrun tf tf_echo turtle1 turtle2

You will see the transform displayed as the tf_echo listener receives the frames broadcast over ROS.

  • [ INFO] 1253585683.529245000: Started node [/tf2_echo_1253585683508144000], pid [24418], bound on [aqy], xmlrpc port [41125], tcpros port [57369], logging to [~/ros/ros/log/tf2_echo_1253585683508144000_24418.log], using [real] time
    Exception thrown:Frame id /turtle1 does not exist! When trying to transform between /turtle2 and /turtle1.
    The current list of frames is:
    
    Success at 1253585684.557003974
    [0.000000 0.000000 0.140754 0.990045] Euler(0.282446 -0.000000 0.000000)
    Translation: [-0.000036 -0.000010 0.000000]
    Success at 1253585685.544698953
    [0.000000 0.000000 0.140754 0.990045] Euler(0.282446 -0.000000 0.000000)
    Translation: [-0.000036 -0.000010 0.000000]
    Success at 1253585686.557049989
    [0.000000 0.000000 0.140754 0.990045] Euler(0.282446 -0.000000 0.000000)
    Translation: [-0.000036 -0.000010 0.000000]
    Success at 1253585687.552628993
    [0.000000 0.000000 0.140754 0.990045] Euler(0.282446 -0.000000 0.000000)
    Translation: [-0.000036 -0.000010 0.000000]
    Success at 1253585688.553683042
    [0.000000 0.000000 0.140754 0.990045] Euler(0.282446 -0.000000 0.000000)
    Translation: [-0.000036 -0.000010 0.000000]
    Success at 1253585688.910640001
    [0.000000 0.000000 0.140754 0.990045] Euler(0.282446 -0.000000 0.000000)
    Translation: [-0.000036 -0.000010 0.000000]

As you drive your turtle around you will see the transform change as the two turtles move relative to each other.

rviz and tf2

rviz is a visualization tool that is useful for examining tf2 frames. Let's look at our turtle frames using rviz. Let's start rviz with the turtle_tf2 configuration file using the -d option for rviz:

$ rosrun rviz rviz -d `rospack find turtle_tf2`/rviz/turtle_rviz.rviz

attachment:turtle_tf_rviz.png

In the side bar you will see the frames broadcast by tf2. As you drive the turtle around you will see the frames move in rviz.

Now that we have examined the turtle_tf2_demo, let's look at how to write the broadcaster (Python) (C++) for this demo.

Wiki: tf2/Tutorials/Introduction to tf2 (last edited 2022-10-06 16:51:29 by ShaneLoretz)