-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJaco2SwivelIK.m
266 lines (237 loc) · 13.2 KB
/
Jaco2SwivelIK.m
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
function [q, cmod] = Jaco2SwivelIK(Tgoal,phi, swivelIKOption)
%% IK solver for Kinova Jaco2 7dof manipulator based on Swivel Angle.
% The function can be used in three ways:
%
% - Starting from a specified target pose (Tgoal), the function calculates the
% configuration (q) at a desired swivel or elbow angle phi. In this case,
% the swivelIKOption must be 'fixed'
% - The function returns the solution with the higher manipulability index.
% In that case, the swivelIKOption must be 'optimize'.
% - The function returns all the possible set of solutions. In that case,
% the swivelIKOption must be 'all'
% The reference frame (r.f.) definition is done with the Denhavit-Hartenberg
% (D.H.) convention and it is consistent with the official Kinova user guide:
% https://drive.google.com/file/d/1xQbkx1-v3SfAentKR9f3p3c2SVdViyQl/view
% Thus, the configuration can be passed through ROS or SDK to the real
% robot to obain the same software configuration.
% The script was successfully tested on a Dell XPS machine with an
% Intel i7-10510U @ 1.8 GHz processor and Linux Ubuntu 20.04 LTS. The
% average time for the 'fixed' option is 0.001 s and 0.11 s for the 'optimize'
% option.
% -------------------------------------------------------------------------
% INPUT
% Tgoal : Homogeneous 4x4 Transformation Matrix from robot base (b) to end
% effector (7)
% phi : swivel angle [0,2*pi] (rad)
% swivelIKOption : 'fixed' : IK is solved with the given phi angle
% 'optimize' : IK is solved optimizing the manipulabilty
% index of the linear twists part of the jacobian matrix
% of the manipulator
% 'all' : IK is solved for aech specified value of the
% discretized elbow angle range phi=[0,2*pi]
% -------------------------------------------------------------------------
% OUTPUT
% q : joint angles vector if 'fixed' or 'optimize' 7x1 (rad)
% if 'all' 7xnpoints (rad)
% cmod : value of the modified manipulability index.
% if 'fixed' or 'optimize' 1x1
% if 'all' npoints x 1 vector
% -------------------------------------------------------------------------
% Author : Giovanni Colucci & Luigi Tagliavini - Politecnico di Torino -
% Torino (TO), Italy
% Last edit : 11/14/2022
% -------------------------------------------------------------------------
%% Robot parameters
% Define the fixed transformation matrix from the base frame of the
% manipulator to link 0.
Tb0 = [1 0 0 0;
0 -1 0 0;
0 0 -1 0;
0 0 0 1];
% Geometric parameters and D.H. parameters
D1 = .2755; %m
D2 = .2050;
D3 = D2;
D4 = .2073;
D5 = .1038;
D6 = .1038;
D7 = .16;
e2 = .0098;
a1 = 0; alpha1 = -pi/2 ; d1 = -D1 ; theta1 =0;
a2 = 0; alpha2 = -pi/2 ; d2 = 0 ; theta2 =0;
a3 = 0; alpha3 = -pi/2 ; d3 = -(D2+D3) ; theta3= 0;
a4 = 0; alpha4 = -pi/2 ; d4 = -e2 ; theta4 =0;
a5 = 0; alpha5 = -pi/2 ; d5 = -(D4+D5) ; theta5 =0;
a6 = 0; alpha6 = -pi/2 ; d6 = 0 ; theta6 =0;
a7 = 0; alpha7 = -pi ; d7 = -(D6+D7) ; theta7= 0;
%% Swivel or elbow angle formulation
%Find the position vector of the wrist pw
pw = Tgoal(1:3,4) - ((D6+D7).*Tgoal(1:3,3));
% Shoulder position vector ps
ps = D1 .* [0 0 1]';
% Charateristic lengths in shoulder-elbow-wrist plane
U = D2 + D3; % (m)
L = D4 + D5; % (m)
d = pw - ps;
ddp = sqrt(norm(d)^2 - e2^2);
Lp = sqrt(L^2 + e2^2);
alpha = (acos(((U.^2) + (norm(d).^2) - (Lp.^2))/(2 .* norm(d) .* U)));
% joint angle 4 is immediately calculated as psi angle of the swivel
% formulation
cpsi = (L^2 + U^2 - ddp^2)/(2*U*L); %cosine of psi angle
spsi = sqrt(1 - cpsi^2); %sine of psi angle
%normal unit vector to the sviwel circumference
n = (d)./norm(d);
%to find a orthonormal base, define an arbitrary unit vector a
a = [1 0 0]';
% extract part of a that is the perpendicular to n
u = (a - (dot(a,n) * n))/norm(a - (dot(a,n) * n)); % dot(a,n) * n is the component of a // to n . So a - dot(a,n)*n is the remaining part of a perpendicular to n
% complete the triplet with a third vector that is perpendicular to n and v
v = cross(n,u);
% The position vector of the center of swivel circunference pc can be
% calculated and also its radius R
pc = ps + ((U * cos(alpha)) * n) ;
R = U * sin(alpha);
%% Conditional part to choose between fixed or optimize option
if strcmp(swivelIKOption, 'fixed')
% If phi is fixed, the position vector of the elbow point pe can be
% calculated starting from pc and moving along a direction defined by
% phi of a quantity that is equal to R
pe = pc + (R.*cos(phi).*u) + (R.*sin(phi).*v);
% Now the solveq function can be used to find q. Please go to the
% solveq description to find more info about it
q = Solveq(pe, pw, Tb0, Tgoal, spsi, cpsi, D1, L, e2, a1, alpha1, d1, theta1, a2, alpha2,d2, theta2, a3, alpha3, d3, theta3, a4, alpha4, d4, theta4);
% Define the manipulator joint limits as a 3x2 matrix (there're
% actually limits on joints 2,4,6)
jnt2PositionLimits = (deg2rad([47, 313]));
jnt4PositionLimits = (deg2rad([30, 330]));
jnt6PositionLimits = (deg2rad([65, 295]));
jntPositionLimits = [jnt2PositionLimits; jnt4PositionLimits; jnt6PositionLimits];
grad_h = @(xx) (((jntPositionLimits(:,2)-jntPositionLimits(:,1)).^2) .* (2.*xx - jntPositionLimits(:,2) - jntPositionLimits(:,1)))./(4.*(jntPositionLimits(:,2) - xx).^2 .* (xx - jntPositionLimits(:,1)).^2);
TempJacobian = Jaco2GeometricJacobianv4(q);
% Calculate the penalization factors for each solution due to the joint limits
Pen_Factor_jointlimits = 1./(1 + abs(grad_h(pagetranspose([reshape(q(2,:), 1, 1, size(q,2)) reshape(q(4,:), 1, 1, size(q,2)) reshape(q(6,:), 1, 1, size(q,2))])))).^0.5;
% Modify the Jacobian according to the penalization factors.
TempJacobianMod = TempJacobian .* [ones(6,1, size(q,2)), Pen_Factor_jointlimits(1,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2)), Pen_Factor_jointlimits(2,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2)), Pen_Factor_jointlimits(3,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2))];
cmod = 1./cond(TempJacobianMod(1:3, :, 1));
% Set cmod = 0 if the solution is not feasible due to physical joint
% limit constraints
% Please note that it is not important for the 'optimize' option
% because the penalization factors exlude the solutions that are not
% possible
if q(2) < deg2rad(47) || q(2)>deg2rad(313) || q(4) < deg2rad(30) || q(4)>deg2rad(330) || q(6) < deg2rad(65) || q(6)>deg2rad(295)
cmod = 0;
end
% Optional: add a warning if the solution is not feasible due to the
% joint limits
% if q(2) < deg2rad(47) || q(2)>deg2rad(313)
% warning('Joint 2 exceeds joint limits')
% elseif q(4) < deg2rad(30) || q(4)>deg2rad(330)
% warning('Joint 4 exceeds joint limits')
% elseif q(6) < deg2rad(65) || q(6)>deg2rad(295)
% warning('Joint 6 exceeds joint limits')
% end
% Please note that it is not important for the 'optimize' option
% because the penalization factors exlude the solutions that are not
% possible
elseif strcmp(swivelIKOption, 'optimize')
% If phi can be optimized to find the solution with the highest
% manipulability index, we can define a discretization for phi
phi = 0:1e-3:2*pi;
% The position vector of the elbow can be found as a function of phi
pe = pc + (R.*cos(phi).*u) + (R.*sin(phi).*v);
% Define the manipulator joint limits as a 3x2 matrix (there're
% actually limits on joints 2,4,6)
jnt2PositionLimits = (deg2rad([47, 313]));
jnt4PositionLimits = (deg2rad([30, 330]));
jnt6PositionLimits = (deg2rad([65, 295]));
jntPositionLimits = [jnt2PositionLimits; jnt4PositionLimits; jnt6PositionLimits];
% define the gradient funciton that can be used to penalize the
% jacobian matrix
grad_h = @(xx) (((jntPositionLimits(:,2)-jntPositionLimits(:,1)).^2) .* (2.*xx - jntPositionLimits(:,2) - jntPositionLimits(:,1)))./(4.*(jntPositionLimits(:,2) - xx).^2 .* (xx - jntPositionLimits(:,1)).^2);
% declare q as a zeros matrix
q = zeros(7, size(pe,2));
% declare the manipulability index klin3
cmod = zeros(size(pe,2), 1);
% Use the function solveq to find all the solution associated to the
% phi range. q is a 7x(length(phi) matrix.
% Please go to the solveq function description to find more info about
% it.
q = Solveq(pe, pw, Tb0, Tgoal, spsi, cpsi, D1, L, e2, a1, alpha1, d1, theta1, a2, alpha2,d2, theta2, a3, alpha3, d3, theta3, a4, alpha4, d4, theta4);
% Evaluate the geometric jacobian matrix of each solution as a
% 6x7x(length(phi)) matrix. Each layer of the multi-dimensional matrix
% corresponds to the jacobian matrix of a solution
TempJacobian = Jaco2GeometricJacobianv4(q);
% Calculate the penalization factors for each solution due to the joint limits
Pen_Factor_jointlimits = 1./(1 + abs(grad_h(pagetranspose([reshape(q(2,:), 1, 1, size(q,2)) reshape(q(4,:), 1, 1, size(q,2)) reshape(q(6,:), 1, 1, size(q,2))])))).^0.5;
% Modify the Jacobian according to the penalization factors.
TempJacobianMod = TempJacobian .* [ones(6,1, size(q,2)), Pen_Factor_jointlimits(1,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2)), Pen_Factor_jointlimits(2,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2)), Pen_Factor_jointlimits(3,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2))];
% Evaluate the 2-norm condition number of the linear twists part of the jacobian matrix.
for count_pe = 1 : size(pe, 2)
cmod(count_pe) = 1./cond(TempJacobianMod(1:3, :, count_pe));
end
% Find the max value of the cmod factor
[cmod, index] = max(cmod);
% Find the associated best solution
q = q(:,index);
elseif strcmp(swivelIKOption, 'all')
% If phi can be optimized to find the solution with the highest
% manipulability index, we can define a discretization for phi
phi = 0:1e-3:2*pi;
% The position vector of the elbow can be found as a function of phi
pe = pc + (R.*cos(phi).*u) + (R.*sin(phi).*v);
% Define the manipulator joint limits as a 3x2 matrix (there're
% actually limits on joints 2,4,6)
jnt2PositionLimits = (deg2rad([47, 313]));
jnt4PositionLimits = (deg2rad([30, 330]));
jnt6PositionLimits = (deg2rad([65, 295]));
jntPositionLimits = [jnt2PositionLimits; jnt4PositionLimits; jnt6PositionLimits];
% define the gradient funciton that can be used to penalize the
% jacobian matrix
grad_h = @(xx) (((jntPositionLimits(:,2)-jntPositionLimits(:,1)).^2) .* (2.*xx - jntPositionLimits(:,2) - jntPositionLimits(:,1)))./(4.*(jntPositionLimits(:,2) - xx).^2 .* (xx - jntPositionLimits(:,1)).^2);
% declare q as a zeros matrix
q = zeros(7, size(pe,2));
% declare the manipulability index klin3
cmod = zeros(size(pe,2), 1);
% Use the function solveq to find all the solution associated to the
% phi range. q is a 7x(length(phi) matrix.
% Please go to the solveq function description to find more info about
% it.
q = Solveq(pe, pw, Tb0, Tgoal, spsi, cpsi, D1, L, e2, a1, alpha1, d1, theta1, a2, alpha2,d2, theta2, a3, alpha3, d3, theta3, a4, alpha4, d4, theta4);
% Evaluate the geometric jacobian matrix of each solution as a
% 6x7x(length(phi)) matrix. Each layer of the multi-dimensional matrix
% corresponds to the jacobian matrix of a solution
TempJacobian = Jaco2GeometricJacobianv4(q);
% Calculate the penalization factors for each solution due to the joint limits
Pen_Factor_jointlimits = 1./(1 + abs(grad_h(pagetranspose([reshape(q(2,:), 1, 1, size(q,2)) reshape(q(4,:), 1, 1, size(q,2)) reshape(q(6,:), 1, 1, size(q,2))])))).^0.5;
% Modify the Jacobian according to the penalization factors.
TempJacobianMod = TempJacobian .* [ones(6,1, size(q,2)), Pen_Factor_jointlimits(1,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2)), Pen_Factor_jointlimits(2,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2)), Pen_Factor_jointlimits(3,1,:).*ones(6,1, size(q,2)),...
ones(6,1, size(q,2))];
% Evaluate the 2-norm condition number of the linear twists part of the jacobian matrix.
for count_pe = 1 : size(pe, 2)
cmod(count_pe) = 1./cond(TempJacobianMod(1:3, :, count_pe));
% Impose cmod = 0 if the solution is forbidden due to physical joint
% limit constraints
% Please note that it is not important for the 'optimize' option
% because the penalization factors exlude the solutions that are not
% possible
if q(2, count_pe) < deg2rad(47) || q(2, count_pe)>deg2rad(313)
cmod(count_pe) = 0;
elseif q(4, count_pe) < deg2rad(30) || q(4, count_pe)>deg2rad(330)
cmod(count_pe) = 0;
elseif q(6, count_pe) < deg2rad(65) || q(6, count_pe)>deg2rad(295)
cmod(count_pe) = 0;
end
end
else
error('Select a valid option entry')
end
end