MATLAB program to simulate the forward kinematics of a 2R Robotic Arm.
1. Aim: -
A program in MATLAB to simulate the forward kinematics of a 2R Robotic Arm.
2. code: -
clear all
close all
clc
%inputs
ct=1;
%Angles made by links to the horizontal surface
theta1=linspace(0,90,24);
theta2=linspace(0,90,24);
%lengths of the links in m
L1=1;
L2=0.5;
%Coordinates of starting point of link 1
X0=0;
Y0=0;
for i=1:length(theta1)
Theta1=theta1(i);
for j=1:length(theta2)
Theta2=theta2(j);
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])
M(ct)=getframe(gcf);
ct=ct+1;
pause(0.0005)
end
end
movie(M)
videofile=VideoWriter('Roboticarm.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
3. Code explanation: -
The code is written in MATLAB R2017a.
The code is started with clear all, close all and clc as shown in the image below.
These commands will close all the stored data from the WORKSPACE window. And close all the plots or graphs that were used before so that we are ready to code are new program.
Now inputs are given to the MATLAB as shown in the picture below.
Ct=1 says that counter will count from 1 or it is used to start the counter, theta1, theta2 are stored by linspace command i.e. linspace(starting value, end value, number of values).
L1 is 1 and L2 is 0.5 in metres each. These are the lengths of the link 1 and link 2 respectively. The starting point of link one is defined as (0,0).and the link1 starting points are written as X0=0; Y=0;(semicolon is given to end the command and so that this shouldn’t show in command window).
The main body of the program starts with the for loop, so the ‘for loop’ is given first and inside the for loop a variable i is defined a “ i = 1:length(theta1);” i.e. the value of i ranges from 1 to length of theta. Then Theta1=theta1(i); is written so that all values of i in theta1 will store in the variable named as Theta1.
Again a ‘for loop’ is started in which a variable j is defined as “j=1:length(theta2);”
And the value of the j in theta to is stored in the variable named as Theta2 and it is written as Theta2=theta2(j);
Now the end points of the link one is given by (X1,Y2)
And in MATLAB it is written as
X1=L1.*cosd(Theta1);
Y1=L1.*sind(Theta1);
End point of the link1 is the starting point of the link2
Now the end point if link2 is (X2,Y2) and it is written as
X2=X1+L2.*cosd(Theta2);
Y2=Y1+L2.*sind(Theta2);
Now plotting of the points is going to be done as following: -
Command- plot([X0 X1], [Y0 Y1], [X1 X2], [Y1 Y2], ‘linewidth’,3)
axis of the plot is defined as axis([-0.1 1.5 0 1.5])
now to start the animation of the each and every plot we have used following command
M(ct)=getframe(gcf);
This command will store every frame in the memory.
ct=ct+1; this command will count one more frame and stores in the memory.
After that pause of 0.0005 sec is given between every frame to play and it is written as pause(0.0005)
Then both the ‘for loops’ are ended by writing end two times as shown below.
Commands below are used to perform the animation and create the video file.
videofile=VideoWriter( ‘name_of_the_file.avi’,’uncompressed AVI’);
The above command is case sensitive command.
A blank Videofile is created and open (Videofile) command is used to open it and writeVideo(videofile,M) is used to add frames stored in M to the videofile and the close (videofile) is used to close the file and save.
Now the program is saved and run by pressing F5 or clicking on the Run button.
4. Video link of the output: - https://youtu.be/Kp6UdejKHY4
5. Screenshots of the output: -
6. Errors: -
the only error that has arrived during my first run of the program is that I have written ‘videowriter’ instead of ‘VideoWriter’ because it is case sensitive.
7. RESULT: - Successfully performed the forward kinematics of the 2R Robotic Arm.