測試 launch 檔案如下, test.py 廣播 tf 變換, listen_tf.py 檔案監聽tf變換。
廣播 test.py 內容如下,發布了兩個tf 的資料
#!/usr/bin/env python
#-*-coding: utf-8 -*-
import rospy
from geometry_msgs.msg import twist
import tf
class arduinoros():
def __init__(self):
# 唯一的節點名 日誌級別為debug 等級排序為 debug info warn error fatal
rospy.init_node('arduino', log_level=rospy.debug)
# self. 變數, 從 .yaml 獲取引數
self.base_frame = rospy.get_param("~base_frame", 'base_link')
self.base_frame1 = rospy.get_param("base_frame", 'base_link')
self.yaw = 0
rospy.on_shutdown(self.shutdown)
while not rospy.is_shutdown():
#print "param : " + self.base_frame + " " + self.base_frame1
des_br = tf.transformbroadcaster()
# 發布 tf 變換
des_br.sendtransform((1000, 1000, 0),
(0, 0, 0, 1),
rospy.time.now(),
"tf_destination",
"world")
gps_br = tf.transformbroadcaster()
gps_br.sendtransform((800, 1200, 0),
(0, 0, 0, 1),
rospy.time.now(),
"tf_gps",
"world")
rospy.sleep(1)
def shutdown():
print "shut down node..."
if __name__ == '__main__':
myarduino = arduinoros()
監聽的 listen_tf.py 的內容如下,
(trans,rot) = listener.lookuptransform('/tf_gps', '/tf_destination', rospy.time(0))
返回值trans rot是啥, 在網上苦苦找尋無果,只好自己測試得出
#!/usr/bin/env python
#-*-coding: utf-8 -*-
import rospy
from geometry_msgs.msg import twist
import tf
import math
class arduinoros():
def __init__(self):
# 唯一的節點名 日誌級別為debug 等級排序為 debug info warn error fatal
rospy.init_node('listen', log_level=rospy.debug)
listener = tf.transformlistener()
rospy.on_shutdown(self.shutdown)
while not rospy.is_shutdown():
try:
(trans,rot) = listener.lookuptransform('/tf_gps', '/tf_destination', rospy.time(0))
rospy.loginfo("trans: %f %f %f ", trans[0], trans[1], trans[2])
rospy.loginfo(" rot : %f %f %f %f", rot[0], rot[1], rot[2], rot[3])
#print trans, rot
#angular = 0.4 * math.atan2(trans[1], trans[0])
#linear = 0.4 * math.sqrt(trans[0] ** 2 + trans[1] ** 2)
#rospy.loginfo("trans = %f %f ,linear = %f ,angular = %f", trans[0], trans[1], linear, angular)
except (tf.lookupexception, tf.connectivityexception, tf.extrapolationexception):
print "param : "
rospy.sleep(1)
def shutdown():
print "shut down node..."
if __name__ == '__main__':
myarduino = arduinoros()
測試結果如下:
測試結論:
(trans,rot) = listener.lookuptransform('/tf_gps', '/tf_destination', rospy.time(0))
返回的trans 是三個軸x y z的相對距離
返回的 rot 是四元數變換,不過在這裡沒有深究
現在知道了,這兩句話是什麼意思了
angular = math.atan2(trans[1], trans[0])
linear = math.sqrt(trans[0] ** 2 + trans[1] ** 2)
ROS中TF變換詳解
前言 1.python實現tf發布 2.c 實現tf發布 ros中發布tf變換 tf變換時描述機械人各個座標系之間的位置變換關係,在ros中發布這些關係需要使用tf工具。tf變換具體內容可以參考官方文件 tf變換需要設定的內容在ros wiki中已有詳細介紹 發布主要由兩部分構成,分別是平移和旋轉變...
Ros中tf的相關功能包
能夠監聽當前時刻所有通過ros廣播的tf座標系,並繪製出樹狀圖表示座標系之間的連線關係儲存到離線檔案中 rosrun tf view frames雖然view frames能夠將當前座標系關係儲存在離線檔案中,但是無法實時反映座標關係,所以可以用rqt tf tree實時重新整理顯示座標系關係 ro...
老王說ros的tf庫
ros的tf庫 為了這個題目,我是拿出了擠溝的精神擠時間,是下了功夫的,線性代數 矩陣論複習了,慣性導航裡的dcm 四元數也了解了,剛體力學也翻了,wiki裡的尤拉角也讀了,tf的tutorial source code也都看了。說實在的,經過這次努力,我是有點了然於胸了,我也非常想了然於紙上與小夥...