{{{#!wiki tip Using ROS 2? Check out the [[https://docs.ros.org/en/rolling/Tutorials/Intermediate/Tf2/Tf2-Main.html|ROS 2 tf2 tutorials]]. }}} ## For instruction on writing tutorials ## http://www.ros.org/wiki/WritingTutorials #################################### ##FILL ME IN #################################### ## for a custom note with links: ## note = This tutorial assumes you have completed the '''intermediate''' tutorials. ## for the canned note of "This tutorial assumes that you have completed the previous tutorials:" just add the links ## note.0= ## descriptive title for the tutorial ## title = Debugging tf2 problems ## multi-line description to be displayed in search ## description = This tutorial gives a systematic approach for debugging tf2 related problems. ## the next tutorial description (optional) ## next = ## links to next tutorial (optional) ## next.0.link= ## next.1.link= ## what level user is this tutorial for ## level= AdvancedCategory ## keywords = #################################### <> <> This tutorial walks you through the steps to debug a typical tf2 problem. It will apply the steps explailed in the [[tf2/Troubleshooting|tf2 troubleshooting guide]] to an example using [[turtlesim]]. It will also use many of the [[tf2/Debugging tools|tf2 debugging tools]]. == Starting the example == For this tutorial we set up a demo application which has a number of problems. The goal of this tutorial is to apply a systematic approach to find these problems. First, let's build the example: {{{ $ roscd turtle_tf2 $ rosmake }}} And, let's just run it to see what happens: {{{ $ roslaunch start_debug_demo.launch }}} You'll see the [[turtlesim]] come up. If you '''select the terminal window''' from where you launched the demo, you can use the arrow keys to drive one of the robot around. In the upper left corner there is a second robot. If the demo would be working correctly, this second robot should be following the robot you can command with the arrow keys. Obviously, it does not... because we have to solve some problems first. What you do see, is the following error message: {{{ [ERROR] 1254263539.785016000: Frame id /turtle3 does not exist! When trying to transform between /turtle1 and /turtle3. }}} == Finding the tf2 request == So, if you look at the [[tf2/Tutorials/Debugging tf2 problems|debugging tf2 problems guide]], you'll see we first need to find out what exactly we are asking tf2 to do. So, therefore we go into the part of the code that is using tf2. Open the `src/turtle_tf2_listener.cpp` file. This is the code you'll see: {{{#!cplusplus block=listener #include #include #include #include int main(int argc, char** argv){ ros::init(argc, argv, "my_tf2_listener"); ros::NodeHandle node; ros::service::waitForService("spawn"); ros::ServiceClient add_turtle = node.serviceClient("spawn"); turtlesim::Spawn srv; add_turtle.call(srv); ros::Publisher turtle_vel = node.advertise("turtle2/command_velocity", 10); tf2::TransformListener listener; ros::Rate rate(10.0); while (node.ok()){ tf2::Stamped transform; try{ listener.lookupTransform("/turtle3", "/turtle1", ros::Time::now(), transform); } catch (tf2::TransformException ex){ ROS_ERROR("%s",ex.what()); ros::Duration(1.0).sleep(); } turtlesim::Velocity vel_msg; vel_msg.angular = 4 * atan2(transform.getOrigin().y(), transform.getOrigin().x()); vel_msg.linear = 0.5 * sqrt(pow(transform.getOrigin().x(), 2) + pow(transform.getOrigin().y(), 2)); turtle_vel.publish(vel_msg); rate.sleep(); } return 0; }; }}} Take a look at lines 25-28: <> Here we do the actual request to tf2. The first three arguments tell us directly what we are asking tf2: transform from frame `/turtle3` to frame `/turtle1` at time "now". Now, let's take a look at why this request to tf2 is failing. == Checking the Frames == First we want to find out if tf2 knows about our transform between /turtle3 and /turtle1: {{{ $ rosrun tf2 tf2_echo turtle3 turtle1 }}} The output tells us that frame `turtle3` does not exist: {{{ Exception thrown:Frame id /turtle3 does not exist! When trying to transform between /turtle1 and /turtle3. The current list of frames is: Frame /turtle1 exists with parent /world. Frame /world exists with parent NO_PARENT. Frame /turtle2 exists with parent /world. }}} The last three lines of the above message tell us what frames '''do''' exist. If you like to get a graphical representation of this, type: {{{ $ rosrun tf2_tools view_frames $ evince frames.pdf }}} And you'll get the following output. {{attachment:frames.png||height="516",width="752"}} So obviously the problem is that we are requesting `turtle3`, which does not exist. To fix this bug, replace `turtle3` with `turtle2` in lines 25-28: {{{#!cplusplus try{ listener.lookupTransform("/turtle2", "/turtle1", ros::Time::now(), transform); } }}} And now stop the running demo (`Ctrl-c`), build it, and run it again: {{{ $ make $ roslaunch start_debug_demo.launch }}} And right away we run into the next problem: {{{ [ERROR] 1254264620.183142000: You requested a transform that is 0.116 miliseconds in the past, but the most recent transform in the tf2 buffer is 3.565 miliseconds old. When trying to transform between /turtle1 and /turtle2. }}} == Checking the Timestamp == Now that we solved the frame name problem, it is time to look at the timestamps. Remember we are trying to get the transform between `turtle2` and `turtle1` at time "now". To get statistics on the timing, run: {{{ $ rosrun tf2 tf2_monitor turtle2 turtle1 }}} The result should look something like this: {{{ RESULTS: for /turtle2 to /turtle1 Chain currently is: /turtle1 -> /turtle2 Net delay avg = 0.008562: max = 0.05632 Frames: Broadcasters: Node: /broadcaster1 40.01641 Hz, Average Delay: 0.0001178 Max Delay: 0.000528 Node: /broadcaster2 40.01641 Hz, Average Delay: 0.0001258 Max Delay: 0.000309 }}} The key part here is the delay for the chain from `turtle2` to `turtle1`. The output shows there is an average delay of 8 milliseconds. This means that tf2 can only transform between the turtles after 8 milliseconds are passed. So, if we would be asking tf2 for the transformation between the turtles 8 milliseconds ago instead of "now", tf2 would be able to give us an answer sometimes. Let's test this quickly by changing lines 25-28 to: {{{#!cplusplus try{ listener.lookupTransform("/turtle2", "/turtle1", ros::Time::now()-ros::Duration(0.1), transform); } }}} So in the new code we are asking for the transform between the turtles 100 milliseconds ago (Why not 8? Just to be safe...). Stop the demo (`Ctrl-c`), build and run: {{{ $ make $ roslaunch start_debug_demo.launch }}} And you should finally see the turtle move! {{attachment:turtle_tf2_start.png}} That last fix we made is not really what you want to do, it was just to make sure that was our problem. The real fix would look like this: {{{#!cplusplus try{ listener.lookupTransform("/turtle2", "/turtle1", ros::Time(0), transform); } }}} or like this: {{{#!cplusplus try{ ros::Time now = ros::Time::now(); listener.waitForTransform("/turtle2", "/turtle1", now, ros::Duration(1.0)); listener.lookupTransform("/turtle2", "/turtle1", now, transform); } }}} ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE ## DebugCategory