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 JointChain:
00038 '''
00039 This represents a chain of actuated joints
00040 parameters = [gearing*num_of_joints]
00041 '''
00042 def __init__(self, config):
00043 self.root = config['root']
00044 self.tip = config['tip']
00045 self._joints = config['joints']
00046 self._active = config['active_joints']
00047 self._M = len(config['active_joints'])
00048
00049 rospy.logdebug("Initializing joint chain with [%u] links", self._M)
00050
00051 self._transforms = config['transforms']
00052 self._axis = numpy.matrix([ eval(str(x)) for x in config['axis']], float)
00053 self._cov_dict = config['cov']
00054 self._gearing = config['gearing']
00055
00056 def calc_free(self, free_config):
00057 return [x == 1 for x in free_config['gearing']]
00058
00059 def params_to_config(self, param_vec):
00060 config_dict = {}
00061 config_dict['axis'] = self._axis.tolist()
00062 config_dict['gearing'] = (array(param_vec)[:,0]).tolist()
00063 config_dict['cov'] = self._cov_dict
00064 config_dict['root'] = self.root
00065 config_dict['tip'] = self.tip
00066 return config_dict
00067
00068
00069 def inflate(self, param_vec):
00070 self._gearing = array(param_vec)[:,0].tolist()
00071
00072
00073 def deflate(self):
00074 return matrix(self._gearing).T
00075
00076
00077 def get_length(self):
00078 return self._M
00079
00080
00081
00082
00083 def fk(self, chain_state, link_num=-1):
00084 if link_num < 0:
00085 link_num = self._M
00086
00087 pos_trimmed = chain_state.position[0:(link_num+1)]
00088 gearing_trimmed = self._gearing[0:(link_num+1)]
00089
00090 pos_scaled = [cur_pos * cur_gearing for cur_pos, cur_gearing in zip(pos_trimmed, gearing_trimmed)]
00091
00092 T = matrix([ [ 1, 0, 0, 0 ],
00093 [ 0, 1, 0, 0 ],
00094 [ 0, 0, 1, 0 ],
00095 [ 0, 0, 0, 1 ] ])
00096
00097 m = 0
00098 for joint_name in self._joints:
00099 if m > link_num:
00100 break
00101 transform = self._transforms[joint_name]
00102 if joint_name in self._active:
00103 k = int(self._axis[0,m])
00104 p = transform.deflate()
00105 p = p.T.tolist()[0]
00106 if k > 0:
00107 p[k-1] = p[k-1] + pos_scaled[m]
00108 else:
00109 p[abs(k)-1] = p[abs(k)-1] - pos_scaled[m]
00110 T = T * link_T( p )
00111 m += 1
00112 else:
00113 T = T * transform.transform
00114
00115 return T
00116
00117 def link_T(link_params):
00118 r = link_params[3]
00119 p = link_params[4]
00120 y = link_params[5]
00121
00122 T_roll = matrix([ [ 1, 0, 0, 0 ],
00123 [ 0, cos(r), -sin(r), 0 ],
00124 [ 0, sin(r), cos(r), 0 ],
00125 [ 0, 0, 0, 1 ] ])
00126
00127 T_pitch = matrix([ [ cos(p), 0, sin(p), 0 ],
00128 [ 0, 1, 0, 0 ],
00129 [ -sin(p), 0, cos(p), 0 ],
00130 [ 0, 0, 0, 1 ] ])
00131
00132 T_yaw = matrix([ [ cos(y), -sin(y), 0, 0 ],
00133 [ sin(y), cos(y), 0, 0 ],
00134 [ 0, 0, 1, 0 ],
00135 [ 0, 0, 0, 1 ] ])
00136
00137
00138 T_trans = matrix([ [ 1, 0, 0, link_params[0] ],
00139 [ 0, 1, 0, link_params[1] ],
00140 [ 0, 0, 1, link_params[2] ],
00141 [ 0, 0, 0, 1 ] ])
00142
00143 return T_trans * T_roll * T_pitch * T_yaw
00144