samplerobot_reference_force_updater.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 import sys
17 
18 def init ():
19  global hcf, init_pose, reset_pose, wrench_command0, wrench_command1, hrpsys_version
20  hcf = HrpsysConfigurator()
21  hcf.getRTCList = hcf.getRTCListUnstable
22  hcf.init ("SampleRobot(Robot)0", "$(PROJECT_DIR)/../model/sample1.wrl")
23  hrpsys_version = hcf.seq.ref.get_component_profile().version
24  print("hrpsys_version = %s"%hrpsys_version)
25  if hcf.rfu != None:
26  hcf.connectLoggerPort(hcf.rfu, 'ref_rhsensorOut')
27  hcf.connectLoggerPort(hcf.rfu, 'ref_lhsensorOut')
28 
29 # demo functions
31  print >> sys.stderr, "1. getParam"
32  for limb_name in ['rarm', 'larm']:
33  [ret, rfup] = hcf.rfu_svc.getReferenceForceUpdaterParam(limb_name)
34  assert(ret)
35  [ret, rfup] = hcf.rfu_svc.getReferenceForceUpdaterParam('rarm2') # Invalid name
36  assert(not ret)
37  print >> sys.stderr, " =>OK"
38 
40  print >> sys.stderr, "2. setParam"
41  print >> sys.stderr, " Valid limb access"
42  for limb_name in ['rarm', 'larm']:
43  [ret, rfup] = hcf.rfu_svc.getReferenceForceUpdaterParam(limb_name)
44  ret = hcf.rfu_svc.setReferenceForceUpdaterParam(limb_name, rfup)
45  assert(ret)
46  print >> sys.stderr, " Invalid limb access"
47  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm2', rfup) # Invalid name
48  assert(not ret)
49  print >> sys.stderr, " =>OK"
50 
52  print >> sys.stderr, "3. Reference Force Update"
53  import numpy as np
54  i=1;
55  #print >> sys.stderr, i,". get param";i+=1
56  p = hcf.rfu_svc.getReferenceForceUpdaterParam('larm')[1]
57  p.update_freq=100
58  p.p_gain=0.1
59  # set rmfo
60  r_fmop = hcf.rmfo_svc.getForceMomentOffsetParam("rhsensor")[1]
61  r_fmop.link_offset_centroid = [0,0.0368,-0.076271]
62  r_fmop.link_offset_mass = 0.80011
63  l_fmop = hcf.rmfo_svc.getForceMomentOffsetParam("lhsensor")[1]
64  l_fmop.link_offset_centroid = [0,-0.0368,-0.076271]
65  l_fmop.link_offset_mass = 0.80011
66  # Set param
67  hcf.rmfo_svc.setForceMomentOffsetParam("rhsensor", r_fmop)
68  hcf.rmfo_svc.setForceMomentOffsetParam("lhsensor", l_fmop)
69  for armName,portName in zip(['rarm', 'larm'],['ref_rhsensorOut','ref_lhsensorOut']):
70  hcf.rfu_svc.setReferenceForceUpdaterParam(armName,p)
71  print >> sys.stderr, " 3."+str(i)+". set ref_force from seq [10,0,0]";i+=1
72  # set ref_force from seq
73  hcf.seq_svc.setWrenches([0]*12+[10,0,0,0,0,0]*2,1);time.sleep(1)
74  portData=checkDataPortFromLog(portName)
75  print >> sys.stderr, portName,portData[0:3]
76  ret = np.linalg.norm(portData) > 9.9;
77  assert (ret)
78  # start/stop rfu
79  print >> sys.stderr, " 3."+str(i)+". start/stop param for " + armName; i+=1
80  ##start rfu
81  hcf.rfu_svc.startReferenceForceUpdater(armName);time.sleep(1)
82  portData=checkDataPortFromLog(portName)
83  print >> sys.stderr, portName,portData[0:3]
84  ret = np.linalg.norm(portData) < 0.1;
85  assert (ret)
86  ##stop rfu
87  hcf.rfu_svc.stopReferenceForceUpdater(armName);time.sleep(1)
88  portData=checkDataPortFromLog(portName)
89  print >> sys.stderr, portName,portData[0:3]
90  ret = np.linalg.norm(portData) > 9.9;
91  assert (ret)
92  # reset ref_force from seq
93  print >> sys.stderr, " 3."+str(i)+". set ref_force from seq [0,0,0]";i+=1
94  hcf.seq_svc.setWrenches([0]*24,1);time.sleep(1)
95 
97  print >> sys.stderr, "4. setParam while active"
98  print >> sys.stderr, " 4.0 Start"
99  hcf.rfu_svc.startReferenceForceUpdater('rarm')
100  print >> sys.stderr, " 4.1 Check setParam without any changes"
101  [ret, rfup_org] = hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')
102  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup_org)
103  assert(ret)
104  print >> sys.stderr, " 4.2 Check setParam which cannot be changed while active"
105  [ret, rfup] = hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')
106  rfup.frame = 'world'
107  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
108  assert(not ret)
109  [ret, rfup] = hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')
110  rfup.update_freq = rfup.update_freq*10
111  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
112  assert(not ret)
113  [ret, rfup] = hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')
114  rfup.update_time_ratio = rfup.update_time_ratio*10
115  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
116  assert(not ret)
117  print >> sys.stderr, " 4.3 Check setParam which can be changed while active"
118  [ret, rfup] = hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')
119  rfup.motion_dir = tmp_value = [0,0,-1]
120  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
121  print >> sys.stderr, " motion_dir ..."
122  assert(ret and (map (lambda x,y : abs(x-y)<1e-5, hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')[1].motion_dir, tmp_value)))
123  rfup.p_gain = tmp_value = rfup.p_gain*0.1
124  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
125  print >> sys.stderr, " p_gain ..."
126  assert(ret and (abs(hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')[1].p_gain-tmp_value) < 1e-5))
127  rfup.d_gain = tmp_value = rfup.d_gain*0.1
128  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
129  print >> sys.stderr, " d_gain ..."
130  assert(ret and (abs(hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')[1].d_gain-tmp_value) < 1e-5))
131  rfup.i_gain = rfup.i_gain*0.1
132  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
133  print >> sys.stderr, " i_gain ..."
134  assert(ret and (abs(hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')[1].i_gain-tmp_value) < 1e-5))
135  rfup.is_hold_value = tmp_value = not rfup.is_hold_value
136  ret = hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup)
137  print >> sys.stderr, " is_hold_value ..."
138  assert(ret and hcf.rfu_svc.getReferenceForceUpdaterParam('rarm')[1].is_hold_value == tmp_value)
139  print >> sys.stderr, " 4.4 Stop"
140  hcf.rfu_svc.stopReferenceForceUpdater('rarm')
141  hcf.rfu_svc.setReferenceForceUpdaterParam('rarm', rfup_org)
142  print >> sys.stderr, " =>OK"
143 
145  print >> sys.stderr, "5. FootOriginExtMoment"
146  ret = hcf.rfu_svc.startReferenceForceUpdater('footoriginextmoment')
147  ret = hcf.rfu_svc.stopReferenceForceUpdater('footoriginextmoment') and ret
148  assert(ret)
149  print >> sys.stderr, " =>OK"
150 
151 def saveLogForCheckParameter(log_fname="/tmp/test-samplerobot-reference-force-updater-check-port"):
152  hcf.setMaxLogLength(1);hcf.clearLog();time.sleep(0.1);hcf.saveLog(log_fname)
153 
154 def checkDataPortFromLog(port_name, log_fname="/tmp/test-samplerobot-reference-force-updater-check-port",save_log=True, rtc_name="rfu"):
155  if save_log:
156  saveLogForCheckParameter(log_fname)
157  return map(float, open(log_fname+"."+rtc_name+"_"+port_name, "r").readline().split(" ")[1:-1])
158 
159 def demo():
160  init()
161  from distutils.version import StrictVersion
162  if StrictVersion(hrpsys_version) >= StrictVersion('315.9.0'):
166  if StrictVersion(hrpsys_version) >= StrictVersion('315.13.0'):
169 
170 if __name__ == '__main__':
171  demo()
def saveLogForCheckParameter(log_fname="/tmp/test-samplerobot-reference-force-updater-check-port")
def checkDataPortFromLog(port_name, log_fname="/tmp/test-samplerobot-reference-force-updater-check-port", save_log=True, rtc_name="rfu")


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