Program to simulate the forward kinematics of a 2R Robotic arm
Where:
The first arm is of length L1 metres and makes an angle of theta1 with respect to the x-axis
The second arm is of length L2 metres and makes an angle of theta2 with respect to the x-axis
Objective: To find the position of the 2R Robotic arm for different values of theta1 and theta2
Code:
clc
close all
clear all
%taking 20 values of theta1 and theta2 between 0 and 90 degrees
theta1 = linspace(0,90,20);
theta2 = linspace(0,90,20);
%initializing the length of link 1 and 2 and the origin coordinates of
%link1
L1 = 1.15;
L2 = 0.75;
x0 = 0;
y0 = 0;
%initializing a counter to store the value of each generated figure
ctr=1;
%creating an outer loop that will access the 20 values of theta1
%sequentially
for i=1:length(theta1)
%creating an inner loop to access all 20 values of theta2 in sequence
%for the same value of theta1
for j=1:length(theta2)
%calculating the end coordinates of link1 using value of theta1
x1 = L1.*cosd(theta1(i));
y1 = L1.*sind(theta1(i));
%calculating the end coordinates of link2 using value of theta2
x2 = x1 + L2.*cosd(theta2(j));
y2 = y1 + L2.*sind(theta2(j));
%plotting the coordinate values of link1 and link2 for all set of theta1
%and theta2 values
plot ([x0 x1],[y0 y1],[x1 x2],[y1 y2],'LineWidth',2)
axis([-0.2 2.5 0 2.5])
pause (0.1)
%capturing each generate plot and storing it in a structure M containing
%image data
frame = getframe(gcf);
M(ctr) = frame;
ctr = ctr + 1; %updating the value of counter by 1 to capture next frame
end
end
movie(M) %creating the movie will all figure frames
videofile = VideoWriter("forward_kinematics.avi","Uncompressed AVI");
open(videofile);
writeVideo(videofile,M)
close(videofile)
Errors faced and steps taken to fix the errors:
1. 
The error occurred due to incorrect syntax usage using the capture current figure command. The error was fixed by replacing 'gfc' by 'gcf'
2. 
Incorrect function name used, replaced 'Movie' with 'movie'.
3. 
To fix the issue the previous statement was broken down into two parts
frame = getframe(gcf);
M(ctr) = frame;
4. 
Issue was fixed by replacing 'Uncompressed.avi' with 'Uncompressed AVI' and by replacing single quotes with double quotes
5. 
Issue occurred due to incorrect function name usage, replaced 'videoWriter' with 'VideoWriter' to fix the issue
AVI file google drive link: https://drive.google.com/file/d/1AAY6zv8EJj6COCxLbybhKcW2dhoZwSJP/view?usp=drive_link