MATLAB program to simulate forward kinematics of a 2R Robotic Arm
I had tried to simulate different positions of a 2 link robotic arm in first quadrant (i.e. angle of links varies from 0 to 90 degrees), using MATLAB.
Assumptions:
- Robotic arm is pivoted at the origin(0,0).
- Different links are connected to each other using hinge joint.
- Length of first link is 2 units and that of link 2 is 1.5 units.
Mathematical calculations:
The basic idea behind this is to plot both links using two end points at different angles of both links. Co-ordinates of end points can be calculated using simple trignometry at any particular angle of both links.
θ = Angle of first link with positive x-axis
ϕ = Angle of second link with positive x-axis
l1 = Length of first link
l2 = length of second link
Above image shows different dimension of robotic arm.
Co-ordinates:
x0=0
y0=0
x1 = l1*cos(θ)
y1 = l1*sin(θ)
x2 = x1 + l2*cos(ϕ)
y2 = y1 + l2*sin(ϕ )
MATLAB code:
%Program to simulate forward kinetics of a 2R robotic arm
clear all
close all
clc
% INPUT
l1 = 2; % Length of first link
l2 = 1.5; % Length of second link
theta = linspace(0,90,20) % Creating 20 different values for first angle
phi = linspace(0,90,20) % Creating 20 different values for second angle
ct = 1; % counter to save each figure in an array
for i = 1:length(theta)
THETA = theta(i);
for j = 1:length(phi)
PHI = phi(j);
x0 = 0; % x co-ordinates of first link at starting end
y0 = 0; % y co-ordinates of first link at starting end
x1 = l1*cosd(THETA); % x co-ordinates of first link at other end or start of second link
y1 = l1*sind(THETA); % y co-ordinates of first link at other end or start of second link
x2 = x1 + (l2*cosd(PHI)); % x co-ordinates of second link at other end
y2 = y1 + (l2*sind(PHI)); % y co-ordinates of second link at other end
%PLOT
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',2); %plotting both links in a single plot
axis([-0.5,4,0,4]); % setting visiblity limits of x and y axis
pause(0.01); % pause each frame of plot for 0.01 sec
N(ct) = getframe(gcf); % saving each frame in an array named as N
ct = ct + 1;
end
end
movie(N) % creating video file of animation
videofile = VideoWriter('Forward kinematics of 2link robotic arm','Uncompressed AVI');
open(videofile); % opening video file
writeVideo(videofile,N); % writing each frame in video file
close(videofile); % close video file
Output:
Link to view video simulation of robotic arm :
Output videos shows diffrent positions of robotic arm which can be use to identify how much total space robotic arm can cover o what are the maximum and minimum length of rocotic arm at which it can be operated.