samplerobot_emergency_stopper.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, init_pose, reset_pose, wrench_command0, wrench_command1, hrpsys_version
18  hcf = HrpsysConfigurator()
19  hcf.getRTCList = hcf.getRTCListUnstable
20  hcf.init ("SampleRobot(Robot)0", "$(PROJECT_DIR)/../model/sample1.wrl")
21  if hcf.es != None:
22  for sen in filter(lambda x: x.type == "Force", hcf.sensors):
23  hcf.connectLoggerPort(hcf.es, sen.name+"Out")
24  init_pose = [0]*29
25  reset_pose = [0.0,-0.772215,0.0,1.8338,-1.06158,0.0,0.523599,0.0,0.0,-2.44346,0.15708,-0.113446,0.637045,0.0,-0.772215,0.0,1.8338,-1.06158,0.0,0.523599,0.0,0.0,-2.44346,-0.15708,-0.113446,-0.637045,0.0,0.0,0.0]
26  wrench_command0 = [0.0]*24
27  wrench_command1 = [1.0]*24
28  hrpsys_version = hcf.seq.ref.get_component_profile().version
29  print("hrpsys_version = %s"%hrpsys_version)
30 
31 def arrayDistance (angle1, angle2):
32  return sum([abs(i-j) for (i,j) in zip(angle1,angle2)])
33 
34 def arrayApproxEqual (angle1, angle2, thre=1e-3):
35  return arrayDistance(angle1, angle2) < thre
36 
37 def saveLogForCheckParameter(log_fname="/tmp/test-samplerobot-emergency-stopper-check-param"):
38  hcf.setMaxLogLength(1);hcf.clearLog();time.sleep(0.1);hcf.saveLog(log_fname)
39 
40 def checkParameterFromLog(port_name, log_fname="/tmp/test-samplerobot-emergency-stopper-check-param", save_log=True, rtc_name="es"):
41  if save_log:
42  saveLogForCheckParameter(log_fname)
43  return map(float, open(log_fname+"."+rtc_name+"_"+port_name, "r").readline().split(" ")[1:-1])
44 
47  return reduce(lambda x,y: x+y, (map(lambda fs : checkParameterFromLog(fs+"Out", save_log=False), ['lfsensor', 'rfsensor', 'lhsensor', 'rhsensor'])))
48 
49 # demo functions
51  print >> sys.stderr, "1. test stopMotion and releaseMotion for joint angle"
52  hcf.es_svc.releaseMotion()
53  hcf.seq_svc.setJointAngles(init_pose, 1.0)
54  hcf.waitInterpolation()
55  time.sleep(0.1)
56  tmp_angle1 = hcf.getActualState().angle
57  play_time = 10
58  hcf.seq_svc.setJointAngles(reset_pose, play_time)
59  print >> sys.stderr, " send angle_vector of %d [sec]" % play_time
60  time.sleep(4)
61  print >> sys.stderr, " check whether robot pose is changing"
62  tmp_angle2 = hcf.getActualState().angle
63  if arrayApproxEqual(init_pose, tmp_angle1) and not(arrayApproxEqual(tmp_angle1, tmp_angle2)):
64  print >> sys.stderr, " => robot is moving."
65  assert (arrayApproxEqual(init_pose, tmp_angle1) and not(arrayApproxEqual(tmp_angle1, tmp_angle2)))
66  print >> sys.stderr, " stop motion"
67  hcf.es_svc.stopMotion()
68  time.sleep(0.1)
69  print >> sys.stderr, " check whether robot pose remained still"
70  tmp_angle1 = hcf.getActualState().angle
71  time.sleep(3)
72  tmp_angle2 = hcf.getActualState().angle
73  if arrayApproxEqual(tmp_angle1, tmp_angle2):
74  print >> sys.stderr, " => robot is not moving. stopMotion is working succesfully."
75  assert (arrayApproxEqual(tmp_angle1, tmp_angle2))
76  print >> sys.stderr, " release motion"
77  hcf.es_svc.releaseMotion()
78  print >> sys.stderr, " check whether robot pose changed"
79  tmp_angle1 = hcf.getActualState().angle
80  hcf.waitInterpolation()
81  time.sleep(0.1)
82  tmp_angle2 = hcf.getActualState().angle
83  if (not(arrayApproxEqual(tmp_angle1, tmp_angle2)) and arrayApproxEqual(tmp_angle2, reset_pose)):
84  print >> sys.stderr, " => robot is moving. releaseMotion is working succesfully."
85  assert(not(arrayApproxEqual(tmp_angle1, tmp_angle2)) and arrayApproxEqual(tmp_angle2, reset_pose))
86  hcf.es_svc.releaseMotion()
87  hcf.seq_svc.setJointAngles(init_pose, 1.0)
88  hcf.waitInterpolation()
89 
91  print >> sys.stderr, "1. test stopMotion and releaseMotion with key interaction for joint angle"
92  pose_list = [reset_pose, init_pose] * 4
93  play_time = 5
94  hcf.seq_svc.playPattern(pose_list, [[0,0,0]]*len(pose_list), [[0,0,0]]*len(pose_list), [play_time]*len(pose_list))
95  print >> sys.stderr, " send angle_vector_sequence of %d [sec]" % (play_time*len(pose_list))
96  print >> sys.stderr, " press Enter to stop / release motion"
97  while True:
98  raw_input()
99  print >> sys.stderr, " stop motion"
100  hcf.es_svc.stopMotion()
101  if hcf.seq_svc.isEmpty(): break
102  raw_input()
103  print >> sys.stderr, " release motion"
104  hcf.es_svc.releaseMotion()
105  if hcf.seq_svc.isEmpty(): break
106  hcf.es_svc.releaseMotion()
107 
109  print >> sys.stderr, "2. test stopMotion and releaseMotion for wrench"
110  hcf.es_svc.releaseMotion()
111  hcf.seq_svc.setJointAngles(init_pose, 1.0)
112  hcf.seq_svc.setWrenches(wrench_command0, 1.0)
113  hcf.waitInterpolation()
114  time.sleep(0.1)
115  tmp_wrench1 = getWrenchArray()
116  play_time = 10
117  hcf.seq_svc.setWrenches(wrench_command1, play_time)
118  print >> sys.stderr, " send wrench command of %d [sec]" % play_time
119  time.sleep(4)
120  print >> sys.stderr, " check whether wrench is changing"
121  tmp_wrench2 = getWrenchArray()
122  if arrayApproxEqual(wrench_command0, tmp_wrench1) and not(arrayApproxEqual(tmp_wrench1, tmp_wrench2)):
123  print >> sys.stderr, " => wrench is changing."
124  assert (arrayApproxEqual(wrench_command0, tmp_wrench1) and not(arrayApproxEqual(tmp_wrench1, tmp_wrench2)))
125  print >> sys.stderr, " stop motion"
126  hcf.es_svc.stopMotion()
127  time.sleep(0.1)
128  print >> sys.stderr, " check whether wrench remained still"
129  tmp_wrench1 = getWrenchArray()
130  time.sleep(3)
131  tmp_wrench2 = getWrenchArray()
132  if arrayApproxEqual(tmp_wrench1, tmp_wrench2):
133  print >> sys.stderr, " => wrench is not changing. stopMotion is working succesfully."
134  assert (arrayApproxEqual(tmp_wrench1, tmp_wrench2))
135  print >> sys.stderr, " release motion"
136  hcf.es_svc.releaseMotion()
137  print >> sys.stderr, " check whether wrench changed"
138  tmp_wrench1 = getWrenchArray()
139  hcf.waitInterpolation()
140  time.sleep(1.0)
141  tmp_wrench2 = getWrenchArray()
142  if (not(arrayApproxEqual(tmp_wrench1, tmp_wrench2)) and arrayApproxEqual(tmp_wrench2, wrench_command1)):
143  print >> sys.stderr, " => wrench is changing. releaseMotion is working succesfully."
144  assert(not(arrayApproxEqual(tmp_wrench1, tmp_wrench2)) and arrayApproxEqual(tmp_wrench2, wrench_command1))
145  hcf.es_svc.releaseMotion()
146  hcf.seq_svc.setWrenches(wrench_command0, 1.0)
147  hcf.waitInterpolation()
148 
150  print >> sys.stderr, "3. test transition to release mode when deactivated"
151  print >> sys.stderr, " stop motion"
152  hcf.es_svc.stopMotion()
153  hcf.hes_svc.stopMotion()
154  if((hcf.es_svc.getEmergencyStopperParam()[1].is_stop_mode == True) and (hcf.hes_svc.getEmergencyStopperParam()[1].is_stop_mode == True)):
155  print >> sys.stderr, " emergency stopper become stop mode succesfully"
156  print >> sys.stderr, " deactivate and activate es and hes"
157  hcf.es.stop()
158  hcf.hes.stop()
159  hcf.es.start()
160  hcf.hes.start()
161  if((hcf.es_svc.getEmergencyStopperParam()[1].is_stop_mode == False) and (hcf.hes_svc.getEmergencyStopperParam()[1].is_stop_mode == False)):
162  print >> sys.stderr, " emergency stopper become release mode succesfully"
163  assert((hcf.es_svc.getEmergencyStopperParam()[1].is_stop_mode == False) and (hcf.hes_svc.getEmergencyStopperParam()[1].is_stop_mode == False))
164 
165 def demo(key_interaction=False):
166  init()
167  from distutils.version import StrictVersion
168  if StrictVersion(hrpsys_version) >= StrictVersion('315.6.0'):
169  if key_interaction:
171  else:
175 
176 if __name__ == '__main__':
177  demo()
def arrayApproxEqual(angle1, angle2, thre=1e-3)
def checkParameterFromLog(port_name, log_fname="/tmp/test-samplerobot-emergency-stopper-check-param", save_log=True, rtc_name="es")
def saveLogForCheckParameter(log_fname="/tmp/test-samplerobot-emergency-stopper-check-param")


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