Simulation of forward kinematics of a 2R Robotic arm in matlab
AIM:
Simulation of forward Kinematics of a 2R robotic arm
THEORY :
A robotic arm is a type of mechanical arm, usually programmable with similar functions to a human arm; the arm may be the sum total of the mechanism or may be part of a more complex robot The links of such a manipulator are connected by joints allowing either rotational motion (such as in an articulated robot) or translational (linear) displacement. The links of the manipulator can be considered to form a kinematic chain. The terminus of the kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.
If we can figure out how the robot is going to move then we can understand its workspace .Understanding the worksapace is important for designing a robot for complex applications.
CODING EXPLANATION :

1) Defining the inputs :
length of link 1 , l1 = 1;
length of link 2 , l2 = 0.5;
theta1 = 45;
theta2 = 30;
2) defining the co-ordinates
The position of link 1 is calculated ;
co-ordinate of start = (0,0)
x0=0
y0 = 0
The co-ordinate of end of link 1 ;
x1 = l1*cosd(theta1)
y1 = l1*sind(theta1)
the co-ordinate of link 2 is calculated
x2 = l1 +l2*cosd(theta2)
y2 = l1+l2*sind(theta2)
3)Since there are three points , line command is used to visualise the links
line([x0 x1],[y0 y1],'linewidth',3,'color','b')
If the program is executed we get an plot btween link 1 nd link 2 at a certain value of theta1 and theta 2 . This is not the aim . The main objective is to get multiple values of theta1 and theta2 and for each value of theta1 and theta2 plots will be ctreated and that will be stitched to get an animation.
4)So an array of theta1 and theta2 is created using the linspace command
theta1 = linspace(0,90,20)
theta2 = linspace(0,90,20)
5) To change the value of theta1 and theta2 everytime ,a for loop is executed with an loop counter variable
for i = 1:length(theta1):
T1 = theta1(i)
What this means basically is , as the value of i changes we are taking the vaklue of theta1 from original array and assigning it to variable called T1
6)Now for each value of link1 , link 2 can take up multiple positions , depending on theta2 , so a for loop for theta2 is executed inside the for loop for theta1 with a different loop counter variable
for j=1:length(theta2):
T2=theta2(j)

7)Now the entire coordinates [ x0,y0],[x1,y1],[x2,y2] is wriiten inside this with only one chqnge of writing T1 and T2 insted of theta1 and theta2 respectively.

To visulaise it perfectly a counter is created 'ct' and it is assigned a value 1 and declared before the for loop starts
To get individual frames and save this to an array M;
M(ct) = getframe(gcf);
the counter is updated
ct = ct+1;
To generate a movie :
Movie(M)
A new avi file is to be created where all those frames will be dumped
Videofile = VideoWriter('forward_kinematics.avi','Uncompressed.AVI');
open(Videofile)
WriteVideo(Videofile,M)
close (Videofile)
The Matlab code is pasted below
% forward kinematics of a 2R robotic arm
clear all
close all
clc
%inputs
theta1 = linspace(0,90,20);
theta2 = linspace(0,90,20);
l1 = 1;
l2 = 0.5;
ct = 1;
%To change the value of theta1 and theta2 by programming
for i = 1:length(theta1)
T1 = theta1(i);
for j = 1:length(theta2)
T2 = theta2(j);
x0 = 0;
y0 = 0;
x1 = l1*cosd(T1);
y1 = l1*sind(T1);
x2 = l1+l2*cosd(T2);
y2 = l1+l2*sind(T2);
%plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',2)
axis([-0.1,3,0,3])
grid on
pause(0.1)
M(ct) = getframe(gcf);
ct = ct+1;
end
end
%To create a movie in matlab
movie(M)
videofile = VideoWriter('forward_Kinematics.avi');
open(videofile)
writeVideo(videofile,M)
close(videofile)
OUTPUT :
A smooth animation is created. By tracing the position of the manipulator , the workspace can be estimated.
the youtube link of the animation is posted below
https://youtu.be/DBWcIuONC4kAIM
A screenshot of the animation is posted :

CONCLUSION :
The lengths of the link 1 and link 2 can be varied to simulate different robotic arms.
The axes can also be changed .