Project

FORWARD KINEMATICS OF 2R ROBOTIC ARM

PROJECT NAME - FORWARD KINEMATICS OF 2R ROBOTIC ARM

AIM-

  • To develop a programme for 2R robotic arm and find out final position of robotic manipulator with respect toθ1 and θ2 (position of θ2 with respect to θ1).

OBJECTIVE-

  1. To understand kinematics of linkages and by that understanding you can easily understand how the robot will be going to move and eventually you will figure out the movement of the robot.
  2. To find out final positions of the manipulator by determining workspace of the robot.

PROCEDURE-

When you are going to learn robotics the first thing you will have is kinematics of the linkages and if you are able to understand how the kinematics will work you can easily understand how robot will be going to move if you can figure out how the robot moves then you can easily figure out its workspace.

Workspace is the possible location that the robot can reach, this is really important when people design really complex robotics such as complex robotics welding machine. So, first you are going to draw base of the robot and 2R robotic arm consists of 2 links and 2 rotational joints therefore we are first going to draw the base of the robot.

Fig - Schematic diagram of the robotic arm

Now with respect to forward kinematics problem comes as if θ1 and θ2 are given then what is the final position of the manipulator? (Manipulator can be a welding tool or it can be a machining tool).

This problem is also called as final kinematics so to solve the problem

Step 1 = calculate position of link 1.

Link 1 has 2 endpoints starting point and the end point. Starting point is fixed to the base of the robot and ending point is connected to link 2 so the starting point for link 1 is (0,0) and the ending points are Lcos(θ) and Lsin(θ) where θ = θ1

Step 2 = calculate position of link2.

Note that the starting point for link 2 is the ending point for link1 and now we will assign (x1, y1) coordinates at starting point for link 2 and assign (x2, y2) to the manipulator

Now x2 = x1 + L2 cos θ2

y2 = y1 + l2 sin θ2

So till now we have determined coordinates for 3 points (origin points) end points for the first link or starting point or the second link and end points for link 2 with these 3 coordinates I will be able to plot 3 links solution set [x0,y0], [x1,y1], [x2,y2] and when you will do programming you can do the required animation and will determine the workspace

So now we will start doing programming in MATLAB –

When you run the programme you will see graph like this-

%  Forward kinematics of 2R robotic arm manipulator 
clear all
close all
clc

% Inputs
l1 = 2;
l2 = 1;

theta1 = 45;
theta2 = 30;

% Co-ordinates
x0 = 0;
y0 = 0;

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

x2 = x1 + l2*cosd(theta2);
y2 = y1 + l2*sind(theta2);

% Plotting 
line([x0 x1], [y0 y1],'linewidth', 3,'color','r');
line([x1 x2],[y1 y2], 'linewidth', 3, 'color','k');

Fig – Manipulation of 2R robotic arm

Now you can always play with these values of theta1 and theta2 by changing their values and looking back to these changing positions of red and black lines.

But this is very simple forward kinematics simulator. we can animate the plot so for each value of theta 1 and theta 2 robotic arm takes the different position so our objective becomes to plot multiple values of the theta 1 and theta 2 and for each value we will be creating plots.

So whats the logic we will be going to use multiple values of theta (in this case 10 values and for each value of theta1 and theta2 we need to repeat the old calculations to plot different graphs of different values.)

So here we will create a programme to change the values of theta1 and theta2 automatically and for such purpose we will use loop command and in loop command we will type as,

Now to run this programme we will save it as forward_kinematics_with_for_loop.m and when you run this value of theta2 gets printed in command window.

So in above programme placing of theta1 and theta2 may be confusing to understand so what we are trying to do is that, in first line we are doing i = 1:length(theta1) refers to the numbers of values in data 1 so in command window if you type length(theta1) you will get ans as 10 which means that there are 10 values and your value of i is going to change from 1:10 (1 to 10)

So if you type in command window i = 1:10 you will get

i =

1 2 3 4 5 6 7 8 9 10

However, if you type this in for loop its value will change from 1 to 10 each and every time its value from 1 to 10 going to store only one value its not going to be an array and each time the the value changes its going to execute set of commands that are present inside the loop

i In for loop used as a loop counter i.e. how many times for loop is going to get executed and as the value of i changes you are going to take theta1 and theta2 from the original array and assign it to THETA1 and THETA2. these capital letters of THETA1 AND THETA2 are variable not an array as it contains only 1 value while theta1 and theta2 have 10 values and we need to use i to access particular value so when i = 1 we take first value of theta and when

i = 2 we take second value of theta so logically we use the value of i to take theta1 and theta2’s particular value now the key difference here is that for each position of link 1 link 2 can take multiple positions and it depends on theta 2 then in other words THETA2 can be varied using another for loop and this for loop is j.

therefore, if THETA1 = theta1(i)

then, THETA2 = theta2(j);

