00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 import unittest
00024 from PyKDL import *
00025 from math import *
00026 import random
00027
00028 class KinfamTestFunctions(unittest.TestCase):
00029
00030 def setUp(self):
00031 self.chain = Chain()
00032 self.chain.addSegment(Segment(Joint(Joint.RotZ),
00033 Frame(Vector(0.0,0.0,0.0))))
00034 self.chain.addSegment(Segment(Joint(Joint.RotX),
00035 Frame(Vector(0.0,0.0,0.9))))
00036 self.chain.addSegment(Segment(Joint(Joint.None),
00037 Frame(Vector(-0.4,0.0,0.0))))
00038 self.chain.addSegment(Segment(Joint(Joint.RotY),
00039 Frame(Vector(0.0,0.0,1.2))))
00040 self.chain.addSegment(Segment(Joint(Joint.None),
00041 Frame(Vector(0.4,0.0,0.0))))
00042 self.chain.addSegment(Segment(Joint(Joint.TransZ),
00043 Frame(Vector(0.0,0.0,1.4))))
00044 self.chain.addSegment(Segment(Joint(Joint.TransX),
00045 Frame(Vector(0.0,0.0,0.0))))
00046 self.chain.addSegment(Segment(Joint(Joint.TransY),
00047 Frame(Vector(0.0,0.0,0.4))))
00048 self.chain.addSegment(Segment(Joint(Joint.None),
00049 Frame(Vector(0.0,0.0,0.0))))
00050
00051 self.jacsolver = ChainJntToJacSolver(self.chain)
00052 self.fksolverpos = ChainFkSolverPos_recursive(self.chain)
00053 self.fksolvervel = ChainFkSolverVel_recursive(self.chain)
00054 self.iksolvervel = ChainIkSolverVel_pinv(self.chain)
00055 self.iksolverpos = ChainIkSolverPos_NR(self.chain,self.fksolverpos,self.iksolvervel)
00056
00057 def testFkPosAndJac(self):
00058 deltaq = 1E-4
00059 epsJ = 1E-4
00060
00061 F1=Frame()
00062 F2=Frame()
00063
00064 q=JntArray(self.chain.getNrOfJoints())
00065 jac=Jacobian(self.chain.getNrOfJoints())
00066
00067 for i in range(q.rows()):
00068 q[i]=random.uniform(-3.14,3.14)
00069
00070 self.jacsolver.JntToJac(q,jac)
00071
00072 for i in range(q.rows()):
00073 oldqi=q[i];
00074 q[i]=oldqi+deltaq
00075 self.assert_(0==self.fksolverpos.JntToCart(q,F2))
00076 q[i]=oldqi-deltaq
00077 self.assert_(0==self.fksolverpos.JntToCart(q,F1))
00078 q[i]=oldqi
00079 Jcol1 = diff(F1,F2,2*deltaq)
00080 Jcol2 = Twist(Vector(jac[0,i],jac[1,i],jac[2,i]),
00081 Vector(jac[3,i],jac[4,i],jac[5,i]))
00082 self.assertEqual(Jcol1,Jcol2);
00083
00084 def testFkVelAndJac(self):
00085 deltaq = 1E-4
00086 epsJ = 1E-4
00087
00088 q=JntArray(self.chain.getNrOfJoints())
00089 qdot=JntArray(self.chain.getNrOfJoints())
00090 for i in range(q.rows()):
00091 q[i]=random.uniform(-3.14,3.14)
00092 qdot[i]=random.uniform(-3.14,3.14)
00093
00094 qvel=JntArrayVel(q,qdot);
00095 jac=Jacobian(self.chain.getNrOfJoints())
00096
00097 cart = FrameVel.Identity();
00098 t = Twist.Zero();
00099
00100 self.jacsolver.JntToJac(qvel.q,jac)
00101 self.assert_(self.fksolvervel.JntToCart(qvel,cart)==0)
00102 MultiplyJacobian(jac,qvel.qdot,t)
00103 self.assertEqual(cart.deriv(),t)
00104
00105 def testFkVelAndIkVel(self):
00106 epsJ = 1E-7
00107
00108 q=JntArray(self.chain.getNrOfJoints())
00109 qdot=JntArray(self.chain.getNrOfJoints())
00110 for i in range(q.rows()):
00111 q[i]=random.uniform(-3.14,3.14)
00112 qdot[i]=random.uniform(-3.14,3.14)
00113
00114 qvel=JntArrayVel(q,qdot)
00115 qdot_solved=JntArray(self.chain.getNrOfJoints())
00116
00117 cart = FrameVel()
00118
00119 self.assert_(0==self.fksolvervel.JntToCart(qvel,cart))
00120 self.assert_(0==self.iksolvervel.CartToJnt(qvel.q,cart.deriv(),qdot_solved))
00121
00122 self.assertEqual(qvel.qdot,qdot_solved);
00123
00124
00125 def testFkPosAndIkPos(self):
00126 q=JntArray(self.chain.getNrOfJoints())
00127 for i in range(q.rows()):
00128 q[i]=random.uniform(-3.14,3.14)
00129
00130 q_init=JntArray(self.chain.getNrOfJoints())
00131 for i in range(q_init.rows()):
00132 q_init[i]=q[i]+0.1*random.random()
00133
00134 q_solved=JntArray(q.rows())
00135
00136 F1=Frame.Identity()
00137 F2=Frame.Identity()
00138
00139 self.assert_(0==self.fksolverpos.JntToCart(q,F1))
00140 self.assert_(0==self.iksolverpos.CartToJnt(q_init,F1,q_solved))
00141 self.assert_(0==self.fksolverpos.JntToCart(q_solved,F2))
00142
00143 self.assertEqual(F1,F2)
00144 self.assertEqual(q,q_solved)
00145
00146
00147 def suite():
00148 suite = unittest.TestSuite()
00149 suite.addTest(KinfamTestFunctions('testFkPosAndJac'))
00150 suite.addTest(KinfamTestFunctions('testFkVelAndJac'))
00151 suite.addTest(KinfamTestFunctions('testFkVelAndIkVel'))
00152 suite.addTest(KinfamTestFunctions('testFkPosAndIkPos'))
00153 return suite
00154
00155
00156
00157