00001
00002
00003
00004
00005
00006
00007 import numpy as np, math
00008 import copy
00009 from threading import RLock
00010
00011 import roslib; roslib.load_manifest('equilibrium_point_control')
00012
00013 try:
00014 import hrl_lib.geometry as hg
00015 except ImportError, e:
00016 print '<hrl_arm.py> WARNING:', e
00017
00018
00019 class HRLArm():
00020 def __init__(self, kinematics):
00021
00022 self.kinematics = kinematics
00023 self.ep = None
00024 self.kp = None
00025 self.kd = None
00026 self.q = None
00027 self.qdot = None
00028 self.joint_names_list = None
00029 self.lock = RLock()
00030
00031 def get_joint_velocities(self):
00032 with self.lock:
00033 return copy.copy(self.qdot)
00034
00035 def get_joint_angles(self):
00036 with self.lock:
00037 return copy.copy(self.q)
00038
00039 def set_ep(self, *args):
00040 raise RuntimeError('Unimplemented Function')
00041
00042
00043 def publish_rviz_markers(self):
00044 raise RuntimeError('Unimplemented Function')
00045
00046 def get_ep(self):
00047 with self.lock:
00048 return copy.copy(self.ep)
00049
00050
00051
00052 def get_joint_impedance(self):
00053 with self.lock:
00054 return copy.copy(self.kp), copy.copy(self.kd)
00055
00056 def get_joint_names(self):
00057 with self.lock:
00058 return copy.copy(self.joint_names_list)
00059
00060
00061 def freeze(self):
00062 self.set_ep(self.ep)
00063
00064 def get_end_effector_pose(self):
00065 return self.kinematics.FK(self.get_joint_angles())
00066
00067
00068 class HRLArmKinematics():
00069 def __init__(self, n_jts):
00070 self.tooltip_pos = np.matrix([0.,0.,0.]).T
00071 self.tooltip_rot = np.matrix(np.eye(3))
00072 self.n_jts = n_jts
00073
00074
00075 def FK_vanilla(self, q, link_number=None):
00076 raise RuntimeError('Unimplemented Function')
00077
00078
00079
00080
00081 def FK(self, q, link_number=None):
00082 if link_number == None:
00083 link_number = self.n_jts
00084 if link_number > self.n_jts:
00085 raise RuntimeError('Link Number is greater than n_jts: %d'%link_number)
00086 pos, rot = self.FK_vanilla(q, link_number)
00087
00088 if link_number == self.n_jts:
00089 tooltip_baseframe = rot * self.tooltip_pos
00090 pos += tooltip_baseframe
00091 rot = rot * self.tooltip_rot
00092 return pos, rot
00093
00094
00095
00096
00097
00098
00099
00100 def IK(self, pos, rot, q_guess=None):
00101 last_link_pos = pos - rot * self.tooltip_rot.T * self.tooltip_pos
00102 last_link_rot = rot * self.tooltip_rot.T
00103 return self.IK_vanilla(last_link_pos, last_link_rot, q_guess)
00104
00105
00106 def IK_vanilla(self, p, rot, q_guess=None):
00107 raise RuntimeError('Unimplemented Function')
00108
00109
00110
00111 def IK(self, p, rot, q_guess=None):
00112
00113 pass
00114
00115
00116 def Jacobian(self, q, pos=None):
00117 raise RuntimeError('Unimplemented Function')
00118
00119
00120 def jacobian(self, q, pos=None):
00121 raise RuntimeError('Unimplemented Function')
00122
00123
00124 def get_joint_limits(self):
00125 raise RuntimeError('Unimplemented Function')
00126
00127
00128 def set_tooltip(self, p, rot=np.matrix(np.eye(3))):
00129 self.tooltip_pos = copy.copy(p)
00130 self.tooltip_rot = copy.copy(rot)
00131
00132
00133
00134
00135
00136
00137 def arm_config_to_points_list(self, q):
00138 return [self.FK(q, i)[0].A1[0:2] for i in range(len(q)+1)]
00139
00140
00141
00142 def distance_from_ee_along_arm(self, q, pt):
00143 p_l = self.arm_config_to_points_list(q)
00144 ee = self.FK(q)[0]
00145 d_ee = hg.distance_along_curve(ee, p_l)
00146 d_pt = hg.distance_along_curve(pt, p_l)
00147 assert(d_ee >= d_pt)
00148 return d_ee - d_pt
00149
00150
00151 def distance_from_arm(self, q, pt):
00152 p_l = self.arm_config_to_points_list(q)
00153 return hg.distance_from_curve(pt, p_l)
00154
00155
00156
00157
00158
00159
00160
00161
00162 def is_contact_at_joint(self, pt, q, dist_threshold):
00163 pts_list = [self.FK(q, i)[0].A1 for i in range(len(q)+1)]
00164 proj_pt = hg.project_point_on_curve(pt, pts_list)
00165
00166
00167 for jt in pts_list[:-1]:
00168 dist = np.linalg.norm(np.matrix(jt).T-proj_pt)
00169 if dist <= dist_threshold:
00170 return True
00171 return False
00172
00173
00174
00175
00176
00177
00178
00179
00180