Challenge

Week 2- 2R Robotic Arm Challenge

AIM : To create a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.

OBJECTIVES : To write the program for forward kinematics of robotic arm in matlab

To make and animation of 2R robotic arm.

CONCEPT :

A robotic arm is atype of mechanical arm which is usually controlled by programming with same fuctions as of humans arm. The arm may be the sum total of the mechanism or may be part of a more complex robot.

The links of such a manipulator are connected by joints allowing either rotational motion or linear displacement.

The links of the manipulator can be considered to form a kinematic chain. The terminus of the kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.

Firstly we drew the ase of robotic arm.. which contains 2 (l1 and l2) links and 2 rotatinal joints and shown in above figure.

Link 1 makes an angle of theta1 and

Link2 makes an angle of theta 2 wrt X axis respectibely.

Lets assume link1 = L1 and link2 = L2

Now we must calculate position of link1 . Link1 has 2 end points i.e. the starting point and ending point.

startting point is fixed to the base and end point is connectted to link2

so the co ordinates of starting point are (0,0) and co ordinates of end point are (L cos theta and L sind theta ) respectively.

where theta = theta1 i.e

x1 = l1cos theta1

y1 = l1sin theat1

and secondly we need to find position of link2 .

So for that we know that end point of link1 is starting point of link 2 so lets assign co ordinates(x1,y1) to this point. and lets assign co ordinates(x2,y2) to end point.

Thus,

x2 = x1+l2cos theta2

y2 = y1+l2sin theta2.

PROGRAM :

% forward kinematics of 2R robotic arm
clear all
close all
clc

%inputs
l1 = 1;
l2 = 0.5;

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);


%plot

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

PROGRAM EXPLANATION :

We started by assigning values to the inputs

l1 = 1st link length = 1m

l2 = 2nd length = 0.5m

Then we will need theta 1 and theta 2, so lets assume

theta 1 = 45

theta2 = 30

Then the co ordinates at starting point of link 1 are

x0 = 0 & y0 = 0,

x1 = l1*cosd(theta1);

y2 = l1*sind(theta1);

and

x2 = x1+l2*cosd(theta2);

y2 = y1+l2*sind(theta2);

Now after we run the program we get the values for x0 x1 x2 and y0 y1 y2

Then we proceeded to plot with plot commands,

line([x0 x1],[y0 y1],'linewidth',3,'color','b')
line([x1 x2],[y1 y2],'linewidth',3,'color','r')
After we run this program we get the following plot.
PLOT :
Now for animation of the plot for each value of theta 1 and theta 2 we make the plot and stitch all the points.
CODE :
clear all
close all
clc

%INPUTS
l1 = 1;
l2 = 0.5;
theta1 = linspace(0,90,30);
theta2 = linspace(0,90,30);

ct = 1;
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);


    %plot
    
    
    plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
    axis([-0.1 1.5 0 2])
    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)



CODE EXPLANATION :
we started with clear all ,close all and clc commands and then by assigning the inputs for theta1 and theta 2
theta1 = linspace(0,90,10)
theta2 = linspace(0,90,10)
Now tbtea1 is an array which we represented using linspace command.
we defined l1 = 1m and l2 =0.5m
For each value of theta 1 and theta2 we will use for loop
for i = length(theta1)
theta(2)
end
length(theta1) represents to numbers of value in theta 1
then ,THETA 1 = theta1(i)
THETA2 = theta2(i)
here as value for i changes we took theta1 and theat2 from their original arrays and assigned them to the variables
THETA1 & THETA2 and theta 2 is varied using another for loop.
Then we proceeded with a for loop for which the functions are going to be repeated i.e
ct = 1;
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
here ct is a variable to which we assigned the value 1 which will be store and represent each frame.
After that we determined the length and position of arm
x0 = 0;
y0 = 0;
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
x2 = x1 + l2*cosd(THETA2);
y2 = y1 + l2*sind(THETA2);
%plot
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
axis([-0.1 1.5 0 2])
pause(0.03)
M(ct) = getframe(gcf);
ct = ct+1;
end
end
We used this command to create a variable M which will store the ct value which contains the frames of each position of arm.
movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Here we made a video with above commands and movie is a fucntion used to store all the frame which we will get.
RESULTS :
The following animation of 2R robotic arm has been obtained and provided with the link below.
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.