00001
00002
00003 PKG = 'tf'
00004 import roslib
00005 roslib.load_manifest(PKG)
00006
00007 from geometry_msgs.msg import Pose, Point, Quaternion
00008 from tf import transformations
00009 import PyKDL
00010 import tf
00011 import rospy
00012 import numpy
00013
00014
00015
00016 def fromTf(tf):
00017 position, quaternion = tf
00018 x, y, z = position
00019 Qx, Qy, Qz, Qw = quaternion
00020 return PyKDL.Frame(PyKDL.Rotation.Quaternion(Qx, Qy, Qz, Qw),
00021 PyKDL.Vector(x, y, z))
00022
00023 def toTf(f):
00024 return ((f.p[0], f.p[1], f.p[2]),f.M.GetQuaternion())
00025
00026
00027
00028
00029 def fromMsg(p):
00030 return PyKDL.Frame(PyKDL.Rotation.Quaternion(p.orientation.x,
00031 p.orientation.y,
00032 p.orientation.z,
00033 p.orientation.w),
00034 PyKDL.Vector(p.position.x, p.position.y, p.position.z))
00035
00036 def toMsg(f):
00037 p = Pose()
00038 p.orientation.x, p.orientation.y, p.orientation.z, p.orientation.w = f.M.GetQuaternion()
00039 p.position.x = f.p[0]
00040 p.position.y = f.p[1]
00041 p.position.z = f.p[2]
00042 return p
00043
00044
00045
00046 def fromMatrix(m):
00047 return PyKDL.Frame(PyKDL.Rotation(m[0,0], m[0,1], m[0,2],
00048 m[1,0], m[1,1], m[1,2],
00049 m[2,0], m[2,1], m[2,2]),
00050 PyKDL.Vector(m[0,3], m[1, 3], m[2, 3]))
00051
00052 def toMatrix(f):
00053 return numpy.array([[f.M[0,0], f.M[0,1], f.M[0,2], f.p[0]],
00054 [f.M[1,0], f.M[1,1], f.M[1,2], f.p[1]],
00055 [f.M[2,0], f.M[2,1], f.M[2,2], f.p[2]],
00056 [0,0,0,1]])
00057
00058
00059
00060 def fromCameraParams(cv, rvec, tvec):
00061 m = numpy.array([ [ 0, 0, 0, tvec[0,0] ],
00062 [ 0, 0, 0, tvec[1,0] ],
00063 [ 0, 0, 0, tvec[2,0] ],
00064 [ 0, 0, 0, 1.0 ] ], dtype = numpy.float32)
00065 cv.Rodrigues2(rvec, m[:3,:3])
00066 return fromMatrix(m)
00067
00068
00069
00070
00071 def main():
00072 print "\n\n==== To and from tf"
00073 tf = ((1,2,3), (1,0,0,0))
00074 print tf
00075 print toTf(fromTf(tf))
00076
00077 print "\n\n==== To and from msg"
00078 p = Pose()
00079 p.orientation.x = 1
00080 p.position.x = 4
00081 print p
00082 print toMsg(fromMsg(p))
00083
00084 print "\n\n==== To and from matrix"
00085 m = numpy.array([[0,1,0,2], [0,0,1,3],[1,0,0,4],[0,0,0,1]])
00086 print m
00087 print toMatrix(fromMatrix(m))
00088
00089 print "\n\n==== Math"
00090 print toTf( fromMsg(p) * fromMatrix(m))
00091 print PyKDL.diff(fromMsg(p), fromMatrix(m))
00092 print PyKDL.addDelta(fromMsg(p), PyKDL.diff(fromMatrix(m), fromMsg(p)), 2.0)
00093
00094 if __name__ == '__main__': main()