@@ -13,11 +13,21 @@ def transformation_matrix(self):
13
13
a = self .dh_params_ [2 ]
14
14
d = self .dh_params_ [3 ]
15
15
16
+ '''
16
17
trans = np.array(
17
18
[[math.cos(theta), -math.sin(theta), 0, a],
18
19
[math.cos(alpha) * math.sin(theta), math.cos(alpha) * math.cos(theta), -math.sin(alpha), -d * math.sin(alpha)],
19
20
[math.sin(alpha) * math.sin(theta), math.sin(alpha) * math.cos(theta), math.cos(alpha), d * math.cos(alpha)],
20
21
[0, 0, 0, 1]])
22
+ '''
23
+ st = math .sin (theta )
24
+ ct = math .cos (theta )
25
+ sa = math .sin (alpha )
26
+ ca = math .cos (alpha )
27
+ trans = np .array ([[ct , - st * ca , st * sa , a * ct ],
28
+ [st , ct * ca , - ct * sa , a * st ],
29
+ [0 , sa , ca , d ],
30
+ [0 , 0 , 0 , 1 ]])
21
31
22
32
return trans
23
33
@@ -35,44 +45,66 @@ def __init__(self, dh_params_list):
35
45
for i in range (len (dh_params_list )):
36
46
self .link_list .append (Link (dh_params_list [i ]))
37
47
38
- self .fig = plt .figure ()
39
- self .ax = Axes3D (self .fig )
40
-
41
48
def transformation_matrix (self ):
42
49
trans = np .identity (4 )
43
50
for i in range (len (self .link_list )):
44
51
trans = np .dot (trans , self .link_list [i ].transformation_matrix ())
45
52
return trans
46
53
47
- def forward_kinematics (self ):
54
+ def forward_kinematics (self , plot = False ):
48
55
trans = self .transformation_matrix ()
49
56
50
57
x = trans [0 , 3 ]
51
58
y = trans [1 , 3 ]
52
59
z = trans [2 , 3 ]
53
- alpha = math .atan2 (trans [1 , 2 ], trans [1 , 3 ])
54
- beta = math .atan2 (trans [0 , 2 ] * math .cos (alpha ) + trans [1 , 2 ] * math .sin (alpha ), trans [2 , 2 ])
55
- 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 ))
60
+ alpha , beta , gamma = self .calc_euler_angle ()
61
+
62
+ if plot :
63
+ self .fig = plt .figure ()
64
+ self .ax = Axes3D (self .fig )
65
+
66
+ x_list = []
67
+ y_list = []
68
+ z_list = []
69
+
70
+ trans = np .identity (4 )
71
+
72
+ x_list .append (trans [0 , 3 ])
73
+ y_list .append (trans [1 , 3 ])
74
+ z_list .append (trans [2 , 3 ])
75
+ for i in range (len (self .link_list )):
76
+ trans = np .dot (trans , self .link_list [i ].transformation_matrix ())
77
+ x_list .append (trans [0 , 3 ])
78
+ y_list .append (trans [1 , 3 ])
79
+ z_list .append (trans [2 , 3 ])
80
+
81
+ self .ax .plot (x_list , y_list , z_list , "o-" , color = "#00aa00" , ms = 4 , mew = 0.5 )
82
+ self .ax .plot ([0 ], [0 ], [0 ], "o" )
83
+
84
+ self .ax .set_xlim (- 1 , 1 )
85
+ self .ax .set_ylim (- 1 , 1 )
86
+ self .ax .set_zlim (- 1 , 1 )
87
+
88
+ plt .show ()
56
89
57
90
return [x , y , z , alpha , beta , gamma ]
58
91
59
- def basic_jacobian (self , ref_ee_pose ):
92
+ def basic_jacobian (self , ee_pose ):
60
93
basic_jacobian_mat = []
61
94
62
95
trans = np .identity (4 )
63
96
for i in range (len (self .link_list )):
97
+ basic_jacobian_mat .append (self .link_list [i ].basic_jacobian (trans , ee_pose [0 :3 ]))
64
98
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
99
67
- #print(np.array(basic_jacobian_mat).T)
68
100
return np .array (basic_jacobian_mat ).T
69
101
70
- def inverse_kinematics (self , ref_ee_pose ):
71
- ee_pose = self .forward_kinematics ()
72
- diff_pose = ee_pose - np .array (ref_ee_pose )
102
+ def inverse_kinematics (self , ref_ee_pose , plot = False ):
103
+ for cnt in range (500 ):
104
+ ee_pose = self .forward_kinematics ()
105
+ diff_pose = np .array (ref_ee_pose ) - ee_pose
73
106
74
- for cnt in range (1000 ):
75
- basic_jacobian_mat = self .basic_jacobian (ref_ee_pose )
107
+ basic_jacobian_mat = self .basic_jacobian (ee_pose )
76
108
alpha , beta , gamma = self .calc_euler_angle ()
77
109
78
110
K_zyz = np .array ([[0 , - math .sin (alpha ), math .cos (alpha ) * math .sin (beta )],
@@ -82,12 +114,45 @@ def inverse_kinematics(self, ref_ee_pose):
82
114
K_alpha [3 :, 3 :] = K_zyz
83
115
84
116
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
-
117
+ self .update_joint_angles (theta_dot / 100. )
118
+
119
+ if plot :
120
+ self .fig = plt .figure ()
121
+ self .ax = Axes3D (self .fig )
122
+
123
+ x_list = []
124
+ y_list = []
125
+ z_list = []
126
+
127
+ trans = np .identity (4 )
128
+
129
+ x_list .append (trans [0 , 3 ])
130
+ y_list .append (trans [1 , 3 ])
131
+ z_list .append (trans [2 , 3 ])
132
+ for i in range (len (self .link_list )):
133
+ trans = np .dot (trans , self .link_list [i ].transformation_matrix ())
134
+ x_list .append (trans [0 , 3 ])
135
+ y_list .append (trans [1 , 3 ])
136
+ z_list .append (trans [2 , 3 ])
137
+
138
+ self .ax .plot (x_list , y_list , z_list , "o-" , color = "#00aa00" , ms = 4 , mew = 0.5 )
139
+ self .ax .plot ([0 ], [0 ], [0 ], "o" )
140
+
141
+ self .ax .set_xlim (- 1 , 1 )
142
+ self .ax .set_ylim (- 1 , 1 )
143
+ self .ax .set_zlim (- 1 , 1 )
144
+
145
+ self .ax .plot ([ref_ee_pose [0 ]], [ref_ee_pose [1 ]], [ref_ee_pose [2 ]], "o" )
146
+ plt .show ()
147
+
87
148
def calc_euler_angle (self ):
88
149
trans = self .transformation_matrix ()
89
150
90
151
alpha = math .atan2 (trans [1 ][2 ], trans [0 ][2 ])
152
+ if - math .pi / 2 <= alpha and alpha <= math .pi / 2 :
153
+ alpha = math .atan2 (trans [1 ][2 ], trans [0 ][2 ]) + math .pi
154
+ if - math .pi / 2 <= alpha and alpha <= math .pi / 2 :
155
+ alpha = math .atan2 (trans [1 ][2 ], trans [0 ][2 ]) - math .pi
91
156
beta = math .atan2 (trans [0 ][2 ] * math .cos (alpha ) + trans [1 ][2 ] * math .sin (alpha ), trans [2 ][2 ])
92
157
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
158
@@ -102,6 +167,9 @@ def update_joint_angles(self, diff_joint_angle_list):
102
167
self .link_list [i ].dh_params_ [0 ] += diff_joint_angle_list [i ]
103
168
104
169
def plot (self ):
170
+ self .fig = plt .figure ()
171
+ self .ax = Axes3D (self .fig )
172
+
105
173
x_list = []
106
174
y_list = []
107
175
z_list = []
@@ -119,6 +187,10 @@ def plot(self):
119
187
120
188
self .ax .plot (x_list , y_list , z_list , "o-" , color = "#00aa00" , ms = 4 , mew = 0.5 )
121
189
self .ax .plot ([0 ], [0 ], [0 ], "o" )
190
+
191
+ self .ax .set_xlabel ("x" )
192
+ self .ax .set_ylabel ("y" )
193
+ self .ax .set_zlabel ("z" )
122
194
123
195
self .ax .set_xlim (- 1 , 1 )
124
196
self .ax .set_ylim (- 1 , 1 )
0 commit comments