ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

tf2_ros.Buffer.transform() in Python (Tutorial is lacking?)

asked 2021-01-18 13:20:08 -0600

blubbi321 gravatar image

updated 2021-01-18 13:24:19 -0600


I have the following rough TF(2) setup (simpified):

odom -> base_link -> tool_link


  • odom is generated by the diff drive controller from here.
  • base_link is the "root" of my mobile base (ie it moves around)
  • tool_link is the tf frame of a tool into which I want to transform coordinates

Now I would like to transform sensor measurements that I receive as PoseStamped messages in the odom frame into the tool_link frame.

What I tried

  • I followed the Python tutorials until here (writing a tf2 listener in Python). The solution seems to be at the bottom of the page. However, the MessageFilter approach mentioned there only contains an example for C++.
  • Searching rosanswers brought up this - where the guy just implemented his own filter in python. The answer is also pretty old so Id hope that there is something more recent?

The Current Code

My code follows the abovementioned tutorial, ie (stripped down version):

import tf2_ros
tfBuffer = tf2_ros.Buffer()
listener = tf2_ros.TransformListener(tfBuffer)
    trans = tfBuffer.lookup_transform(turtle_name, 'turtle1', rospy.Time())
    # MARKER (Transform point here? See below)
except (tf2_ros.LookupException, tf2_ros.ConnectivityException, tf2_ros.ExtrapolationException):

This works so far. However, it leaves me with a transform message that I would still need to apply to the input message The problem here is basically this - but that answer is for tf1. For tf2, as far as I understand the following should be used to transform a PointStamped (and therefore be inserted at MARKER in the above code):

tfBuffer.transform(pose_stamped_to_be_converted, "tool_link", timeout=rospy.Duration(10.0))

But that line fails with

Lookup would require extrapolation into the past.  Requested time 166.496000000 but the earliest data is at time 170.614000000, when looking up transform from frame [odom] to frame [m2_blade_horizon]

The delta between requested and earliest time is always in the range above ~2-8s. As you can see I have picked a very large timeout (to hopefully rule out this), even though the tf publishers for the revelant frames at all running at 10+Hz.


  • How do I get the above transform to work (actually using tf2 - what is the point of using tf2 if I still have to do the transform myself?)
  • Is there any tf2 python documentation that I missed?
  • Was an "official" python message filter developed in the meantime?
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-01-23 07:19:51 -0600

blubbi321 gravatar image

Found the fix here (pretty sure I tried it before without it working though):

The trick is to just take the last available transform from the buffer, ie for my example above:

pose_stamped_to_be_converted = PoseStamped()
pose_stamped_to_be_converted.header.stamp = rospy.Time(0)
transformed_posestamped = tfBuffer.transform(pose_stamped_to_be_converted, "tool_link", timeout=rospy.Duration(10.0))
edit flag offensive delete link more

Question Tools



Asked: 2021-01-18 13:20:08 -0600

Seen: 2,633 times

Last updated: Jan 23 '21