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

Calculating velocity from tf

asked 2012-02-04 23:41:08 -0600

jwrobbo gravatar image

updated 2014-01-28 17:11:16 -0600

ngrennan gravatar image

When listening to transforms on tf, is there a way to ask specifically for the one before the latest one? And when you have received a transform, how do you query it for its timestamp?

I have position information being intermittently broadcast onto tf. Because I don't know exactly when this is going to happen I can't ask for a transform with a specific time delay. I would like to ask for the latest two transforms, find the time delay between them and divide by this to give an estimate of velocity.

Any help would be much appreciated!

James

edit retag flag offensive close merge delete

4 Answers

Sort by ยป oldest newest most voted
7

answered 2012-02-06 11:37:37 -0600

tfoote gravatar image

updated 2012-02-06 11:41:22 -0600

As @dlaz mentioned you want to use Transformer::lookupTwist()

If you use a target_time of zero it will give you the latest reading. And if you set a very small averaging_interval to a very small value(less than the minimum time between your tf updates) it will effectively do what you want. This will cause the target time to be the last update. And it will use linear interpolation between the last two updates to compute the other endpoint of the interval.

Note: This will be very noisy data! You are doing discreet differentiation over small time samples, the result will be very biased by noise in the tf positions.

edit flag offensive delete link more
5

answered 2012-02-05 02:59:41 -0600

updated 2012-02-05 03:03:17 -0600

The standard way to get velocities from TF is with Transformer::lookupTwist(), which will return velocity as a geometry_msgs/Twist. I'm not sure how that will work with your intermittent TF broadcasting though.

edit flag offensive delete link more
0

answered 2022-01-10 23:55:38 -0600

lucasw gravatar image

updated 2022-01-11 13:10:41 -0600

update

(This doesn't exactly answer the question, the noise concerns are still valid, but possibly some searches that lead here want to do something like this, and with rospy)

I made a twist lookup from scratch, parent frame is the fixed frame (e.g. "map" or "odom" in many cases) and child frame is the moving frame:

# get the stamp of the most recent valid transform
tfs_ref = tf_buffer.lookup_transform(parent_frame, 
                                     child_frame,
                                     rospy.Time(0))
stamp = tfs_ref.header.stamp
old_stamp = stamp - rospy.Duration(duration)

tfs = tf_buffer.lookup_transform_full(child_frame, old_stamp,
                                      child_frame, stamp,
                                      parent_frame)

delta_t = 1.0 / duration
tr = tfs.transform.translation
vel_vec = Vector3(tr.x * delta_t, tr.y * delta_t, tr.z * delta_t)

rot = tfs.transform.rotation
quat = [rot.x, rot.y, rot.z, rot.w]
euler = transformations.euler_from_quaternion(quat)
rot_vec = Vector3(euler[0] * delta_t, euler[1] * delta_t, euler[2] * delta_t)
twist = Twist(vel_vec, rot_vec)

I'm not sure about the correctness, if it is producing the output any others desire, but visualizing it in rviz with mostly 2D x,y, theta motions looks reasonable (as opposed to using lookupTwist, where the twist changes as a function of angle relative to the fixed frame, when I just want the twist relative to the moving frame). Didn't look too closely at the angular rates, mostly just the linear.


(couldn't get this lookupTwist to work, even with suggested change from https://github.com/ros/geometry/issue...)

For python it requires using tf:

import tf
from geometry_msgs.msg import Twist    
from geometry_msgs.msg import Vector3

tl = tf.TransformListener()
...
# get twist relative to map calculated from the most recent
# valid sample to a sample 0.5 seconds before that
tw = tl.lookupTwist(moving_frame_id, "map",
                    rospy.Time(0.0), rospy.Duration(0.5))
twist = Twist(Vector3(tw[0][0], tw[0][1], tw[0][2]),
              Vector3(tw[1][0], tw[1][1], tw[1][2]))

The return value isn't a Twist(), instead it's six values in two tuples which are linear xyz and then angular xyz.

Maybe it was intended to be added to tf2 but the lookupTwist is commented out: https://github.com/ros/geometry2/blob...

Maybe this https://github.com/ros/geometry/blob/... could be adapted into tf2 easily.

edit flag offensive delete link more

Comments

1

It was explicitly removed in the port from tf to tf2 because it was too noisy and people were getting bad results from the noisy signal and didn't have enough visibilty into the process to debug it. If you want to do it yourself you can do the discrete differentiation such that you have control of the system.

tfoote gravatar image tfoote  ( 2022-01-11 01:56:40 -0600 )edit

I couldn't get lookupTwist to work even with perfectly smooth synthetic data, so made my own and added the code above.

lucasw gravatar image lucasw  ( 2022-01-11 13:09:11 -0600 )edit
-1

answered 2018-02-12 17:30:57 -0600

constructiverealities gravatar image

updated 2018-02-12 17:52:15 -0600

@tfoote So this is a bit older, but I'm running into an Issue with TF2 in that I can't find a substitute function for TFs lookupTwist. Was this dropped from TF2? if so, Oops. It's really useful.

Any tips on how to calculate it myself? I've create a related question here.

edit flag offensive delete link more

Comments

2

Please don't use an answer to ask a question. This isn't a forum. Please create a new question and reference this question.

jayess gravatar image jayess  ( 2018-02-12 17:49:33 -0600 )edit

^ I edited to link to the related question

constructiverealities gravatar image constructiverealities  ( 2018-02-12 17:53:04 -0600 )edit
2

This still doesn't align with the Q&A format that we have here. Please delete this answer and you can leave a comment (on your question) referencing this one.

jayess gravatar image jayess  ( 2018-02-12 18:22:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2012-02-04 23:41:08 -0600

Seen: 5,061 times

Last updated: Jan 11 '22