Project

Simulate the forward kinematics of a 2R Robotic Arm using matlab

Objective- Simulation of forward kinematics of a 2R Robotic Arm using matlab

Software used - Matlab

1. Introduction-

  • The robotic arm consists of two links of lengths L1 and L2.
  • L1 makes an angle of θ1 with the horizontal and L2 makes an angle of θ2 with the horizontal drawn from the end of L1 as shown in figure.
  • A manipulator is attached to the end of the link2 which may be a tool , a welding head etc. that can be used for various applications.
  • There is a certain range up to which the arms can reach out. All these possible locations that the robot can reach is called the workspace.

2. Inputs–

Consider lengths of links L1=1m, L2=0.5m.

We want the values of theta to vary from 0 to 90 degrees to observe the wide range of motion of the arm.

3 .Program code with explanation-

Now we start our program by giving it a tittle comment and regular close and clc commands

%forward kinematics of a robotic arm

close all

clc

//Now the inputs values are assigned. //

%inputs

L1=1;

L2=0.5;

theta1=linspace (0,90,10);

theta2=linspace (0,90,10);

//we have used linspace command to create arrays of theta1 and theta2 which contains values from 0 to 90 degrees with increments of 10 degrees. //

%coordinates

x0=0;

y0=0;

ct=1;

//

  • As we can see from the figure if the links are resolved along the horizontal and vertical axis their components are obtained which can be used to get the coordinates of x1, y1, x2, y2.
  • cosd(theta1) is used instead of cos(theta1) because the input value is degrees and cosd(theta1) takes the values in degrees.
  • Now for every angle of theta1 we keep the link 1 as constant and rotate link2 with all its possible values .so now for all the ten different values of theta1 we get total 100 combinations with theta2.
  • So now we use a for loop initialising a variable i and making it run from 1 to length(theta1) which is 10.now we need another for loop for variation in values of theta2 so we create a loop initialising another variable j.
  • So now for each value of i we have ten values of j. //

for i=1: length(theta1)

THETA1=theta1(i)

for j=1:length(theta2)

THETA2=theta2(j)

x1=L1*cosd (THETA1);

y1=L1*sind (THETA1);

x2= x1+L2*cosd(THETA2);

y2= y1+L2*sind (THETA2);

plot ([x0, x1], [y0, y1], [x1, x2], [y1, y2],'linewidth',3);

axis([-0.1 1.5 0 1.5])

pause (0.5)

M(ct)= getframe(gcf);

ct=ct+1

end

end

//

  • Here we also created temporary variables THETA1, THETA2 so that when a element from arrays of theta1 and theta2 are called these values are assigned to temporary variables THETA1 and THETA2. So, with these temporary values the x1, x2, y1, y2 values are calculated.
  • Now we use plot command to plot the positions of the links lines with obtained coordinates of x1, y1, x2, y2;
  • So, every time when there is a change in variables i and j there will be new temporary variables THETA1 and THETA2 which will give us new coordinates x1, y1, x2, y2.
  • We also select the axis range in both x and y directions.
  • We use pause(time) so that the plot pauses for some time before going to the next frame .
  • After that we create an array M and use the function getframe to get all the frames and store them in M ,so that we can have a complete animation. We created a variable ct and initialised it to 1. Ct is used to point out the index of the stored frame in the array. //

movie(M)

videofile=VideoWriter('robotic_arm.avi');

open(videofile)

writeVideo(videofile,M);

close(videofile)

  • Now we stitch all the frames together to make as a movie.
  • Now using the command VideoWriter we create a videofile and give it a name,as well as the extension type like .AVI
  • After creating we open the file and write the movie M into it and close the videofile.

Now in the same directory file of matlab the movie is created.

4. Program code-

%forward kinematics of a robotic arm
close all
clc
%inputs
L1=1;
L2=0.5;
theta1=linspace(0,90,10);
theta2=linspace(0,90,10);
ct=1;
%coordinates
x0=0;
y0=0;

for i=1:length(theta1)
    THETA1=theta1(i)
    for j=1:length(theta2)
        THETA2=theta2(j)
        x1=L1*cosd(THETA1);
y1=L1*sind(THETA1);
x2= x1+L2*cosd(THETA2);
y2= y1+L2*sind(THETA2);
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3);
axis([-0.1 1.5 0 1.5])
pause(0.5)
M(ct)= getframe(gcf);
ct=ct+1;

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

5 .Output -

6. Conclusion-

The movement of 2R robotic arm is sucessfully simulated and in the output, the animations are displayed showing us the movement of the robotic arm from which its workspace can be tracked

The complete animation can be viewed by visiting the link-

https://youtu.be/Mfbtbq-xTRE

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.