Using ROS 2? Check out the ROS 2 tf2 tutorials.

Note: This tutorial assumes you have completed the tf2 and time (Python) (C++) 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.

Time travel with tf2 (Python)

Description: This tutorial teaches you about advanced time travel features of tf2

Tutorial Level: ADVANCED

In the previous tutorial we discussed the basic concept of tf2 and time. This tutorial will take this one step further, and expose one of the most powerful tf2 tricks.

Time travel

So let's go back to where we ended in the previous tutorial. Go to your package for the tutorial:

  $ roscd learning_tf2

Now, instead of making the second turtle go to where the carrot is now, make the second turtle go to where the first carrot was 5 seconds ago. Edit nodes/turtle_tf2_listener.py:

   1         try:
   2             past = rospy.Time.now() - rospy.Duration(5.0)
   3             trans = tfBuffer.lookup_transform(turtle_name, 'carrot1', past, rospy.Duration(1.0))
   4         except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):

So now, if you would run this, what would you expect to see? Definitely during the first 5 seconds the second turtle would not know where to go, because we do not yet have a 5 second history of the first turtle. But what after these 5 seconds? Let's just give it a try:

  $ catkin_make
  $ roslaunch learning_tf2 start_demo.launch
  • random.png

Is your turtle driving around uncontrollably like in this screenshot? So what is happening?

  • We asked tf2, "What was the pose of /turtle1 5 seconds ago, relative to /turtle2 5 seconds ago?". This means we are controlling the second turtle based on where it was 5 seconds ago as well as where the first turtle was 5 seconds ago.

  • What we really want to ask is, "What was the pose of /turtle1 5 seconds ago, relative to the current position of the /turtle2?".

Advanced API for lookupTransform

So how can we ask tf2 a question like that? This API gives us the power to say explicitly when each frame is transformed. This is what the code would look like:

   1         try:
   2             past = rospy.Time.now() - rospy.Duration(5.0)
   3             trans = tfBuffer.lookup_transform_full(
   4                 target_frame=turtle_name,
   5                 target_time=rospy.Time.now(),
   6                 source_frame='carrot1',
   7                 source_time=past,
   8                 fixed_frame='world',
   9                 timeout=rospy.Duration(1.0)
  10             )

The advanced API for lookupTransform() takes six arguments:

  1. Give the transform from this frame,
  2. at this time ...
  3. ... to this frame,
  4. at this time.
  5. Specify the frame that does not change over time, in this case the "/world" frame, and
  6. the time-out.

    time_travel.png

This figure shows what tf2 is doing in the background. In the past it computes the transform from the first turtle to the world. In the world frame tf2 time travels from the past to now. And at time now tf2 computes the transform from the world to the second turtle.

Checking the results

Let's run the simulator again, this time with the advanced time-travel API:

  $ roslaunch learning_tf2 start_demo.launch

And yes, the second turtle is directed to where the first carrot was 5 seconds ago!

Wiki: tf2/Tutorials/Time travel with tf2 (Python) (last edited 2022-10-06 16:53:36 by ShaneLoretz)