and as the loop counter variables along with loop starts performing its operations from here then we have to code our program from here and now what we will do is that we will paste our old programme in the for loop programme and change its theta1 and theta2 to THETA1 & THETA2 in the x1, y1, x2, y2 formula

now when you run this command you will see for each value of theta1 – theta2 changing as shown in the below figure but visualizing all this in the same plot is a bit confusing

%  Forward kinematics of 2R robotic arm manipulator 
clear all
close all
clc

% Inputs
l1 = 2;
l2 = 1;

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

for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        
% Co-ordinates
x0 = 0;
y0 = 0;

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

x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);

% Plotting 

line([x0 x1], [y0 y1], 'linewidth',  3,  'color',  'r');
line([x1 x2],[y1 y2], 'linewidth', 3, 'color', 'c');

    end
end

Fig - plot of different values of position of θ2 with respect to θ1.

So again what we can do, we will go in editor window you will type (before the start of the loop and before the end of the loop) you will type ct = 1 and ct = ct+1 and for clear visualized plot we will reduce the number of plots in linspace command –

theta1 = linspace(0,90,4) and theta2 = (0,90,4)

also we will change line command to plot command

when you will run this command you will see 16 plots for each value of theta1 and theta2 so clearly its not a good way to represent the plot, but when you run this programme you will just see one link that’s because you are using plot command and the most recent plot command will get displayed to display both the command use the hold on command so that plot don’t get overwritten and when you will run this command you will get same 16 graphs however, advantage of using plot command is if you remove figure(ct) command and add pause(0.03) [you can change the interval as per your need].

%  Forward kinematics of 2R robotic arm manipulator 
clear all
close all
clc

% Inputs
l1 = 2;
l2 = 1;

theta1 = linspace(0,90,4);
theta2 = linspace(0,90,4);
ct = 1;
for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        
% Co-ordinates
x0 = 0;
y0 = 0;

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

x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);

% Plotting 
figure(ct)
hold on
plot([x0 x1], [y0 y1], 'linewidth',  3,  'color',  'r');
plot([x1 x2],[y1 y2], 'linewidth', 3, 'color', 'c');
ct = ct +1;
    end
end
%  Forward kinematics of 2R robotic arm manipulator 
clear all
close all
clc

% Inputs
l1 = 2;
l2 = 1;

theta1 = linspace(0,90,4);
theta2 = linspace(0,90,4);
ct = 1;
for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        
% Co-ordinates
x0 = 0;
y0 = 0;

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

x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);

% Plotting 

hold on
plot([x0 x1], [y0 y1], 'linewidth',  3,  'color',  'r');
plot([x1 x2],[y1 y2], 'linewidth', 3, 'color', 'c');
pause(0.3)
ct = ct +1;
    end
end

Fig – such 16 graphs of various angles will be displayed on the screen

after second plot you will run the command you can see proper plot how theta2 changing for each value of theta1 however we still not getting what we want so if we combine those 2 plot commands into a single one when you execute this you will get same thing but a little proper animation and different colors for each and every link and also colors changing randomly.

%  Forward kinematics of 2R robotic arm manipulator 
clear all
close all
clc

% Inputs
l1 = 2;
l2 = 1;

theta1 = linspace(0,90,4);
theta2 = linspace(0,90,4);
ct = 1;
for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        
% Co-ordinates
x0 = 0;
y0 = 0;

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

x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);

% Plotting 

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

pause(0.3)
ct = ct +1;
    end
end

Fig - Combing 2 plot commands into 1

we can fix this by removing hold on command now a graph of changing values of theta2 w.r.t theta1 and to modify it bit more after plot type axis [-1 1 0 1.5] and when you run the command you can see more elaborated animated robotic arm again if you change the axis [-0.1 1.5 0 1.5]

you will see more good graph now to make it bit more interesting we can increase the resolution of theta1 and theta2

theta1 = linspace(0,90,40);

theta2 = linspace(0,90,40);and you will see more good graph and more animation of changing value and by editing pause timing you can see much smoother animations

Fig – when we remove the figure(ct) command

but using pause command pauses the complete programme and its not a good practice

so what we will do we will create a movie so after pause(0.03) command we will type ct = ct + 1 by erasing same old command from that old spot and we will reduce the number of frames from 40 to 10 and we will type m(ct) = getframe(gcf)

to get individual frames (gcf) represents the curent current figure nd now we will create the new video file where we will dump all our frames and after for loop we will type movie(m)

and now we will type

videofile = videowriter(‘forward_kinematics.avi’,’uncompressed AVI’)

open(videofile)

writevideo(videofile.m)

close(videofile)

amd you will see the animation

%  Forward kinematics of 2R robotic arm manipulator 
clear all
close all
clc

% Inputs
l1 = 1;
l2 = 0.5;

theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
ct = 1;
for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        
% Co-ordinates
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.1 1.5 0 1.5])
pause(0.03)
M(ct) = getframe(gcf);
ct = ct +1;


    end
end

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

link of the video

https://youtu.be/_nJkMjAGyeI

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.