Simulation of Forward Kinematics of 2R Robotic Arm using MATLAB
Aim:
To simulate the forward kinematics of a 2R Robotic arm using MATLAB
Introduction:
A robotic arm is a mechanical arm that can be programmed to function similar to a human arm. The links of the manipulator are connected by joints which can allow either rotational motion or translational motion. Hence, the links form a kinematic chain.
Note : The above image was drawn by me based on the lecture notes
There are two links or arms which can be programmed to give motion as desired.
The angle made by link 1 with the base is `theta_1` and link 2 makes an angle `theta_2`.
It is assumed that the initial position of link 1 is `(x_0,y_0) = (0,0)` and the initial position of link 2 is `(x_1,y_1) = (L_1 cos theta_1, L_2 sin theta_1)` for simplicity in calculation.
Hence the position at the manipulator will be the sum of two coordinates , that is `(x_2,y_2) = (x_1 + L_2 cos theta_2 , y_1 + L_2 sin theta_2)`
MATLAB Program:
%Forward kinematics of robotic arm
%Arm lengths l1 and l2 in metres
l1 = 1.5;
l2 = 1;
%Theta1 and Theta2
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
%Initial position of arm 1
x0 = 0;
y0 = 0;
%Counter variable
k=1;
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
%Arm Positions
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 3 0 3])
pause(0.1)
%To make a Video
V(k) = getframe(gcf)
k = k+1;
end
end
movie(V)
videofile = VideoWriter('Forward_kinematics_of_robotic_arm','Uncompressed AVI');
open(videofile)
writeVideo(videofile,V)
close
Screenshots of my Output:
Explanation:
The length of the links or arms are designated as l1 and l2 . The length of l1 is 1.5 m and l2 is 1 m.
The angles to be rotated by the two arms are set to 0 to 90 degrees using the 'linspace' command and they are assigned to theta1 and theta2 respectively.
'k' is a counter variable which is used in the for loop and is initially assigned a value of 1.
There is a 'nested for loop' (or one for loop within the other) to create a 90 degree rotation of link2 for every rotation of link 1.
The variables THETA1 and THETA2 are used for iterations inside the 'for loop' .
This creates a movie when each of the movement of the links are captured in a frame and placed one after the other.
The variable 'V' is used for the video
A video is made using the commands after the 'for loop'. At the end of each movement, the 'getframe' command captures and stores them which then are made into a video.
The 'VideoWrite' function creates a video file from any MATLAB movie and so with the help of that command, a video of the arm movements is made and stores in '.avi' file format.
The YouTube link of my video is as follows:
Conclusion :
The forward kinematics of 2R robotic arm was simulated using MATLAB and explanation is given with the help of screenshots from MATLAB window. The animated video of the simulation was uploaded to YouTube and the link was shared.