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

Note: This tutorial assumes you have completed the writing a tf2 broadcaster tutorial (Python) (C++).
(!) Please ask about problems and questions regarding this tutorial on Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Writing a tf2 listener (C++)

Description: This tutorial teaches you how to use tf2 to get access to frame transformations.

Tutorial Level: BEGINNER

Next Tutorial: Adding a frame (C++)

In the previous tutorials we created a tf2 broadcaster to publish the pose of a turtle to tf2. In this tutorial we'll create a tf2 listener to start using tf2.

How to create a tf2 listener

Let's first create the source files. Go to the package we created in the previous tutorial:

 $ roscd learning_tf2

The Code

Fire up your favorite editor and paste the following code into a new file called src/turtle_tf2_listener.cpp.

   1 #include <ros/ros.h>
   2 #include <tf2_ros/transform_listener.h>
   3 #include <geometry_msgs/TransformStamped.h>
   4 #include <geometry_msgs/Twist.h>
   5 #include <turtlesim/Spawn.h>
   7 int main(int argc, char** argv){
   8   ros::init(argc, argv, "my_tf2_listener");
  10   ros::NodeHandle node;
  12   ros::service::waitForService("spawn");
  13   ros::ServiceClient spawner =
  14     node.serviceClient<turtlesim::Spawn>("spawn");
  15   turtlesim::Spawn turtle;
  16   turtle.request.x = 4;
  17   turtle.request.y = 2;
  18   turtle.request.theta = 0;
  19 = "turtle2";
  22   ros::Publisher turtle_vel =
  23     node.advertise<geometry_msgs::Twist>("turtle2/cmd_vel", 10);
  25   tf2_ros::Buffer tfBuffer;
  26   tf2_ros::TransformListener tfListener(tfBuffer);
  28   ros::Rate rate(10.0);
  29   while (node.ok()){
  30     geometry_msgs::TransformStamped transformStamped;
  31     try{
  32       transformStamped = tfBuffer.lookupTransform("turtle2", "turtle1",
  33                                ros::Time(0));
  34     }
  35     catch (tf2::TransformException &ex) {
  36       ROS_WARN("%s",ex.what());
  37       ros::Duration(1.0).sleep();
  38       continue;
  39     }
  41     geometry_msgs::Twist vel_msg;
  43     vel_msg.angular.z = 4.0 * atan2(transformStamped.transform.translation.y,
  44                                     transformStamped.transform.translation.x);
  45     vel_msg.linear.x = 0.5 * sqrt(pow(transformStamped.transform.translation.x, 2) +
  46                                   pow(transformStamped.transform.translation.y, 2));
  47     turtle_vel.publish(vel_msg);
  49     rate.sleep();
  50   }
  51   return 0;
  52 };

The Code Explained

Now, let's take a look at the code that is relevant to publishing the turtle pose to tf2.

 #include <tf2_ros/transform_listener.h>

The tf2 package provides an implementation of a TransformListener to help make the task of receiving transforms easier. To use the TransformListener, we need to include the tf2/transform_listener.h header file.

 tf2_ros::Buffer tfBuffer;
 tf2_ros::TransformListener tfListener(tfBuffer);

Here, we create a TransformListener object. Once the listener is created, it starts receiving tf2 transformations over the wire, and buffers them for up to 10 seconds. The TransformListener object should be scoped to persist otherwise its cache will be unable to fill and almost every query will fail. A common method is to make the TransformListener object a member variable of a class.

   transformStamped = tfBuffer.lookupTransform("turtle2", "turtle1",
 catch (tf2::TransformException &ex) {

Here, the real work is done, we query the listener for a specific transformation. Let's take a look at the four arguments (full details of lookupTransform() found here.)

  1. We want the transform to this frame (target frame) ...
  2. ... from this frame (source frame).
  3. The time at which we want to transform. Providing ros::Time(0) will just get us the latest available transform.

  4. Duration before timeout. (optional, default=ros::Duration(0.0))

All this is wrapped in a try-catch block to catch possible exceptions.

Running the listener

Now that we created the code, lets compile it first. Open the CMakeLists.txt file and insert :

add_executable(turtle_tf2_listener src/turtle_tf2_listener.cpp)

If everything went well, you should have a binary file called turtle_tf2_listener in your bin folder. If so, we're ready add it the launch file for this demo. With your text editor, open the launch file called start_demo.launch, and merge the node block below inside the <launch> block:

    <node pkg="learning_tf2" type="turtle_tf2_listener"
          name="listener" />

First, make sure you stopped the launch file from the previous tutorial (use ctrl-c). Now you're ready to start your full turtle demo:

 $ roslaunch learning_tf2 start_demo.launch

You should see the turtle sim with two turtles.

Checking the results

To see if things work, simply drive around the first turtle using the arrow keys (make sure your terminal window is active, not your simulator window), and you'll see the second turtle following the first one!

When the turtlesim starts up you may see:

  • [ERROR] [1418082761.220546623]: "turtle2" passed to lookupTransform argument target_frame does not exist.
    [ERROR] [1418082761.320422000]: "turtle2" passed to lookupTransform argument target_frame does not exist.

This happens because our listener is trying to compute the transform before turtle 2 is spawned in turtlesim and broadcasting a tf2 frame.

Transforming poses, points, etc. from frame to frame

Now that you know the transform from frame "turtle1" to frame "turtle2", you can transform vectors from one to the other. See (Using Stamped datatypes with tf2_ros::MessageFilter)

Now you're ready to move on to the next tutorial, where you'll learn how to add a frame (Python) (C++)

Wiki: tf2/Tutorials/Writing a tf2 listener (C++) (last edited 2022-10-06 16:55:15 by ShaneLoretz)