code
function [q1, q2, q3, q4, q5, q6] = IRB2600_inverse_kinematics(X, Y, Z, phi)
L1 = 0.6500; % link 1 length
L2 = 0.0350; % link 2 length
L3 = 0.6700; % link 3 length
L4 = 0.0350; % link 4 length
L5 = 0.6100; % link 5 length
L6 = 0.1750; % link 6 length
% Base frame to tool frame transformation matrix
T = [cos(phi) -sin(phi) 0 0;
sin(phi) cos(phi) 0 0;
0 0 1 L6;
0 0 0 1];
% Transform end effector position and orientation to base frame
X_b = X - T(1, 4);
Y_b = Y - T(2, 4);
Z_b = Z - T(3, 4);
T_inv = inv(T);
phi_b = atan2(-T_inv(1, 2), T_inv(1, 1));
psi_b = atan2(-T_inv(3, 2), T_inv(3, 1));
theta_b = atan2(T_inv(2, 3), sqrt(T_inv(1, 3)^2 + T_inv(3, 3)^2));
% Solve for joint angles q1 to q6
q1 = atan2(Y_b, X_b);
A = sqrt(X_b^2 + Y_b^2) - L2;
B = Z_b - L1;
q3_a = acos((A^2 + B^2 - L3^2 - L5^2)/(2*L3*L5));
q3_b = -acos((A^2 + B^2 - L3^2 - L5^2)/(2*L3*L5));
q3 = q3_a; % choose solution a
q2_a = atan2(B, A) - atan2(L5*sin(q3_a), L3 + L5*cos(q3_a));
q2_b = atan2(B, A) - atan2(L5*sin(q3_b), L3 + L5*cos(q3_b));
q2 = q2_a; % choose solution a
T1 = [cos(q1) -sin(q1) 0 0;
sin(q1) cos(q1) 0 0;
0 0 1 0;
0 0 0 1];
T2 = [cos(q2) -sin(q2) 0 L2;
0 0 -1 0;
sin(q2) cos(q2) 0 0;
0 0 0 1];
T3 = [cos(q3) -sin(q3) 0 L3;
sin(q3) cos(q3) 0 0;
0 0 1 0;
0 0 0 1];
T4 = [cos(q4) -sin(q4) 0 L4;
sin(q4) cos(q4) 0 0;
0 0 1 0;
0 0 0 1];
T5 = [cos(q5) -sin(q5) 0 0;
0 0 -1 0;
sin(q5) cos(q5) 0 0;
0 0 0 1];
T6 = [cos(q6) -sin(q6) 0 0;
0 0 1 L6;
-sin(q6) -cos(q6) 0 0;
0 0 0 1];
T01 = T1;
T02 = T01*T2;
T03 = T02*T3;
T04 = T03*T4;
T05 = T04*T5;
T06 = T05*T6;
T06_b = T_inv*T06;
% Solve for wrist angles q4 to q6
q4 = atan2(T06_b(3,3),-T06_b(1,3));
q5 = atan2(sqrt(T06_b(1,3)^2 + T06_b(3,3)^2), T06_b(2,3));
q6 = atan2(-T06_b(2,2),T06_b(2,1));
end