Matlab program to illustrate the Forward Kinematics of a 2-D Robotic arm Manipulator
AIM:
- To write a Matlab program to demonstrate the animation of a 2D Robotic Arm.
INTRODUCTION:
Robotic Manipulator is a device that performs the task as instructed by the operator.The robot need to be programmed to complete the required task.Hence the programmer must know the workspace of a robot.
To understand the motion of a robot end effector, one must know the kinematics of the robotic links.This project illustrates the animation of a 2-D Robotic arm using Matlab.
FIGURE:
GOVERNING EQUATIONS:
- x1=L1 cosθ1
- y1=L1 sinθ1
- x2=x1+L2 cosθ2
- y2=y1+L2 sinθ2
Description:
Here the base co-ordinates are (x0,y0) which are initially at the origin. The lengths of the first and second links are L1 and L2 respectively.
The co-ordinates of the joint 1 are (x1,y1).
Similarly (x2,y2) are the co-ordinates of End effector attached to link 2.
L1 cosθ1 and L1 sinθ1 are the horizontal and vertical projections of link 1 on to the axes x and y respectively.
Similarly for the link 2, L2 cosθ2 and L2 sinθ2 are the projections .
To get the co-ordinates (x2,y2) we need to add the corresponding projections of link2 to the values of (x1,y1) respectively.
OBJECTIVES OF THE PROJECT:
- To understand the Kinematics of a 2-D Robotic Arm Manipulator.
- To write a Matlab Code to illustrate and animate the motion of the Robotic Arm.
- To Visualize the workspace of a robot end-effector in 2-D.
Flow chart to follow while writing the Matlab program :
Explanation:
The program for the above task is written in Matlab which is given below.
clear all
close all
clc
%Base Co-Ordinates
x0=0;y0=0;
% counter value
ct=1;
% Ranges of angles of two arms of a robot
% theta1=angle between the horizontal and first arm
% theta2=Angle between the horizontal and the second arm
theta1=linspace(0,90,10);
theta2=linspace(0,90,10);
% length of robot arms of first and second link
L1=input('Enter the length of the first link');
L2=input('Enter the length of the second link');
%plotting and animating using for loop
for i=1:length(theta1)
x1=L1*cosd(theta1(i));
y1=L1*sind(theta1(i));
for j=1:length(theta2)
x2=x1+L2*cosd(theta2(j));
y2=y1+L2*sind(theta2(j));
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',2);
axis([-0.1,(L1+L2+2),0,(L1+L2+2)]);
% Storing each frame into the array
M(ct)=getframe(gcf);
ct=ct+1;
end
end
% Joining each frame and making a movie
movie(M);
% creating a video file
videofile=VideoWriter('Forward_kinematics_of_robotic_arm.avi','uncompressed AVI');
open(videofile);
% writing the data to a video file
writeVideo(videofile,M);
close(videofile);
CODE EXPLANATION:
The code is explained in the following steps:
- Initially the recquired variables are declared with initial value of counter(ct) as 1.
- The value of θ1 and θ2 have a range of zero to 90 degrees,this is done using linspace command.
- The length of 1st arm and second arm is taken as input .The input command asks the input from the user .
- The final co-ordinates (x1,y1) of link 1 are the projections of the length L1 on x-axis and y-axis respectively.
- The final co-ordinates of 2nd arm are calculated by adding the projections of L2 to the co-ordinates (x1,y1) respectively.
- Hence, depending on the value of θ1 and θ2 the plot will vary.
- For each value of θ1,the θ2 varies from 0 t0 90 degrees.
- This is done using a nested for loop.
The tracing for the loop is explained below:
The value of angles are:
θ1=[0 10 20 30 40 50 60 70 80 90]
θ2=[0 10 20 30 40 50 60 70 80 90]
The following table indicates the different values of θ2 for one value of θ1:
| Value of θ1 | 0 | |||||||||
| Value of θ2 | 0 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 |
Hence the inner for loop will execute 10 times for each value of outer for loop.Thus there will 10 plots for first value of θ1.similarly it happens for the other values of θ1.
9.Hence there would be 100 frames in total.
10. Each frame is stored in an array M[ct] where 'ct' gets incremented each during the execution of inner for loop.This is done using getframe command
The statement for recording the frames is => M(ct)=getframe(gcf). where gcf-Get Current frame.
11.A movie is formed by joining each of the frame .This is done by the movie command.
12.A videofile called "Forward_Kinematics_of_Robotic_Arm.avi" is created to store the animation.
13."open(videofile)" command is used to open the created file.
14."writeVideo(videofile,M)" command writes the frames from 'M' to video file.
15.It is closed using "close(videofile)" command.
16.The animation illustrating the Forward Kinematics of robotic arm will be stored in the respective folder of Matlab in 'avi' format.
The Animation of robotic arm is given in the video below:
ERRORS OCCURED:
1.Improper representation of trignometric function during the calculation of projections.
Error: x1=L1*sin(theta1) ,This takes the valueof angle in radians but we need the angle in degrees.
correction: x1=L1*sind(theta1)
2. Usage of line command instead of plot command.
If we use line command instead of plot command the graphs get overlapped each time and we get the graph as shown.This plot is obtained only for 5 different values of theta1 and theta2.
Correction: But we need to record frames and make a movie. Therefore we need to make use of plot command to get the recquired output. The plot command plots the most recent cocordinates and hence
the output looks like an animation.
The command to be written is:plot([x0 x1],[y0,y1],[x1 x2],[y1 y2]);
CONCLUSION:
This project thus explains the method of implementing the matlab to make a animation that ilustrates the FORWARD KINEMATICS OF 2-D ROBOTIC ARM .
FURTHER IMPROVEMENTS:
The project can be further extended to the Robotic arm in 3-Dimensional Space.this can be done using 3-Dimensional space Co-ordinates.