Project

Simulating Forward Kinematics Of 2R Robotic Arm

AIM:

To write a MATLAB program to simulate the Forward Kinematics of a 2R Robotic Arm.

2R ROBOTIC ARM:

A robotic arm is a type of mechanical arm, usually programmable, with similar functions to a human arm, the arm may be the sum total of the mechanism or may be part of a more complex robot. The links of such a manipulator are connected by joints allowing either rotational motion (such as in an articulated robot) or translational (linear) displacement. The links of the manipulator can be considered to form a kinematic chain. The terminus of the kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.

In robotics, an end effector is the device at the end of a robotic arm, designed to interact with the environment.

Forward kinematics refers to the use of the kinematic equations of a robot to compute the position of the end-effector from specified values for the joint parameters.

GOVERNING EQUATIONS:

Figure 1

Figure 2

  1. Above diagram represents a robotic arm with a Base, Link1 , Link2 and Manipulator.
  2. θ1 and θ2 are the angles formed by Link1 and Link2 with the horizontal(i.e. x-axis). For Link1 base of the robotic arm is taken as horizontal.
  3. The position of the manipulator depends on the values of θ1 and θ2.
  4. Here the lower end of Link1, which is connected to base is fixed and the co-ordinates for it are assigned as (X0,Y0).
  5. Now for the other end of Link1, co-ordinates are assigned as as (X1,Y1) which is also starting end for Link2.
  6. (X2,Y2) are the co-ordinates assigned for the other end of Link2 where manipulator is fixed.
  7. Now we need to calculate the positions of Link1 and Link2 in order to get different positions of the robotic arm.
  8. To calculate the position of Link1 we need (X0,Y0) and (X1, Y1) for different values of θ1. Since (X0,Y0) is fixed position it is taken as (0,0) , now to calculate (X1,Y1) following equations are used:-

`X1= l1*cos(theta1)`

`Y1= l1*sin(theta1)`

Where ,

`l1` = Length of Link1.

  1. Now to calculate the position Link2 we (X1,Y1) and (X2,Y2) for different values of θ2. Since we have got (X1,Y1) from above equation , (X2,Y2) can be calculated using following eqution:-

`X2 = X1+l2*cos(theta2)`

`Y2 = Y1+l2*sin(theta2)`

Where,

`l2`= Length of Link2.

  1. Using these equations we will be writing a program in MATLAB to simulate forward kinematics of 2R Robotic arm .

OBJECTIVES:

  1. To simulate the Forward Kinematics of 2R Robotic Arm using MATLAB program.
  2. To plot arms of the robot at different angles.
  3. To create a animation video of robotic arms for different arm positions .

PROGRAM:

CODE :

% FORWARD KINEMATICS OF A 2R ROBOTIC MANIPULATOR
close all 
clear all
clc
 
%INPUTS
%length in meters
l1=1;
l2=0.5;
 
% angles in degrees
theta1=45;
theta2 =90; 
 
%CO-ORDINATES
X0=0;
Y0=0;
 
X1 =l1*cosd(theta1);
Y1=l1*sind(theta1);
 
X2=X1+l2*cosd(theta2);
Y2=Y1+l2*sind(theta2);
 
%PLOTTING
line([X0 X1],[Y0 Y1],'linewidth',3,'color','b');
line([X1 X2],[Y1 Y2],'linewidth',3,'color','r');

EXPLANATION:

  1. The above code is a simple MATLAB program for simulating forward kinematics of 2R Robotic Manipulator.
  2. Here the inputs for program are the length of Links (l1 and l2) and angles made by links (θ1 and θ2).
  3. Since lengths of the Link are fixed and constant, l1 and l2 here are given a fixed values.
  4. Values of θ1 and θ2 here are taken randomly to run the program, these values can be changed as per the requirements . In MATLAB program θ is typed as ‘theta’.
  5. Now for the co-ordinates ,as stated above values for X0 and Y0 are typed and equations for X1,Y1 and X2,Y2 are typed in the code. Here in equations ‘cosd()’ and ‘sind()’ are used ,since angles are in degrees. If only ‘cos()’ and ‘sin()’ are used the angles will be taken in radians by MATLAB and we will get wrong values as a result. Below figure represents the difference between ‘cosd()’ , ‘sind()’ and ‘cos()’, ‘sin()’.

