Project

Forward Kinematics 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 consists of 2 arms i.e. Link 1 (length l1) and Link 2 (length l2).
  • Link 1 is fixed at base coordinates [x0 y0] at an angle of "THETA 1"; Link 2 is fixed at the end of Link 1 at coordinates [x1 y1] at an angle of "THETA 2". The manipulator works as a welding tool or machining tool and is fixed at the end of Link 2.

CODE:

clear all
close all
clc

l1 = 1
l2 = 0.5

theta1 = linspace(0,90,15);
theta2 = linspace(0,90,15);

ct = 1;
for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        
        %coordinates
        
        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 2 0 2])
        pause(0.02)
        M(ct) = getframe(gcf);
        ct = ct+1
    end
end

%converting frames to a video

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

PROCEDURE:

  1. We always start with "clear all, close all, clc" at the start of any program.
  2. We give the inputs of lengths of both the Links and assign the angles as THETA 1 and 2 respectively. We use the 'LINSPACE' command to divide the range of theta values ( 0-90) into 15 divisions.
  3. "ct" is counter command for repeating every frame. First, it is 1 then it is incremented by 1 for repetitive iteration.
  4. "for" loop is used in the next step for continuous repetition of the action of the arms w.r.t the theta values.
  5. The coordinates of (x0 y0), (x1 y1), (x2 y2) are taken for plotting. x0 and y0 are the coordinates of the base of Link 1 , x1 and y1 are the coordinates of end point of Link 1 and the starting pointsof link 2 , x2 and y2 are the end point of link 2, where, x0 = 0 , y0 = 0 ; x1 = l1 × cosd(theta1) , y1 = l1 × sind(theta1) ; x2 = x1 + l2 × cosd(theta2) , y2 = y2 + l2 × sind(theta2).
  6. "plot" command helps us to plot the movement of the 2R robotic arm at each an every position.
  7. "axis" command is to specify the axes X and Y and "pause" command (in seconds) to create the movie and individual frames are saved in an array called M(ct) where ct represent each and every frame. By command movie(M) we create a movie with all frames together. videowriter command is used to create the video file. Write the video file with all frames. In the end, we close video file.

OUTPUT:

ERRORS:

Here the *WriteVideo* was case sensitive - undefined function or variable 'WriteVideo'.

Correction: "writeVideo"

Here the *writeVideo(videofile_M)* showed error - undefined function or variable 'videofile_M'.

Correction: "writeVideo(videofile,M).

Here the *THETA = theta(i)* showed error -undefined function or variable 'theta'.

Correction: "THETA = theta1(i)

Youtube link: https://www.youtube.com/watch?v=eG5HvdafLao

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.