Go to the documentation of this file.00001
00002 import numpy as np
00003
00004 from ur_kin_py.ur5_kin_py import forward as ur5_forward
00005 from ur_kin_py.ur5_kin_py import inverse as ur5_inverse
00006 from ur_kin_py.ur10_kin_py import forward as ur10_forward
00007 from ur_kin_py.ur10_kin_py import inverse as ur10_inverse
00008
00009 class Kinematics(object):
00010 UR5_ID = 'ur5'
00011 UR10_ID = 'ur10'
00012 def __init__(self, model):
00013 if model == 'ur5':
00014 self._forward = ur5_forward
00015 self._inverse = ur5_inverse
00016 elif model == 'ur10':
00017 self._forward = ur10_forward
00018 self._inverse = ur10_inverse
00019 else:
00020 print 'Model %s unknown (use "ur5" or "ur10")'
00021
00022
00023 def forward(self, q):
00024 q_arr = np.array(q)*1.0
00025 if len(q_arr) != 6:
00026 print 'q should be array-like of length 6'
00027 return None
00028 return np.mat(self._forward(q_arr))
00029
00030 def inverse_all(self, x, q_guess=None, q_min=6*[-2.*np.pi], q_max=6*[2.*np.pi]):
00031 x_mat = np.array(x)
00032 if x_mat.shape != (4,4):
00033 print 'q should be array-like of length 6'
00034 return None
00035
00036 if q_guess is None:
00037 q_guess = np.zeros(6)
00038 q_min = np.array(q_min)
00039 q_max = np.array(q_max)
00040 sols = self._inverse(x_mat, q_guess[5])
00041 closest_sols = []
00042 for sol in sols:
00043 test_sol = np.ones(6)*9999.
00044 for i in range(6):
00045 for add_ang in [-2.*np.pi, 0]:
00046 test_ang = sol[i] + add_ang
00047 if (test_ang >= q_min[i] and test_ang <= q_max[i] and
00048 abs(test_ang) < 2.*np.pi and
00049 abs(test_ang - q_guess[i]) < abs(test_sol[i] - q_guess[i])):
00050 test_sol[i] = test_ang
00051 if np.all(test_sol != 9999.):
00052
00053 if np.allclose(np.linalg.inv(self.forward(test_sol)) * x, np.eye(4)):
00054 closest_sols.append(test_sol)
00055 return closest_sols
00056
00057 def inverse(self, x, q_guess=None, q_min=6*[-2.*np.pi], q_max=6*[2.*np.pi], weights=6*[1.]):
00058 x_mat = np.array(x)
00059 if x_mat.shape != (4,4):
00060 print 'q should be array-like of length 6'
00061 return None
00062
00063 if q_guess is None:
00064 q_guess = np.zeros(6)
00065 closest_sols = self.inverse_all(x_mat, q_guess=q_guess, q_min=q_min, q_max=q_max)
00066 if len(closest_sols) == 0:
00067 return None
00068 best_sol_ind = np.argmin(np.sum((weights*(closest_sols - np.array(q_guess)))**2,1))
00069 best_sol = closest_sols[best_sol_ind]
00070 if False:
00071 print 'q_guess', q_guess
00072 print 'q_resid', q_resid
00073 print 'sols', sols
00074 print 'closest_sols', closest_sols
00075 print 'best_sol', best_sol
00076 return best_sol
00077
00078
00079
00080
00081