Project

Forward Kinematics of a 2R Robotic Arm using Matlab.

Simulate the Workspace of a 2R Robotic Arm using Matlab.

AIM : To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.

INTRODUCTION :

The Robotic Arm is a type of Mechanical Arm which is programmable to perform different tasks. It is composed of a set of joints separated in space by the arm links. The joints are where the motion in the arm occurs. In basic, a robot arm consists of different parts which are the base, joints, links, and a gripper. Workspace or work envelope is defined as the set of points representing the maximum and minimum extent or reach of the manipulator in all directions.

Description of a 2R Robotic Arm :

A 2R Robotic Arm consists of two links (`l_1,l_2`) and two rotational joints and is pivoted to the base as shown in the following figure. Let `theta_1` and `theta_2` be the angle made by the links `l_1` and `l_2` respectively with the horizontal axis. Let `(x_0,y_0),(x_1,y_1),(x_2,y_2)` be the co-ordinates of the points as shown.

In practice the Robotic Arm moves at different positions to perform a particular task. The simulation of workspace of 2R Robotic Arm can be done using Matlab.

MATLAB Program :

Inputs –

The values of links `l_1` and `l_2` are assumed to be 3m and 1.5m respectively and the angle made by the two links are in between 0o and 90o with an increment of 15o to obtain smooth animation. The values of `x_0` and `y_0` are considered to be 0 and `x_1,y_1,x_2,y_2` are calculated by using trigonometric relations.

%Forward Kinemtacis of Robotic Arm Animation
clear all
close all
clc

%Inputs
%Assuming length of link 1 as 3m and length of link 2 as 1.5m.
l1 = 3;
l2 = 1.5;

%t1 and t2 are the angle of traverse of 1st arm and 2nd arm respectively
t1 = linspace(0,90,15); 
t2 = linspace(0,90,15);
ct = 1;

for i = 1:length(t1)
    T1 = t1(i);
    for j = 1:length(t2)
        T2 = t2(j);
        x0 = 0;
        y0 = 0;

        x1 = l1*cosd(T1);
        y1 = l1*sind(T1);

        x2 = x1 + l2*cosd(T2);
        y2 = y1 + l2*sind(T2);
        
        %Plotting
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
        axis([-0.3 5 0 5])
        title('2R Robotic Arm')
        pause(0.01)
        M(ct) = getframe(gcf);
        ct=ct+1;
    end
end

%Animation of Workspace of Arm
movie(M)
videofile = VideoWriter('Robotic_Arm.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)

Step-wise Explanation of the Code :

  1. The code starts with clear all, close all and clc where,
  • ‘clear all’ command is used to clear the data stored in the workspace.
  • ‘close all’ command is used to close all plots and figures.
  • ‘clc’ command is used to clear the command window.
  1. The input values of the links `l_1` and `l_2` and their respective angle of traverse t1 and t2 are assumed. Here ‘linspace’ command is used to have a constant increment of 15o between 0o and 90o for t1 and t2.
  2. Then counter ‘ct’ is initialized. Here ‘ct’ is a variable that simply increments by a specific step value in every iteration of a loop.
  3. To provide continuous motion to the Robotic Arm, ‘for’ loop is used where
  • The value of ‘i’ is going to change from 1 to the length of t1 and the value of ‘j’ is going to change from 1 to the length of t2.
  • As the values of ‘i’ and ‘j’ changes, t1 and t2 are taken from original arrays and are assigned to new variable ‘T1’ and ‘T2’ which contains single value.
  • Loop counter variable is used to access that particular value.
  • So for each value of t1 or each position of link 1, link 2 can take multiple position depending on the value of t2.
  1. Then enter the values and equations for `(x_0,y_0),(x_1,y_1),(x_2,y_2)`. Here ‘cosd(T1)’ is used instead of ‘cos(T1)’ as simply using ‘cos’ results input in radians whereas ‘cosd’ converts radians to degree. This method applies to all trigonometric functions.
  2. Now to plot the graph, use ‘plot’ command. The ‘linewidth’ command is used to in provide the thickness of the line.
  3. Then the graph axis is shifted by using ‘axis’ command to have a clear vision of the motion of the lines and the counter is incremented by 1.
  4. The ‘pause’ command is used to achieve better animation speed.
  5. Then ‘getframe’ command is used to capture the motion of the lines and stored in a variable M.
  6. By using ‘movie’ command, Matlab is going to make a movie with all the frames stored in variable M
  7. To save this movie, the ‘videofile’ command is used which has the option to store the video in an Uncompressed ‘avi’ format along with the file name.
  8. Then the video file is opened after the plot, the movie is run and after completion the video stops .

ERRORS :

  • Invalid command for axis –Solution – Use square brackets ‘[ ]’ to define matrices and input limits.
  • Undefined function ‘writevideo’ -Solution – ‘V’ in ‘writeVideo’ must be uppercase to have a valid function in Matlab.

ANIMATION :

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.