77from mpl_toolkits .mplot3d import Axes3D
88import matplotlib .pyplot as plt
99
10+
1011class Link :
1112 def __init__ (self , dh_params ):
1213 self .dh_params_ = dh_params
@@ -28,16 +29,22 @@ def transformation_matrix(self):
2829
2930 return trans
3031
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 ]])
3438
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 ))
3641 return basic_jacobian
37-
42+
3843
3944class NLinkArm :
4045 def __init__ (self , dh_params_list ):
46+ self .fig = plt .figure ()
47+ self .ax = Axes3D (self .fig )
4148 self .link_list = []
4249 for i in range (len (dh_params_list )):
4350 self .link_list .append (Link (dh_params_list [i ]))
@@ -47,7 +54,7 @@ def transformation_matrix(self):
4754 for i in range (len (self .link_list )):
4855 trans = np .dot (trans , self .link_list [i ].transformation_matrix ())
4956 return trans
50-
57+
5158 def forward_kinematics (self , plot = False ):
5259 trans = self .transformation_matrix ()
5360
@@ -57,15 +64,12 @@ def forward_kinematics(self, plot=False):
5764 alpha , beta , gamma = self .euler_angle ()
5865
5966 if plot :
60- self .fig = plt .figure ()
61- self .ax = Axes3D (self .fig )
62-
6367 x_list = []
6468 y_list = []
6569 z_list = []
66-
70+
6771 trans = np .identity (4 )
68-
72+
6973 x_list .append (trans [0 , 3 ])
7074 y_list .append (trans [1 , 3 ])
7175 z_list .append (trans [2 , 3 ])
@@ -74,56 +78,61 @@ def forward_kinematics(self, plot=False):
7478 x_list .append (trans [0 , 3 ])
7579 y_list .append (trans [1 , 3 ])
7680 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 )
7984 self .ax .plot ([0 ], [0 ], [0 ], "o" )
80-
85+
8186 self .ax .set_xlim (- 1 , 1 )
8287 self .ax .set_ylim (- 1 , 1 )
8388 self .ax .set_zlim (- 1 , 1 )
84-
89+
8590 plt .show ()
8691
8792 return [x , y , z , alpha , beta , gamma ]
8893
8994 def basic_jacobian (self ):
9095 ee_pos = self .forward_kinematics ()[0 :3 ]
9196 basic_jacobian_mat = []
92-
93- trans = np .identity (4 )
97+
98+ trans = np .identity (4 )
9499 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 ))
96102 trans = np .dot (trans , self .link_list [i ].transformation_matrix ())
97-
103+
98104 return np .array (basic_jacobian_mat ).T
99105
100106 def inverse_kinematics (self , ref_ee_pose , plot = False ):
101107 for cnt in range (500 ):
102108 ee_pose = self .forward_kinematics ()
103109 diff_pose = np .array (ref_ee_pose ) - ee_pose
104-
110+
105111 basic_jacobian_mat = self .basic_jacobian ()
106112 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 )]])
111118 K_alpha = np .identity (6 )
112119 K_alpha [3 :, 3 :] = K_zyz
113120
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 ))
115124 self .update_joint_angles (theta_dot / 100. )
116-
125+
117126 if plot :
118127 self .fig = plt .figure ()
119128 self .ax = Axes3D (self .fig )
120-
129+
121130 x_list = []
122131 y_list = []
123132 z_list = []
124-
133+
125134 trans = np .identity (4 )
126-
135+
127136 x_list .append (trans [0 , 3 ])
128137 y_list .append (trans [1 , 3 ])
129138 z_list .append (trans [2 , 3 ])
@@ -132,48 +141,54 @@ def inverse_kinematics(self, ref_ee_pose, plot=False):
132141 x_list .append (trans [0 , 3 ])
133142 y_list .append (trans [1 , 3 ])
134143 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 )
137147 self .ax .plot ([0 ], [0 ], [0 ], "o" )
138-
148+
139149 self .ax .set_xlim (- 1 , 1 )
140150 self .ax .set_ylim (- 1 , 1 )
141151 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" )
144155 plt .show ()
145-
156+
146157 def euler_angle (self ):
147158 trans = self .transformation_matrix ()
148-
159+
149160 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 ):
151162 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 ):
153164 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+
157172 return alpha , beta , gamma
158-
173+
159174 def set_joint_angles (self , joint_angle_list ):
160175 for i in range (len (self .link_list )):
161176 self .link_list [i ].dh_params_ [0 ] = joint_angle_list [i ]
162177
163178 def update_joint_angles (self , diff_joint_angle_list ):
164179 for i in range (len (self .link_list )):
165180 self .link_list [i ].dh_params_ [0 ] += diff_joint_angle_list [i ]
166-
181+
167182 def plot (self ):
168183 self .fig = plt .figure ()
169184 self .ax = Axes3D (self .fig )
170-
185+
171186 x_list = []
172187 y_list = []
173188 z_list = []
174189
175190 trans = np .identity (4 )
176-
191+
177192 x_list .append (trans [0 , 3 ])
178193 y_list .append (trans [1 , 3 ])
179194 z_list .append (trans [2 , 3 ])
@@ -182,15 +197,16 @@ def plot(self):
182197 x_list .append (trans [0 , 3 ])
183198 y_list .append (trans [1 , 3 ])
184199 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 )
187203 self .ax .plot ([0 ], [0 ], [0 ], "o" )
188204
189205 self .ax .set_xlabel ("x" )
190206 self .ax .set_ylabel ("y" )
191- self .ax .set_zlabel ("z" )
192-
207+ self .ax .set_zlabel ("z" )
208+
193209 self .ax .set_xlim (- 1 , 1 )
194210 self .ax .set_ylim (- 1 , 1 )
195- self .ax .set_zlim (- 1 , 1 )
211+ self .ax .set_zlim (- 1 , 1 )
196212 plt .show ()
0 commit comments