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

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 transform at 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 is now, 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 for lookupTransform() 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五秒前的轨迹,即可以实现功能。















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值