学习ROS笔记之TF——learning tf(二)

来源:互联网 发布:中央网络电视台官网 编辑:程序博客网 时间:2024/03/28 19:13

1,add a frame (C++)

tf builds up a tree structure of frames; it does not allow a closed loop in the frame structure. This means that a frame only has one single parent, but it can have multiple children. Currently our tf tree contains three frames: world, turtle1 and turtle2. The two turtles are children of world. If we want to add a new frame to tf, one of the three existing frames needs to be the parent frame, and the new frame will become a child frame.tf建立了frames的树形结构,在这个结构中不允许闭环,意思是一个frame只能有一个父母,但是它可以有很多孩子,这个例子中包含了三个frame:world,turtle1和turtle2,其中turtle1和turtle2是world的孩子,如果我们想要建立一个新的frame,这个新的frame需要成为这三个frame的其中一个的孩子。

frame_tf_broadcaster.cpp:

  1 #include <ros/ros.h>   2 #include <tf/transform_broadcaster.h>   3    4 int main(int argc, char** argv){   5   ros::init(argc, argv, "my_tf_broadcaster");   6   ros::NodeHandle node;   7    8   tf::TransformBroadcaster br;   9   tf::Transform transform;  10   11   ros::Rate rate(10.0);  12   while (node.ok()){  13     transform.setOrigin( tf::Vector3(0.0, 2.0, 0.0) );  14     transform.setRotation( tf::Quaternion(0, 0, 0) );  15     br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "turtle1", "carrot1"));  16     rate.sleep();  17   }  18   return 0;  19 };
这个文件与上一节中的turtle_tf_broadcaster.cpp很相似,第15行定义了一个新的frame名称为carrot1,它的父母是turtle1,它距离父母的距离是在y侧即在左侧2米左右。

当修改listener.cpp的文件,将turtle2跟随turtle1改变改为跟随carrot1,这时运行后会发现,乌龟2跟着乌龟1改变,但是始终在乌龟1左侧2米左右,也就是现在乌龟2跟随的是在乌龟1左侧的新的frame—carrot1移动。

2, learning about tf and time

In the previous tutorials we learned about how tf keeps track of a tree of coordinate frames. This tree changes over time, and tf stores a time snapshot for every transform (for up to 10 seconds by default). Until now we used the lookupTransform() function to get access to the latest available transforms in that tf tree, without knowing at what time that transform was recorded. This tutorial will teach you how to get a transformat a specific time.在前面的教程里面我们知道了tf跟随记录树形结构的坐标系,即当这棵树随着时间变化时,每次变化(默认为10s)tf就储存了时间快照。我们使用的是lookupTransform()函数查看tf树的最近可能的变化,不用知道什么时候变换被记录。这里教你如何得到一个transform在一个特殊的时间。

将前面的turtle_tf_listener.cpp中的time(0)改为now(),并添加waitForTransform()语句,如果不添加就出现我们上一节的错误,是因为每个listener都有一个缓冲器用来存储所有来自不同tf的broadcaster的坐标系,当一个broadcaster发送一个转换时,转换消息进入缓冲器之前需要一段时间(通常只有几毫秒)所以当你需要一个转换消息的时间是now即现在时,你应该要等待消息到来的那几个毫秒。而waitForTransform将会延迟一段时间直到两个turtle能运行(这个语句会消耗几毫秒)而将time(0)改为now(),time(0)意思是查看tf树最近的变化而不是现在的变化now,故改为now()后,发现turtle2的行动更自然了。不过通常情况下,都是设为time(0).

3  time travel with tf

Now, instead of making the second turtle go to where the first turtle isnow, make the second turtle go to where the first turtle was 5 seconds ago

这一节我们不让第二只乌龟跟随这第一只乌龟运动,而是让第二只乌龟跟着第一只乌龟五秒前的轨迹运动。

    ros::Time now = ros::Time::now();    ros::Time past = now - ros::Duration(5.0);    listener.waitForTransform("/turtle2", now,                              "/turtle1", past,                              "/world", ros::Duration(1.0));    listener.lookupTransform("/turtle2", now,                             "/turtle1", past,                             "/world", transform);

The advanced API forlookupTransform() takes six arguments:

  1. Give the transform from this frame,
  2. at this time ...
  3. ... to this frame,
  4. at this time.
  5. Specify the frame that does not change over time, in this case the "/world" frame, and
  6. the variable to store the result in.
lookupTransform()中的六个部分,寻找turtle2的现在的轨迹,寻找turtle1五秒以前的轨迹,world框架不随时间变换而转变,而框架turtle2跟随的是turtle1五秒前的轨迹,即可以实现功能。















0 0
原创粉丝点击