00001
00002
00003 import yaml
00004 import types
00005
00006
00007 EPSILON = 1e-5
00008
00009 def get_yaml_data (fname):
00010 return yaml.load(open(fname).read());
00011
00012 def eps_equal (v1, v2, eps=EPSILON):
00013 return abs(v1 - v2) < eps;
00014
00015 def list_eps_equal (v1, v2, eps=EPSILON):
00016 return all(map (lambda x, y : eps_equal (x,y, eps), v1, v2))
00017
00018 def check_var (t1, t2, prop_name, eps=EPSILON):
00019 if type(t1.get(prop_name)) == types.StringType:
00020 v1 = map (lambda x : float(x), t1.get(prop_name).split(','))
00021 func = list_eps_equal
00022 else:
00023 v1 = t1.get(prop_name)
00024 func = eps_equal
00025 if type(t2.get(prop_name)) == types.StringType:
00026 v2 = map (lambda x : float(x), t2.get(prop_name).split(','))
00027 else:
00028 v2 = t2.get(prop_name)
00029 if v1 == None and v2 == None:
00030 print "Neglected"
00031 return True
00032 elif v1 != None and v2 != None:
00033 if func(v1, v2, eps):
00034 print "OK"
00035 return True;
00036 else:
00037 print "NG, check fail ", v1, " vs ", v2
00038 return False;
00039 else:
00040 print "NG, data missing among ", v1, " ", v2
00041 return False;
00042
00043 def check_str_var (t1, t2, prop_name):
00044 v1 = t1.get(prop_name)
00045 v2 = t2.get(prop_name)
00046 if v1 and v2:
00047 if v1 == v2:
00048 print "OK"
00049 return True;
00050 else:
00051 print "NG", v1, v2
00052 return False;
00053 else:
00054 print None
00055 return True
00056
00057 import unittest
00058 class TestRobotModelYaml(unittest.TestCase):
00059 def check_var(self, prop_name, param_name='Links', check_func=check_var):
00060 print " check", param_name
00061 ret = []
00062 for f in test2.get(param_name).keys():
00063 t2 = test2.get(param_name).get(f)
00064 t1 = test1.get(param_name).get(f)
00065 print " ", f, prop_name,
00066 if t1 and t2:
00067 ret.append(check_func(t1, t2, prop_name))
00068 else:
00069 print None
00070 return all(ret)
00071
00072
00073
00074
00075
00076 def test_Total_Mass (self):
00077 print 'check Total'
00078 print ' Mass ',
00079 self.assertTrue(check_var(test1.get('Total'), test2.get('Total'), 'Mass', 1e-3))
00080 def test_Total_GlobalCom (self):
00081 print 'check Total'
00082 print ' GlobalCom ',
00083 self.assertTrue(check_var(test1.get('Total'), test2.get('Total'), 'GlobalCom'))
00084 def test_Total_GlobalInertia (self):
00085 print 'check Total'
00086 print ' GlobalInertia ',
00087 self.assertTrue(check_var(test1.get('Total'), test2.get('Total'), 'GlobalInertia', 1e-4))
00088 def test_Link_Mass (self):
00089 self.assertTrue(self.check_var('Mass'))
00090 def test_Link_LocalCom (self):
00091 self.assertTrue(self.check_var('LocalCom'))
00092 def test_Link_GlobalCom (self):
00093 self.assertTrue(self.check_var('GlobalCom'))
00094 def test_Link_ComNorm (self):
00095 self.assertTrue(self.check_var('ComNorm'))
00096 def test_Link_LocalInertia (self):
00097 self.assertTrue(self.check_var('LocalInertia'))
00098 def test_Link_GlobalInertia (self):
00099 self.assertTrue(self.check_var('GlobalInertia'))
00100 def test_Link_InertiaNorm (self):
00101 self.assertTrue(self.check_var('InertiaNorm'))
00102 def test_Link_LocalPos (self):
00103 self.assertTrue(self.check_var('LocalPos'))
00104 def test_Link_LocalRot (self):
00105 self.assertTrue(self.check_var('LocalRot'))
00106 def test_Link_GlobalPos (self):
00107 self.assertTrue(self.check_var('GlobalPos'))
00108 def test_Link_GlobalRot (self):
00109 self.assertTrue(self.check_var('GlobalRot'))
00110 def test_Joint_LowerLimit (self):
00111 self.assertTrue(self.check_var('LowerLimit', 'Joints'))
00112 def test_Joint_UpperLimit (self):
00113 self.assertTrue(self.check_var('UpperLimit', 'Joints'))
00114 def test_Joint_VelocityLimit (self):
00115 self.assertTrue(self.check_var('VelocityLimit', 'Joints'))
00116 def test_Sensor_LocalPos (self):
00117 self.assertTrue(self.check_var('LocalPos', 'Sensors'))
00118 def test_Sensor_LocalRot (self):
00119 self.assertTrue(self.check_var('LocalRot', 'Sensors'))
00120 def test_Sensor_GlobalPos (self):
00121 self.assertTrue(self.check_var('GlobalPos', 'Sensors'))
00122 def test_Sensor_GlobalRot (self):
00123 self.assertTrue(self.check_var('GlobalRot', 'Sensors'))
00124
00125 if __name__ == '__main__':
00126 import sys
00127 import rostest
00128 global test1, test2
00129 test1=get_yaml_data(sys.argv[1])
00130 test2=get_yaml_data(sys.argv[2])
00131 str1=sys.argv[1].split('/')[-1].split('.')[0].replace('_robot_model', '')
00132 str2=sys.argv[2].split('/')[-1].split('.')[0].replace('_robot_model', '')
00133 rostest.unitrun('euslisp_model_conversion_tester', 'test_robot_model_yaml_'+str1+"_"+str2, TestRobotModelYaml)
00134