Figure 3

  1. To get the different positions of links at different angles, line command is used.
  2. Using line command (line()), line is drawn between (X0,Y0) and (X1,Y1), thus representing Link1. Similarly a line is drawn between (X1,Y1) and (X2,Y2) using line command representing
  3. Along with the line command, color and line width command is also typed to differentiate both the links and their angles.

Figure 4

  1. In above figure we can observe that the BLUE color represents Link1 and RED color represents Link2 and they are at an angle 45 degrees and 90 degrees respectively.
  2. Therefore by changing θ1 and θ2 values we can get different positions of Links thus we get different position of the arm. In the below figure you can observe the change in position of the arm by change θ1 and θ2 value to 45 degrees and 30 degrees respectively.

Figure 5

  1. In this code ,we have to enter θ1 and θ2 values manually in order to get different positions of the arm, so to overcome this problem, a FOR LOOP is used which will help us to get different positions in a single go, automatically which will in turn create a animation.

IMPROVED CODE:

% FORWARD KINEMATICS OF A 2R ROBOTIC MANIPULATOR
 
clear all
close all
clc
 
% INPUTS
l1=1;
l2=0.5;
theta1=linspace(0,90,4);
theta2=linspace(0,90,4);
 
%FOR LOOP
  for i=1:length(theta1);
      THETA1=theta1(i);
      for j=1:length(theta2);
          THETA2= theta2(j);
          X0=0;
          Y0=0;
 
           X1 =l1*cosd(THETA1);
           Y1=l1*sind(THETA1);
 
          X2=X1+l2*cosd(THETA2);
          Y2=Y1+l2*sind(THETA2);
 
      %PLOTTING 
         line([X0 X1],[Y0 Y1],'linewidth',3,'color','b');
         line([X1 X2],[Y1 Y2],'linewidth',3,'color','r');
         
      end
      
  end

EXPLANATION:

  1. In this code the l1 and l2 are same as previous code since they are fixed values and θ1 and θ2 values are entered using ‘linspace’ command so that multiple values can be used. For example in the code ‘theta= linspace(0,90,4)’ is entered which means that 4 values between 0 to 90 will be taken. By changing number of values between 0 to 90 we can get more values and output figure in will be more smooth. Here for easy calculations only 4 values are taken.
  2. Similarly for θ2 also linspace command is used.
  3. ‘FOR LOOP’ is used so that we can get different angle positions of Link2 for each and every angle of Link1,by which we achieve different positions of the
  4. Now in the FOR LOOP ‘ i=1:length(theta1)’ means that ‘i’ is a variable where it varies from 0 to last number which is defined for θ1. Here ‘i’ is a loop counter variable which takes a new value every time it is repeated in FOR LOOP.
  5. Now a new variable THETA1 is assigned and given as it is equal to theta1(i),which means THETA1 will take a new value of theta1 within the limits of ‘i’ whenever the loop is repeated. When ‘i’ is one that means it will take first value of theta1 and it will be assigned to THETA1.
  6. Now for each angle of Link1 ,different positions of Link2 is achieved by using second FOR LOOP and this time defining values for θ2.
  7. In second FOR LOOP ‘j=1:length(theta2)’ is entered which means same as the step 4 but the variable is different as this is for defining θ2. Here ‘j’ is a loop counter
  8. New variable THETA2 is assigned and given as it is equal to theta2(j). The meaning is same that of step 5. Here when ‘j’ is one that means it will take first value of theta2 and it will be assigned to THETA2.
  9. Now value of X0,Y0 are assigned and equations for X1,Y1,X2,Y2 same the previous code ,but theta1 and theta2 are replaced with THETA1 and THETA2 respectively , as values of θ1 and θ2 are changed every time in the FOR LOOP.
  10. Same line commands are used as previous code to represent both the links with different color.
  11. END command is used to close the For LOOP.

