Project

Forward kinematics of a 2R Robotic Arm

Aim:

To simulate the forward kinematics of a 2R robotic arm.

Governing Equation:

Coordinate of end of arm1 = (x1, y1)

where, `x1 = l1xxcos(theta1)` , `y1 = l1xxsin(theta1)`

Coordinate of end of arm2 = (x2, y2)

where, `x2 = x1 + l2xxcos(theta2)` , `y2 = y1 + l2xxsin(theta2)`

Objectives:

To create an animation file of the plot for varying angles.

Body of Content:

The programming is done in MATLAB. Length of arm1 and arm2 are taken as 1m and 0.5m respectively. To simulate the arms, I am varying the angles between 0 to 90 degrees with 12 steps using ‘linspace’ command for both the arms. To animate such that smaller arm2 completes its motion for each step of arm1 I need to use for loop in another for loop. All the values of angle theta1 and theta2 as obtained by ‘linspace’ command are stored in an array THETA1 and THETA2 respectively. Then using that array I have specified coordinates of end points of arm1 and arm2 using equation as given. Then comes part of plotting where I have specified end coordinates of arms and these values are updated in every iteration (because of for loop) to give a continuous relative motion. Finally to create a movie, I have created a video file with its name and type. Then I overwrote the frames generated in 'for' loop in that video file.

clear all
close all 
clc
%Inputs
l1 = 1; %lenght of first arm.
l2 = 0.5;   %length of secomd arm.
theta1 = linspace(0,90,12); % dividing angle from 0 to 90 in 12 steps
theta2 = linspace(0,90,12);

ct = 1;
for i = 1:length(theta1)
    THETA1 = theta1(i); %storing all the values of theta1 into an array THETA1.
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        x0 = 0;
        y0 = 0;

        x1 = l1*cosd(THETA1);   %x-coordinate of end of arm1
        y1 = l1*sind(THETA1);

        x2 = x1 + l2*cosd(THETA2); 
        y2 = y1 + l2*sind(THETA2);  %y-coordinate of end of arm2.
        
       %plotting
       plot([x0 x1],[y0 y1],[x1 x2],[y1 y2], 'linewidth', 3)
       axis([-0.5 1.5 0 2]) %defining end limits of axes.
       grid on
       pause(0.07) %pause for second arm.
       M(ct) = getframe(gcf);
       ct = ct+1; 
    end
    pause(0.15) %pause for first arm.

end
movie(M)
videofile = VideoWriter('Robotic_arm.avi', 'Uncompressed avi'); %writing videofile name and it's type.
open(videofile)
writeVideo(videofile, M) %overwriting created videofile with the frames generated in 
'for' loop.
close(videofile)

Errors: at first, I was unable to create a movie, later I realized that ‘w’ in ‘VideoWriter’ should be capital so then I rectified my mistake.

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.