#################################### ##FILL ME IN #################################### ## for a custom note with links: ## note =This tutorial assumes you have a working knowledge of compiling programs in ROS and you have completed the [[tf/Tutorials/Introduction to tf | Introduction to tf]] tutorial ## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links ## note.0= ## descriptive title for the tutorial ## title =Writing a tf broadcaster (Python) ## multi-line description to be displayed in search ## description = This tutorial teaches you how to broadcast the state of a robot to tf. ## the next tutorial description (optional) ## next = Writing a tf listener ## links to next tutorial (optional) ## next.0.link=[[tf/Tutorials/Writing a tf listener (Python)|(Python)]] ## next.1.link= ## what level user is this tutorial for ## level= BeginnerCategory ## keywords = #################################### <> {{{#!wiki caution 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. }}} <> <> === Create package === NOTE: Work on this section ONLY IF you haven't done [[http://goo.gl/Bs5vY|Writing a tf broadcaster (C++) tutorial]]. <> === The Code === Let's start by making a new directory called `nodes` in our `learning_tf` package. {{{ $ mkdir nodes }}} Fire up your favorite editor and paste the following code into a new file called '''nodes/turtle_tf_broadcaster.py'''. {{{ #!python block=broadcaster #!/usr/bin/env python import roslib roslib.load_manifest('learning_tf') import rospy import tf import turtlesim.msg def handle_turtle_pose(msg, turtlename): br = tf.TransformBroadcaster() br.sendTransform((msg.x, msg.y, 0), tf.transformations.quaternion_from_euler(0, 0, msg.theta), rospy.Time.now(), turtlename, "world") if __name__ == '__main__': rospy.init_node('turtle_tf_broadcaster') turtlename = rospy.get_param('~turtle') rospy.Subscriber('/%s/pose' % turtlename, turtlesim.msg.Pose, handle_turtle_pose, turtlename) rospy.spin() }}} Don't forget to make the node executable: {{{ chmod +x nodes/turtle_tf_broadcaster.py }}} === The Code Explained === Now, let's take a look at the code that is relevant to publishing the turtle pose to tf. <> 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 '''launch/start_demo.launch''', and add the following lines: {{{ }}} Now you're ready to start your own turtle broadcaster demo: {{{ $ roslaunch learning_tf start_demo.launch }}} You should see the turtlesim with '''one turtle'''. <> <> ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## TutorialTurtlesim ## PythonCategory ## LearningCategory