| Note: This tutorial assumes you have a working knowledge of compiling programs in ROS and you have completed the Introduction to tf2 tutorial. |
Writing a tf2 broadcaster (C++)
Description: This tutorial teaches you how to broadcast coordinate frames of a robot to tf2.Tutorial Level: BEGINNER
Next Tutorial: Writing a tf2 listener (Python) (C++)
Contents
In the next two tutorials we will write the code to reproduce the demo from the tf2 introduction tutorial. After that, the following tutorials focus on extending the demo with more advanced tf2 features.
Before we get started, you need to create a new ros package for this project. In the sandbox folder, create a package called learning_tf2 that depends on tf2, roscpp, rospy and turtlesim:
$ roscd ros_pkg_tutorials $ roscreate-pkg learning_tf2 tf2 roscpp rospy turtlesim $ rosmake learning_tf2
How to broadcast transforms
This tutorial teaches you how to broadcast coordinate frames to tf2. In this case, we want to broadcast the changing coordinate frames of the turtles, as they move around.
Let's first create the source files. Go to the package we just created:
$ roscd learning_tf2
The Code
Create a folder called src/ and fire up your favorite editor to paste the following code into a new file called src/turtle_tf2_broadcaster.cpp.
1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
2 <html><head>
3 <title>404 Not Found</title>
4 </head><body>
5 <h1>Not Found</h1>
6 <p>The requested URL /svn/ros-pkg/branches/trunk_cturtle/stacks/ros_pkg_tutorials/turtle_tf2/src/turtle_tf2_broadcaster.cpp was not found on this server.</p>
7 <hr>
8 <address>Apache/2.2.14 (Ubuntu) Server at code.ros.org Port 443</address>
9 </body></html>
The Code Explained
Now, let's take a look at the code that is relevant to publishing the turtle pose to tf2.
2 <html><head>
The tf2 package provides an implementation of a TransformBroadcaster to help make the task of publishing transforms easier. To use the TransformBroadcaster, we need to include the tf2/transform_broadcaster.h header file.
9 </body></html>
Here, we create a TransformBroadcaster object that we'll use later to send the transformations over the wire.
<<GetCode: execution failed [list index out of range] (see also the log)>>
Here we create a Transform object, and copy the information from the 2D turtle pose into the 3D transform.
<<GetCode: execution failed [list index out of range] (see also the log)>>
This is where the real work is done. Sending a transform with a TransformBroadcaster requires four arguments.
- First, we pass in the transform itself.
Now we need to give the transform being published a timestamp, we'll just stamp it with the current time, ros::Time::now().
Then, we need to pass the name of the parent frame of the link we're creating, in this case "world"
- Finally, we need to pass the name of the child node of the link we're creating, in this case this is the name of the turtle itself.
Note: sendTransform and StampedTransform have opposite ordering of parent and child.
Running the broadcaster
Now that we created the code, lets compile it first. Open the CMakeLists.txt file, and add the following line on the bottom:
rosbuild_add_executable(turtle_tf2_broadcaster src/turtle_tf2_broadcaster.cpp)
and, try to build your package:
$ make
If everything went well, you should have a binary file called turtle_tf2_broadcaster in your bin folder. If so, we're ready to create a launch file for this demo. With your text editor, create a new file called start_demo.launch, and add the following lines:
<launch>
<!-- Turtlesim Node-->
<node pkg="turtlesim" type="turtlesim_node" name="sim"/>
<node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>
<!-- Axes -->
<param name="scale_linear" value="2" type="double"/>
<param name="scale_angular" value="2" type="double"/>
<node pkg="learning_tf2" type="turtle_tf2_broadcaster"
args="/turtle1" name="turtle1_tf2_broadcaster" />
<node pkg="learning_tf2" type="turtle_tf2_broadcaster"
args="/turtle2" name="turtle2_tf2_broadcaster" />
</launch>First, make sure you stopped the launch file from the previous tutorial (use ctrl-c). Now, you're ready to start your own turtle broadcaster demo:
$ roslaunch learning_tf2 start_demo.launch
You should see the turtle sim with one turtle.
Checking the results
Now, use the tf2_echo tool to check if the turtle pose is actually getting broadcast to tf2:
$ rosrun tf2 tf2_echo /world /turtle1
This should show you the pose of the first turtle. Drive around the turtle using the arrow keys (make sure your terminal window is active, not your simulator window). If you run tf2_echo for the transform between the world and turtle 2, you should not see a transform, because the second turtle is not there yet. However, as soon as we add the second turtle in the next tutorial, the pose of turtle 2 will be broadcast to tf2.
To actually use the transforms broadcast to tf2, you should move on to the next tutorial about creating a tf2 listener (Python) (C++)
Feedback
If you have any comments on this tutorial please comment below:






