samplerobot_remove_force_offset.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 try:
4  from hrpsys.hrpsys_config import *
5  import OpenHRP
6 except:
7  print "import without hrpsys"
8  import rtm
9  from rtm import *
10  from OpenHRP import *
11  import waitInput
12  from waitInput import *
13  import socket
14  import time
15 
16 def init ():
17  global hcf, initial_pose, hrpsys_version
18  hcf = HrpsysConfigurator()
19  hcf.getRTCList = hcf.getRTCListUnstable
20  hcf.init ("SampleRobot(Robot)0", "$(PROJECT_DIR)/../model/sample1.wrl")
21  # set initial pose from sample/controller/SampleController/etc/Sample.pos
22  initial_pose = [-7.779e-005, -0.378613, -0.000209793, 0.832038, -0.452564, 0.000244781, 0.31129, -0.159481, -0.115399, -0.636277, 0, 0, 0.637045, -7.77902e-005, -0.378613, -0.000209794, 0.832038, -0.452564, 0.000244781, 0.31129, 0.159481, 0.115399, -0.636277, 0, 0, -0.637045, 0, 0, 0]
23  hcf.seq_svc.setJointAngles(initial_pose, 2.5)
24  hcf.waitInterpolation()
25  hrpsys_version = hcf.seq.ref.get_component_profile().version
26  print("hrpsys_version = %s"%hrpsys_version)
27 
28 def saveLogForCheckParameter(log_fname="/tmp/test-samplerobot-remove-force-offset-check-param"):
29  hcf.setMaxLogLength(1);hcf.clearLog();time.sleep(0.1);hcf.saveLog(log_fname)
30 
31 def checkParameterFromLog(port_name, log_fname="/tmp/test-samplerobot-remove-force-offset-check-param", save_log=True, rtc_name="rmfo"):
32  if save_log:
33  saveLogForCheckParameter(log_fname)
34  return map(float, open(log_fname+"."+rtc_name+"_"+port_name, "r").readline().split(" ")[1:-1])
35 
37  print >> sys.stderr, "1. GetForceMomentOffsetParam"
38  for fs_name in ["rhsensor", "lhsensor"]:
39  ret = hcf.rmfo_svc.getForceMomentOffsetParam(fs_name)
40  if ret[0]:
41  print >> sys.stderr, " getForceMomentOffsetParam('", fs_name,"') => OK"
42  assert(ret[0] is True)
43 
45  print >> sys.stderr, "2. SetForceMomentOffsetParam"
46  print >> sys.stderr, " Force and moment are large because of link offsets"
48  for fs_name in ["rhsensor", "lhsensor"]:
49  fm = numpy.linalg.norm(checkParameterFromLog("off_"+fs_name, save_log=False))
50  vret = fm > 5e-2
51  print >> sys.stderr, " no-offset-removed force moment (",fs_name,") ", fm, "=> ", vret
52  assert(vret)
53  print >> sys.stderr, " Set link offsets (link_offset_centroid and link_offset_mass are identified value)."
54  # Get param
55  r_fmop = hcf.rmfo_svc.getForceMomentOffsetParam("rhsensor")[1]
56  r_fmop.link_offset_centroid = [0,0.0368,-0.076271]
57  r_fmop.link_offset_mass = 0.80011
58  l_fmop = hcf.rmfo_svc.getForceMomentOffsetParam("lhsensor")[1]
59  l_fmop.link_offset_centroid = [0,-0.0368,-0.076271]
60  l_fmop.link_offset_mass = 0.80011
61  # Set param
62  hcf.rmfo_svc.setForceMomentOffsetParam("rhsensor", r_fmop)
63  hcf.rmfo_svc.setForceMomentOffsetParam("lhsensor", l_fmop)
64  # Check values
65  ret = hcf.rmfo_svc.getForceMomentOffsetParam("rhsensor")
66  if ret[0] and ret[1].link_offset_mass == r_fmop.link_offset_mass and ret[1].link_offset_centroid == r_fmop.link_offset_centroid:
67  print >> sys.stderr, " getForceMomentOffsetParam('rhsensor') => OK"
68  assert((ret[0] and ret[1].link_offset_mass == r_fmop.link_offset_mass and ret[1].link_offset_centroid == r_fmop.link_offset_centroid))
69  ret = hcf.rmfo_svc.getForceMomentOffsetParam("lhsensor")
70  if ret[0] and ret[1].link_offset_mass == l_fmop.link_offset_mass and ret[1].link_offset_centroid == l_fmop.link_offset_centroid:
71  print >> sys.stderr, " getForceMomentOffsetParam('lhsensor') => OK"
72  assert((ret[0] and ret[1].link_offset_mass == l_fmop.link_offset_mass and ret[1].link_offset_centroid == l_fmop.link_offset_centroid))
73  print >> sys.stderr, " Force and moment are reduced"
75  for fs_name in ["rhsensor", "lhsensor"]:
76  fm = numpy.linalg.norm(checkParameterFromLog("off_"+fs_name, save_log=False))
77  vret = fm < 5e-2
78  print >> sys.stderr, " no-offset-removed force moment (",fs_name,") ", fm, "=> ", vret
79  assert(vret)
80 
82  print >> sys.stderr, "3. Dump and load parameter file"
83  print >> sys.stderr, " Get and set param"
84  r_fmop = hcf.rmfo_svc.getForceMomentOffsetParam("rhsensor")[1]
85  r_fmop.link_offset_centroid = [0,0.0368,-0.076271]
86  r_fmop.link_offset_mass = 0.80011
87  l_fmop = hcf.rmfo_svc.getForceMomentOffsetParam("lhsensor")[1]
88  l_fmop.link_offset_centroid = [0,-0.0368,-0.076271]
89  l_fmop.link_offset_mass = 0.80011
90  hcf.rmfo_svc.setForceMomentOffsetParam("rhsensor", r_fmop)
91  hcf.rmfo_svc.setForceMomentOffsetParam("lhsensor", l_fmop)
92  print >> sys.stderr, " Dump param as file"
93  ret = hcf.rmfo_svc.dumpForceMomentOffsetParams("/tmp/test-rmfo-offsets.dat")
94  print >> sys.stderr, " Value check"
95  data_str=filter(lambda x : x.find("lhsensor") >= 0, open("/tmp/test-rmfo-offsets.dat", "r").read().split("\n"))[0]
96  vcheck = map(float, data_str.split(" ")[7:10]) == l_fmop.link_offset_centroid and float(data_str.split(" ")[10]) == l_fmop.link_offset_mass
97  data_str=filter(lambda x : x.find("rhsensor") >= 0, open("/tmp/test-rmfo-offsets.dat", "r").read().split("\n"))[0]
98  vcheck = vcheck and map(float, data_str.split(" ")[7:10]) == r_fmop.link_offset_centroid and float(data_str.split(" ")[10]) == r_fmop.link_offset_mass
99  import os
100  if ret and os.path.exists("/tmp/test-rmfo-offsets.dat") and vcheck:
101  print >> sys.stderr, " dumpForceMomentOffsetParams => OK"
102  assert((ret and os.path.exists("/tmp/test-rmfo-offsets.dat") and vcheck))
103  print >> sys.stderr, " Resetting values"
104  r_fmop2 = hcf.rmfo_svc.getForceMomentOffsetParam("rhsensor")[1]
105  r_fmop2.link_offset_centroid = [0,0,0]
106  r_fmop2.link_offset_mass = 0
107  l_fmop2 = hcf.rmfo_svc.getForceMomentOffsetParam("lhsensor")[1]
108  l_fmop2.link_offset_centroid = [0,0,0]
109  l_fmop2.link_offset_mass = 0
110  hcf.rmfo_svc.setForceMomentOffsetParam("rhsensor", r_fmop2)
111  hcf.rmfo_svc.setForceMomentOffsetParam("lhsensor", l_fmop2)
112  print >> sys.stderr, " Load from file"
113  ret = hcf.rmfo_svc.loadForceMomentOffsetParams("/tmp/test-rmfo-offsets.dat")
114  r_fmop3 = hcf.rmfo_svc.getForceMomentOffsetParam("rhsensor")[1]
115  l_fmop3 = hcf.rmfo_svc.getForceMomentOffsetParam("lhsensor")[1]
116  vcheck = r_fmop3.link_offset_mass == r_fmop.link_offset_mass and r_fmop3.link_offset_centroid == r_fmop.link_offset_centroid and l_fmop3.link_offset_mass == l_fmop.link_offset_mass and l_fmop3.link_offset_centroid == l_fmop.link_offset_centroid
117  if ret and vcheck:
118  print >> sys.stderr, " loadForceMomentOffsetParams => OK"
119  assert((ret and vcheck))
120 
122  print >> sys.stderr, "4. remove force sensor offset"
123  print >> sys.stderr, " Test valid calibration"
124  ret = hcf.removeForceSensorOffsetRMFO(tm=1.0) # all sensors by default
125  print >> sys.stderr, " Test invalid calibration"
126  ret = ret and not hcf.removeForceSensorOffsetRMFO(["testtest"], 1.0) # invalid sensor name
127  if ret:
128  print >> sys.stderr, " removeforcesensorlinkoffset => OK"
129  assert(ret)
130 
131 def demo():
132  import numpy
133  init()
134  from distutils.version import StrictVersion
135  if StrictVersion(hrpsys_version) >= StrictVersion('315.5.0'):
140 
141 if __name__ == '__main__':
142  demo()
def checkParameterFromLog(port_name, log_fname="/tmp/test-samplerobot-remove-force-offset-check-param", save_log=True, rtc_name="rmfo")
def saveLogForCheckParameter(log_fname="/tmp/test-samplerobot-remove-force-offset-check-param")


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51