Figure 6

  1. This is the figure we will get after executing the above code. For each and every angle of Link1 we have got different positions of Link2.
  2. But here the visualizing all the positions in same plot is difficult and it is not a good way for any representation , so some changes are made in the above code for better visualization.

REVISED CODE 1:

% FORWARD KINEMATICS OF A 2R ROBOTIC MANIPULATOR
 
clear all
close all
clc
 
% INPUTS
l1=1;
l2=0.5;
theta1=linspace(0,90,4);
theta2=linspace(0,90,4);
 
%FOR LOOP
ct=1;
  for i=1:length(theta1);
      THETA1=theta1(i);
      for j=1:length(theta2);
          THETA2= theta2(j);
          X0=0;
          Y0=0;
 
           X1 =l1*cosd(THETA1);
           Y1=l1*sind(THETA1);
 
          X2=X1+l2*cosd(THETA2);
          Y2=Y1+l2*sind(THETA2);
 
      %PLOTTING
      figure(ct);
         line([X0 X1],[Y0 Y1],'linewidth',3,'color','b');
         line([X1 X2],[Y1 Y2],'linewidth',3,'color','r');
         ct=ct+1;
      end
      
  end

EXPLANATION:

  1. Here a counter is again used but not this time its outside FOR LOOP.
  2. Counter variable here is ‘ct’. Here ‘ct’ represents each new figure window when FOR LOOP is repeated each and every time.
  3. A statement ‘ct=ct+1’ is entered at the end of second FOR LOOP which means that each and every time FOR LOOP is executed counter is incremented by one .
  4. Figure() command is used so that for each ‘ct’ value there is different figure generated by MATLAB showing different positions of the links individually.
  5. Below figure show the number of figure generated .

Figure 7

Figure 8

Figure 9

Figure 10

  1. Here since number of values given for θ1 and θ2 are 4 for each , therefore 16 different figures are generated, representing particular combinations of θ1 and θ2. Thus representing 16 different positions of the arm.
  2. Still this is not a good way to represent the plot or positions of the robotic arm, further changes are done to the code to make it easy.

REVISED CODE 2:

 % FORWARD KINEMATICS OF A 2R ROBOTIC MANIPULATOR
 
clear all
close all
clc
 
% INPUTS
l1=1;
l2=0.5;
theta1=linspace(0,90,4);
theta2=linspace(0,90,4);
 
%FOR LOOP
ct=1;
  for i=1:length(theta1);
      THETA1=theta1(i);
      for j=1:length(theta2);
          THETA2= theta2(j);
          X0=0;
          Y0=0;
 
           X1 =l1*cosd(THETA1);
           Y1=l1*sind(THETA1);
 
          X2=X1+l2*cosd(THETA2);
          Y2=Y1+l2*sind(THETA2);
 
      %PLOTTING
      figure(ct);
      hold on
         plot([X0 X1],[Y0 Y1],'linewidth',3,'color','b');
         plot([X1 X2],[Y1 Y2],'linewidth',3,'color','r');
         ct=ct+1;
      end
      
  end

EXPLANATION:

  1. Here the changes that are done are ‘line()’ command is replaced with plot command and a ‘hold on’ command is added.
  2. If the hold on command is not used here following outputs are observed.

Figure 11

Figure 12

  1. From the above pictures it is seen that only a single line is shown in all 16 figures, this is because when ‘plot’ command is used the last plot command over-writes the previous ‘plot’ commands. To avoid this error ‘hold on’ command is used , which prevents ‘plot’ command from over-writing.
  2. Now even after adding these commands the results are same as above. So code is further changed .

REVISED CODE 3:

% FORWARD KINEMATICS OF A 2R ROBOTIC MANIPULATOR
 
clear all
close all
clc
 
% INPUTS
l1=1;
l2=0.5;
theta1=linspace(0,90,4);
theta2=linspace(0,90,4);
 
