Challenge

Week 2- 2R Robotic Arm Challenge

Report for Forward kinematics of a 2R Robotic Arm

1) Aim of the project 

To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm and execute the result in animation to understand better the movements of link 1 and link 2 of a 2R robotic arm.

2) Objective

a) To study the movements of manipulator that is connected to linkages.

b) To study the different angles of link 1 and link 2.

c) To plot a graph of link 1 and link 2 at the end.

d) To create a animation of movements made by link 1 and link 2 at different angles.

3) Theory

What is Kinematics?

> Kinematics is the sudy of forces that are acting on an object under a particular mechanism.

What is forward kinematics?

> Forward kinematics allows us to determine the position and orientation of the end-effector in terms of the joint variables.

Forward Kinematics of a 2R Robotic Arm

It contains 2 links and 2 rotational joints with manipulator connected at the end points of link 2

Above is the image created by paint 3D software for reference. With respect to forward kinematics we can reduce the problem as following. If θ1 and θ2 is given then what will be the final position of the manipulator? Let's find it through the simple calculations and MATlab codes (Note: Manipulator can be a welding tool or a machining tool)

4) Calculation

Step 1: Calculate the position of the link 1

The 2R robotic arm consist of base, two links (link1 and link 2) and a manipulator. Link 1 has two end points the starting point is fixed to the base of the robot and ending point is connected to link 2.

Co-ordinate of starting point of link 1 (0,0)

Co-ordinate of ending point of link 1 (L1*cosθ, L1*sinθ) (θ=θ1) (Note: X1 = L1*cosθ1, X2 = L1*sinθ1)

Step 2: Calculate the position of link 2

Here i have taken the link 2 diagram seperately to explain it in a better way. The ending point of link 1 is the starting point of link 2 at an angle of θ2 and the co-ordinates is as follows.

Co-ordinate of starting point of link 2 (x1, y1)

Co-ordinate of ending point of link 2 (x2, y2)

where wkt x2 = x1+L2*cosθ2 and y2 = y1+L2*sinθ2 (θ=θ2)

So far we have determined the co-ordinates of 3 points

1) The origin point (x0, y0).

2) The end point of the first link L1 or the starting point of the second link L2 (x1, y1).

3) The end point of the link 2 (x2, y2).

5) Programming using MATLAB Codes

% Forward kinematics of a 2R robotic arm manupulator


clear all
close all
clc

%Inputs
l1 = 1;
l2 = 0.5;

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

ct = 1;

%for loops

for i = 1:length(theta1)
    THETA1 = theta1(i);
    for j = 1:length(theta2)
        THETA2 = theta2(j);
        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

%Inserting frames in the animation to make movie

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

 6) Step wise Explanation of the code 

a) clear all, close all and clc clears workspace, closes all figures and clear command window.

b) Inputs - l1=1 and l2=0.5 are the length of link 1 and link 2.

c) theta1 = linspace(0,90,10) and theta2 = linspace(0,90,10), As we know linspace is a command that is similar to semicolon (:) theta1 and theta2 will get 10 values between 0 and 90.

d) ct = 1 ( ct is a variable that increments or decrements by a specfic step value in every iteration of loop here ct=1 is used for iterations of the frame and save it and display it in the form of a movie).

e) %for loops

for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
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
 
(A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number number of times)
 
for i = 1:length(theta1) (%using for loop for theta1 here we are using length(theta1) basically represents the number of values in theta 1 as we all know theta 1 has 10 values since theta1=linspace(0,90,10). Hence length(theta1)=10 so here the value of 'i' is going to change from 1 to theta i.e., 10)
THETA1 = theta1(i); (As the values of 'i' changes theta1 will be assigned to a new variable 'THETA1' from their original array).
 
for j = 1:length(theta2) (%using for loop for theta2 here for each value of theta1 or each position of link 1 the link 2 can take multiple position where the position of link 2 depends on theta2. Hence theta2 needs to be varied by another loop)
 
THETA2 = theta2(j); (As the values of 'j' changes theta2 will be assigned to a new variable 'THETA1' from their original array).
 
%Co-ordinates of starting point of link 1
x0 = 0;
y0 = 0;
 
%Co-ordinates of ending point of link 1 and the starting point of link 2
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
 
%Co-ordinates of ending point of link 2
x2 = x1+l2*cosd(THETA2);
y2 = y1+l2*sind(THETA2);
 
%Plotting
(plotting (x,y) basically creates a 2D line plot of data in Y versus corresponding values in X)
 
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3) 
(Here we are plotting the set of co-ordinates first we take list of X-coordinates of 2 points of link 1 that make up the line then list of Y-coordinates of 2 points of link 1. Then we take list of X-cordinates of 2 points of link 2 that make up the line and then list of Y-coordinates of 2 points of link2).
 
axis([-0.1 1.5 0 1.5]) (%the limits given to x-axis and y-axis)
pause(0.03) (%To pause the animation with the given speed to visualize the movements of the arm even more clear)
M(ct) = getframe(gcf); (%To create a movie we are using this where to get individual frames and save it in an array called 'M')
ct = ct+1; (%Increment of the counter)
end (%end for 'j' loop)
end (%end for 'i' loop)
 
%Inserting frames in the animation to make movie
 
movie(M) (%To make a movie with the collected frames)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI'); (%Saving the movie in 'AVI' format)
open(videofile) (%opening the video file)
writeVideo(videofile,M) (%Write the frames we capture to the video file)
close(videofile) (%Finally close the video file).
 
This is how the program is executed for a forward kinematic 2R robotic manipulator if we execute this we will get an animation of the movements of link 1 and link 2 which is attached in the below link.
 
7) Errors found while programming
 
The only error i faced while programming is mistakenly i have added a closing bracket after the co-ordinates [y1 y2] hence it was not allowing to proceed the error code was "File: forward_kinematics_of_2R_robotic_arm_manipulator.m Line: 35 Column: 60
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise,
check for mismatched delimiters".
 
Steps to overcome the errors

 After couple of tries i found the error rectified it removed the closing bracket there and executed the program and the program was successfully executed.

8) Result of the program we get an animation that is attached below with my youtube link "Title of the video"= "Skill lync project (Forward kinematics of a 2R robotic arm manipulator)

https://www.youtube.com/watch?v=gFwUQwNR71A

9) References

a) Skill lync sarang videos to help understanding better of codes and the way to execute it

b) MATlab website for definitions and code understanding

c) Google for definitions and better understanding of the concept

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.