Solving Inverse Kinematics using pseudo-inverse Jacobian method
Show older comments
I need help with writing the code to solve this equation attached. I have tried severally but my head seems to be running in a circle and I need an extra eye.

where: ζis a constant value, say 0.1.
"i" is the number of iterations.
k is the kinematics matrix:
. Where q1 and q2 are joint angles to be calculated.
. Where q1 and q2 are joint angles to be calculated.
J is the Jacobian of the kinematics k(q).
xf is the final position of the end effector
I need to solve the equation iteratively until the following condition is met:
, where
or less.
, where This is the code I have written but seems like trash:
function [] = Jacobian_Pseudo_Inverse_Method()
clc;
disp('Jacobian Pseudo-Inverse Method selected');
syms q1 q2 q3 q4 q5
syms a1 a2 a3 a4 a5
%select the degree of freedom(DOF)
disp(['Select Manipulators DOF' newline '1. 2-DOF' newline '2. 3-DOF' newline '3. 4-DOF' newline '4. 5-DOF']);
dof = str2double(input('DOF: ','s'));
%Global variables
goal_point = input('Goal point (xf) = ');
xf = transpose(goal_point);
xi = input('ξ = ');
error_margin = input('Error margin (ε) = ');
%The section below is a switch case function to select which method to
%execute
switch dof
case 1
%inputs
init_config = input('Initial Config. (q0) = ');
q(0) = transpose(init_config);
kinematics = input('Kinematics Matrix (k) = ');
a1 = input('link length 1 = ');
a2 = input('link length 2 = ');
%calculate Jacobian and Jacobian transpose of kinematics matrix
Ja = jacobian(kinematics,[q1;q2]);
Jt = transpose(Ja);
JJt = Ja*Jt;
JJt_inverse = inv(JJt);
J_hash = Jt*JJt_inverse;
%J_hash = Jt/JJt_inverse;
%calculate qi
for i = 0:5000
q(i+1) = q(i) + xi*J_hash(q(i))*(xf - kinematics*(q(i)));
dist = norm(xf - kinematics*(q(i)));
while dist >= error_margin
i = i + 1;
end
disp('Number of iterations = %d',i);
end
%Find the values of q1 and q2
%S = solve(kinematics,[q1 q2]);
%theta1 = simplify(S.q1)
%theta2 = simplify(S.q2)
end
end
6 Comments
Sam Chak
on 25 Apr 2022
Can you provide some examples of the inputs for the desired position of the end effector and the initial joint angles? You really need some real input values to test whether the core of your Inverse Kinematics Solver works or not. Once the results are validated, then you can take your sweet time to beautify the code to accept user input for demonstrating the Human–Computer Interaction.
How large is the search space? Are there constraints in the joint angles (they can rotate 360°)?
David Asogwa
on 25 Apr 2022
Edited: David Asogwa
on 27 Apr 2022
David Asogwa
on 27 Apr 2022
Edited: David Asogwa
on 27 Apr 2022
Riccardo Scorretti
on 27 Apr 2022
Left aside programming errors, it seems to me that the target point (4,7) is not feasible: with arms of length a1 = 2 and a2 = 3, the target point is bound to be inside a circle of radius a1+a2 = 5. Perpahs I don't have a good understanding of the problem.
David Asogwa
on 27 Apr 2022
Torsten
on 27 Apr 2022
Array indices must be positive. You address q(0) since your i-loop runs from 0 to 5000.
Accepted Answer
More Answers (1)
Hi David,
in your code you tried to use the Symbolic Computational Toolbox. Personally, for such a problem I prefer to go numerically. Let me know if it is what your are looking for:
deg = pi/180; % = conversion factor degrees --> radians
% Set the initial configuration
Q0 = [15 -30]'*deg;
A = [3 2]';
% Set the desired final configuration
Xf = compute_jacobian(A, [45*deg 90*deg]');
% Set the required tollerance
toll = 1.0E-3;
% Set the parameter chi
chi = 0.1;
% Here we go ...
Q = Q0;
[X, Jac] = compute_jacobian(A, Q0);
Q_rec = Q0 ; X_rec = X; % This is just to record the iterations
while norm(X - Xf) > toll
Q = Q + chi*pinv(Jac)*(Xf - X);
[X, Jac] = compute_jacobian(A, Q);
Q_rec(:,end+1) = Q ; X_rec(:,end+1) = X; % This is just to record the iterations
end
nbIter = size(X_rec, 2);
% Let's see if it worked ...
figure
subplot(2, 1, 1);
plot(Q_rec(1,:)/deg, 'r.-') ; hold on ; plot(Q_rec(2,:)/deg, 'b.-');
ylabel('Q') ; grid on ; box on
subplot(2, 1, 2);
plot(X_rec(1,:), 'r.-') ; hold on ; plot(X_rec(2,:), 'b.-');
plot([0 nbIter], [Xf(1) Xf(1)], 'r--');
plot([0 nbIter], [Xf(2) Xf(2)], 'b--');
ylabel('X') ; grid on ; box on
xlabel('Iteration');
function [Kmat, Jmat] = compute_jacobian(A, Q)
a1 = A(1) ; a2 = A(2);
q1 = Q(1) ; q2 = Q(2);
Kmat = [ ...
a1*cos(q1)+a2*cos(q1+q2) ; ...
a1*sin(q1)+a2*sin(q1+q2) ...
];
Jmat = [ ...
-a1*sin(q1)-a2*sin(q1+q2) -a2*sin(q1+q2) ; ...
a1*cos(q1)+a2*cos(q1+q2) a2*cos(q1+q2) ...
];
end
Interestingly (at least for me), when I programmed it, I expected that Q converged to the value [45 90]*rad, which I used to compute a feasible value of Xf. This is not the case, but one sees that the algorithm found as well an acceptable solution.
By the way, a test ought to be added to stop the algorithm if the target Xf is not feasible.
1 Comment
David Asogwa
on 28 Apr 2022
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
