1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| class Inverse_Kinematics: def DH(self,theta, d, a, alpha): return np.array([ [math.cos(theta), -math.sin(theta) * math.cos(alpha), math.sin(theta) * math.sin(alpha), a * math.cos(theta)], [math.sin(theta), math.cos(theta) * math.cos(alpha), -math.cos(theta) * math.sin(alpha), a * math.sin(theta)], [0, math.sin(alpha), math.cos(alpha), d], [0, 0, 0, 1] ]) def inverse_kinematics(self,pos, rot, d, a): R = np.array(rot) pos = np.array(pos) nx = R[0, 0]; ny = R[1, 0]; nz = R[2, 0]; ox = R[0, 1]; oy = R[1, 1]; oz = R[2, 1]; ax = R[0, 2]; ay = R[1, 2]; az = R[2, 2]; px = pos[0]; py = pos[1]; pz = pos[2]; m = d[5] * ay - py n = ax * d[5] - px theta1 = np.empty((1, 2)) theta1[0, 0] = math.atan2(m, n) - math.atan2(d[3], math.sqrt(m ** 2 + n ** 2 - d[3] ** 2)) theta1[0, 1] = math.atan2(m, n) - math.atan2(d[3], -math.sqrt(m ** 2 + n ** 2 - d[3] ** 2)) theta5 = np.empty((2, 2)) theta5[0, :2] = np.arccos(ax * np.sin(theta1) - ay * np.cos(theta1)) theta5[1, :2] = -np.arccos(ax * np.sin(theta1) - ay * np.cos(theta1)) mm = nx * np.sin(theta1) - ny * np.cos(theta1); nn = ox * np.sin(theta1) - oy * np.cos(theta1); theta6 = np.empty((2, 2)) theta6[0, :2] = np.arctan2(mm, nn) - np.arctan2(np.sin(theta5[0, :2]), 0) theta6[1, :2] = np.arctan2(mm, nn) - np.arctan2(np.sin(theta5[1, :2]), 0) mmm = np.empty((2, 2)) nnn = np.empty((2, 2)) mmm[0, :2] = d[4] * ( np.sin(theta6[0, :2]) * (nx * np.cos(theta1) + ny * np.sin(theta1)) + np.cos(theta6[0, 0:2]) * ( ox * np.cos(theta1) + oy * np.sin(theta1))) \ - d[5] * (ax * np.cos(theta1) + ay * np.sin(theta1)) + px * np.cos(theta1) + py * np.sin(theta1) nnn[0, :2] = pz - d[0] - az * d[5] + d[4] * (oz * np.cos(theta6[0, :2]) + nz * np.sin(theta6[0, :2])) mmm[1, :2] = d[4] * ( np.sin(theta6[1, :2]) * (nx * np.cos(theta1) + ny * np.sin(theta1)) + np.cos(theta6[1, :2]) * ( ox * np.cos(theta1) + oy * np.sin(theta1))) \ - d[5] * (ax * np.cos(theta1) + ay * np.sin(theta1)) + px * np.cos(theta1) + py * np.sin(theta1) nnn[1, :2] = pz - d[0] - az * d[5] + d[4] * (oz * np.cos(theta6[1, :2]) + nz * np.sin(theta6[1, :2])) theta3 = np.empty((4, 2)) theta3[0:2, :] = np.arccos((np.square(mmm) + np.square(nnn) - (a[1]) ** 2 - (a[2]) ** 2) / (2 * a[1] * a[2])) theta3[2:4, :] = -np.arccos((np.square(mmm) + np.square(nnn) - (a[1]) ** 2 - (a[2]) ** 2) / (2 * a[1] * a[2])) mmm_s2 = np.empty((4, 2)) nnn_s2 = np.empty((4, 2)) mmm_s2[0:2, :] = mmm mmm_s2[2:4, :] = mmm nnn_s2[0:2, :] = nnn nnn_s2[2:4, :] = nnn s2 = ((a[2] * np.cos(theta3) + a[1]) * nnn_s2 - a[2] * np.sin(theta3) * mmm_s2) / ( (a[1]) ** 2 + (a[2]) ** 2 + 2 * a[1] * a[2] * np.cos(theta3)) c2 = (mmm_s2 + a[2] * np.sin(theta3) * s2) / (a[2] * np.cos(theta3) + a[1]) theta2 = np.arctan2(s2, c2) theta = np.empty((8, 6)) theta[0:4, 0] = theta1[0, 0]; theta[4:8, 0] = theta1[0, 1] theta[:, 1] = [theta2[0, 0], theta2[2, 0], theta2[1, 0], theta2[3, 0], theta2[0, 1], theta2[2, 1], theta2[1, 1], theta2[3, 1]] theta[:, 2] = [theta3[0, 0], theta3[2, 0], theta3[1, 0], theta3[3, 0], theta3[0, 1], theta3[2, 1], theta3[1, 1], theta3[3, 1]] theta[0:2, 4] = theta5[0, 0]; theta[2:4, 4] = theta5[1, 0]; theta[4:6, 4] = theta5[0, 1]; theta[6:8, 4] = theta5[1, 1]; theta[0:2, 5] = theta6[0, 0]; theta[2:4, 5] = theta6[1, 0]; theta[4:6, 5] = theta6[0, 1]; theta[6:8, 5] = theta6[1, 1]; theta[:, 3] = np.arctan2( -np.sin(theta[:, 5]) * (nx * np.cos(theta[:, 0]) + ny * np.sin(theta[:, 0])) - np.cos(theta[:, 5]) * \ (ox * np.cos(theta[:, 0]) + oy * np.sin(theta[:, 0])), oz * np.cos(theta[:, 5]) + nz * np.sin(theta[:, 5])) - theta[:, 1] - theta[:, 2]; return theta def euler_to_rotation_matrix(self,yaw, pitch, roll): """ 将欧拉角转换为旋转矩阵w
Args: yaw (float): Yaw(偏航)角度,单位:度 pitch (float): Pitch(俯仰)角度,单位:度 roll (float): Roll(翻滚)角度,单位:度
Returns: np.array: 旋转矩阵 """ yaw = np.radians(yaw) pitch = np.radians(pitch) roll = np.radians(roll)
cy = np.cos(yaw) sy = np.sin(yaw) cp = np.cos(pitch) sp = np.sin(pitch) cr = np.cos(roll) sr = np.sin(roll)
rotation_matrix = np.array([ [cy * cp, cy * sp * sr - sy * cr, cy * sp * cr + sy * sr], [sy * cp, sy * sp * sr + cy * cr, sy * sp * cr - cy * sr], [-sp, cp * sr, cp * cr] ])
return rotation_matrix def main(self,back_pos,in_rot): d = [152, 0, 0, 130, 102, 100] a = [0, -425, -395, 0, 0, 0] rx = in_rot[0] ry = in_rot[1] rz = in_rot[2] rot_test = [rx,ry,rz] back_rot = self.euler_to_rotation_matrix(rz, ry, rx) theta_new = self.inverse_kinematics(back_pos, back_rot, d, a) theta_new = np.degrees(theta_new) print('逆运动学结果:', theta_new) return theta_new,rot_test
|