% program in Matlab to simulate the forward kinematics of a 2R Robotic Arm
close all
clearvars
clc
% Arms lengths, l1 attached at origin and l2 attached at the end of l1
l1= 1;
l2 = 0.5;
% theta1 and theta2 are angle of arms wrt x-axis
theta = linspace(0, 90, 20);
%theta2 = linspace(0,180, 4);
ct = 1;
for i= 1:length(theta)
THETA1 = theta(i);
for j= 1:length(theta)
THETA2 = theta(j);
x0 = 0;
y0 = 0; % origin point
x1 = l1*cosd(THETA1);
y1 = l1*cosd(THETA1); % end point of arm 1
x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);
%plotting
%
% hold on
plot([x0 x1], [y0 y1], [x1 x2], [y1 y2], LineWidth=2.5)
axis([-0.1 2 0 2])
pause(0.1)
M(ct) = getframe(gcf)
ct = ct+1;
end
end
movie(M)
videofile = VideoWriter('Robotic_Arm.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)