[ROS in 5 mins] 057 – How to publish the position of a robot using TransformBroadcaster in python
Hello ROS Developers,
welcome to this new post on the “ROS In 5 Minutes” series!
In this post we are going to see how to publish the position of a robot with Python using the
TransformBroadcaster. For that we are going to use Robot Ignite Academy, which is the best platform for those who want to Learn ROS Fast.
Before we start, in case you are new to ROS, remember that we have ROS Basics courses in Python and C++:
Ok, let’s get started. In order to develop our position publisher in ROS we first need to create a ROS package. Our package will be located on the
src folder of the
~/catkin_ws (catkin workspace) and its name will be
tutorial. Given that we will use python, we add the
rospy dependency. For that we use the following commands:
mkdir ~/catkin_ws/src/ -p
catkin_create_pkg tutorial rospy
Now that our package is created, let’s create a python file called broadcaster on its
src folder and make it executable with the commands below:
chmod +x my_broadcaster.py
Now let’s add the code that publishes the positions to our
my_broadcaster.py file. The content is as follows:
#! /usr/bin/env python
from tf import TransformBroadcaster
from rospy import Time
b = TransformBroadcaster()
translation = (0.0, 0.0, 0.0)
rotation = (0.0, 0.0, 0.0, 1.0)
rate = rospy.Rate(5) # 5hz
x, y = 0.0, 0.0
while not rospy.is_shutdown():
if x >= 2:
x, y = 0.0, 0.0
x += 0.1
y += 0.1
translation = (x, y, 0.0)
b.sendTransform(translation, rotation, Time.now(), 'ignite_robot', '/world')
if __name__ == '__main__':
Now that we have everything in place, we just run our code with:
rosrun tutorial my_broadcaster.py
With the code running we don’t see any message. That is the expected behavior since we didn’t call any log functions in our code.
In order to really see the code in action we have to open RViz, which stands for
ROS Visualization. For that we need to run in a different shell the following command:
rosrun rviz rviz
Once RViz is running, in order to see it you have to click on the Open Graphical Interface button, the one which is being pointed by a red arrow on the image below:
Once you click that button you will be able to see RViz. The Fixed Frame will be probably pointing to /map, so you have to change it to
world as we can see on the next image:
Once we selected the frame we are using as the fixed one we have to add the TF Display. If you look at the previous image you will see on the bottom left a button called “Add“. Let’s click that button and select TF, as in the image below:
After that you should be able to see the positions of our robot being published. The image below shows that.
Remember that we also have a video on YouTube that details everything written in this post.
To conclude, I hope you have liked the post and the video. If so, please give us a thumbs up and subscribe to our channel on YouTube: https://www.youtube.com/watch?v=8U-cwv6db3U.
Also, whether you like the video or not, please leave a comment on the comments section of YouTube. Feel free also to share this content with your friends.
Keep pushing your ROS Learning.
- Click to share on Twitter (Opens in new window)
- Click to share on Facebook (Opens in new window)
- Click to share on Reddit (Opens in new window)
- Click to email this to a friend (Opens in new window)
- Click to print (Opens in new window)
- Click to share on Google+ (Opens in new window)
- Click to share on Tumblr (Opens in new window)
- Share on Skype (Opens in new window)
- Click to share on Pinterest (Opens in new window)
- Click to share on WhatsApp (Opens in new window)
- Click to share on Telegram (Opens in new window)