|
| 1 | +import numpy as np |
| 2 | +import math |
| 3 | +from mpl_toolkits.mplot3d import Axes3D |
| 4 | +import matplotlib.pyplot as plt |
| 5 | + |
| 6 | +class Link: |
| 7 | + def __init__(self, dh_params): |
| 8 | + self.dh_params_ = dh_params |
| 9 | + |
| 10 | + def calc_transformation_matrix(self): |
| 11 | + theta = self.dh_params_[0] |
| 12 | + alpha = self.dh_params_[1] |
| 13 | + a = self.dh_params_[2] |
| 14 | + d = self.dh_params_[3] |
| 15 | + |
| 16 | + trans = np.array( |
| 17 | + [[math.cos(theta), -math.sin(theta), 0, a], |
| 18 | + [math.cos(alpha) * math.sin(theta), math.cos(alpha) * math.cos(theta), -math.sin(alpha), -d * math.sin(alpha)], |
| 19 | + [math.sin(alpha) * math.sin(theta), math.sin(alpha) * math.cos(theta), math.cos(alpha), d * math.cos(alpha)], |
| 20 | + [0, 0, 0, 1]]) |
| 21 | + |
| 22 | + return trans |
| 23 | + |
| 24 | +class NLinkArm: |
| 25 | + def __init__(self, dh_params_list): |
| 26 | + self.link_list = [] |
| 27 | + for i in range(len(dh_params_list)): |
| 28 | + self.link_list.append(Link(dh_params_list[i])) |
| 29 | + |
| 30 | + self.fig = plt.figure() |
| 31 | + self.ax = Axes3D(self.fig) |
| 32 | + |
| 33 | + def calc_transformation_matrix(self): |
| 34 | + trans = np.array([[1, 0, 0, 0], |
| 35 | + [0, 1, 0, 0], |
| 36 | + [0, 0, 1, 0], |
| 37 | + [0, 0, 0, 1]]) |
| 38 | + for i in range(len(self.link_list)): |
| 39 | + trans = np.dot(trans, self.link_list[i].calc_transformation_matrix()) |
| 40 | + |
| 41 | + return trans |
| 42 | + |
| 43 | + def forward_kinematics(self): |
| 44 | + trans = self.calc_transformation_matrix() |
| 45 | + |
| 46 | + x = trans[0, 3] |
| 47 | + y = trans[1, 3] |
| 48 | + z = trans[2, 3] |
| 49 | + alpha = math.atan2(trans[1, 2], trans[1, 3]) |
| 50 | + beta = math.atan2(trans[0, 2] * math.cos(alpha) + trans[1, 2] * math.sin(alpha), trans[2, 2]) |
| 51 | + 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)) |
| 52 | + |
| 53 | + return [x, y, z, alpha, beta, gamma] |
| 54 | + |
| 55 | + def set_joint_angles(self, joint_angle_list): |
| 56 | + for i in range(len(self.link_list)): |
| 57 | + self.link_list[i].dh_params_[0] = joint_angle_list[i] |
| 58 | + |
| 59 | + def plot(self): |
| 60 | + x_list = [] |
| 61 | + y_list = [] |
| 62 | + z_list = [] |
| 63 | + |
| 64 | + trans = np.identity(4) |
| 65 | + |
| 66 | + x_list.append(trans[0, 3]) |
| 67 | + y_list.append(trans[1, 3]) |
| 68 | + z_list.append(trans[2, 3]) |
| 69 | + for i in range(len(self.link_list)): |
| 70 | + trans = np.dot(trans, self.link_list[i].calc_transformation_matrix()) |
| 71 | + x_list.append(trans[0, 3]) |
| 72 | + y_list.append(trans[1, 3]) |
| 73 | + z_list.append(trans[2, 3]) |
| 74 | + |
| 75 | + self.ax.plot(x_list, y_list, z_list, "o-", color="#00aa00", ms=4, mew=0.5) |
| 76 | + self.ax.plot([0], [0], [0], "o") |
| 77 | + |
| 78 | + self.ax.set_xlim(-1, 1) |
| 79 | + self.ax.set_ylim(-1, 1) |
| 80 | + self.ax.set_zlim(-1, 1) |
| 81 | + plt.show() |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + n_link_arm = NLinkArm([[0., -math.pi/2, .1, 0.], |
| 85 | + [math.pi/2, math.pi/2, 0., 0.], |
| 86 | + [0., -math.pi/2, 0., .4], |
| 87 | + [0., math.pi/2, 0., 0.], |
| 88 | + [0., -math.pi/2, 0., .321], |
| 89 | + [0., math.pi/2, 0., 0.], |
| 90 | + [0., 0., 0., 0.]]) |
| 91 | + |
| 92 | + print(n_link_arm.forward_kinematics()) |
| 93 | + n_link_arm.set_joint_angles([1, 1, 1, 1, 1, 1, 1]) |
| 94 | + n_link_arm.plot() |
0 commit comments