Project

A program in Matlab to simulate the forward kinematics of a 2R Robotic Arm

In this project with the help of a Matlab program we have to determine the workspace of the 2R Robotic Arm. The workspace refers to the possible locations the robot can reach. In order to do this we have to analyze the forward kinematics of the 2R Robotic arm.

A 2R Robotic-arm contains 2 links and two rotational joints as shown in the figure

Now let us consider that length of link1 is `l_1` which is making an angle `theta_1` with the x-axis and the length of link2 is `l_2` which is making an angle `theta_2` with x-axis as shown in the figure.

Now consider only the link1

As shown in the figure the co-ordinates of end of the link1 which is connected with the robot body is `(X_0,Y_0)` and the coordinates of the end of the link 1 which is further connected to link2 is `(X_1,Y_1)`

Now the relation between these two ends can be written as

`X_1=X_0+l_1costheta_1`

`Y_1=Y_0+l_1sintheta_1`

Now consider only the link 2

As shown in the figure the co-ordinates of end of the link2 which is connected with the link1 is `(X_1,Y_1)` and the coordinates of the end of the link2 which is further connected to the manipulator is `(X_2,Y_2)`

Now the relation between these two ends can be written as

`X_2=X_1+l_2costheta_2`

`Y_2=Y_1+l_2sintheta_2`

Now if the values of `theta_1 & theta_2` are known to us then we can determine the location of the manipulator.

Now let us consider that `l_1 =2`m and `l_2 =1.5 m`

Now assume that both the links can rotate from `0^0-90^0`that is `0^0<=theta_1<=90^0` and `0^0<=theta_2<=90^0`

Now we have to construct two arrays namely theta1 and theta 2 which will have random values between `0^0` to `90^0`, with the help of linspace function we can do that.

Now let us consider that `(X_0,Y_0)=(0,0)`

Now our objective is to locate the position of the manipulator that is `(X_2,Y_2)`for different values of `theta_1` & `theta_2` so for this we will run a loop in which for each value of `theta_1` & `theta_2` we will plot the line joining `(X_0,Y_0) with(X_1,Y_1)and(X_1,Y_1) with (X_2,Y_2)`.

Now this will generate a plot for each value of `theta_1` & `theta_2` but in order to get the simulation we have to hold the screen and pause the code for a certain interval of time.

Now in order to get the animation we will capture the frames and store it in an array say M and then we have to convert it in a video file .

So the Matlab code for the simulation of the forward kinematics of a 2R Robotic Arm is

%Simulation the forward kinematics of a 2R Robotic Arm
clear all;
close all;
clc;

%input
l1 = 2; %length of link1 in m
l2 = 1.5;%length of link2 in m
theta1 = linspace(0,90,40);
theta2 = linspace(0,90,40);
x0 = 0;
y0 = 0;
t=1;

%plot

for i = 1:length(theta1)
    o1 = theta1(i);

    for j = 1:length(theta2)
        o2 = theta2(j);
        x1 = x0 + l1*cosd(o1);
        y1 = y0 + l1*sind(o1);
        
        x2 = x1 + l2*cosd(o2);
        y2 = y1 + l2*sind(o2);
        
        % plotting
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',2)
        axis([-0.1 3.5 0 3.5])
        pause (0.05)
        M(t)= getframe(gcf);
        t=t+1;
    end

end

%video formation

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

Output -

Now in the output animation we can see that for different values of `theta_1` & `theta_2` we can get the exact location of the manipulator.

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.