-
Notifications
You must be signed in to change notification settings - Fork 11
/
cgr_ikine2.m
66 lines (54 loc) · 1.56 KB
/
cgr_ikine2.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
function [q, iter_taken, err] = cgr_ikine2(r, p, lambda, treshold, max_iter)
% Compute the inverse kinematics the damped least square method
%
% http://math.ucsd.edu/~sbuss/ResearchWeb/ikmethods/iksurvey.pdf
% See Equ. 11.
%
% Inputs:
% r - sructure of the robot.
% p - target cartesian position (3x1 vector)
% lambda - damping factor
% treshold - terminate the iteration when err < treshold
% max_iter - maximum iiterations
%
% Outputs:
% q - computed joint values
% iter_taken - total iteration number
% err - actual error between the actual position and the target position
%
global N_DOFS;
if nargin < 3
treshold = 0.01;
max_iter = 100;
elseif nargin < 4
max_iter = 100;
end
% Get current pose.
q = r.qc;
T = cgr_fkine(r, q);
x = T(1:3, 4, end);
jac = cgr_jac(r, q);
iter_taken = 1;
while 1
iter_taken = iter_taken + 1;
delta_x = p - x;
% Keep in mind then inverse operation fails when matrix is not full
% rank, instead we will use pinv, Therefore, when lambda = 0, this
% method is the same as the pseudo-inverse method.
if lambda > 0
K = (jac'*jac + lambda^2 .* eye(N_DOFS))\jac';
else
K = pinv(jac);
end
delta_q = K*delta_x;
q = q + delta_q;
q = min(r.ub, max(q, r.lb)); % This is a saturation function.
[~, x] = cgr_fkine_ee(r, q);
jac = cgr_jac(r, q);
err = norm(delta_x);
if err < treshold || iter_taken > max_iter
%fprintf('**cgr_ikine2** breaks after %i iterations with errror %f.\n', iter_taken, err);
break;
end
end
end