test_hironx.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 
4 PKG = 'hironx_ros_bridge'
5 # rosbuild needs load_manifest
6 try:
7  import roslib
8  import hironx_ros_bridge
9 except:
10  import roslib; roslib.load_manifest(PKG)
11  import hironx_ros_bridge
12 
13 from hironx_ros_bridge import hironx_client as hironx
14 from hrpsys.hrpsys_config import euler_from_matrix
15 from hrpsys import rtm
16 
17 import os
18 import unittest
19 import time
20 import tempfile
21 
22 import math
23 import random
24 import numpy
25 import re
26 
27 from rtm import connectPorts, disconnectPorts
28 
29 class TestHiro(unittest.TestCase):
30 
31  @classmethod
32  def setUpClass(cls):
33  #modelfile = rospy.get_param("hironx/collada_model_filepath")
34  #rtm.nshost = 'hiro024'
35  #robotname = "RobotHardware0"
36 
37  cls.robot = hironx.HIRONX()
38  #cls.robot.init(robotname=robotname, url=modelfile)
39  cls.robot.init()
40 
41  @classmethod
42  def tearDownClass(cls):
43  #self.write_output_to_pdf("test-hironx.pdf") # don't know how to call this mehtod
44  True
45 
46  ###
48  # send initial pose
49  av0 = [-0.6, 0, -100, 15.2, 9.4, 3.2]
50  av1 = [-0.6, -90, -100, 15.2, 9.4, 3.2]
51  self.robot.setJointAnglesOfGroup( "rarm" , av0, 5)
52  pose0 = robot.getReferencePose("RARM_JOINT5")
53  print pose0
54 
55  # make sure setJointAnglesOfGroup with wait
56  tm0 = time.time()
57  self.robot.setJointAnglesOfGroup( "rarm" , av1, 5)
58  self.robot.setJointAnglesOfGroup( "rarm" , av0, 5)
59  tm1 = time.time()
60  pose1 = self.robot.getReferencePose("RARM_JOINT5")
61  print "setJointAnglesOfGroup : reach goal? ", numpy.linalg.norm(numpy.array(pose1)-numpy.array(pose0)) < 5.0e-3
62  print " : spend 10 sec?", abs((tm1 - tm0) - 10.0) < 0.1, " " , tm1 - tm0
63 
64  # make sure setJointAnglesOfGorup with wita=False returns immediately
65  self.robot.setJointAnglesOfGroup( "rarm" , av1, 5)
66  tm0 = time.time()
67  self.robot.setJointAnglesOfGroup( "rarm" , av0, 5, wait=False)
68  tm1 = time.time()
69  pose1 = self.robot.getReferencePose("RARM_JOINT5")
70  print "setJointAnglesOfGroup(wait=False)", numpy.linalg.norm(numpy.array(pose1)-numpy.array(pose0)) > 5.0e-3
71  print " : spend 10 sec?", abs((tm1 - tm0) - 0.0) < 0.1, " " , tm1 - tm0
72 
73  # make sure that waitInterpolationOfGroup wait until target reaches the goal
74  self.robot.waitInterpolationOfGroup("rarm")
75  tm1 = time.time()
76  pose1 = self.robot.getReferencePose("RARM_JOINT5")
77  print "waitInterpolationOfGroup", numpy.linalg.norm(numpy.array(pose1)-numpy.array(pose0)) < 5.0e-3
78  print " : spend 10 sec?", abs((tm1 - tm0) - 5.0) < 0.1, " " , tm1 - tm0
79 
80  # load from log data, [[p0,p1,....p25],[p0,p1,....p25],...,[p0,p1,....p25]]
81  def check_q_data(self,name):
82  import math
83  data = []
84  name = name+".q"
85  f = open(name)
86  start_time = None
87  for line in f:
88  current_time = float(line.split(' ')[0])
89  current_value = float(line.split(' ')[7])
90  if not start_time:
91  start_time = current_time
92  data.append([current_time, current_value*180/math.pi])
93  # print "%f %f" % (current_time - start_time, current_value*180/math.pi)
94  f.close()
95  self.filenames.append(name)
96  return data
97 
98  # load from log data, [[p0,p1,....p25],[p0,p1,....p25],...,[p0,p1,....p25]] # degree
99  def load_log_data(self,name):
100  import math
101  data = []
102  f = open(name)
103  start_time = None
104  for line in f:
105  data.append([float(i)*180/math.pi for i in line.split(' ')[1:-1]])
106  # print "%f %f" % (current_time - start_time, current_value*180/math.pi)
107  f.close()
108  self.filenames.append(name)
109  return data
110 
111  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
112  _tm_data = len(data)/200.0
113  _min_data = min([d[idx] for d in data])
114  _max_data = max([d[idx] for d in data])
115  _tm_thre = tm_thre
116  # min_data = [min_data, min_thre]
117  if isinstance(min_data, (int, float)):
118  min_data = [min_data, 5]
119  if isinstance(max_data, (int, float)):
120  max_data = [max_data, 5]
121 
122  print "time (= ", _tm_data, ") == ", tm_data, " -> ", abs(_tm_data - tm_data) < tm_data*_tm_thre
123  print " min (= ", _min_data, ") == ", min_data, " -> ", abs(_min_data - min_data[0]) < min_data[1]
124  print " max (= ", _max_data, ") == ", max_data, " -> ", abs(_max_data - max_data[0]) < max_data[1]
125  self.assertTrue(abs(_tm_data - tm_data) < tm_data*_tm_thre)
126  self.assertTrue(abs(_min_data - min_data[0]) < min_data[1])
127  self.assertTrue(abs(_max_data - max_data[0]) < max_data[1])
128 
129  # check acceleration
130  flag = True
131  for i in range(1, len(data)-1):
132  for j in range(len(data[i])):
133  p0 = data[i-1][j]
134  p1 = data[i+0][j]
135  p2 = data[i+1][j]
136  v0 = p1 - p0
137  v1 = p2 - p1
138  if abs(v1 - v0) > acc_thre:
139  flag = False
140  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))
141  self.assertTrue(flag)
142 
143  def write_d_dd_data(self, name):
144  name_d = os.path.splitext(name)[0]+".dq"
145  name_dd = os.path.splitext(name)[0]+".ddq"
146  f = open(name)
147  f_d = open(name_d, 'w')
148  f_dd = open(name_dd, 'w')
149  line_0 = None
150  line_1 = None
151  line_2 = None
152  for line_0 in f:
153  if line_2 and line_1 :
154  tm = float(line_1.split(' ')[0])
155  f_d.write(str(tm))
156  f_dd.write(str(tm))
157  for i in range(1,len(line_0.split(' '))-1):
158  p0 = float(line_0.split(' ')[i])
159  p1 = float(line_1.split(' ')[i])
160  p2 = float(line_2.split(' ')[i])
161  v0 = p1 - p0
162  v1 = p2 - p1
163  f_d.write(" %.10f"%v0)
164  f_dd.write(" %.10f"%(v1 - v0))
165  f_d.write('\n')
166  f_dd.write('\n')
167  line_2 = line_1
168  line_1 = line_0
169  f.close()
170  f_d.close()
171  f_dd.close()
172  print "[%s] write %s"%(__file__, name_d)
173  print "[%s] write %s"%(__file__, name_dd)
174 
175  def write_all_joint_pdf(self, name, pdf_name):
176  print "[%s] write pdf %s from log data %s"%(__file__, pdf_name, name)
177  self.write_d_dd_data(name)
178  _pdf_names = []
179  for _name in [name, os.path.splitext(name)[0]+".dq", os.path.splitext(name)[0]+".ddq"]:
180  _pdf_names.append(_name+"_"+pdf_name)
181  cmd = "gnuplot -p -e \"set terminal pdf; set output '"+_pdf_names[-1]+"'; plot "
182  f = open(_name)
183  l = f.readline().split(' ')[:-1]
184  f.close()
185  for i in range(1,len(l)):
186  cmd += "'"+_name+"' using 1:"+str(i+1)+" title 'joint "+str(i)+"' with lines"
187  if i != len(l)-1:
188  cmd += ","
189  cmd += "\""
190  os.system(cmd)
191  cmd_str = 'pdfunite '+' '.join(_pdf_names) + ' ' + pdf_name
192  cmd_str = cmd_str.replace('(', '\(')
193  cmd_str = cmd_str.replace(')', '\)')
194  os.system(cmd_str)
195  return
196 
197  def check_acceleration(self, name):
198  name1 = name+".q"
199  name2 = name+".acc"
200  f1 = open(name1)
201  f2 = open(name2, 'w')
202  line_0 = None
203  line_1 = None
204  line_2 = None
205  for line_0 in f1:
206  if line_2 and line_1 :
207  tm = float(line_1.split(' ')[0])
208  f2.write(str(tm))
209  for i in range(1,len(line_0.split(' '))-1):
210  p0 = float(line_0.split(' ')[i])
211  p1 = float(line_1.split(' ')[i])
212  p2 = float(line_2.split(' ')[i])
213  v0 = p1 - p0
214  v1 = p2 - p1
215  if abs(v1 - v0) > 0.0001:
216  print("%s Acceleration vaiorated! : p0 %f, p1 %f, p2 %f, v1 %f, v2 %f, acc %f" % (name, p0, p1, p2, v0, v1, v1-v0))
217  self.assertTrue(abs(v1 - v0) < 0.0001)
218  f2.write(' ' + str(v1 - v0))
219  f2.write('\n')
220  line_2 = line_1
221  line_1 = line_0
222  f1.close()
223  f2.close()
224 
225 
226  def assertTrue(self,a):
227  assert(a)
228 
229  def write_output_to_pdf (self,name):
230  print ";; write log data to "+name
231  global filenames
232  import os
233  cmd = "gnuplot -p -e \"set terminal pdf; set output '"+name+"'; plot "
234  for name in self.filenames:
235  cmd += "'"+name+"' using 0:8 title '"+name+"' with lines"
236  if name != self.filenames[-1]:
237  cmd += ","
238  cmd += "\""
239  os.system(cmd)
240  return cmd
241 
242 
243 # for debug
244 # $ python -m unittest test_hironx.TestHiro.test_impedance_controller
245 #
246 if __name__ == '__main__':
247  import rostest
248  rostest.rosrun(PKG, 'test_hronx', TestHiro)
249 
def write_output_to_pdf(self, name)
Definition: test_hironx.py:229
def check_log_data(self, data, idx, tm_data, min_data, max_data, acc_thre=0.06, tm_thre=0.1)
Definition: test_hironx.py:111
def tearDownClass(cls)
Definition: test_hironx.py:42
def check_q_data(self, name)
Definition: test_hironx.py:81
def set_joint_angles_no_wait_test(self)
Definition: test_hironx.py:47
def assertTrue(self, a)
Definition: test_hironx.py:226
def write_all_joint_pdf(self, name, pdf_name)
Definition: test_hironx.py:175
def load_log_data(self, name)
Definition: test_hironx.py:99
#define min(a, b)
def write_d_dd_data(self, name)
Definition: test_hironx.py:143
def check_acceleration(self, name)
Definition: test_hironx.py:197


hironx_ros_bridge
Author(s): Kei Okada , Isaac I.Y. Saito
autogenerated on Thu May 14 2020 03:52:05