@@ -7,7 +7,7 @@ class Link:
7
7
def __init__ (self , dh_params ):
8
8
self .dh_params_ = dh_params
9
9
10
- def calc_transformation_matrix (self ):
10
+ def transformation_matrix (self ):
11
11
theta = self .dh_params_ [0 ]
12
12
alpha = self .dh_params_ [1 ]
13
13
a = self .dh_params_ [2 ]
@@ -21,6 +21,14 @@ def calc_transformation_matrix(self):
21
21
22
22
return trans
23
23
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
+
24
32
class NLinkArm :
25
33
def __init__ (self , dh_params_list ):
26
34
self .link_list = []
@@ -30,18 +38,14 @@ def __init__(self, dh_params_list):
30
38
self .fig = plt .figure ()
31
39
self .ax = Axes3D (self .fig )
32
40
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 )
38
43
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 ())
41
45
return trans
42
46
43
47
def forward_kinematics (self ):
44
- trans = self .calc_transformation_matrix ()
48
+ trans = self .transformation_matrix ()
45
49
46
50
x = trans [0 , 3 ]
47
51
y = trans [1 , 3 ]
@@ -52,9 +56,50 @@ def forward_kinematics(self):
52
56
53
57
return [x , y , z , alpha , beta , gamma ]
54
58
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
+
55
96
def set_joint_angles (self , joint_angle_list ):
56
97
for i in range (len (self .link_list )):
57
98
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 ]
58
103
59
104
def plot (self ):
60
105
x_list = []
@@ -67,7 +112,7 @@ def plot(self):
67
112
y_list .append (trans [1 , 3 ])
68
113
z_list .append (trans [2 , 3 ])
69
114
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 ())
71
116
x_list .append (trans [0 , 3 ])
72
117
y_list .append (trans [1 , 3 ])
73
118
z_list .append (trans [2 , 3 ])
0 commit comments