Week 2- 2R Robotic Arm Challenge
AIM : Write a Program in MATLAB to simulate the forward kinematics of a 2R Robotic Arm.
THEORY : The 2R Robotic Arm consists of base, two arms or kinematic links and two rotational joints. These links will have
motion in them when they are programmed. The link one will make make an angle theta1 eith respect to base, similarly link
two will make an angle theta2.
Forward Kinematics
If θ1 and θ2 are given, calculate the final position of the manipulator.
Step 1 : Calculate the position of link one (L1)
Step 2 : Calculate the position of link two (L2)
Let the starting point of link one will be (0,0) ; the end point of link one is the starting point of link two i.e, (x1,y1); the point
on the end manipulator will be (x2,y2)
now, x2= x1 + L2 cosθ
and y2 = y1 + L2 sinθ
where θ is going to be θ2.
Till now we have determined the co-ordinates of three points ( the origin point, the end point of link 1, the end point of link 2)
i.e, [x0,y0] [x1,y1] [x2,y2]
lets consider, length of the first arm L1 = 1
& length of the secong arm L2 = 0.5
as it consists of 2 arms, it has 3 coordinate points.
the first coordinate point would be x=0 & Y =0
(Note: We can change (x0,y0) as per problem. For this particular problem lets consider (0,0))
calculate Second and Third coordinate in later part.
Now,
theta1 is angle between x-axis and first arm, theta2 is angle for second arm.
here we can take any value for theta1 and theta2, but we need to show the result in animated form, so I have used for loop.
theta1=linspace(0,90,10)
The value of theta1 varies from 0 to 90 degrees.
theta2=linspace(0,90,10)
The value of theta2 varies from 0 to 90 degrees.
In order to get corresponding values for theta1 and theta2,we are using for loop.
for i=1:length(theta1)
THETA1= theta1(i);
for j= 1:length(theta2)
THETA2= theta2(j);
end
end
Explaination
Now in matlab, if we use "length" keyword,then it computes number of elements.
Therefore, length(theta1)= 10, as theta1 has 10 elements or values.
i=1:10 and
length(theta2)= 10, as theta2 has 10 elements or values.
j=1:10
In "for loop"we don't get a set of array, it will store only one value for every execution of 'i'. So each value of 'i' and 'j', we will
get different values for THETA1 and THETA2. we need to provide different names for different values of theta1 and theta2
angles as we calculate corresponding angles. THETA1=theta(i) and THETA2=theta(j).
Here we are making a "for loop", so we can get 10 values of THETA2 for every value of THETA1.
i.e., suppose, i=1
THETA1=0
corresponding values for THETA2= ranging from 0 to 90 because 'j' varies from 1 to 10.
For first arm, the coordinates will be:
x1=L1*cosd(THETA1);
y1=L1*sind(THETA1);
For second arm, the coordinates will be:
x2=x1+L2*cosd(THETA2);
y2=y1+L2*sind(THETA2);
Now, in order to get different coordinates for different THETA1 and THETA2, we need to put x1,y1,x2 and y2 equations in "for
loop".
for i=1:leng ngth(theta1)
THETA1=theta1(i);
for j= 1:length(theta2)
THETA2=theta2(j);
x0=0;
y0=0;
x1=L1*cosd(THETA1);
y1=L1*sind(THETA1); x2=x1+L2*cosd(THETA2); y2=y1+L2*sind(THETA2);
line([x0 x1],[y0 y1],'color','r')
line([x1 x2],[y1 y2],'color','b')
end
end
We also can plot graph for each individual movement using figure command.
For this purpose i will use only 3 values for theta1 and theta2, so we will get 9 graphs.
ct(1)
for i=1:length(theta1)
THETA1=theta1(i);
for j= 1:length(theta2)
THETA2=theta2(j);
x0=0;
y0=0;
x1=L1*cosd(THETA1);
y1=L1*sind(THETA1);
x2=x1+L2*cosd(THETA2);
y2=y1+L2*sind(THETA2);
figure(ct)
line([x0 x1],[y0 y1],'color','r')
line([x1 x2],[y1 y2],'color','b')
ct=ct+1 ;
end
end
Now, In order to make animation, Instead of using line command we can use plot command.
In plot command, we can add both the command into one and we also can use pause so that we can make animation with a
fixed interval of code execution time.
Pause(0.3), so code will execute after 0.3 seconds.To make animation slow we can incrase the value in pause command.
Using 'linewidth' we can set thickness of arm/line. Using 'axis' command we can fix the frame for animation. If we dont use
'axis' command, lenght of x-axis and y-axis will vary for each calculation. So it is important to fix the frame to get a fixed
frame animation.
ct(1)
for i=1:length(theta1)
THETA1=theta1(i);
for j= 1:length(theta2)
THETA2=theta2(j);
x0=0;
y0=0;
x1=L1*cosd(THETA1);
y1=L1*sind(THETA1);
x2=x1+L2*cosd(THETA2);
y2=y1+L2*sind(THETA2);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',4)
axis([0 1.5 0 1.5])
pause(0.3)
ct=ct+1 ;
end
end
Pause command actually pauses the code execution each time. But we need a variable where we can store all these figures to
make a animation. Here we take 'M' as variable.
In this we can store figures, m=getframe(gcf)..... used gcf so that we can store current frame.
Make sure that, M=getframe(gcf) is used before ct=ct+1. To avoid errors.
Now to make video of animation, use command movie outside for loop.
movie(M)
videofile=VideoWriter('2R_Robotic_Arm_Challenge.avi','uncompressed avi')
open(videofile)
writeVideo(videofile,M)
close(videofile)
First define whose movie you want to make. Here we are making movie of 'M', movie(M).
Then provide name and profile for video file using 'videofile' command.
Open video file, use writeVideo command to write in that particular file.
Now close it.
You will get following message after successful creation of video file.
clear all
close all
clc
%Inputs
l1 = 1;
l2 = 0.5;
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);
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([-0.1 1.5 0 1.5])
pause(0.03)
M(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)