[ROS Q&A] 197 – Error in Writing a tf2 broadcaster Tutorial (Python)

Written by Alberto Ezquerro

23/10/2019

Learn how to create a Python program (broadcaster) for broadcasting a tf2 between 2 frames. This video is an answer to the following question found on ROS Answers.


Related Resources


Step 1: Get your development environment ready

Either of the following will do:

  1. Use the ROS Development Studio (ROSDS), an online platform for developing for ROS within a PC browser. Easy-peasy. I’m using this option for this post
    1. Once you log in, click on the New ROSject button to create a project that will host your code. Then Click on Open ROSject to launch the development/simulation environment.
    2. To open a “terminal” on ROSDSpick the Shell app from the Tools menu.
    3. You can find the IDE app on the Tools menu.
  2. You have ROS installed on a local PC. Okay, skip to Step 2.

Next step!

Step 2: Create the TF ROS package for the tf2 broadcaster Python node

The tutorial the user tried to follow is at http://wiki.ros.org/tf2/Tutorials/Writing%20a%20tf2%20broadcaster%20%28Python%29. We will follow the same tutorial.

Open a terminal, browse to your catkin workspace source directory and create the package as indicated in the tutorial:

user:~$ cd catkin_ws/src/
user:~/catkin_ws/src$ catkin_create_pkg learning_tf2 tf2 tf2_ros roscpp rospy turtlesim
Created file learning_tf2/package.xml
Created file learning_tf2/CMakeLists.txt
Created folder learning_tf2/include/learning_tf2
Created folder learning_tf2/src
Successfully created files in /home/user/catkin_ws/src/learning_tf2. Please adjust the values in package.xml.
user:~/catkin_ws/src$ cd learning_tf2
user:~/catkin_ws/src/learning_tf2$ mkdir nodes
user:~/catkin_ws/src/learning_tf2$ touch nodes/turtle_tf2_broadcaster.py
user:~/catkin_ws/src/learning_tf2$ chmod +x nodes/turtle_tf2_broadcaster.py
user:~/catkin_ws/src/learning_tf2$ mkdir launch
user:~/catkin_ws/src/learning_tf2$ touch launch/start_demo.launch
user:~/catkin_ws/src/learning_tf2$

Step 3: Add Python code and launch file for launching the tf2 broadcaster Python node

Open the IDE, locate the file turtle_tf_broadcaster.py file and paste in the following code:

#!/usr/bin/env python  
import rospy

# Because of transformations
import tf_conversions

import tf2_ros
import geometry_msgs.msg
import turtlesim.msg


def handle_turtle_pose(msg, turtlename):
    br = tf2_ros.TransformBroadcaster()
    t = geometry_msgs.msg.TransformStamped()

    t.header.stamp = rospy.Time.now()
    t.header.frame_id = "world"
    t.child_frame_id = turtlename
    t.transform.translation.x = msg.x
    t.transform.translation.y = msg.y
    t.transform.translation.z = 0.0
    q = tf_conversions.transformations.quaternion_from_euler(0, 0, msg.theta)
    t.transform.rotation.x = q[0]
    t.transform.rotation.y = q[1]
    t.transform.rotation.z = q[2]
    t.transform.rotation.w = q[3]

    br.sendTransform(t)

if __name__ == '__main__':
    rospy.init_node('tf2_turtle_broadcaster')
    turtlename = rospy.get_param('~turtle')
    rospy.Subscriber('/%s/pose' % turtlename,
                     turtlesim.msg.Pose,
                     handle_turtle_pose,
                     turtlename)
    rospy.spin()

Also, find the start_demo.launch file and paste in the given code:

<launch>
      <node pkg="turtlesim" type="turtlesim_node" name="sim"/>
      <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>


    <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>

Step 4: Compile and run the package

user:~/catkin_ws/src/learning_tf2$ cd
user:~$ cd catkin_ws/
user:~/catkin_ws$ catkin_make

After that is done, then:

user:~/catkin_ws$ roslaunch learning_tf2 start_demo.launch

You should now see the classical turtle. In order to see the Turtle on ROSDS, go to

  • Tools -> Graphical Tools. (PS: In case it does not appear well, you can use the Resize opened app button to resize the screen).

Now, use the tf_echo tool to check if the turtle pose is actually getting broadcast to tf2. Open another terminal and run:

user:~$ rosrun tf tf_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), and you should see these values change.

Ok now, we are done, right? No, we are not! We need to find out what was wrong with the user’s code.

Step 5: Break the Python code so that we can help the user find out the problem with their code!

Yes, really. If you haven’t broken something you probably haven’t learned much, they say! So now let’s break the Python code so we can find out why the user’s code did not run:

  • Remove the very first line of the turtle_tf_broadcaster.py file that reads #!/usr/bin/env python and save it.
  • Now try to launch the node again: stop the current launch command using Ctrl + C and then launch it again:
# Ctrl + C to stop the current one
user:~/catkin_ws$ roslaunch learning_tf2 start_demo.launch

Now we have the following ugly error that the user had:

“`

Unable to launch [turtle1_tf2_broadcaster-4].
If it is a script, you may be missing a ‘#!’ declaration at the top.
The traceback for the exception was written to the log file

“`

The moral: Don’t tamper with that “shebang” at the top of a python script – it’s what identifies it as an executable python script.

Yep, we are done now. I hope that you were able to learn one or two things from this post.

PS: Don’t forget to restore the shebang line 🙂

Extra: Video of this post

We made a video showing how we create a package for broadcasting tf2 between two frames and how we found out the problem with the user’s code. If you prefer “sights and sounds” to “black and white”, here you go:

Feedback

Did you like this post? Do you have any questions about the explanations? Whatever the case, please leave a comment on the comments section below, so we can interact and learn from each other.

If you want to learn about other ROS or ROS2 topics, please let us know in the comments area and we will do a video or post about it.

#ROStutorials #tf #TF #ROS #Robot #Broadcaster #Python


Edited by Bayode Aderinola

You May Also Like…

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

FOR CAMPUS

ROS & Robotics Curriculum Designed for Remote Teaching

Ready for your Robotics career?

Create an account, and start learning and developing robots

Share This