Week 2- 2R Robotic Arm Challenge
Aim: To write the program in matlab to simulate the forward kinematics of a 2R Robotic arm and create an animation vedio.
PROGRAM:
% Forward kinematics of a 2R robotic arm manipulator
clear all
close all
clc
%inputs
l1=3;
l2=2;
theta1=60;
theta2=45;
%coordinates
X0=0;
Y0=0;
X1=l1*cosd(theta1);
Y1=l1*sind(theta2);
X2=X1+l2*cosd(theta2);
Y2=X1+l2*sind(theta2);
%plotting
line([X0 X1],[Y0 Y1],'linewidth',2,'color','b')
line([X1 X2],[Y1 Y2],'linewidth',2,'color','r')
EXPLANATION:
we are here writing a program for robotic arm kinematics.
first we are having the input values of l1 and l2. so lets first understand basic program of robotic arm and then come to final program.
In the basic program we will given the first arm angle and second arm angle and we will see the corrseponding plot of the arm
given first arm angle as 60 and second arm angle 45 and aslo the length of the two arms.
since the arm (first) end is connected to second arm starting lets consider first arm starting point coordinates as x0,yo and ending point as x1,y1 and this x1,y1 coordinates will be given toi the second arm starting point (sinces there are connected) and the ending of second arm as x2,y2.
now,
x0=0,y0=0 (considering as base point)
x1 y1 will be given as length of arm*cosd(theta) for x1
and length of arm*sind(theta) for y1. here angle is first arm angle.
then we will calculate for x2=x1+length of arm*cosd(theta)
y2=y1+length of arm*sind(theta) .here angle is second arm angle.
this gives how the arms positions with respect to the given angles.
clear all
close all
clc
% Forward kinamatics for 2R robot and making the video of output
%inputs
%lengths
l1=3;
l2=2;
%angle
theta1=linspace(0,90,10);
theta2=linspace(0,90,10);
ct=1;
for i=1:length(theta1)
THETA1=theta1(i);
for j=1:length(theta2)
THETA2=theta2(j);
%coordinates
X0=0;
Y0=0;
X1=l1*cosd(THETA1);
Y1=l1*sind(THETA1);
X2=X1+l2*cosd(THETA2);
Y2=Y1+l2*sind(THETA2);
%plotting
plot([X0 X1],[Y0 Y1], [X1 X2],[Y1 Y2], 'linewidth',3)
axis([-1 5 0 5])
pause(0.2)
m(ct) = getframe(gcf);
ct = ct+1;
end
end
% For makling the video file of the plot
movie(m)
videofile = VideoWriter('forward_kinamatics.avi','Uncompressed AVI');
open (videofile)
writeVideo(videofile,m)
close(videofile)
Now,
lets come to final program where we will slove for each angle of arm one how will the second arm tales the position. this can be done by using for loop.
using for loops i.e., we are using the condition for every angle of arm 1 the second arm takes the position until the next angle of the first arm.
angle for first arm have 10 angles (from 0 to 90). for these every angle between 0 to 90 of fitst angle the second arm takes theangles 0 tp 90.
the conditons goes like these:
for each one valuse of angle of first arm
angle of second arm goes from 0 to 90 and the loop cloise for first angle of first.
I did few mistakes while writing the program the errors shown are:



then rechecked every parameter and every line and made required corrections according to that.
Vedio link: