Index exceeds the number of array elements (0).

5 views (last 30 days)
Preston Stinnett
Preston Stinnett on 4 Dec 2021
I keep getting the error mentioned above. Index exceeds the number of array elements (0). Error (line 46) T4=RobotConv (0,L(k),0,0);
I have been trying to fix this but cannot get it to work. Does anyone have any tips on this?
clc;
clear all;
display('Joint 1');
display('=======================');
angle1=input('Please key in theta_1 (degree):');
d1=input('Please key in distance_1 (d):');
al1=input('Please key in alpha_1 (degree):');
display('Joint 2');
display('=======================');
angle2=input('Please key in theta_2 (degree):');
d2=input('Please key in distance_2 (d):');
al2=input('Please key in alpha_2 (degree):');
display('Joint 3');
display('=======================');
angle3=input('Please key in theta_3 (degree):');
d3=input('Please key in the minimum distance_3 (d):');
d4=input('Please key in the maximum distance_3 (d):');
al3=input('Please key in alpha_3 (degree):');
display('Joint 4');
display('=======================');
angle4=input('Please key in theta_4 (degree):');
d4=input('Please key in distance_4 (d):');
al4=input('Please key in alpha_4 (degree):');
L1=min(d3);
L2=max(d4);
L3=d4;
theta1=0:angle1/100:angle1;
theta2=0:angle2/100:angle2;
theta3=0:angle3/100:angle3;
L=L2:(L3-L2)/100:L3;
framemax=100;
M=moviein(framemax);
set(gcf,'Position',[100 100 640 480]);
for k=1:framemax
T1=RobotConv(theta1(k), d1, 0, al1);
T2=RobotConv(theta2(k), d2, 0, al2);
T3=RobotConv(theta3(k), d3, 0, al3);
T4=RobotConv(0, L(k), 0, 0);
p0=[0 0 0];
p1=RobotPosition(T1);
p2=RobotPosition(T1*T2);
p3=RobotPosition(T1*T2*T3);
p4=RobotPosition(T1*T2*T3*T4);
figure(1)
X=[p0(1) p1(1) p2(1) p3(1)];
Y=[p0(2) p1(2) p2(2) p3(2)];
Z=[p0(3) p1(3) p2(3) p3(3)];
subplot(221),plot3(X,Y,Z,'o-')
axis([-10 10 -10 10 0 10]);
grid
subplot(222),plot(X,Y,'o-')
axis([-10 10 -10 10]);
title('X-Y')
grid
subplot(223),plot(X,Z,'o-')
axis([-10 10 0 10]);
title('X-Z')
grid
subplot(224),plot(Y,Z,'o-')
axis([-10 10 0 10]);
title('Y-Z')
grid
M(k)=getframe(gcf);
end
function P = RobotPosition(T)
x = T(1,4);
y = T(2,4);
z = T(3,4);
P = [x y z];
end
function T = RobotConv(theta,d,a,alpha)
rad = pi/180;
M_theta = [cos(theta*rad) -sin(theta*rad) 0 0;sin(theta*rad) cos(theta*rad) 0 0;0 0 1 0;0 0 0 1];
M_d = [1 0 0 0;0 1 0 0;0 0 1 d;0 0 0 1];
M_a = [1 0 0 a;0 1 0 0;0 0 1 0;0 0 0 1];
M_alpha = [1 0 0 0;0 cos(alpha*rad) -sin(alpha*rad) 0;0 sin(alpha*rad) cos(alpha*rad) 0; 0 0 0 1];
T=M_theta*M_d*M_a*M_alpha;
end

Answers (1)

VBBV
VBBV on 4 Dec 2021
clc;
clear all;
display('Joint 1');
Joint 1
display('=======================');
=======================
angle1= 45; %input('Please key in theta_1 (degree):');
d1=10; %input('Please key in distance_1 (d):');
al1=10; %input('Please key in alpha_1 (degree):');
display('Joint 2');
Joint 2
display('=======================');
=======================
angle2= 30;%input('Please key in theta_2 (degree):');
d2=10; %input('Please key in distance_2 (d):');
al2= 15;% input('Please key in alpha_2 (degree):');
display('Joint 3');
Joint 3
display('=======================');
=======================
angle3=35; %input('Please key in theta_3 (degree):');
d3=2; %input('Please key in the minimum distance_3 (d):');
d4=50; %input('Please key in the maximum distance_3 (d):');
al3=30; %input('Please key in alpha_3 (degree):');
display('Joint 4');
Joint 4
display('=======================');
=======================
angle4= 35; %input('Please key in theta_4 (degree):');
d4=[10 50]; %input('Please key in distance_4 (d):');
al4=40; %input('Please key in alpha_4 (degree):');
L1=min(d3);
L2=max(d4);
L3=d4;
theta1=0:angle1/100:angle1;
theta2=0:angle2/100:angle2;
theta3=0:angle3/100:angle3;
L=L2:(L3-L2)/100:L3
L = 1×101
50.0000 49.6000 49.2000 48.8000 48.4000 48.0000 47.6000 47.2000 46.8000 46.4000 46.0000 45.6000 45.2000 44.8000 44.4000 44.0000 43.6000 43.2000 42.8000 42.4000 42.0000 41.6000 41.2000 40.8000 40.4000 40.0000 39.6000 39.2000 38.8000 38.4000
size(L);
framemax=100;
M=moviein(framemax);
set(gcf,'Position',[100 100 640 480]);
for k=1:framemax
T1=RobotConv(theta1(k), d1, 0, al1);
T2=RobotConv(theta2(k), d2, 0, al2);
T3=RobotConv(theta3(k), d3, 0, al3);
T4=RobotConv(0, L(k), 0, 0);
p0=[0 0 0];
p1=RobotPosition(T1);
p2=RobotPosition(T1*T2);
p3=RobotPosition(T1*T2*T3);
p4=RobotPosition(T1*T2*T3*T4);
figure(1)
X=[p0(1) p1(1) p2(1) p3(1)];
Y=[p0(2) p1(2) p2(2) p3(2)];
Z=[p0(3) p1(3) p2(3) p3(3)];
subplot(221),plot3(X,Y,Z,'o-')
axis([-10 10 -10 10 0 10]);
grid
subplot(222),plot(X,Y,'o-')
axis([-10 10 -10 10]);
title('X-Y')
grid
subplot(223),plot(X,Z,'o-')
axis([-10 10 0 10]);
title('X-Z')
grid
subplot(224),plot(Y,Z,'o-')
axis([-10 10 0 10]);
title('Y-Z')
grid
M(k)=getframe(gcf);
end
function P = RobotPosition(T)
x = T(1,4);
y = T(2,4);
z = T(3,4);
P = [x y z];
end
function T = RobotConv(theta,d,a,alpha)
rad = pi/180;
M_theta = [cos(theta*rad) -sin(theta*rad) 0 0;sin(theta*rad) cos(theta*rad) 0 0;0 0 1 0;0 0 0 1];
M_d = [1 0 0 0;0 1 0 0;0 0 1 d;0 0 0 1];
M_a = [1 0 0 a;0 1 0 0;0 0 1 0;0 0 0 1];
M_alpha = [1 0 0 0;0 cos(alpha*rad) -sin(alpha*rad) 0;0 sin(alpha*rad) cos(alpha*rad) 0; 0 0 0 1];
T=M_theta*M_d*M_a*M_alpha;
end
  2 Comments
Preston Stinnett
Preston Stinnett on 4 Dec 2021
I tried your code and it still doesn't work. I made a couple changes but still can't get it to work. I get the same error.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!