Project

Forward kinematics of a 2R - Robotic Arm

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

ii)Create an animation file of the plot.

DESCRIPTION:

Here we are going to deal with the linkage known as Forward kinematics of a 2R robotic arm(2R means 2-Rotating link i.e., 2 is a number of links and R is a rotation) using basic arms of the robot.

1. A 2R robotic arm contains 2 links and r rotational joints.

2. Length of link1 is l1 and length of link2 is l2.

3. Link1 is making an angle θ">θθ1 w.r.t x-axis and link2 is making an angle θ">θθ2 w.r.t x-axis.

Calculating the position of link1:

Link 1 has 2 end points.Starting point is fixed to the base or hinged support of the robot. and its coordinates are (X0,Y0).

Ening point is connected to link2 and its coordinates are (X1,Y1).

X0 = 0

Y0= 0

X1 = l1*cosd(θ1">θ1θ1)

Y1 = l1*cosd(θ1">θ1θ1).

Calculating the position of the Link-2.

Link2 has 2 end points. Starting point of link2 is the ending point of link1 and its coordinates are (X1,Y1).

Ending points are (X2,Y2).

X2 = X1 + l2*cosd(θ2">θ2θ2)

Y2 = Y1 + l2*cosd(θ2">θ2θ2)

Basic Program:

Basic program represents the basic idea of the plot where we get only 2D plot but no animation.

% Forward kinematics of a 2R robotic arm mainpulator
clear all
close all 
clc
% Inputs 
l1 = 1;
l2 = 0.5;

theta1 = 45;
theta2 = 30;

% Coordinates 
x0 = 0;
y0 = 0;

x1 = x0 + l1*cosd(theta1);
y1 = y0 + 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','m')

Result of the basic program:

Program for arm animation:
To do animation we are creating a new file.Here we are taking multiple values for θ1">θ1θ1 & θ2">θ2θ2.
We are input values to the angles θ1,θ2">θ1,θ2θ1,θ2 through linspace command. In linspace command first value represents starting value of the angle, second value represents end point of the angle and third point represents number of values between them.
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
To change the angle we are using for loop.
For each value of θ1">θ1θ1i.e., position of link1 , link2 can take multiple positions this position depends on θ2">θ2θ2. So,we are using two for loops for two angles.
for i = 1:length(theta1);
    THETA1 = theta1(i);
    for j= 1:length(theta2);
    THETA2 = theta2(j);
For kinematic calculation, we are copying the code from our basic program.
x0 = 0;
y0 = 0;

x1 = x0 + l1*cosd(theta1);
y1 = y0 + 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','m')
  • We got an animated plot with different colors
Matlab code for animated plot:
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);
    x0 = 0;
    y0 = 0;

    x1 = x0 + l1*cosd(THETA1)
    y1 = y0 + 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)
Creating a movie:
To create a movie type M(ct) = getframe(gcf) below the pause(0.03) and at extreme end command type movie(m).
Counters are useful programming tools, they are used to keep track of how many times a command has been executed.
ct: It represents every frame.
getfram : This command represents to getting the current figure in the gcf window.

ERRORS:

for i = 1:length(theta1);
    THETA1 = theta1(i);
    for j= 1:length(theta2);
    THETA2 = theta2(i);
We should be careful while using two for loops.Giving same loop counter 'i' to both angles doesn't provide any ouput.
video link:
https://youtu.be/1dx3RwuRFfg
Conclusion :
Hence, we have created an animated plot and a movie for 2R robotic arm using this program.
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.