Using ROS 2? Check out the ROS 2 tf2 tutorials.
![]() |
Learning about tf2 and time (Python)
Description: This tutorial teaches you to use the timeout in lookup_transform function to wait for a transform to be available on the tf2 tree.Tutorial Level: INTERMEDIATE
Next Tutorial: Time travel with tf2 (Python)
Contents
Edit nodes/turtle_tf2_listener.py and change the lookupTransform() call to:
1 while not rospy.is_shutdown():
2 try:
3 trans = tfBuffer.lookup_transform('turtle2', 'carrot1', rospy.Time.now())
Also, edit the exception handling by adding raise to be able to see the exception:
1 except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):
2 raise
3 rate.sleep()
4 continue
So, all of the sudden lookup_transform() is failing, telling you that the frame does not exist or that the data is in the future. To fix this, edit your code as shown below. Note: once this change is made, remove the raise line from the except() block that we added above or the code will continue to fail.
The lookup_transform takes four arguments. The 4th is an optional timeout. It will block for up to that duration waiting for it to timeout.
So lookup_transform() will actually block until the transform between the two turtles becomes available (this will usually take a few milli-seconds). Once the timeout has been reached (1 second in this case), an exception will be raised if the transform is still not available.