Note: This tutorial assumes you have completed the writing a tf listener tutorial (Python) (C++).
(!) 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.

Adding a frame (C++)

Description: This tutorial teaches you how to add an extra fixed frame to tf.

Tutorial Level: BEGINNER

Next Tutorial: tf and time (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.

In the previous tutorials we recreated the turtle demo by adding a tf broadcaster and a tf listener. This tutorial will teach you how to add an extra frame to the tf tree. This is very similar to creating the tf broadcaster, and will show some of the power of tf.

Why adding frames

For many tasks it is easier to think inside a local frame, e.g. it is easier to reason about a laser scan in a frame at the center of the laser scanner. tf allows you to define a local frame for each sensor, link, etc in your system. And, tf will take care of all the extra frame transforms that are introduced.

Where to add frames

tf builds up a tree structure of frames; it does not allow a closed loop in the frame structure. This means that a frame only has one single parent, but it can have multiple children. Currently our tf tree contains three frames: world, turtle1 and turtle2. The two turtles are children of world. If we want to add a new frame to tf, one of the three existing frames needs to be the parent frame, and the new frame will become a child frame.

  • tree.png

How to add a frame

In our turtle example, we'll add a new frame to the first turtle. This frame will be the "carrot" for the second turtle.

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

 $ roscd learning_tf

The Code

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

   1 #include <ros/ros.h>
   2 #include <tf/transform_broadcaster.h>
   3 
   4 int main(int argc, char** argv){
   5   ros::init(argc, argv, "my_tf_broadcaster");
   6   ros::NodeHandle node;
   7 
   8   tf::TransformBroadcaster br;
   9   tf::Transform transform;
  10 
  11   ros::Rate rate(10.0);
  12   while (node.ok()){
  13     transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) );
  14     transform.setRotation( tf::Quaternion(0, 0, 0, 1) );
  15     br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1"));
  16     rate.sleep();
  17   }
  18   return 0;
  19 };

The code is very similar to the example in the tf broadcaster tutorial.

The Code Explained

Let's take a look at the key line in this piece of code:

  13     transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) );
  14     transform.setRotation( tf::Quaternion(0, 0, 0, 1) );
  15     br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1"));

Here we create a new transform, from the parent turtle1 to the new child carrot1. The carrot1 frame is 2 meters offset to the left from the turtle1 frame.

Running the frame broadcaster

Now that we created the code, lets compile it first. Open the CMakeLists.txt file, and add the following line on the bottom:

add_executable(frame_tf_broadcaster src/frame_tf_broadcaster.cpp)
target_link_libraries(frame_tf_broadcaster ${catkin_LIBRARIES})

Build your package at the top folder of your catkin workspace:

 $ catkin_make

  rosbuild_add_executable(frame_tf_broadcaster src/frame_tf_broadcaster.cpp)

Build your package:

 $ make

If everything went well, you should have a binary file called frame_tf_broadcaster in your bin folder. If so, we're ready to edit the start_demo.launch launch file. Simply merge the node block below inside the launch block::

  <launch>
    ...
    <node pkg="learning_tf" type="frame_tf_broadcaster"
          name="broadcaster_frame" />
  </launch>

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

 $ roslaunch learning_tf start_demo.launch

Checking the results

So, if you drive the first turtle around, you notice that the behavior didn't change from the previous tutorial, even though we added a new frame. That's because adding an extra frame does not affect the other frames, and our listener is still using the previously defined frames. So, let's change the behavior of the listener.

Open the src/turtle_tf_listener.cpp file, and simple replace "/turtle1" with "/carrot1" in lines 26-27:

   1   listener.lookupTransform("/turtle2", "/carrot1",
   2                            ros::Time(0), transform);

And now the good part: just rebuild and restart the turtle demo, and you'll see the second turtle following the carrot instead of the first turtle! Remember that the carrot is 2 meters to the left of turtle1. There is no visual representation for the carrot, but you should see the second turtle moving to that point.

 $ catkin_make
 $ roslaunch learning_tf start_demo.launch

 $ make
 $ roslaunch learning_tf start_demo.launch

Broadcasting a moving frame

The extra frame we published in this tutorial is a fixed frame that doesn't change over time in relation to the parent frame. However, if you want to publish a moving frame you can change the broadcaster to change over time. Let's modify the /carrot1 frame to change relative to /turtle1 over time.

   1     transform.setOrigin( tf::Vector3(2.0*sin(ros::Time::now().toSec()), 2.0*cos(ros::Time::now().toSec()), 0.0) );
   2     transform.setRotation( tf::Quaternion(0, 0, 0, 1) );

And now the good part: just rebuild and restart the turtle demo, and you'll see the second turtle following a moving carrot.

 $ catkin_make
 $ roslaunch learning_tf start_demo.launch

You're now ready to move to the next tutorial about tf and time (Python) (C++)

Wiki: tf/Tutorials/Adding a frame (C++) (last edited 2021-04-01 07:43:15 by FelixvonDrigalski)