Skip to content

Commit c49f982

Browse files
committed
implement inverse kinematics
1 parent 2d90bd5 commit c49f982

File tree

3 files changed

+72
-10
lines changed

3 files changed

+72
-10
lines changed

ArmNavigation/n_joint_arm_3d/NLinkArm.py

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Link:
77
def __init__(self, dh_params):
88
self.dh_params_ = dh_params
99

10-
def calc_transformation_matrix(self):
10+
def transformation_matrix(self):
1111
theta = self.dh_params_[0]
1212
alpha = self.dh_params_[1]
1313
a = self.dh_params_[2]
@@ -21,6 +21,14 @@ def calc_transformation_matrix(self):
2121

2222
return trans
2323

24+
def basic_jacobian(self, trans_prev, ee_pose):
25+
pos_prev = np.array([trans_prev[0, 3], trans_prev[1, 3], trans_prev[2, 3]])
26+
z_axis_prev = np.array([trans_prev[0, 2], trans_prev[1, 2], trans_prev[2, 2]])
27+
28+
basic_jacobian = np.hstack((np.cross(z_axis_prev, ee_pose - pos_prev), z_axis_prev))
29+
return basic_jacobian
30+
31+
2432
class NLinkArm:
2533
def __init__(self, dh_params_list):
2634
self.link_list = []
@@ -30,18 +38,14 @@ def __init__(self, dh_params_list):
3038
self.fig = plt.figure()
3139
self.ax = Axes3D(self.fig)
3240

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]])
41+
def transformation_matrix(self):
42+
trans = np.identity(4)
3843
for i in range(len(self.link_list)):
39-
trans = np.dot(trans, self.link_list[i].calc_transformation_matrix())
40-
44+
trans = np.dot(trans, self.link_list[i].transformation_matrix())
4145
return trans
4246

4347
def forward_kinematics(self):
44-
trans = self.calc_transformation_matrix()
48+
trans = self.transformation_matrix()
4549

4650
x = trans[0, 3]
4751
y = trans[1, 3]
@@ -52,9 +56,50 @@ def forward_kinematics(self):
5256

5357
return [x, y, z, alpha, beta, gamma]
5458

59+
def basic_jacobian(self, ref_ee_pose):
60+
basic_jacobian_mat = []
61+
62+
trans = np.identity(4)
63+
for i in range(len(self.link_list)):
64+
trans = np.dot(trans, self.link_list[i].transformation_matrix())
65+
basic_jacobian_mat.append(self.link_list[i].basic_jacobian(trans, ref_ee_pose[0:3]))
66+
67+
#print(np.array(basic_jacobian_mat).T)
68+
return np.array(basic_jacobian_mat).T
69+
70+
def inverse_kinematics(self, ref_ee_pose):
71+
ee_pose = self.forward_kinematics()
72+
diff_pose = ee_pose - np.array(ref_ee_pose)
73+
74+
for cnt in range(1000):
75+
basic_jacobian_mat = self.basic_jacobian(ref_ee_pose)
76+
alpha, beta, gamma = self.calc_euler_angle()
77+
78+
K_zyz = np.array([[0, -math.sin(alpha), math.cos(alpha) * math.sin(beta)],
79+
[0, math.cos(alpha), math.sin(alpha) * math.sin(beta)],
80+
[1, 0, math.cos(beta)]])
81+
K_alpha = np.identity(6)
82+
K_alpha[3:, 3:] = K_zyz
83+
84+
theta_dot = np.dot(np.dot(np.linalg.pinv(basic_jacobian_mat), K_alpha), np.array(diff_pose))
85+
self.update_joint_angles(theta_dot)
86+
87+
def calc_euler_angle(self):
88+
trans = self.transformation_matrix()
89+
90+
alpha = math.atan2(trans[1][2], trans[0][2])
91+
beta = math.atan2(trans[0][2] * math.cos(alpha) + trans[1][2] * math.sin(alpha), trans[2][2])
92+
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))
93+
94+
return alpha, beta, gamma
95+
5596
def set_joint_angles(self, joint_angle_list):
5697
for i in range(len(self.link_list)):
5798
self.link_list[i].dh_params_[0] = joint_angle_list[i]
99+
100+
def update_joint_angles(self, diff_joint_angle_list):
101+
for i in range(len(self.link_list)):
102+
self.link_list[i].dh_params_[0] += diff_joint_angle_list[i]
58103

59104
def plot(self):
60105
x_list = []
@@ -67,7 +112,7 @@ def plot(self):
67112
y_list.append(trans[1, 3])
68113
z_list.append(trans[2, 3])
69114
for i in range(len(self.link_list)):
70-
trans = np.dot(trans, self.link_list[i].calc_transformation_matrix())
115+
trans = np.dot(trans, self.link_list[i].transformation_matrix())
71116
x_list.append(trans[0, 3])
72117
y_list.append(trans[1, 3])
73118
z_list.append(trans[2, 3])
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import math
2+
from NLinkArm import NLinkArm
3+
import random
4+
import time
5+
6+
7+
n_link_arm = NLinkArm([[0., -math.pi/2, .1, 0.],
8+
[math.pi/2, math.pi/2, 0., 0.],
9+
[0., -math.pi/2, 0., .4],
10+
[0., math.pi/2, 0., 0.],
11+
[0., -math.pi/2, 0., .321],
12+
[0., math.pi/2, 0., 0.],
13+
[0., 0., 0., 0.]])
14+
15+
#n_link_arm.inverse_kinematics([-0.621, 0., 0., 0., 0., math.pi / 2])
16+
n_link_arm.inverse_kinematics([-0.5, 0., 0.1, 0., 0., math.pi / 2])
17+
n_link_arm.plot()

0 commit comments

Comments
 (0)