Wait for TF listern queue to not be empty

I am noticing an issue with TF LIsteren in Electric that doesn't seem to be covered by other questions. Mainly, if I create a TF listener tf_, then immediately go into a callback that calls tf_.waitForTransform(...), I get: "terminate called after throwing an instance of 'tf::ExtrapolationException' what(): Unable to lookup transform, cache is empty, when looking up transform from frame ...."

What it seems is that the listener hasn't been active long enough to get any TF information before waitForTransform is called, and when you call waitForTransform on a listener with an empty cache/queue, is throws this exception deep down (I can't catch it at the level of calling waitForTransform) and the process is ended.

I'd like to be able to first look to see if the cache is not empty, or be able to catch this at the high level.

Any suggestions?

BTW, the only way I've found around this so far is to sleep for a couple of seconds after creating the listener and before calling waitForTransform().

edit retag close merge delete

Sort by » oldest newest most voted

The problem was that when running Gazebo (and it being VERY slow due to having walls -- I have a separate bug Trac on that that hasn't been answered), TF comes in much slower than the program executes, so waitForTransform bombs out (cannot catch exception because something catches it first and quits).

I can get around this by using canTransform() and throwing out the scan is that fails, or catching the TransformException on the transformPoint() call directly. That is easiest.

more

This is a complete shot in the dark, but have you tried calling ros::spinOnce() before the waitForTransform call?

Or equivalently, creating the listener outside of the main ros loop, and calling the waitForTransform after ros::spin() has been entered?

more

Yes. I did try that. The problem here was that the simulation was running so slow, that TF entries are VERY slow to arrive in real world time. Being outside the main loop only works if you know what the frame id is ahead of time, and yes I tried it anyway and it didn't work.

( 2012-02-16 01:22:39 -0500 )edit