-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIK_functions.py
More file actions
212 lines (158 loc) · 4.8 KB
/
Copy pathIK_functions.py
File metadata and controls
212 lines (158 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import math
import numpy as np
"""
# {Alessandro Bassi}
# {alebas@kth.se}
"""
import math
import numpy as np
print(f"Il file è stato caricato")
def scara_IK(point):
x = point[0]
y = point[1]
z = point[2]
q = [0.0, 0.0, 0.0]
l1 = 0.3
l2 = 0.35
l0 = 0.07
# define the x axe with respect the robot frame
x = x - l0
#q2
cos_theta_2 = (x**2 + y**2 - l1**2 - l2**2) / (2 * l1 * l2)
theta_2 = math.acos(cos_theta_2)
#q1
sin_theta_1 = y * (l1 + l2 * math.cos(theta_2)) - x * l2 * math.sin(theta_2)
cos_theta_1 = x * (l1 + l2 * math.cos(theta_2)) + y * l2 * math.sin(theta_2)
theta_1 = math.atan2(sin_theta_1, cos_theta_1)
#q3
d3 = z
q[0] = theta_1
q[1] = theta_2
q[2] = d3
return q
# prende in ingresso Ts=[T_01, T_12, T_23, T_34, T_45, T_56, T_67] e restituisce v = [x, y, z]
def Direct_Kinematics(Ts):
T_07 = Ts[0] @ Ts[1] @ Ts[2] @ Ts[3] @ Ts[4] @ Ts[5] @ Ts[6]
pose = T_07[:3, 3]
return pose
# Prende in ingresso i parametri di D-H e restituisce la matrice di trasformazione
def Transfer_Matrix(alpha, d, a, theta):
T = np.array([
[math.cos(theta), - math.sin(theta) * math.cos(alpha), math.sin(theta) * math.sin(alpha), a * math.cos(theta)],
[math.sin(theta), math.cos(theta) * math.cos(alpha), - math.cos(theta) * math.sin(alpha), a * math.sin(theta)],
[0, math.sin(alpha), math.cos(alpha), d],
[0, 0, 0, 1]
])
return T
# prende in ingresso Ts=[T_01, T_12, T_23, T_34, T_45, T_56, T_67] e restituisce lo Jacobiano
def Jacobian(Ts):
#L'end-effector e' traslato rispetto al giunto 7
T_07 = Ts[0] @ Ts[1] @ Ts[2] @ Ts[3] @ Ts[4] @ Ts[5] @ Ts[6]
T_0ee = T_07 @ np.array([0, 0, 0.078, 1])
p_ee = T_0ee[0:3]
#L'asse 0 e' fissato
z_0 = np.array([0, 0, 1])
T_01 = Ts[0]
T_02 = T_01 @ Ts[1]
T_03 = T_02 @ Ts[2]
T_04 = T_03 @ Ts[3]
T_05 = T_04 @ Ts[4]
T_06 = T_05 @ Ts[5]
z_1 = T_01[:3, 2]
z_2 = T_02[:3, 2]
z_3 = T_03[:3, 2]
z_4 = T_04[:3, 2]
z_5 = T_05[:3, 2]
z_6 = T_06[:3, 2]
z = []
z.append(z_0)
z.append(z_1)
z.append(z_2)
z.append(z_3)
z.append(z_4)
z.append(z_5)
z.append(z_6)
p_1 = np.array([0, 0, 0])
p_2 = T_01[:3, 3]
p_3 = T_02[:3, 3]
p_4 = T_03[:3, 3]
p_5 = T_04[:3, 3]
p_6 = T_05[:3, 3]
p_7 = T_06[:3, 3]
p = []
p.append(p_1)
p.append(p_2)
p.append(p_3)
p.append(p_4)
p.append(p_5)
p.append(p_6)
p.append(p_7)
Jv = []
Jw = []
#Calcoliamo le componenti dello Jacobiano
for i in range(len(p)):
v = np.cross(z[i], p_ee - p[i]) #v_i = z_{i-1} X (p_ee - p_i)
Jv.append(v)
Jw.append(z[i]) #per giunti rotazionali
Jv = np.column_stack(Jv)
Jw = np.column_stack(Jw)
Jacobian = np.vstack((Jv, Jw))
print(Jacobian)
return Jacobian
def kuka_IK(point, R, joint_positions):
x = point[0]
y = point[1]
z = point[2]
q = joint_positions #it must contain 7 elements
L = 0.4 #[m]
M = 0.39 #[m]
#trasformo tutti gli array in NumPy array
point = np.array(point)
R = np.array(R)
q = np.array(q)
tollerance = 0.01
# Calcliamo la cinematica inversa
for i in range(100):
#1. CALCOLO DELLA CINEMATICA DIRETTA
Ts = []
T_01 = Transfer_Matrix(math.pi/2, 0.311, 0, q[0])
T_02 = Transfer_Matrix(-math.pi/2, 0, 0, q[1])
T_03 = Transfer_Matrix(-math.pi/2, L, 0, q[2])
T_04 = Transfer_Matrix(math.pi/2, 0, 0, q[3])
T_05 = Transfer_Matrix(math.pi/2, M, 0, q[4])
T_06 = Transfer_Matrix(-math.pi/2, 0, 0, q[5])
T_07 = Transfer_Matrix(0, 0, 0, q[6])
Ts.append(T_01)
Ts.append(T_02)
Ts.append(T_03)
Ts.append(T_04)
Ts.append(T_05)
Ts.append(T_06)
Ts.append(T_07)
T_07 = Ts[0] @ Ts[1] @ Ts[2] @ Ts[3] @ Ts[4] @ Ts[5] @ Ts[6]
T_0ee = T_07 @ np.array([0, 0, 0.078, 1])
p_ee = T_0ee[0:3]
R_e = T_07[:3, :3]
#2. CALCOLO DELL'ERRORE
n_e = R_e[:3, 0]
s_e = R_e[:3, 1]
a_e = R_e[:3, 2]
n = R[:3, 0]
s = R[:3, 1]
a = R[:3, 2]
#2.1 ERRORE SULL'ORIENTAMENTO
e_o = 0.5 * (np.cross(n_e, n) + np.cross(s_e, s) + np.cross(a_e, a))
#2.2 ERRORE SULLA POSIZIONE
e_p = p_ee - point
error = np.concatenate((e_p, e_o))
print(f"Iter {i}: Error norm = {np.linalg.norm(error)}")
if np.linalg.norm(error)< tollerance:
print("CONVERGENZA")
break
J = Jacobian(Ts)
Inv_Jacobian = np.linalg.pinv(J)
eps_theta = Inv_Jacobian @ error
q = q - eps_theta
if (i == 99):
print("Nessuna Convergenza")
return q