Using ROS 2? Check out the ROS 2 tf2 tutorials.
Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
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:
Also, edit the exception handling by adding raise to be able to see the exception:
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.