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