00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import numpy
00034 from numpy import matrix, vsplit, sin, cos, reshape, pi, array
00035 import rospy
00036
00037 class DhChain:
00038 def __init__(self, config = [[0,0,0,0]]):
00039
00040
00041
00042 self._M = len(config['dh'])
00043 rospy.logdebug("Initializing dh chain with [%u] links", self._M)
00044
00045 param_mat = numpy.matrix([ [eval(str(x)) for x in y] for y in config['dh']], float)
00046 assert(self._M*4 == param_mat.size)
00047 self._length = param_mat.size
00048 self._config = param_mat
00049
00050 self._cov_dict = config['cov']
00051 self._gearing = config['gearing']
00052 assert( len(self._cov_dict['joint_angles']) == self._M)
00053
00054 def calc_free(self, free_config):
00055
00056 assert( len(free_config['dh']) == self._M)
00057 assert( len(free_config['gearing']) == self._M )
00058
00059
00060 flat_config = sum(free_config['dh'], [])
00061
00062 assert( len(flat_config) == self._M * 4)
00063
00064 flat_config = flat_config + free_config['gearing']
00065
00066
00067 flat_free = [x == 1 for x in flat_config]
00068
00069 return flat_free
00070
00071 def params_to_config(self, param_vec):
00072 assert(param_vec.shape == (self._M * 5, 1))
00073 dh_param_vec = param_vec[0:(self._M * 4),0]
00074 gearing_param_vec = param_vec[(self._M * 4):,0]
00075
00076 param_mat = reshape( dh_param_vec.T, (-1,4))
00077 config_dict = {}
00078 config_dict['dh'] = param_mat.tolist()
00079 config_dict['gearing'] = (array(gearing_param_vec)[:,0]).tolist()
00080 config_dict['cov'] = self._cov_dict
00081 return config_dict
00082
00083
00084 def inflate(self, param_vec):
00085 param_mat = param_vec[:self._M*4,:]
00086 self._config = reshape(param_mat, (-1,4))
00087 self._gearing = array(param_vec[self._M*4:,:])[:,0].tolist()
00088
00089
00090 def deflate(self):
00091 param_vec = reshape(self._config, (-1,1))
00092 param_vec = numpy.concatenate([param_vec, matrix(self._gearing).T])
00093 return param_vec
00094
00095
00096 def get_length(self):
00097 return self._M*5
00098
00099
00100
00101
00102 def fk(self, chain_state, link_num=-1):
00103 if link_num < 0:
00104 link_num = self._M
00105
00106 dh_trimmed = self._config[0:(link_num+1), :]
00107 pos_trimmed = chain_state.position[0:(link_num+1)]
00108 gearing_trimmed = self._gearing[0:(link_num+1)]
00109
00110 pos_scaled = [cur_pos * cur_gearing for cur_pos, cur_gearing in zip(pos_trimmed, gearing_trimmed)]
00111
00112 eef_pose = chain_T(dh_trimmed, pos_scaled)
00113 return eef_pose
00114
00115
00116
00117
00118
00119
00120 def chain_T(dh_params, joint_pos):
00121 if numpy.mod(dh_params.size,4) != 0:
00122 raise Exception("input size must be of length multiple of 4. Current is %s = %u" % (dh_params.shape, dh_params.size))
00123 M = dh_params.size/4
00124
00125 if len(joint_pos) != M:
00126 raise Exception("joint_positions must length must be [%u]. Currently is [%u]" % (M, len(joint_pos)))
00127
00128
00129 dh_final = numpy.array(dh_params.copy())
00130 dh_final.shape = (M, 4)
00131
00132 for m in range(0,M):
00133 dh_final[m,0] = dh_final[m,0]+joint_pos[m]
00134
00135
00136 return reduce( lambda x,y: x*y, [ link_T(r[0]) for r in vsplit(dh_final, M)] )
00137
00138
00139
00140
00141
00142 def link_T(dh_params):
00143
00144 theta = dh_params[0]
00145 alpha = dh_params[1]
00146 a = dh_params[2]
00147 d = dh_params[3]
00148
00149 T_theta = matrix([ [ cos(theta), -sin(theta), 0, 0 ],
00150 [ sin(theta), cos(theta), 0, 0 ],
00151 [ 0, 0, 1, 0 ],
00152 [ 0, 0, 0, 1 ] ])
00153
00154 T_alpha = matrix([ [ 1, 0, 0, 0 ],
00155 [ 0, cos(alpha), -sin(alpha), 0 ],
00156 [ 0, sin(alpha), cos(alpha), 0 ],
00157 [ 0, 0, 0, 1 ] ])
00158
00159 T_trans = matrix([ [ 1, 0, 0, a ],
00160 [ 0, 1, 0, 0 ],
00161 [ 0, 0, 1, d ],
00162 [ 0, 0, 0, 1 ] ])
00163
00164 return T_theta * T_trans * T_alpha