samplerobot_kalman_filter.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 math
17 
18 def init ():
19  global hcf, pitch_poses, roll_poses, yaw_poses, roll_pitch_poses, initial_pose, hrpsys_version
20  hcf = HrpsysConfigurator()
21  hcf.getRTCList = hcf.getRTCListUnstable
22  hcf.init ("SampleRobot(Robot)0", "$(PROJECT_DIR)/../model/sample1.wrl")
23  hcf.connectLoggerPort(hcf.kf, 'baseRpyCurrent')
24  # initialize poses
25  # pose1 = [0]*29
26  # pose2 = [0]*29
27  # pose2[3] = 20*3.14159/180.0
28  # pose2[3+7+6] = 20*3.14159/180.0
29  # pose2[4] = -10*3.14159/180.0
30  # pose2[4+7+6] = -10*3.14159/180.0
31  # pose3 = [0]*29
32  # pose3[3] = -20*3.14159/180.0
33  # pose3[3+7+6] = -20*3.14159/180.0
34  # pose3[4] = 10*3.14159/180.0
35  # pose3[4+7+6] = 10*3.14159/180.0
36  # hcf.seq_svc.setJointAngles(pose1, 2.5)
37  # hcf.seq_svc.waitInterpolation()
38  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, -7.77902e-005, -0.378613, -0.000209794, 0.832038, -0.452564, 0.000244781, 0.31129, 0.159481, 0.115399, -0.636277, 0, 0, 0, 0, 0, 0]
39  # reset-pose+ 50 legup + 30deg pitch
40  pitch_pose1=[0.000112,-0.553688,-0.00028,1.15739,-0.602847,-1.330313e-05,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,0.000112,-0.553703,-0.00028,1.15739,-0.602833,-1.330597e-05,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
41  pitch_pose2=[8.200401e-05,-0.980787,-0.000375,0.598284,-0.140235,-9.749026e-05,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,8.206224e-05,-0.980716,-0.000375,0.598026,-0.140049,-9.749664e-05,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
42  pitch_pose3=[0.000229,0.313052,-0.000276,0.779751,-0.568344,-3.437126e-05,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,0.000229,0.313082,-0.000276,0.779709,-0.568332,-3.436731e-05,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
43  pitch_poses = [pitch_pose1, pitch_pose2, pitch_pose3]
44  # roll
45  roll_pose1=[0.000112,-0.553688,-0.00028,1.15739,-0.602847,-1.330313e-05,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,0.000112,-0.553703,-0.00028,1.15739,-0.602833,-1.330597e-05,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
46  roll_pose2=[-0.475317,-0.568231,0.022036,1.1549,-0.595553,0.138268,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,-0.447984,-0.378428,0.020283,0.753855,-0.383755,0.106576,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
47  roll_pose3=[0.44835,-0.377793,-0.020814,0.752451,-0.38309,-0.106633,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,0.475616,-0.567819,-0.022616,1.15389,-0.595074,-0.138377,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
48  roll_poses = [roll_pose1, roll_pose2, roll_pose3]
49  # yaw
50  yaw_pose1=[-0.000113, -0.523213, -0.000225, 1.15585, -0.631772, 0.000245, 0.31129, -0.159481, -0.115399, -0.636277, 0.0, 0.0, 0.0, -0.000113, -0.523213, -0.000225, 1.15585, -0.631772, 0.000245, 0.31129, 0.159481, 0.115399, -0.636277, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
51  yaw_pose2=[-0.000113, -0.523213, -0.000225, 1.15585, -0.631772, 0.000245, 0.31129, -0.159481, -0.115399, -0.636277, 0.0, 0.0, 0.0, -0.000113, -0.523213, -0.000225, 1.15585, -0.631772, 0.000245, 0.31129, 0.159481, 0.115399, -0.636277, 0.0, 0.0, 0.0, 0.0, 0.0, 0.785398]
52  yaw_pose3=[-0.000113, -0.523213, -0.000225, 1.15585, -0.631772, 0.000245, 0.31129, -0.159481, -0.115399, -0.636277, 0.0, 0.0, 0.0, -0.000113, -0.523213, -0.000225, 1.15585, -0.631772, 0.000245, 0.31129, 0.159481, 0.115399, -0.636277, 0.0, 0.0, 0.0, 0.0, 0.0, 1.5708]
53  yaw_poses = [yaw_pose1, yaw_pose2, yaw_pose3]
54  # roll + pitch
55  roll_pitch_pose1=[0.000111,-0.681981,-0.000333,1.41142,-0.728577,-7.604916e-05,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,0.000111,-0.682003,-0.000333,1.41142,-0.728553,-7.605437e-05,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
56  roll_pitch_pose2=[-0.486326,-1.18821,-0.026531,0.908889,-0.267927,0.130916,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,-0.430362,-0.964194,0.009303,0.590166,-0.173131,0.103544,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
57  roll_pitch_pose3=[0.463158,0.281851,-0.0701,0.747965,-0.514677,-0.108534,0.31129,-0.159481,-0.115399,-0.636277,0.0,0.0,0.0,0.486068,0.189331,-0.083976,1.08676,-0.76299,-0.139173,0.31129,0.159481,0.115399,-0.636277,0.0,0.0,0.0,0.0,0.0,0.0]
58  roll_pitch_poses = [roll_pitch_pose1, roll_pitch_pose2, roll_pitch_pose3]
59  hrpsys_version = hcf.seq.ref.get_component_profile().version
60  print("hrpsys_version = %s"%hrpsys_version)
61 
62 def test_kf_plot (test_motion_func, optional_out_file_name): # time [s]
63  # Reset KF and store data during motion
64  hcf.log_svc.clear()
65  test_motion_func()
66  hcf.log_svc.save("/tmp/test-kf-samplerobot-{0}".format(optional_out_file_name))
67  # Parse data
68  # Estimated base link rpy from KF : time, roll, pitch, yaw
69  estimated_base_rpy_ret=[]
70  for line in open("/tmp/test-kf-samplerobot-{0}.kf_baseRpyCurrent".format(optional_out_file_name), "r"):
71  estimated_base_rpy_ret.append(line.split(" ")[0:-1])
72  # Actual rpy from simualtor : time, posx, posy, posz, roll, pitch, yaw
73  act_rpy_ret=[]
74  for line in open("/tmp/test-kf-samplerobot-{0}.SampleRobot(Robot)0_WAIST".format(optional_out_file_name), "r"):
75  act_rpy_ret.append(line.split(" ")[0:-1])
76  # Get time list
77  initial_sec=int(act_rpy_ret[0][0].split(".")[0])
78  tm_list=map (lambda x : int(x[0].split(".")[0])-initial_sec + float(x[0].split(".")[1]) * 1e-6, act_rpy_ret)
79  # Plotting
80  try:
81  import matplotlib.pyplot as plt
82  plt.clf()
83  color_list = ['r', 'g', 'b']
84  for idx in range(3):
85  plt.plot(tm_list, map(lambda x : 180.0 * float(x[1+3+idx]) / math.pi, act_rpy_ret), color=color_list[idx])
86  plt.plot(tm_list, map(lambda x : 180.0 * float(x[1+idx]) / math.pi, estimated_base_rpy_ret), "--", color=color_list[idx])
87  plt.xlabel("Time [s]")
88  plt.ylabel("Angle [deg]")
89  plt.title("KF actual-estimated data (motion time = {0})".format(optional_out_file_name))
90  plt.legend(("Actual roll", "Estimated base roll",
91  "Actual pitch", "Estimated base pitch",
92  "Actual yaw", "Estimated base yaw"))
93  plt.savefig("/tmp/test-kf-samplerobot-data-{0}.eps".format(optional_out_file_name))
94  except:
95  print >> sys.stderr, "No plot"
96 
97 def test_bending_common (time, poses):
98  hcf.seq_svc.setJointAngles(poses[1], time*0.25)
99  hcf.seq_svc.waitInterpolation()
100  hcf.seq_svc.setJointAngles(poses[2], time*0.5)
101  hcf.seq_svc.waitInterpolation()
102  hcf.seq_svc.setJointAngles(poses[0], time*0.25)
103  hcf.seq_svc.waitInterpolation()
104 
106  test_bending_common(4.0, pitch_poses)
107 
109  test_bending_common(2.0, pitch_poses)
110 
112  test_bending_common(4.0, roll_poses)
113 
115  test_bending_common(2.0, roll_poses)
116 
118  test_bending_common(4.0, yaw_poses)
119 
121  test_bending_common(2.0, yaw_poses)
122 
124  test_bending_common(4.0, roll_pitch_poses)
125 
127  test_bending_common(2.0, roll_pitch_poses)
128 
129 def test_walk ():
130  hcf.abc_svc.goPos(0.1,0,0)
131  hcf.abc_svc.waitFootSteps()
132 
134  print >> sys.stderr, "1. getParameter"
135  ret=hcf.kf_svc.getKalmanFilterParam()
136  if ret[0]:
137  print >> sys.stderr, " getKalmanFilterParam() => OK"
138  assert(ret[0])
139 
141  print >> sys.stderr, "2. setParameter"
142  kfp=hcf.kf_svc.getKalmanFilterParam()[1]
143  kfp.Q_angle = 0.001;
144  kfp.Q_rate = 0.003;
145  kfp.R_angle = 100;
146  ret=hcf.kf_svc.setKalmanFilterParam(kfp)
147  kfp2=hcf.kf_svc.getKalmanFilterParam()[1]
148  ret2 = ret and kfp.Q_angle == kfp2.Q_angle and kfp.Q_rate == kfp2.Q_rate and kfp.R_angle == kfp2.R_angle
149  if ret2:
150  print >> sys.stderr, " setKalmanFilterParam() => OK"
151  assert(ret2)
152 
153 def demo():
154  init()
155  from distutils.version import StrictVersion
156  if StrictVersion(hrpsys_version) >= StrictVersion('315.5.0'):
159 
160  # 3. check log and plot
161  hcf.abc_svc.startAutoBalancer(["rleg", "lleg"])
162 
163  for alg in [OpenHRP.KalmanFilterService.RPYKalmanFilter, OpenHRP.KalmanFilterService.QuaternionExtendedKalmanFilter]:
164  kfp=hcf.kf_svc.getKalmanFilterParam()[1]
165  kfp.kf_algorithm = alg
166  hcf.kf_svc.setKalmanFilterParam(kfp)
167 
168  hcf.kf_svc.resetKalmanFilterState()
169  hcf.seq_svc.setJointAngles(pitch_poses[0], 1.0)
170  hcf.seq_svc.waitInterpolation()
171  test_kf_plot(test_pitch_bending_2s, "pitch-bending-2s"+str(alg))
172 
173  #hcf.kf_svc.resetKalmanFilterState()
174  hcf.seq_svc.setJointAngles(roll_poses[0], 1.0)
175  hcf.seq_svc.waitInterpolation()
176  test_kf_plot(test_roll_bending_2s, "roll-bending-2s"+str(alg))
177 
178  #hcf.kf_svc.resetKalmanFilterState()
179  hcf.seq_svc.setJointAngles(yaw_poses[0], 1.0)
180  hcf.seq_svc.waitInterpolation()
181  test_kf_plot(test_yaw_bending_2s, "yaw-bending-4s"+str(alg))
182 
183  #hcf.kf_svc.resetKalmanFilterState()
184  hcf.seq_svc.setJointAngles(roll_pitch_poses[0], 1.0)
185  hcf.seq_svc.waitInterpolation()
186  test_kf_plot(test_roll_pitch_bending_4s, "roll-pitch-bending-4s"+str(alg))
187 
188  hcf.seq_svc.setJointAngles(initial_pose, 1.0)
189  #hcf.seq_svc.waitInterpolation()
190  #hcf.kf_svc.resetKalmanFilterState()
191  #hcf.seq_svc.setJointAngles(initial_pose, 1.0)
192  hcf.seq_svc.waitInterpolation()
193  test_kf_plot(test_walk, "test_walk"+str(alg))
194 
195 if __name__ == '__main__':
196  demo()
def test_kf_plot(test_motion_func, optional_out_file_name)


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