| 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 (Python)
Description: This tutorial teaches you how to broadcast the state 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
Let's start by making a new directory called nodes in our learning_tf2 package.
$ mkdir nodes
Fire up your favorite editor and paste the following code into a new file called nodes/turtle_tf2_broadcaster.py.
1 #!/usr/bin/env python
2 import roslib
3 roslib.load_manifest('learning_tf2')
4 import rospy
5
6 import tf2
7 import turtlesim.msg
8
9 def handle_turtle_pose(msg, turtlename):
10 br = tf2.TransformBroadcaster()
11 br.sendTransform((msg.x, msg.y, 0),
12 tf2.transformations.quaternion_from_euler(0, 0, msg.theta),
13 rospy.Time.now(),
14 turtlename,
15 "world")
16
17 if __name__ == '__main__':
18 rospy.init_node('turtle_tf2_broadcaster')
19 turtlename = rospy.get_param('~turtle')
20 rospy.Subscriber('/%s/pose' % turtlename,
21 turtlesim.msg.Pose,
22 handle_turtle_pose,
23 turtlename)
24 rospy.spin()
Don't forget to make the node executable:
chmod +x nodes/turtle_tf2_broadcaster.py
The Code Explained
Now, let's take a look at the code that is relevant to publishing the turtle pose to tf2.
19 turtlename = rospy.get_param('~turtle')
This node takes a single parameter "turtle", which specifies a turtle name, e.g. "turtle1" or "turtle2".
The node subscribes to topic "turtleX/pose" and runs function handle_turtle_pose on every incoming message.
The handler function for the turtle pose message broadcasts this turtle's translation and rotation, and publishes it as a transform from frame "world" to frame "turtleX".
Running the broadcaster
Now 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>
<include file="$(find turtle_teleop)/launch/turtle_keyboard.launch" />
<node name="turtle1_tf2_broadcaster" pkg="learning_tf2" type="turtle_tf2_broadcaster.py" respawn="false" output="screen" >
<param name="turtle" type="string" value="turtle1" />
</node>
<node name="turtle2_tf2_broadcaster" pkg="learning_tf2" type="turtle_tf2_broadcaster.py" respawn="false" output="screen" >
<param name="turtle" type="string" value="turtle2" />
</node>
</launch>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++)






