ROS Learning-022 learning_tf-06(编程) 现在与过去中穿梭 (Python版) --- waitForTransformFull() 函数
ROS Indigo learning_tf-06 现在与过去中穿梭 (Python版) — waitForTransformFull() 函数
我使用的虚拟机软件:VMware Workstation 11
使用的Ubuntu系统:Ubuntu 14.04.4 LTS
ROS 版本:ROS Indigo
一 . 前言
这一节要做的事情:使用 tf 的一个强大功能:可以再现在与过去中穿梭。(就是:如何使用 waitForTransformFull() 函数。)
我们不让 turtle2 跟随当前的 turtle1 的坐标, 而是让 turtle2 去跟随 过去的 turtle1 的坐标。
一样,我们只需要改变 监听器 程序 ( turtle_tf_listener.py )中的一点点代码就可以实现。我们就让 turtle2 去跟随5秒钟前的 turtle1 的坐标吧。
跟随过去 的 程序编写
1 . 重点讲解
我们将之前编写的 监听器 程序 ( turtle_tf_listener.py )里面的这一段代码:
try:
(trans, rot) = listener.lookupTransform('/turtle2', '/turtle1', rospy.Time(0))
修改为:
try:
now = rospy.Time.now()
past = now - rospy.Duration(5.0)
listener.waitForTransformFull('/turtle2', now, '/turtle1', now, '/world', rospy.Duration(4.0))
(trans,rot) = listener.lookupTransform('/turtle2', now, '/turtle1', past, '/world')
我们来具体了解一下上面代码中出现的:lookupTransformFull() 函数,这个函数也是坐标系信息转换的功能。这个函数有6个形参: ( 其实它的功能和lookupTransform()函数的功能一样,只不过是个升级版函数 ) :
1 . 父类坐标系节点名
2 . 指定时间
3 . 子类坐标系节点名
4 . 指定时间
5 . 指定不随时间改变的坐标系, 在这里是 “/world” 坐标系.
通过 形参1 和 形参2 就可以得到一个 姿态的信息(线速度和角速度),通过 形参3 和 形参4 就可以得到另一个姿态的信息。
图示 lookupTransformFull() 函数的作用:
解说一下这幅图的意思: lookupTransformFull() 函数 做了下面三步的事情。
第1步 : 将 形参1 在指定的时间 形参2 时的姿态信息转换到 形参5 坐标系上;
第2步 : 接着,将 形参3 在指定的时间 形参4 时的姿态信息转换到 形参5 坐标系上;
第3步 : 最后,将 第1步 得到的姿态信息做为坐标原点(即:以 形参1 的位置做为参考坐标系),求出 第二步 得到的姿态信息 在 这个参考坐标中的姿态信息,将其做为 lookupTransformFull() 函数返回值输出。
2 . 编写代码
在learning_tf 程序包中的 nodes 路径下,新建一个文件:turtle_tf_listener_pastNow.py:
roscd learning_tf/node/
gedit turtle_tf_listener_pastNow.py
下面是完整的程序。将这段程序复制到 turtle_tf_listener_pastNow.py 文件里面:
#!/usr/bin/env python
import roslib
roslib.load_manifest('learning_tf')
import rospy
import math
import tf
import geometry_msgs.msg
import turtlesim.srv
if __name__ == '__main__':
rospy.init_node('tf_turtle')
listener = tf.TransformListener()
rospy.wait_for_service('spawn')
spawner = rospy.ServiceProxy('spawn', turtlesim.srv.Spawn)
spawner(4, 2, 0, 'turtle2')
turtle_vel = rospy.Publisher('turtle2/cmd_vel', geometry_msgs.msg.Twist, queue_size=1)
rate = rospy.Rate(10.0)
listener.waitForTransform("/turtle2", "/turtle1", rospy.Time(), rospy.Duration(4.0))
while not rospy.is_shutdown():
try:
now = rospy.Time.now()
past = now - rospy.Duration(5.0)
listener.waitForTransformFull('/turtle2', now, '/turtle1', now, '/world', rospy.Duration(4.0))
(trans,rot) = listener.lookupTransform('/turtle2', now, '/turtle1', past, '/world')
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
continue
angular = 4 * math.atan2(trans[1], trans[0])
linear = 0.5 * math.sqrt(trans[0] ** 2 + trans[1] ** 2)
cmd = geometry_msgs.msg.Twist()
cmd.linear.x = linear
cmd.angular.z = angular
turtle_vel.publish(cmd)
rate.sleep()
给这个文件添加可执行权限:
chmod 777 turtle_tf_listener_pastNow.py
3 . 编写启动文件
在learning_tf 程序包中的 launch 路径下,新建一个文件:start_demo6.launch:
roscd learning_tf/launch/
gedit start_demo6.launch
将下面的代码拷贝进去。(下面这段代码就是通过 start_demo2.launch 文件改写的,基本和它一模一样)
<launch>
<!-- Turtlesim Node -->
<node pkg="turtlesim" type="turtlesim_node" name="sim" />
<node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen" />
<node name="turtle1_tf_broadcaster" pkg="learning_tf" type="turtle_tf_broadcaster.py" respawn="false" output="screen" >
<param name="turtle" type="string" value="turtle1" />
</node>
<node name="turtle2_tf_broadcaster" pkg="learning_tf" type="turtle_tf_broadcaster.py" respawn="false" output="screen" >
<param name="turtle" type="string" value="turtle2" />
</node>
<node pkg="learning_tf" type="turtle_tf_listener_pastNow.py" name="listener" />
</launch>
4 . 运行
运行 start_demo6.launch 这个启动脚本文件:
$ roslaunch learning_tf start_demo6.launch
这次的运行效果,很不错,当前时间的 turtle2 小海龟 就在跟随这个 过去5秒钟之前的 turtle1 小海龟做运动。
搞定
5 . 扩展: ROS 官方上的错误
下面这段是 ROS 官网中的程序:(这个网站)
try:
now = rospy.Time.now()
past = now - rospy.Duration(5.0)
listener.waitForTransformFull('/turtle2', now, '/turtle1', past, '/world', rospy.Duration(4.0))
(trans,rot) = listener.lookupTransform('/turtle2', now, '/turtle1', past, '/world')
这段代码是错误的。如果运行的话,会输出下面的错误信息:
[listener-6] process has died [pid 8919, exit code 1, cmd /home/aobosir/catkin_ws/src/learning_tf/nodes/turtle_tf_listener_pastNow.py __name:=listener __log:=/home/aobosir/.ros/log/71c1eedc-75ad-11e6-8577-000c29fd0c33/listener-6.log].
log file: /home/aobosir/.ros/log/71c1eedc-75ad-11e6-8577-000c29fd0c33/listener-6*.log
正确的代码就是:(这样就不会出现问题)
try:
now = rospy.Time.now()
past = now - rospy.Duration(5.0)
listener.waitForTransformFull('/turtle2', now, '/turtle1', now, '/world', rospy.Duration(4.0))
(trans,rot) = listener.lookupTransform('/turtle2', now, '/turtle1', past, '/world')
总结:
现在,我们学会了如果在ROS 中进行不同时间段之间的 tf 转换。
接下来,下一节,介绍:如果调试使用 tf 转换的程序(C++ 版)。
ROS Learning-022 learning_tf-06(编程) 现在与过去中穿梭 (Python版) --- waitForTransformFull() 函数的更多相关文章
- ROS Learning-015 learning_tf(编程) 编写一个监听器程序 (Python版)
ROS Indigo learning_tf-02 编写一个 监听器 程序 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 1 ...
- ROS Learning-014 learning_tf(编程) 坐标系变换(tf)广播员 (Python版)
ROS Indigo learning_tf-01 坐标系变换(tf)广播员 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu ...
- ROS Learning-019 learning_tf-03(编程) 添加额外的坐标系 (Python版)
ROS Indigo learning_tf-03 添加额外的坐标系 (Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.0 ...
- ROS Learning-021 learning_tf-05(编程) now() 和 Time(0) 的区别 (Python版)
ROS Indigo learning_tf-05 now() 和 Time(0)的区别 (Python版) - waitForTransform() 函数 我使用的虚拟机软件:VMware Work ...
- 【C语言编程入门笔记】C语言果然博大精深!函数还分内部和外部?
۞ 外部函数与内部函数 前面我们讲解了关于函数的调用都是针对同一个源文件中其他函数进行调用的,而在有些情况下,函数也可以对另外一个源文件中的函数进行调用.当一个程序由多个源文件组成时,根据函数是否能被 ...
- ROS Learning-020 learning_tf-04(编程)让turtle2 海龟跟随turtle1海龟,并绕着 turtle1海龟转圈 (Python版)
ROS Indigo learning_tf-04 (编程)让 turtle2 海龟跟随 turtle1 海龟,并绕着 turtle1 海龟转圈 (Python版) 我使用的虚拟机软件:VMware ...
- ROS Learning-011 beginner_Tutorials (编程) 编写 ROS 话题版的 Hello World 程序(Python版)
ROS Indigo beginner_Tutorials-10 编写 ROS 话题版的 Hello World 程序(Python版) 我使用的虚拟机软件:VMware Workstation 11 ...
- Python并发编程06 /阻塞、异步调用/同步调用、异步回调函数、线程queue、事件event、协程
Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件event.协程 目录 Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件 ...
- ROS Learning-013 beginner_Tutorials (编程) 编写ROS服务版的Hello World程序(Python版)
ROS Indigo beginner_Tutorials-12 编写ROS服务版的Hello World程序(Python版) 我使用的虚拟机软件:VMware Workstation 11 使用的 ...
随机推荐
- Eclipse中Maven的本地仓库引导配置
简单整理一下,方便理解操作. 1.本地拷贝maven文件后,打开maven中的.setting 文件: 2.配置文件: <?xml version="1.0" encodin ...
- javascript常用的数组操作
数组的定义 var arr=new Array(); var arr=[]; var arr=new Array(10);//定义一个长度为10的数组 数组元素的访问 var temp=arr[1]; ...
- c++primer 第三章编程练习答案
3.7.1 #include<iostream> int main() { using namespace std; ; int height,inch,foot; cout <&l ...
- L117
Hoover has become a household word for a vacuum cleaner through the world.Economics are slowly killi ...
- 树莓派(Arduino)仿真软件 —— Fritzing
Fritzing 官网:Fritzing Fritzing 下载地址:Fritzing Download windows 下降 zip 文件解压后,免安装双击 exe 即可运行:
- 已知一个数组a[N]来构造数组b[N]的有趣算法题
给定一个数组a[N],我们希望构造数组b[N],其中b[i]=a[0]*a[1]*...*a[N-1]/a[i].在构造过程要求满足:1.不使用除法:2.O(1)空间复杂度和O(n)时间复杂度:3.除 ...
- UVA - 11922 Permutation Transformer (splay)
题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...
- ACM学习历程—HDU5696 区间的价值(分治 && RMQ && 线段树 && 动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=5696 这是这次百度之星初赛2B的第一题,但是由于正好打省赛,于是便错过了.加上2A的时候差了一题,当时有思路,但 ...
- 微信小程序switch组件尺寸控制
1.修改switch组件的属性值 /* switch */ .wx-switch-input{ width: 82rpx!important; height: 40rpx!important; } / ...
- /etc删了怎么办
实施一个哥们一个手抖,把/etc删掉了:别人无法ssh到上面,除了他.怎么办? 从类似的OK机器中打包一个etc.tar,然后将etc.tar放到OK机器www服务器目录里面:然后在问题机器上面通过w ...