test_hironx.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 PKG = 'hironx_ros_bridge'
00005 # rosbuild needs load_manifest
00006 try:
00007     import roslib
00008     import hironx_ros_bridge
00009 except:
00010     import roslib; roslib.load_manifest(PKG)
00011     import hironx_ros_bridge
00012 
00013 from hironx_ros_bridge import hironx_client as hironx
00014 from hrpsys.hrpsys_config import euler_from_matrix
00015 from hrpsys import rtm
00016 
00017 import os
00018 import unittest
00019 import time
00020 import tempfile
00021 
00022 import math
00023 import random
00024 import numpy
00025 import re
00026 
00027 from rtm import connectPorts, disconnectPorts
00028 
00029 class TestHiro(unittest.TestCase):
00030 
00031     @classmethod
00032     def setUpClass(cls):
00033         #modelfile = rospy.get_param("hironx/collada_model_filepath")
00034         #rtm.nshost = 'hiro024'
00035         #robotname = "RobotHardware0"
00036 
00037         cls.robot = hironx.HIRONX()
00038         #cls.robot.init(robotname=robotname, url=modelfile)
00039         cls.robot.init()
00040 
00041     @classmethod
00042     def tearDownClass(cls):
00043         #self.write_output_to_pdf("test-hironx.pdf") # don't know how to call this mehtod
00044         True
00045 
00046     def test_goInitial(self):
00047         self.limbbody_init()
00048         self.robot.goInitial()
00049 
00050 #    def test_goOffPose(self):
00051 #        self.limbbody_init()
00052 #        self.robot.goOffPose()
00053 
00054     ###
00055     def set_joint_angles_no_wait_test (self):
00056         # send initial pose
00057         av0 = [-0.6, 0, -100, 15.2, 9.4, 3.2]
00058         av1 = [-0.6, -90, -100, 15.2, 9.4, 3.2]
00059         self.robot.setJointAnglesOfGroup( "rarm" , av0,  5)
00060         pose0 = robot.getReferencePose("RARM_JOINT5")
00061         print pose0
00062 
00063         # make sure setJointAnglesOfGroup with wait
00064         tm0 = time.time()
00065         self.robot.setJointAnglesOfGroup( "rarm" , av1,  5)
00066         self.robot.setJointAnglesOfGroup( "rarm" , av0,  5)
00067         tm1 = time.time()
00068         pose1 = self.robot.getReferencePose("RARM_JOINT5")
00069         print "setJointAnglesOfGroup : reach goal?  ", numpy.linalg.norm(numpy.array(pose1)-numpy.array(pose0)) < 5.0e-3
00070         print "                      : spend 10 sec?", abs((tm1 - tm0) - 10.0) < 0.1, " " , tm1 - tm0
00071 
00072         # make sure setJointAnglesOfGorup with wita=False returns immediately
00073         self.robot.setJointAnglesOfGroup( "rarm" , av1,  5)
00074         tm0 = time.time()
00075         self.robot.setJointAnglesOfGroup( "rarm" , av0,  5, wait=False)
00076         tm1 = time.time()
00077         pose1 = self.robot.getReferencePose("RARM_JOINT5")
00078         print "setJointAnglesOfGroup(wait=False)", numpy.linalg.norm(numpy.array(pose1)-numpy.array(pose0)) > 5.0e-3
00079         print "                      : spend 10 sec?", abs((tm1 - tm0) - 0.0) < 0.1, " " , tm1 - tm0
00080 
00081         # make sure that waitInterpolationOfGroup wait until target reaches the goal
00082         self.robot.waitInterpolationOfGroup("rarm")
00083         tm1 = time.time()
00084         pose1 = self.robot.getReferencePose("RARM_JOINT5")
00085         print "waitInterpolationOfGroup", numpy.linalg.norm(numpy.array(pose1)-numpy.array(pose0)) < 5.0e-3
00086         print "                      : spend 10 sec?", abs((tm1 - tm0) - 5.0) < 0.1, " " , tm1 - tm0
00087 
00088     # load from log data, [[p0,p1,....p25],[p0,p1,....p25],...,[p0,p1,....p25]]
00089     def check_q_data(self,name):
00090         import math
00091         data = []
00092         name = name+".q"
00093         f = open(name)
00094         start_time = None
00095         for line in f:
00096             current_time = float(line.split(' ')[0])
00097             current_value = float(line.split(' ')[7])
00098             if not start_time:
00099                 start_time = current_time
00100             data.append([current_time, current_value*180/math.pi])
00101         # print "%f %f" % (current_time - start_time, current_value*180/math.pi)
00102         f.close()
00103         self.filenames.append(name)
00104         return data
00105 
00106     # load from log data, [[p0,p1,....p25],[p0,p1,....p25],...,[p0,p1,....p25]] # degree
00107     def load_log_data(self,name):
00108         import math
00109         data = []
00110         f = open(name)
00111         start_time = None
00112         for line in f:
00113             data.append([float(i)*180/math.pi for i in line.split(' ')[1:-1]])
00114         # print "%f %f" % (current_time - start_time, current_value*180/math.pi)
00115         f.close()
00116         self.filenames.append(name)
00117         return data
00118 
00119     def check_log_data(self, data, idx, tm_data, min_data, max_data, acc_thre=0.06, tm_thre=0.1): # expected, time, min, max of index
00120         _tm_data = len(data)/200.0
00121         _min_data = min([d[idx] for d in data])
00122         _max_data = max([d[idx] for d in data])
00123         _tm_thre = tm_thre
00124         # min_data = [min_data, min_thre]
00125         if isinstance(min_data, (int, float)):
00126             min_data = [min_data, 5]
00127         if isinstance(max_data, (int, float)):
00128             max_data = [max_data, 5]
00129 
00130         print "time (= ", _tm_data, ") == ", tm_data, " -> ", abs(_tm_data - tm_data) < tm_data*_tm_thre
00131         print " min (= ", _min_data, ") == ", min_data, " -> ", abs(_min_data - min_data[0]) < min_data[1]
00132         print " max (= ", _max_data, ") == ", max_data, " -> ", abs(_max_data - max_data[0]) < max_data[1]
00133         self.assertTrue(abs(_tm_data - tm_data) < tm_data*_tm_thre)
00134         self.assertTrue(abs(_min_data - min_data[0]) < min_data[1])
00135         self.assertTrue(abs(_max_data - max_data[0]) < max_data[1])
00136 
00137         # check acceleration
00138         flag = True
00139         for i in range(1, len(data)-1):
00140             for j in range(len(data[i])):
00141                 p0 = data[i-1][j]
00142                 p1 = data[i+0][j]
00143                 p2 = data[i+1][j]
00144                 v0 = p1 - p0
00145                 v1 = p2 - p1
00146                 if abs(v1 - v0) > acc_thre:
00147                     flag = False
00148                     print("Acceleration vaiorated! : n = %d, idx %d, p0 %f, p1 %f, p2 %f, v1 %f, v2 %f, acc %f (%f)" % (i, j, p0, p1, p2, v0, v1, v1-v0, (v1-v0)/180.0*math.pi))
00149         self.assertTrue(flag)
00150 
00151     def write_d_dd_data(self, name):
00152         name_d = os.path.splitext(name)[0]+".dq"
00153         name_dd = os.path.splitext(name)[0]+".ddq"
00154         f = open(name)
00155         f_d = open(name_d, 'w')
00156         f_dd = open(name_dd, 'w')
00157         line_0 = None
00158         line_1 = None
00159         line_2 = None
00160         for line_0 in f:
00161             if line_2 and line_1 :
00162                 tm = float(line_1.split(' ')[0])
00163                 f_d.write(str(tm))
00164                 f_dd.write(str(tm))
00165                 for i in range(1,len(line_0.split(' '))-1):
00166                     p0 = float(line_0.split(' ')[i])
00167                     p1 = float(line_1.split(' ')[i])
00168                     p2 = float(line_2.split(' ')[i])
00169                     v0 = p1 - p0
00170                     v1 = p2 - p1
00171                     f_d.write(" %.10f"%v0)
00172                     f_dd.write(" %.10f"%(v1 - v0))
00173                 f_d.write('\n')
00174                 f_dd.write('\n')
00175             line_2 = line_1
00176             line_1 = line_0
00177         f.close()
00178         f_d.close()
00179         f_dd.close()
00180         print "[%s] write %s"%(__file__, name_d)
00181         print "[%s] write %s"%(__file__, name_dd)
00182 
00183     def write_all_joint_pdf(self, name, pdf_name):
00184         print "[%s] write pdf %s from log data %s"%(__file__, pdf_name, name)
00185         self.write_d_dd_data(name)
00186         _pdf_names = []
00187         for _name in [name, os.path.splitext(name)[0]+".dq", os.path.splitext(name)[0]+".ddq"]:
00188             _pdf_names.append(_name+"_"+pdf_name)
00189             cmd = "gnuplot -p -e \"set terminal pdf; set output '"+_pdf_names[-1]+"'; plot "
00190             f = open(_name)
00191             l = f.readline().split(' ')[:-1]
00192             f.close()
00193             for i in range(1,len(l)):
00194                 cmd += "'"+_name+"' using 1:"+str(i+1)+" title 'joint "+str(i)+"' with lines"
00195                 if i != len(l)-1:
00196                     cmd += ","
00197             cmd += "\""
00198             os.system(cmd)
00199         cmd_str = 'pdfunite '+' '.join(_pdf_names) + ' ' + pdf_name
00200         cmd_str = cmd_str.replace('(', '\(')
00201         cmd_str = cmd_str.replace(')', '\)')
00202         os.system(cmd_str)
00203         return
00204 
00205     def check_acceleration(self, name):
00206         name1 = name+".q"
00207         name2 = name+".acc"
00208         f1 = open(name1)
00209         f2 = open(name2, 'w')
00210         line_0 = None
00211         line_1 = None
00212         line_2 = None
00213         for line_0 in f1:
00214             if line_2 and line_1 :
00215                 tm = float(line_1.split(' ')[0])
00216                 f2.write(str(tm))
00217                 for i in range(1,len(line_0.split(' '))-1):
00218                     p0 = float(line_0.split(' ')[i])
00219                     p1 = float(line_1.split(' ')[i])
00220                     p2 = float(line_2.split(' ')[i])
00221                     v0 = p1 - p0
00222                     v1 = p2 - p1
00223                     if abs(v1 - v0) > 0.0001:
00224                         print("%s Acceleration vaiorated! : p0 %f, p1 %f, p2 %f, v1 %f, v2 %f, acc %f" % (name, p0, p1, p2, v0, v1, v1-v0))
00225                     self.assertTrue(abs(v1 - v0) < 0.0001)
00226                     f2.write(' ' + str(v1 - v0))
00227                 f2.write('\n')
00228             line_2 = line_1
00229             line_1 = line_0
00230         f1.close()
00231         f2.close()
00232 
00233     def fullbody_init (self):
00234         self.filenames = []
00235         self.robot.seq_svc.removeJointGroup("larm")
00236         self.robot.seq_svc.removeJointGroup("rarm")
00237         self.robot.seq_svc.removeJointGroup("head")
00238         #self.robot.seq_svc.removeJointGroup("torso") we use torso for sleep certain time, so not remove this
00239         self.filename_base = tempfile.mkstemp()[1]
00240 
00241     def test_fullbody_setJointAngles_Wait (self):
00242         self.fullbody_init()
00243         # move to initiali position
00244         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -120, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00245         self.robot.waitInterpolation()
00246 
00247         self.robot.clearLog()
00248         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -140, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00249         self.robot.waitInterpolation()
00250         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -100, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00251         self.robot.waitInterpolation()
00252         filename = self.filename_base + "-wait"
00253         self.robot.saveLog(filename)
00254         q_filename = filename+"."+self.robot.rh.name()+"_q"
00255         data = self.load_log_data(q_filename)
00256 
00257         print "check setJointAngles(wait=True)"
00258         self.check_log_data(data, 6, 10.0, -140.0, -100.0)
00259         return True
00260 
00261     def test_fullbody_setJointAngles_NoWait (self):
00262         self.fullbody_init()
00263         clear_time = [4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0]
00264         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -100, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00265         self.robot.waitInterpolation()
00266         for i in range(len(clear_time)):
00267             self.robot.clearLog()
00268             self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -140, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00269             self.robot.setJointAnglesOfGroup("torso", [0], clear_time[i], wait=True);#  time.sleep(clear_time[i]);
00270 
00271             self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -100, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00272             self.robot.waitInterpolation()
00273             filename = self.filename_base + "-no-wait-"+str(clear_time[i])
00274             self.robot.saveLog(filename)
00275             q_filename = filename+"."+self.robot.rh.name()+"_q"
00276             data = self.load_log_data(q_filename)
00277 
00278             print "check setJointAngles(wait=True)"
00279             self.check_log_data(data, 6, (10.0 - (5 - clear_time[i])), [-140+i*40/len(clear_time),20], -100.0)
00280 
00281     def test_fullbody_setJointAngles_Clear (self):
00282         self.fullbody_init()
00283         # check if clear stops interpolation
00284         clear_time = [4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0]
00285         for i in range(len(clear_time)):
00286             self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -100, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00287             self.robot.waitInterpolation()
00288             self.robot.clearLog()
00289             self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -100, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5-clear_time[i]);
00290             self.robot.waitInterpolation()
00291             self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -140, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 5);
00292             self.robot.setJointAnglesOfGroup("torso", [0], clear_time[i], wait=True);#  time.sleep(clear_time[i]);
00293 
00294             self.robot.clear()
00295             self.robot.waitInterpolation()
00296             filename = self.filename_base + "-clear-"+str(clear_time[i])
00297             self.robot.saveLog(filename)
00298             q_filename = filename+"."+self.robot.rh.name()+"_q"
00299             data = self.load_log_data(q_filename)
00300 
00301             print "check setJointAngles(Clear)"
00302             self.check_log_data(data, 6, 5, [-140+(i+1)*40/len(clear_time),20], -100.0, acc_thre=0.2)
00303 
00304     def test_fullbody_setJointAngles_minus(self):
00305         self.fullbody_init()
00306         self.robot.el_svc.setServoErrorLimit("all", 0.01) # default is 0.18
00307         
00308         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -120, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 3);
00309         self.robot.waitInterpolation()
00310         self.robot.clearLog()
00311         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -120, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 1);
00312         self.robot.waitInterpolation()
00313 
00314         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -140, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], -1);
00315         self.robot.waitInterpolation()
00316 
00317         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -140, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 3);
00318         self.robot.waitInterpolation()
00319 
00320         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -120, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 0);
00321         self.robot.waitInterpolation()
00322 
00323         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -120, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 1);
00324         self.robot.waitInterpolation()
00325 
00326         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -140, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 0.1);
00327         self.robot.waitInterpolation()
00328 
00329         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -140, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 1.0);
00330         self.robot.waitInterpolation()
00331 
00332         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -120, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 0.05);
00333         self.robot.waitInterpolation()
00334 
00335         self.robot.setJointAngles([0, 0, 0, 0,   -0.6, 0, -120, 15.2, 9.4, 3.2,  0,0,0,0,    -0.6, 0, -100,-15.2, 9.4,-3.2,   0, 0, 0, 0], 1.0);
00336         self.robot.waitInterpolation()
00337 
00338         filename = self.filename_base + "-minus"
00339         self.robot.saveLog(filename)
00340         # write pdf file
00341         q_filename = filename+"."+self.robot.rh.name()+"_q"
00342         self.write_all_joint_pdf(q_filename, "full_minus_check.pdf")
00343 
00344         # assertion
00345         data = self.load_log_data(q_filename)
00346         print "check setJointAngles(minus)"
00347         self.check_log_data(data, 6, 7.19, -140, -120.0, acc_thre = 1.0)
00348 
00349         self.robot.el_svc.setServoErrorLimit("all", 0.18) # default is 0.18
00350 
00351     ####
00352 
00353     def limbbody_init (self):
00354         self.filenames = []
00355         self.robot.setSelfGroups()
00356         self.filename_base = tempfile.mkstemp()[1]
00357 
00358     def assertTrue(self,a):
00359         assert(a)
00360 
00361     def test_rarm_setJointAngles_Wait (self):
00362         self.limbbody_init()
00363         # move to initiali position
00364         self.robot.setJointAnglesOfGroup("rarm",[-0.6, 0, -120, 15.2, 9.4, 3.2], 5, wait=False);
00365         self.robot.waitInterpolationOfGroup("rarm")
00366         self.robot.clearLog()
00367 
00368         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -140, 15.2, 9.4, 3.2], 5, wait=False);
00369         self.robot.waitInterpolationOfGroup("rarm")
00370         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 5, wait=False);
00371         self.robot.waitInterpolationOfGroup("rarm")
00372         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 5, wait=False);
00373         self.robot.waitInterpolationOfGroup("rarm")
00374 
00375         filename = self.filename_base + "-wait"
00376         self.robot.saveLog(filename)
00377         q_filename = filename+"."+self.robot.rh.name()+"_q"
00378         data = self.load_log_data(q_filename)
00379 
00380         print "check setJointAnglesOfGroup(wait=True)"
00381         self.check_log_data(data, 6, 15.0, -140.0, -100.0)
00382         return True
00383 
00384     def test_rarm_setJointAngles_NoWait (self):
00385         self.limbbody_init()
00386         clear_time = [4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0]
00387         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 5, wait=False);
00388         self.robot.waitInterpolationOfGroup("rarm")
00389         for i in range(len(clear_time)):
00390             self.robot.clearLog()
00391 
00392             self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -140, 15.2, 9.4, 3.2], 5, wait=False);
00393             self.robot.setJointAnglesOfGroup("larm", [-0.6, 0, -100, 15.2, 9.4, 3.2], clear_time[i], wait=True);#  time.sleep(clear_time[i]);
00394             self.robot.setJointAnglesOfGroup("rarm",[-0.6, 0, -100, 15.2, 9.4, 3.2], 5, wait=False);
00395             self.robot.waitInterpolationOfGroup("rarm")
00396 
00397             filename = self.filename_base + "-no-wait-"+str(clear_time[i])
00398             self.robot.saveLog(filename)
00399             q_filename = filename+"."+self.robot.rh.name()+"_q"
00400             data = self.load_log_data(q_filename)
00401 
00402             print "check setJointAnglesOfGroup(wait=True) "+str(clear_time[i])
00403             self.check_log_data(data, 6, (10.0 - (5 - clear_time[i])), [(-140+i*40/len(clear_time)),20], -100.0)
00404 
00405 
00406     def test_rarm_setJointAngles_Clear (self):
00407         self.limbbody_init()
00408         # check if clear stops interpolation
00409         clear_time = [4.5, 4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0]
00410         for i in range(len(clear_time)):
00411             self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -120, 15.2, 9.4, 3.2], 5, wait=False);
00412             self.robot.waitInterpolationOfGroup("rarm")
00413             self.robot.clearLog()
00414 
00415             self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 5, wait=False);
00416             self.robot.waitInterpolationOfGroup("rarm")
00417             self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -140, 15.2, 9.4, 3.2], 5, wait=False);
00418             self.robot.setJointAnglesOfGroup("larm", [-0.6, 0, -100, 15.2, 9.4, 3.2], clear_time[i], wait=True);#  time.sleep(clear_time[i]);
00419             self.robot.clearOfGroup("rarm")
00420             self.robot.waitInterpolationOfGroup("rarm")
00421 
00422             filename = self.filename_base + "-clear-"+str(clear_time[i])
00423             self.robot.saveLog(filename)
00424             q_filename = filename+"."+self.robot.rh.name()+"_q"
00425             data = self.load_log_data(q_filename)
00426 
00427             print "check setJointAnglesOfGroup(clear) "+str(clear_time[i])
00428             self.check_log_data(data, 6, (5 + clear_time[i]), [(-140+i*40/len(clear_time)),20], -100.0)
00429 
00430     def test_rarm_setJointAnglesOfGroup_Override_Acceleration (self):
00431         self.limbbody_init()
00432         #disconnectPorts(self.robot.rh.port("q"), self.robot.log.port("q"))
00433         #connectPorts(self.robot.el.port("q"), self.robot.log.port("q"))
00434 
00435         #self.robot.setJointAnglesOfGroup("rarm", [ 25, -139, -157,  45, 0, 0], 3, wait=False);
00436         #self.robot.setJointAnglesOfGroup("larm", [-25, -139, -157, -45, 0, 0], 3, wait=False);
00437         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -120, 15.2, 9.4, 3.2], 3, wait=False);
00438         self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 3, wait=False);
00439         self.robot.waitInterpolationOfGroup("rarm")
00440         self.robot.clearLog()
00441         #self.robot.log_svc.maxLength(200*30)
00442 
00443         # self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -140, 15.2, 9.4, 3.2], 3, wait=False);
00444         # time.sleep(1.5);
00445         # self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 3, wait=False);
00446         # self.robot.waitInterpolationOfGroup("rarm")
00447         # self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 3, wait=False);
00448         # self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -140,-15.2, 9.4,-3.2], 3, wait=False);
00449         # time.sleep(1.5);
00450         # self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 3, wait=False);
00451         # self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 3, wait=False);
00452         # self.robot.waitInterpolationOfGroup("rarm")
00453         # self.robot.waitInterpolationOfGroup("larm")
00454         # self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 0.1, wait=False);
00455         # self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 0.1, wait=False);
00456         # self.robot.waitInterpolationOfGroup("rarm")
00457         # self.robot.waitInterpolationOfGroup("larm")
00458         # self.robot.setJointAnglesOfGroup("rarm", [ 25, -139, -157,  45, 0, 0], 3, wait=False);
00459         # self.robot.setJointAnglesOfGroup("larm", [-25, -139, -157, -45, 0, 0], 3, wait=False);
00460         # time.sleep(2.5);
00461         # for i in range(1,10):
00462         #     time.sleep(i/30.0)
00463         #     self.robot.setJointAnglesOfGroup("larm",
00464         #                                      [ 0.6, 0, -120,-15.2, 9.4,-3.2]+numpy.random.normal(0,1,6),
00465         #                                      3, wait=False);
00466 
00467         for i in range(3):
00468             self.robot.setJointAnglesOfGroup("rarm",
00469                                              [-0.6, 0, -100, 15.2, 9.4, 3.2]+numpy.random.normal(0,1,6),
00470                                              3, wait=False);
00471             self.robot.setJointAnglesOfGroup("larm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 1.5, wait=True);#  time.sleep(clear_time[i]);
00472 
00473         #self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 1, wait=False);
00474         #self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -120, 15.2, 9.4, 3.2], 1, wait=False);
00475         #self.robot.waitInterpolationOfGroup("rarm")
00476         #self.robot.waitInterpolationOfGroup("larm")
00477         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -140, 15.2, 9.4, 3.2], 3, wait=False);
00478         self.robot.setJointAnglesOfGroup("larm", [-0.6, 0, -100, 15.2, 9.4, 3.2], 1.5, wait=True);#  time.sleep(clear_time[i]);
00479         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -120, 15.2, 9.4, 3.2], 3, wait=False);
00480         #time.sleep(1.5)
00481         #self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 0, wait=False);
00482         #self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -120, 15.2, 9.4, 3.2], 0, wait=False);
00483         self.robot.waitInterpolationOfGroup("rarm")
00484 
00485         filename = self.filename_base + "-no-wait2"
00486         self.robot.saveLog(filename)
00487 
00488         # write pdf file
00489         q_filename = filename+"."+self.robot.rh.name()+"_q"
00490         self.write_all_joint_pdf(q_filename, "rarm_accel_check.pdf")
00491 
00492         # assertion
00493         data = self.load_log_data(q_filename)
00494         print "check setJointAnglesOfGroup(Override_acceleratoin)"
00495         self.check_log_data(data, 6, 9, -135, -100.0)
00496 
00497 
00498     def write_output_to_pdf (self,name):
00499         print ";; write log data to "+name
00500         global filenames
00501         import os
00502         cmd = "gnuplot -p -e \"set terminal pdf; set output '"+name+"'; plot "
00503         for name in self.filenames:
00504             cmd += "'"+name+"' using 0:8 title '"+name+"' with lines"
00505             if name != self.filenames[-1]:
00506                 cmd += ","
00507         cmd += "\""
00508         os.system(cmd)
00509         return cmd
00510 
00511     def test_rarm_setJointAnglesOfGroup_minus(self):
00512         self.limbbody_init()
00513         self.robot.el_svc.setServoErrorLimit("all", 0.01) # default is 0.18
00514 
00515         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -120, 15.2, 9.4, 3.2], 3, wait=False);
00516         self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 3, wait=False);
00517         self.robot.waitInterpolationOfGroup("rarm")
00518         self.robot.clearLog()
00519         self.robot.setJointAnglesOfGroup("rarm", [-0.6, 0, -120, 15.2, 9.4, 3.2], 1, wait=False);
00520         self.robot.setJointAnglesOfGroup("larm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 1, wait=False);
00521         self.robot.waitInterpolationOfGroup("rarm")
00522 
00523         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], -1, wait=True);
00524 
00525         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -140,-15.2, 9.4,-3.2], -1, wait=True);
00526         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -140,-15.2, 9.4,-3.2], 3, wait=True);
00527         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 0, wait=True);
00528         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 1.0, wait=True);
00529         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -140,-15.2, 9.4,-3.2], 0.1, wait=True);
00530         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -140,-15.2, 9.4,-3.2], 1.0, wait=True);
00531         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 0.05, wait=True);
00532         self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -120,-15.2, 9.4,-3.2], 1.0, wait=True);
00533         #self.robot.setJointAnglesOfGroup("rarm", [ 0.6, 0, -140,-15.2, 9.4,-3.2], 1.0, wait=True);
00534 
00535         filename = self.filename_base + "-minus"
00536         self.robot.saveLog(filename)
00537         # write pdf file
00538         q_filename = filename+"."+self.robot.rh.name()+"_q"
00539         self.write_all_joint_pdf(q_filename, "rarm_minus_check.pdf")
00540 
00541         # assertion
00542         data = self.load_log_data(q_filename)
00543         print "check setJointAnglesOfGroup(minus)"
00544         self.check_log_data(data, 6, 7.19, -140, -120.0, acc_thre = 1.5, tm_thre = 0.3)
00545 
00546         self.robot.el_svc.setServoErrorLimit("all", 0.18) # default is 0.18
00547 
00548     def testSetTargetPoseBothArm(self):
00549         tm = 10
00550         self.robot.goInitial()
00551         posl1 = self.robot.getCurrentPosition('LARM_JOINT5')
00552         posl2 = self.robot.getCurrentPosition('LARM_JOINT5')
00553         posr1 = self.robot.getCurrentPosition('RARM_JOINT5')
00554         posr2 = self.robot.getCurrentPosition('RARM_JOINT5')
00555         rpyl1 = self.robot.getCurrentRPY('LARM_JOINT5')
00556         rpyr1 = self.robot.getCurrentRPY('RARM_JOINT5')
00557         posr1[0] += 0.05
00558         posr2[2] += 0.08
00559         posl1[0] -= 0.09
00560         posl2[2] -= 0.07
00561         # Til here initializing.
00562 
00563         if not self.robot.setTargetPose('larm', posl1, rpyl1, tm):
00564             assert(False)
00565         if not self.robot.setTargetPose('rarm', posr1, rpyr1, tm):
00566             assert(False)
00567         print('Before waitInterpolationOfGroup(larm) begins.')
00568         self.robot.waitInterpolationOfGroup('larm')
00569         print('waitInterpolationOfGroup(larm) returned.')
00570         self.robot.waitInterpolationOfGroup('rarm') # just to make sure
00571         print('waitInterpolationOfGroup(rarm) returned.')
00572         if not self.robot.setTargetPose('larm', posl2, rpyl1, tm):
00573             assert(False)
00574         if not self.robot.setTargetPose('rarm', posr2, rpyr1, tm):
00575             assert(False)
00576 
00577         # Making sure if reached here. If any error occurred. If not reached
00578         # assert false should be returned earlier.
00579         assert(True)  
00580 
00581     def testGetReferencePose(self):
00582         def print_pose(msg, pose):
00583             print msg, (pose[3],pose[7],pose[11]), euler_from_matrix([pose[0:3], pose[4:7], pose[8:11]], 'sxyz')
00584         self.robot.goInitial()
00585         posel1 = self.robot.getReferencePose('LARM_JOINT5')
00586         poser1 = self.robot.getReferencePose('RARM_JOINT5')
00587         try:
00588             posel2 = self.robot.getReferencePose('LARM_JOINT5:WAIST')
00589             poser2 = self.robot.getReferencePose('RARM_JOINT5:WAIST')
00590         except RuntimeError as e:
00591             if re.match(r'frame_name \(.+\) is not supported', e.message):
00592                 print(e.message + "...this is expected so pass the test")
00593                 return True
00594             elif self.robot.fk.ref.get_component_profile().version <= '315.2.4':
00595                 print("target version is " + self.robot.fk.ref.get_component_profile().version)
00596                 print(e.message + "...this is expected so pass the test")
00597                 return True
00598             else:
00599                 raise RuntimeError(e.message)
00600         print_pose("robot.getReferencePose('LARM_JOINT5')", posel1);
00601         print_pose("robot.getReferencePose('RARM_JOINT5')", poser1);
00602         print_pose("robot.getReferencePose('LARM_JOINT5:WAIST')", posel2);
00603         print_pose("robot.getReferencePose('RARM_JOINT5:WAIST')", poser2);
00604         numpy.testing.assert_array_almost_equal(numpy.array(posel1),numpy.array(posel2), decimal=3)
00605         numpy.testing.assert_array_almost_equal(numpy.array(poser1),numpy.array(poser2), decimal=3)
00606 
00607         posel1 = self.robot.getReferencePose('LARM_JOINT5:CHEST_JOINT0')
00608         poser1 = self.robot.getReferencePose('RARM_JOINT5:CHEST_JOINT0')
00609         print_pose("robot.getReferencePose('LARM_JOINT5:CHEST_JOINT0')", posel1);
00610         print_pose("robot.getReferencePose('RARM_JOINT5:CHEST_JOINT0')", poser1);
00611         self.robot.setJointAnglesOfGroup('torso', [45], 1)
00612         self.robot.waitInterpolationOfGroup('torso')
00613         posel2 = self.robot.getReferencePose('LARM_JOINT5:CHEST_JOINT0')
00614         poser2 = self.robot.getReferencePose('RARM_JOINT5:CHEST_JOINT0')
00615         print_pose("robot.getReferencePose('LARM_JOINT5:CHEST_JOINT0')", posel2);
00616         print_pose("robot.getReferencePose('RARM_JOINT5:CHEST_JOINT0')", poser2);
00617         numpy.testing.assert_array_almost_equal(numpy.array(posel1),numpy.array(posel2), decimal=3)
00618         numpy.testing.assert_array_almost_equal(numpy.array(poser1),numpy.array(poser2), decimal=3)
00619 
00620         self.robot.setJointAnglesOfGroup('torso', [0], 1)
00621         self.robot.waitInterpolationOfGroup('torso')
00622         pos1 = self.robot.getReferencePosition('LARM_JOINT5')
00623         rot1 = self.robot.getReferenceRotation('LARM_JOINT5')
00624         rpy1 = self.robot.getReferenceRPY('LARM_JOINT5')
00625 
00626         self.robot.setJointAnglesOfGroup('torso', [0], 1)
00627         self.robot.waitInterpolationOfGroup('torso')
00628         pos2 = self.robot.getReferencePosition('LARM_JOINT5', 'CHEST_JOINT0')
00629         rot2 = self.robot.getReferenceRotation('LARM_JOINT5', 'CHEST_JOINT0')
00630         rpy2 = self.robot.getReferenceRPY('LARM_JOINT5', 'CHEST_JOINT0')
00631         numpy.testing.assert_array_almost_equal(numpy.array(pos1),numpy.array(pos2), decimal=3)
00632         numpy.testing.assert_array_almost_equal(numpy.array(rot1),numpy.array(rot2), decimal=3)
00633         numpy.testing.assert_array_almost_equal(numpy.array(rpy1),numpy.array(rpy2), decimal=3)
00634 
00635 
00636     def testGetCurrentPose(self):
00637         def print_pose(msg, pose):
00638             print msg, (pose[3],pose[7],pose[11]), euler_from_matrix([pose[0:3], pose[4:7], pose[8:11]], 'sxyz')
00639         self.robot.goInitial()
00640         posel1 = self.robot.getCurrentPose('LARM_JOINT5')
00641         poser1 = self.robot.getCurrentPose('RARM_JOINT5')
00642         try:
00643             posel2 = self.robot.getCurrentPose('LARM_JOINT5:WAIST')
00644             poser2 = self.robot.getCurrentPose('RARM_JOINT5:WAIST')
00645         except RuntimeError as e:
00646             if re.match(r'frame_name \(.+\) is not supported', e.message):
00647                 print(e.message + "...this is expected so pass the test")
00648                 return True
00649             elif self.robot.fk.ref.get_component_profile().version <= '315.2.4':
00650                 print("target version is " + self.robot.fk.ref.get_component_profile().version)
00651                 print(e.message + "...this is expected so pass the test")
00652                 return True
00653             else:
00654                 raise RuntimeError(e.message)
00655         print_pose("robot.getCurrentPose('LARM_JOINT5')", posel1);
00656         print_pose("robot.getCurrentPose('RARM_JOINT5')", poser1);
00657         print_pose("robot.getCurrentPose('LARM_JOINT5:WAIST')", posel2);
00658         print_pose("robot.getCurrentPose('RARM_JOINT5:WAIST')", poser2);
00659         numpy.testing.assert_array_almost_equal(numpy.array(posel1),numpy.array(posel2), decimal=3)
00660         numpy.testing.assert_array_almost_equal(numpy.array(poser1),numpy.array(poser2), decimal=3)
00661 
00662         posel1 = self.robot.getCurrentPose('LARM_JOINT5:CHEST_JOINT0')
00663         poser1 = self.robot.getCurrentPose('RARM_JOINT5:CHEST_JOINT0')
00664         print_pose("robot.getCurrentPose('LARM_JOINT5:CHEST_JOINT0')", posel1);
00665         print_pose("robot.getCurrentPose('RARM_JOINT5:CHEST_JOINT0')", poser1);
00666         self.robot.setJointAnglesOfGroup('torso', [45], 1)
00667         self.robot.waitInterpolationOfGroup('torso')
00668         posel2 = self.robot.getCurrentPose('LARM_JOINT5:CHEST_JOINT0')
00669         poser2 = self.robot.getCurrentPose('RARM_JOINT5:CHEST_JOINT0')
00670         print_pose("robot.getCurrentPose('LARM_JOINT5:CHEST_JOINT0')", posel2);
00671         print_pose("robot.getCurrentPose('RARM_JOINT5:CHEST_JOINT0')", poser2);
00672         numpy.testing.assert_array_almost_equal(numpy.array(posel1),numpy.array(posel2), decimal=3)
00673         numpy.testing.assert_array_almost_equal(numpy.array(poser1),numpy.array(poser2), decimal=3)
00674 
00675         self.robot.setJointAnglesOfGroup('torso', [0], 1)
00676         self.robot.waitInterpolationOfGroup('torso')
00677         pos1 = self.robot.getCurrentPosition('LARM_JOINT5')
00678         rot1 = self.robot.getCurrentRotation('LARM_JOINT5')
00679         rpy1 = self.robot.getCurrentRPY('LARM_JOINT5')
00680 
00681         self.robot.setJointAnglesOfGroup('torso', [0], 1)
00682         self.robot.waitInterpolationOfGroup('torso')
00683         pos2 = self.robot.getCurrentPosition('LARM_JOINT5', 'CHEST_JOINT0')
00684         rot2 = self.robot.getCurrentRotation('LARM_JOINT5', 'CHEST_JOINT0')
00685         rpy2 = self.robot.getCurrentRPY('LARM_JOINT5', 'CHEST_JOINT0')
00686         numpy.testing.assert_array_almost_equal(numpy.array(pos1),numpy.array(pos2), decimal=3)
00687         numpy.testing.assert_array_almost_equal(numpy.array(rot1),numpy.array(rot2), decimal=3)
00688         numpy.testing.assert_array_almost_equal(numpy.array(rpy1),numpy.array(rpy2), decimal=3)
00689 
00690 #unittest.main()
00691 if __name__ == '__main__':
00692     import rostest
00693     rostest.rosrun(PKG, 'test_hronx', TestHiro) 
00694 


hironx_ros_bridge
Author(s): Kei Okada
autogenerated on Mon Oct 6 2014 07:19:42