Program to simulate the forward kinematics of a 2R Robotic arm using MATLAB
Robotic arm - A robotic arm is a mechanical arm that changes its structure with the help of step motors , robotic arm is programmable and can also use motion sensors to move the arm in the right amount.
2R Robotic arm - A 2R Robotic arm means a robotic arm with two rotations i.e. it has two links which move with respect to each other.
Forward kinematics - It refers to the use of kinematics equations to calculate the movement of manipulator , given the joint angles.
This is a 2R Robotic Arm with two links link 1 and link 2 represented by L1 and L2 respectively.Angle of link 1 with base is θ1 and angle of link 2 with link 1 is θ2.
Considering the start and end co-ordinates of link 1 to be (X0, Y0) and (X1, Y1) respectively.
Similarly, the start and end co-ordinates of link 2 to be (X1, Y1) and (X2, Y2) respectively.
Where,
X0 = 0 Y0 = 0
X1 = L1. Cos (θ1) Y1=L1. Sin (θ1)
X2=X1+L2. Cos(θ2) Y2=Y1+L2. Sin(θ2)
Code Explanation :
At first the variables such as length of links , angle and coordinates are declared and values are assigned to them.
Now for loop is used to move the links at different positions .There are two for loops ,one for the link 1 and other for link 2(under the first for loop as second link move in refrence to first.
Plotting of result is done using plot command and then the plot we get is converted into a animation.
Syntax for converting the plot into animation:
file_text= sprintf(armanimation%05d.png,ct)
saveas(gca, file_text)
Here we are using saveas command instead of getframe because this program is done in octave and octave doesn't support getframe command
In the syntax gca stands for get current axes and file_text is the variable in which all images will be saved and finally file_text becomes an array of images/figures.
armanimation%05d is the file name with which each figure will get saved and %05d represents that the file will get saved with the file name which will be having 5-digits.Thus every time the for loop iterates, the output plot will get saved in the folder where the Octave Script is saved.
Finally the animation is completed with the help of imagemagick software.
CODE:
clear all
close all
clc
%inputs
%length of robotic arm
l1 = 2;
l2 = 1;
%Angle between the base and robotic arm
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
%for end loop
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);
%plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3,'color','r')
axis([-0.5 3.5 0 3.5]);
file_text=sprintf('armanimation%05d.png',ct)
saveas(gca, file_text)
pause(0)
ct = ct+1;
end
end
RESULT:
Resulting animation can be seen in the following youtube link: