Simulation of Forward kinematics of 2R robotic arm using MATLAB Program
Objective:
To simulate the forward kinematics of a 2R Robotic Arm using Matlab program and to create an animation file of the plot.
2-R Robotic Arm
2-R Robotic arm is made up of a base, two links and two rotating joints. The first link is connected to the base and the second link is connected to the first link. The links are connected by rotating joints which provides a rotational degree of freedom in one plane.
Forward Kinematics of a Robotic arm
Forward kinematics is the study of motion of a robot manipulator. It is achieved through the use of kinematic equations of a robot. It is used to compute the position of the end effector and hence to determine the workspace of the robot manipulator. The basic structure of a 2-R Robotic arm is shown below
In the fig. shown above, there are two links, link 1 and link 2 of length l1 and l2 respectively. Link 1 is connected to the base and the link 2 is connected to link 1. Link 1 makes an angle θ1 with the base and link 2 makes an angle θ2 with the link 1. for this case we can avoid the robotic arm and focus on link1 and link 2 The one point of link 1 is fixed in the base and its considered as origin of the arm
(x0,y0 )= (0,0)
The equation to determine the co-ordinates (x1,y1) &(x2,y2)
x1 = L1*cos(θ1)
y1 = L1*sin(θ1)
x2 = x1 + L2*cos(θ2)
y2 = y1 + L2*sin(θ2)
Matlab code
The input parameter
L1=1
L2=0.5
clear all
close all
clc
%Input parameter
L1 = 1; %length of Link1
L2 = 0.5; %length of link2
theta1 = linspace(0,90,10);% Angle between the xaixs and link 1
theta2 = linspace(0,90,10); % Angle between the xaixs and link 2
%coordinate for the starting point of link 1 its (0,0) since its fixed to
%the base
x0 = 0;
y0 = 0;
ct=1;
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
%x,y coordinate for link 1
x1 = L1*cosd(THETA1);
y1 = L1*sind(THETA1);
% x,y coordinate for link 2
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]); %range for the x and y axis in graph
grid on;
pause(0.4); %pause time for each frame
M(ct) = getframe(gcf); % for creating animation file
ct=ct+1;
end
end
movie(M)
videofile = VideoWriter('forward_kinematic.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation
First clear all , close all, clc command is use to clear all the data from the workspace and command window and close all this figure which is open its always good to start with these three command before starting a code
The percent sign is most commonly used to indicate non-executable text within the body of a program. those non-executable text are for better understanding the code
The input parameter for Kinematic equation is defined L1 and L2
The theta 1 and theta 2 values varies from 0 to 90 in 10 division so the linspace command is used
Since the base point of link 1 is fixed in origin we as assign them fixed values for X0,Y0
Then used for loops for iteration to get robotic arm movements at minimum and maximum boundary
For the first for loop we used a range from 1: length of theta1 (i.e 10divison ) so the iteration goes from 0 to 90degree for theta 1
and there is inner for loop for theta2 with range from 1: length of theta2 , the meaning for the inner for loop is ,for each iteration of theta 1 and theta2 will execute 10 iteration
Example in second iteration for the first loop theta1 is going to be 10 degree and once in goes to the inner for loop the theta2 will execute for all values in range defined and once all the values for theta2 is executed its again goes to the first for loop for the third iteration for value 20 degree for theta1 and once again all the values in theta2 range will execute .
these theta1 and theta2 for each iteration is storage in variable called THETA1 and THETA2 respectively
The equation for the coordinated is defined with proper input parameter
plot command is used to plot coordinated for the each iteration which is different theta values
axis command is used to sets the limits for the x, y axis.
pause command is used to mention the pause time for each iteration results to show
M(ct) = getframe(gcf) is used to capture the each frame from the plot
movie(M) command is used to combine those each frame from M(ct) and make a video file
The animation File
Conclusion
The forward kinematics of a 2R Robotic Arm using Matlab program is Simulated and and to create an animation file is created From the plot.