Let’s make the second turtle follow the first turtle, and not the carrot. Change your code to the following:
try{
listener.lookupTransform("/turtle2", "/turtle1",
ros::Time(0), transform);
You can also see we specified a time equal to 0. For tf, time 0 means “the latest available” transform in the buffer. Now, change this line to get the transform at the current time, “now()”:
try{
listener.lookupTransform("/turtle2", “/turtle1”,
ros::Time::now(), transform);
First, make sure you stopped the launch file from the previous tutorial (use ctrl-c). Compile the code, and run it again:
$ roslaunch learning_tf start_demo.launch
So, all of the sudden lookupTransform() is failing, telling you repeatedly:
[ERROR] [1287871653.885277559]: You requested a transform that is 0.018 miliseconds in the past, but the most recent transform in the tf buffer is 7.681 miliseconds old.
When trying to transform between /turtle1 and /turtle2.
…
Why is that? Well, each listener has a buffer where it stores all the coordinate transforms coming from the different tf broadcasters. When a broadcaster sends out a transform, it takes some time before that transform gets into the buffer (usually a couple of milliseconds). So, when you request a frame transform at time “now”, you should wait a few milliseconds for that information to arrive.
tf provides a nice tool that will wait until a transform becomes available. Let’s look at what the code would look like:
try{
ros::Time now = ros::Time::now();
listener.waitForTransform("/turtle2", “/turtle1”,
now, ros::Duration(3.0));
listener.lookupTransform("/turtle2", “/turtle1”,
now, transform);
The waitForTransform() takes four arguments:
Wait for the transform from this frame…
… to this frame,
at this time, and
timeout: don’t wait for longer than this maximum duration
Note: The use of ros::Time::now() is for this example. Usually this would be the timestamp of the data wishing to be transformed.
So waitForTransform() will actually block until the transform between the two turtles becomes available (this will usually take a few milliseconds), OR --if the transform does not become available-- until the timeout has been reached.