Forward kinematics of a 2R robotic arm using MATLAB.
- 1. Aim: To plot the forward kinematics of a 2R robotic arm using MATLAB.
- 2. objective:
- To perform the animation of a 2R robotics arm
- Represent the motion of two arms of a robot.
- Use of for loops to perform the operations.
- 3. Method:
- The program shown below gives the idea of the operation of the two arms.
- All the necessary inputs are given and commands are also specified wherever necessary.
- The most important point here is to understand the working of a 'for loop'.
- Here is the independent motion of both the arms are required and for each motion of 1st arm, a number of motion for 2nd arm are required.
- To obtain this requirement the for loop is used.
% This is a MATLAB program for forward kinematics of a 2R robotic arm
% manipulator
clear all
close all
clc
%input parameters like length and theta angle for two arms
% l1 & l2 = length of 1st & 2nd arm in meter
% theta1 & theta2 are angle from horizontal axis in degree
l1 = 1;
l2 = 0.5;
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
ct =1;
% ct is a counter which represents each and every frame
% for each value of theta1, theta2 is having a number of values, 10 in this
% case. so for this, we need to give another for loop for theta2. for each
% for loop, the loop counter variable is different.
% THATE1 & THETA2 are variables which contain 1 value of theta1
for i = 1: length(theta1)
THETA1 = theta1(i);
for j = 1: length(theta2)
THETA2 = theta2(j);
x0 = 0;
y0 = 0;
%cosd is used as cos only expect the value in radian
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);
%plotting- plot command needs the list of start and end values of
%both x any y
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3)
axis([-0.1 1.5 0 1.5])
pause(0.1)
M(ct) = getframe(gcf);
% M is an array where we are going to store all figure windows
ct = ct+1;
end
end
movie(M) % matlab will generate movie with all frames
videofile = VideoWriter('forward_kinematics1.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
- The plots shown below represent the different motions of the two arms depending upon the theta1 and theta2 angles.
- From the plot and the video, it can be seen that for each theta change of link 1(blue link), link 2(red link) is having a number of motions. Here in this case 10.
- Both links red and blue represent the two robotic arms having some definite motion.
|
Plot at the begin of the program at theta1=0,theta2>0 |
Plot at the begin of the program at theta1>0,theta2>0 |
|
Plot at the begin of the program at theta1=0,theta2>0 |
Plot at the begin of the program at theta1=90,theta2=0 |
![]() |
- The plot is shown represent the error occurred during running the program. - The first error was Movie command which creates a movie has to be as 'movie', whereas in the program by mistake it was 'Movie'. - Another error was on line 44 where 'VideoWriter' was needed instead of 'Videowriter'. |
-4. Learning outcome:
- Understanding the use of a 'for loop'.
- Understanding the application of a for in for loop.
- Able to perform an animation of a robotic arm.
- Understanding the command like linespace, getframe, get current frame, for loop, movie.
-5. Conclusion:
- Using the for in for loop result in two different motion of arms i.e while link 1 is at a particular theta angle, link 2 is able to complete its cycle and this cycle repeats again and again as per limiting values.
- For loop can be used to perform the same task again and again and can be used in a different manner as shown above.
Youtube link: https://youtu.be/j6eEpuilwQ0




