|
| 1 | +""" |
| 2 | +Class of n-link arm in 3D |
| 3 | +Author: Takayuki Murooka (takayuki5168) |
| 4 | +""" |
| 5 | +import numpy as np |
| 6 | +import math |
| 7 | +from mpl_toolkits.mplot3d import Axes3D |
| 8 | +import matplotlib.pyplot as plt |
| 9 | + |
| 10 | +class Link: |
| 11 | + def __init__(self, dh_params): |
| 12 | + self.dh_params_ = dh_params |
| 13 | + |
| 14 | + def transformation_matrix(self): |
| 15 | + theta = self.dh_params_[0] |
| 16 | + alpha = self.dh_params_[1] |
| 17 | + a = self.dh_params_[2] |
| 18 | + d = self.dh_params_[3] |
| 19 | + |
| 20 | + st = math.sin(theta) |
| 21 | + ct = math.cos(theta) |
| 22 | + sa = math.sin(alpha) |
| 23 | + ca = math.cos(alpha) |
| 24 | + trans = np.array([[ct, -st * ca, st * sa, a * ct], |
| 25 | + [st, ct * ca, -ct * sa, a * st], |
| 26 | + [0, sa, ca, d], |
| 27 | + [0, 0, 0, 1]]) |
| 28 | + |
| 29 | + return trans |
| 30 | + |
| 31 | + def basic_jacobian(self, trans_prev, ee_pos): |
| 32 | + pos_prev = np.array([trans_prev[0, 3], trans_prev[1, 3], trans_prev[2, 3]]) |
| 33 | + z_axis_prev = np.array([trans_prev[0, 2], trans_prev[1, 2], trans_prev[2, 2]]) |
| 34 | + |
| 35 | + basic_jacobian = np.hstack((np.cross(z_axis_prev, ee_pos - pos_prev), z_axis_prev)) |
| 36 | + return basic_jacobian |
| 37 | + |
| 38 | + |
| 39 | +class NLinkArm: |
| 40 | + def __init__(self, dh_params_list): |
| 41 | + self.link_list = [] |
| 42 | + for i in range(len(dh_params_list)): |
| 43 | + self.link_list.append(Link(dh_params_list[i])) |
| 44 | + |
| 45 | + def transformation_matrix(self): |
| 46 | + trans = np.identity(4) |
| 47 | + for i in range(len(self.link_list)): |
| 48 | + trans = np.dot(trans, self.link_list[i].transformation_matrix()) |
| 49 | + return trans |
| 50 | + |
| 51 | + def forward_kinematics(self, plot=False): |
| 52 | + trans = self.transformation_matrix() |
| 53 | + |
| 54 | + x = trans[0, 3] |
| 55 | + y = trans[1, 3] |
| 56 | + z = trans[2, 3] |
| 57 | + alpha, beta, gamma = self.euler_angle() |
| 58 | + |
| 59 | + if plot: |
| 60 | + self.fig = plt.figure() |
| 61 | + self.ax = Axes3D(self.fig) |
| 62 | + |
| 63 | + x_list = [] |
| 64 | + y_list = [] |
| 65 | + z_list = [] |
| 66 | + |
| 67 | + trans = np.identity(4) |
| 68 | + |
| 69 | + x_list.append(trans[0, 3]) |
| 70 | + y_list.append(trans[1, 3]) |
| 71 | + z_list.append(trans[2, 3]) |
| 72 | + for i in range(len(self.link_list)): |
| 73 | + trans = np.dot(trans, self.link_list[i].transformation_matrix()) |
| 74 | + x_list.append(trans[0, 3]) |
| 75 | + y_list.append(trans[1, 3]) |
| 76 | + z_list.append(trans[2, 3]) |
| 77 | + |
| 78 | + self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4, mew=0.5) |
| 79 | + self.ax.plot([0], [0], [0], "o") |
| 80 | + |
| 81 | + self.ax.set_xlim(-1, 1) |
| 82 | + self.ax.set_ylim(-1, 1) |
| 83 | + self.ax.set_zlim(-1, 1) |
| 84 | + |
| 85 | + plt.show() |
| 86 | + |
| 87 | + return [x, y, z, alpha, beta, gamma] |
| 88 | + |
| 89 | + def basic_jacobian(self): |
| 90 | + ee_pos = self.forward_kinematics()[0:3] |
| 91 | + basic_jacobian_mat = [] |
| 92 | + |
| 93 | + trans = np.identity(4) |
| 94 | + for i in range(len(self.link_list)): |
| 95 | + basic_jacobian_mat.append(self.link_list[i].basic_jacobian(trans, ee_pos)) |
| 96 | + trans = np.dot(trans, self.link_list[i].transformation_matrix()) |
| 97 | + |
| 98 | + return np.array(basic_jacobian_mat).T |
| 99 | + |
| 100 | + def inverse_kinematics(self, ref_ee_pose, plot=False): |
| 101 | + for cnt in range(500): |
| 102 | + ee_pose = self.forward_kinematics() |
| 103 | + diff_pose = np.array(ref_ee_pose) - ee_pose |
| 104 | + |
| 105 | + basic_jacobian_mat = self.basic_jacobian() |
| 106 | + alpha, beta, gamma = self.euler_angle() |
| 107 | + |
| 108 | + K_zyz = np.array([[0, -math.sin(alpha), math.cos(alpha) * math.sin(beta)], |
| 109 | + [0, math.cos(alpha), math.sin(alpha) * math.sin(beta)], |
| 110 | + [1, 0, math.cos(beta)]]) |
| 111 | + K_alpha = np.identity(6) |
| 112 | + K_alpha[3:, 3:] = K_zyz |
| 113 | + |
| 114 | + theta_dot = np.dot(np.dot(np.linalg.pinv(basic_jacobian_mat), K_alpha), np.array(diff_pose)) |
| 115 | + self.update_joint_angles(theta_dot / 100.) |
| 116 | + |
| 117 | + if plot: |
| 118 | + self.fig = plt.figure() |
| 119 | + self.ax = Axes3D(self.fig) |
| 120 | + |
| 121 | + x_list = [] |
| 122 | + y_list = [] |
| 123 | + z_list = [] |
| 124 | + |
| 125 | + trans = np.identity(4) |
| 126 | + |
| 127 | + x_list.append(trans[0, 3]) |
| 128 | + y_list.append(trans[1, 3]) |
| 129 | + z_list.append(trans[2, 3]) |
| 130 | + for i in range(len(self.link_list)): |
| 131 | + trans = np.dot(trans, self.link_list[i].transformation_matrix()) |
| 132 | + x_list.append(trans[0, 3]) |
| 133 | + y_list.append(trans[1, 3]) |
| 134 | + z_list.append(trans[2, 3]) |
| 135 | + |
| 136 | + self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4, mew=0.5) |
| 137 | + self.ax.plot([0], [0], [0], "o") |
| 138 | + |
| 139 | + self.ax.set_xlim(-1, 1) |
| 140 | + self.ax.set_ylim(-1, 1) |
| 141 | + self.ax.set_zlim(-1, 1) |
| 142 | + |
| 143 | + self.ax.plot([ref_ee_pose[0]], [ref_ee_pose[1]], [ref_ee_pose[2]], "o") |
| 144 | + plt.show() |
| 145 | + |
| 146 | + def euler_angle(self): |
| 147 | + trans = self.transformation_matrix() |
| 148 | + |
| 149 | + alpha = math.atan2(trans[1][2], trans[0][2]) |
| 150 | + if not (-math.pi / 2 <= alpha and alpha <= math.pi / 2): |
| 151 | + alpha = math.atan2(trans[1][2], trans[0][2]) + math.pi |
| 152 | + if not (-math.pi / 2 <= alpha and alpha <= math.pi / 2): |
| 153 | + alpha = math.atan2(trans[1][2], trans[0][2]) - math.pi |
| 154 | + beta = math.atan2(trans[0][2] * math.cos(alpha) + trans[1][2] * math.sin(alpha), trans[2][2]) |
| 155 | + gamma = math.atan2(-trans[0][0] * math.sin(alpha) + trans[1][0] * math.cos(alpha), -trans[0][1] * math.sin(alpha) + trans[1][1] * math.cos(alpha)) |
| 156 | + |
| 157 | + return alpha, beta, gamma |
| 158 | + |
| 159 | + def set_joint_angles(self, joint_angle_list): |
| 160 | + for i in range(len(self.link_list)): |
| 161 | + self.link_list[i].dh_params_[0] = joint_angle_list[i] |
| 162 | + |
| 163 | + def update_joint_angles(self, diff_joint_angle_list): |
| 164 | + for i in range(len(self.link_list)): |
| 165 | + self.link_list[i].dh_params_[0] += diff_joint_angle_list[i] |
| 166 | + |
| 167 | + def plot(self): |
| 168 | + self.fig = plt.figure() |
| 169 | + self.ax = Axes3D(self.fig) |
| 170 | + |
| 171 | + x_list = [] |
| 172 | + y_list = [] |
| 173 | + z_list = [] |
| 174 | + |
| 175 | + trans = np.identity(4) |
| 176 | + |
| 177 | + x_list.append(trans[0, 3]) |
| 178 | + y_list.append(trans[1, 3]) |
| 179 | + z_list.append(trans[2, 3]) |
| 180 | + for i in range(len(self.link_list)): |
| 181 | + trans = np.dot(trans, self.link_list[i].transformation_matrix()) |
| 182 | + x_list.append(trans[0, 3]) |
| 183 | + y_list.append(trans[1, 3]) |
| 184 | + z_list.append(trans[2, 3]) |
| 185 | + |
| 186 | + self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4, mew=0.5) |
| 187 | + self.ax.plot([0], [0], [0], "o") |
| 188 | + |
| 189 | + self.ax.set_xlabel("x") |
| 190 | + self.ax.set_ylabel("y") |
| 191 | + self.ax.set_zlabel("z") |
| 192 | + |
| 193 | + self.ax.set_xlim(-1, 1) |
| 194 | + self.ax.set_ylim(-1, 1) |
| 195 | + self.ax.set_zlim(-1, 1) |
| 196 | + plt.show() |
0 commit comments