%FOR LOOP
ct=1;
  for i=1:length(theta1);
      THETA1=theta1(i);
      for j=1:length(theta2);
          THETA2= theta2(j);
          X0=0;
          Y0=0;
 
           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]);
         pause(0.3)
         ct=ct+1;
      end

EXPLANATION:

  1. Here the changes that are done are ‘plot’ command is reduced to a single line, an ‘axis()’ command is used , ‘pause()’ command is used and ‘figure()’ command is eliminated.
  2. The reason for using ‘plot’ command instead of ‘line’ command is , we can use ‘pause()’ command along with ‘plot()’ more easily.
  3. ‘Axis ()’ command here is used to define the limits of x-axis and y-axis.
  4. ‘Pause()’ command here is used to pause the code for specified time ,this is used to do a animation for different position of the arm.
  5. Once this program is executed you will get a animation in the MATLAB as a result.
  6. Thus animation is created but still using ‘pause()’ command to pause the code is not that efficient, so further changes are done to create a animation video of the robotic arm.

FINAL CODE:

% FORWARD KINEMATICS OF A 2R ROBOTIC MANIPULATOR
 
clear all
close all
clc
 
% INPUTS
l1=1;
l2=0.5;
theta1=linspace(0,90,10);
theta2=linspace(0,90,10);
 
%FOR LOOP
ct=1;
  for i=1:length(theta1);
      THETA1=theta1(i);
      for j=1:length(theta2);
          THETA2= theta2(j);
          X0=0;
          Y0=0;
 
           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]);
         pause(0.03);
         M(ct)=getframe(gcf);
         ct=ct+1;
      end
      
  end

  movie(M)
  videofile = VideoWriter('Forward_Kinematics.avi','Uncompressed AVI');
  open(videofile)
  writeVideo(videofile,M)
  close(videofile)

EXPLANATION:

  1. Here in ‘linspace()’ command the limits for θ1 and θ2 are changed to 10 for smoother animation.
  2. To create a good movie of the above animation , we need to get individual frames and save those frame in a array. Here in the above code array is named as ‘M(ct)’ where ‘ct’ represents the number of frames.
  3. Here the frames should be saved first and then counter should be applied or else MATLAB will show error. This mistake was done by me during the execution of code. So ‘ct=ct+1’ should be typed after the ‘M(ct)’ , which means that first the frame is saved then counter is incremented by one then again second framed is saved and the counter in incremented again by one and the code goes on.

Figure 13

  1. Now ‘getframe(gcf)’ command is used to get frame of current figure window. This is assigned to array ‘M’. Here ‘gcf’ means get current frame.
  2. Every time when loop is executed current figure is saved in the array ‘M’.
  3. After the end of the first FOR LOOP we have entered command ‘Movie(M)’. This command instructs the MATLAB to generate a movie using all the frames in ‘M’.
  4. Now to create a AVI file, where MATLAB will save this video in the selected folder, we have used command ‘VideoWriter()’ where we have to enter file name and profile in this command. In the above code it is specified accordingly. This command is assigned to a variable ‘videofile’.
  5. Now to open this video ,’ open(videofile)’ command is used ,since ‘videofile’ is the variable used so it is used inside the brackets.
  6. Then, we are going to write in the ‘videofile’ , the frames that we captured using ‘writeVideo(videofile,M)’ command, where the ‘videofile’ and ‘M’ are assigned variables.
  7. Now we close this file using ‘close(videofile)’ command.
  8. Now when we execute the code an animation movie for different positions of the robotic arm is generated and a video file is saved in the same folder where this code is saved.
  9. Below link will take you to the animation movie generated using above code and it is the perfect result that we have achieved.

Animated movie link- https://youtu.be/E1gOkSUnvVs

CONCLUSIONS :

  1. Thus Forward Kinematics of 2R Robotic Arm is simulated successfully using MATLAB program.
  2. Using this project, the code can be further modified for a three link robotic arm or can used for other similar projects.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.