MATLAB Answers

0

Index in position 1 exceeds array bounds (must not exceed 4)

Asked by chen chien-fu on 15 Sep 2019 at 18:52
Latest activity Answered by Image Analyst
on 16 Sep 2019 at 1:54
Hi
I get the error from matlab code, which was calculating the inverse kinematic of robotic.
the error says,
Index in position 1 exceeds array bounds (must not exceed 4).Error in inverseKinematicsimulation (line 59)
DH{i} =[ cos(cta(i,k)) -sin(cta(i,k))*cos(alpha(i)) sin(cta(i,k))*sin(alpha(i)) a(i)*cos(cta(i,k));...
I check the dimensions which were satisfied."DH" is a 4*4 matrix.
Could you help me find out what error! thanks a lot!
the code as below.
Cmd_X = 300;
Cmd_Y = 400;
Cmd_Z = 500;
cta_total = 45;
Theta1_now = 0;
Theta2_now = 0;
Theta3_now = 0;
Theta4_now = 0;
JOINT_SIZE = 4+1;
PATH_SIZE = 20;
a = [300, 0, 300, 0]';
alpha = [0, -1/2, -1/2, -1/2]';
d = [0, 0, 0, 0]';
cta = zeros(JOINT_SIZE-1,PATH_SIZE+1);%each joint's angle
P = zeros(JOINT_SIZE-1,PATH_SIZE);% effector's position
cta(1:JOINT_SIZE-1,1) = [ Theta1_now, Theta2_now, Theta3_now, Theta4_now]'*pi/180.0;
T = cell(JOINT_SIZE-1);%transmatrix
intermed = cell(JOINT_SIZE-1);%buffer
intermed{1} = [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1]; %
DH = cell(JOINT_SIZE-1);
target = [Cmd_X;Cmd_Y;Cmd_Z;cta_total];
%calculate initial position
for m=1:JOINT_SIZE-1
DH{m} =[ cosd(cta(m,1)) -sind(cta(m,1))*cosd(alpha(m)) sind(cta(m,1))*sind(alpha(m)) a(m)*cosd(cta(m,1));...
sind(cta(m,1)) cosd(cta(m,1))*cosd(alpha(m)) -cosd(cta(m,1))*sind(alpha(m)) a(m)*sind(cta(m,1));...
0 sind(alpha(m)) cosd(alpha(m)) d(m);...
0 0 0 1];
intermed{m+1} = intermed{m}*DH{m};
end
T{1} = intermed{JOINT_SIZE};
P(1:3,1) = T{1}(1:3, 4);
%calculate new theta
for k=2:PATH_SIZE
% error
e = [target(1) - P(1); target(2) - P(2); target(3) - P(3); target(4)-(cta(1)+cta(2)+cta(3)+cta(4))];
% Jacobian by hand
Jacobian_p = [-a(1)*sind(cta(1,k-1))-a(2)*sind(cta(1,k-1)+cta(2,k-1))-a(3)*sind(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))+a(4)*cosd(cta(1,k-1)+cta(2,k-1))*sind(cta(4,k-1))-a(4)*sind(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))*cosd(cta(4,k-1)),...
-a(2)*sind(cta(1,k-1)+cta(2,k-1))-a(3)*sind(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))+a(4)*cosd(cta(1,k-1)+cta(2,k-1))*sind(cta(4,k-1))-a(4)*sind(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))*cosd(cta(4,k-1)),...
-a(3)*sind(cta(1,k-1)+cta(2,k-1))*sind(cta(3,k-1))-a(4)*sind(cta(1,k-1)+cta(2,k-1))*sind(cta(3,k-1))*cosd(cta(4,k-1)),...
+a(4)*sind(cta(1,k-1)+cta(2,k-1))*cosd(cta(4,k-1))-a(4)*cosd(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))*cosd(cta(4,k-1));...
a(1)*cosd(cta(1,k-1))+a(2)*cosd(cta(1,k-1)+cta(2,k-1))+a(3)*cosd(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))+a(4)*sind(cta(1,k-1)+cta(2,k-1))*sind(cta(4,k-1))+a(4)*cosd(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))*cosd(cta(4,k-1)),...
a(2)*cosd(cta(1,k-1)+cta(2,k-1))+a(3)*cosd(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))+a(4)*sind(cta(1,k-1)+cta(2,k-1))*sind(cta(4,k-1))+a(4)*cosd(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))*cosd(cta(4,k-1)),...
-a(3)*sind(cta(1,k-1)+cta(2,k-1))*sind(cta(3,k-1))-a(4)*sind(cta(1,k-1)+cta(2,k-1))*sind(cta(3,k-1))*cosd(cta(4,k-1)),...
-a(4)*cosd(cta(1,k-1)+cta(2,k-1))*cosd(cta(4,k-1))-a(4)*sind(cta(1,k-1)+cta(2,k-1))*cosd(cta(3,k-1))*cosd(cta(4,k-1));...
0,0,-a(3)*cosd(cta(3,k-1))-a(4)*cosd(cta(3,k-1))*cosd(cta(4,k-1)), a(4)*sind(cta(3,k-1))*sind(cta(4,k-1));...
1,1,1,1];
error =pinv(Jacobian_p).*e;
cta(1:4,k+1) = cta(1:4,k) + error(1:4,4);
for i=1:JOINT_SIZE
DH{i} =[cosd(cta(i,k+1)),-sind(cta(i,k+1))*cosd(alpha(i)),sind(cta(i,k+1))*sind(alpha(i)),a(i)*cosd(cta(i,k+1));...
sind(cta(i,k+1)) cosd(cta(i,k+1))*cosd(alpha(i)) -cosd(cta(i,k+1))*sind(alpha(i)) a(i)*sind(cta(i,k+1));...
0 sind(alpha(i)) cosd(alpha(i)) d(i);...
0 0 0 1];
intermed{i+1} = intermed{i}*DH{i};
%joint_p{k+1}(1:3,i)= intermed{i+1}(1:3,4);
P(1:3,k) = intermed{5}(1:3, 4);
end
end
Theta1_new = cta(1,PATH_SIZE);
Theta2_new = cta(2,PATH_SIZE);
Theta3_new = cta(3,PATH_SIZE);
Theta4_new = cta(4,PATH_SIZE);

  0 Comments

Sign in to comment.

Products

1 Answer

Answer by Image Analyst
on 16 Sep 2019 at 1:54

DH is a 4-by-4 matrix (cell array) but you're only using one index when you assign it:
DH{i} = ............
Why are you not using two indexes? In fact, since you only use it in that loop, why store it at all? Why is it not simply a temporary matrix that you just basically use inside the loop and throw away afterwards?

  0 Comments

Sign in to comment.