Simulate the forward kinematics of a 2R Robotic Arm using MatLab
Objective- Simulation of forward kinematics of a 2R Robotic Arm using MatLab
Software used - MatLab
Introduction-
- The robotic arm consists of two links of lengths L1 and L2.
- L1 makes an angle of θ1 with the horizontal and L2 makes an angle of θ2 with the horizontal drawn from the end of L1 as shown in the figure.
- A manipulator is attached to the end of the link2 which may be a tool, a welding head etc. that can be used for various applications.
- There is a certain range up to which the arms can reach out. All these possible locations that the robot can reach is called the workspace.
Theory
Robotic Arm
A robotic arm is a type of mechanical arm, usually programmable, with similar functions to a human arm. The arm may be 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(such as in an articulated robot) or translational(linear) displacement. The links of the manipulator can be considered to form a kinematic chain. The terminus of a kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.
Application
The robotic arm is used in various applications such as
- Welding
- Painting
- Automotive Robot Solution
- Food, Beverage, Sanitation
- Package Industry
Inputs–
Consider lengths of links L1=1m, L2=0.5m.
We want the values of theta to vary from 0 to 90 degrees to observe the wide range of motion of the arm
Where-
L1 = length of link 1
L2 = length of link 2
theta1 = angle made by link 1 with horizontal
theta2 = angle made by link 2 with horizontal
.Program code with explanation-
Now we start our program by giving it a tittle comment and regular close and clc commands
%forward kinematics of a robotic arm
close all
clc
//Now the inputs values are assigned. //
%inputs
L1=1;
L2=0.5;
theta1=linspace (0,90,10);
theta2=linspace (0,90,10);
//we have used linspace command to create arrays of theta1 and theta2 which contains values from 0 to 90 degrees with increments of 10 degrees. //
%coordinates
x0=0;
y0=0;
ct=1;
//
- As we can see from the figure if the links are resolved along the horizontal and vertical axis their components are obtained which can be used to get the coordinates of x1, y1, x2, y2.
- cosd(theta1) is used instead of cos(theta1) because the input value is degrees and cosd(theta1) takes the values in degrees.
- Now for every angle of theta1 we keep the link 1 as constant and rotate link2 with all its possible values .so now for all the ten different values of theta1 we get total 100 combinations with theta2.
- So now we use a for loop initialising a variable i and making it run from 1 to length(theta1) which is 10.now we need another for loop for variation in values of theta2 so we create a loop initialising another variable j.
- So now for each value of i we have ten values of j. //
for i=1: length(theta1)
THETA1=theta1(i)
for j=1:length(theta2)
THETA2=theta2(j)
x1=L1*cosd (THETA1);
y1=L1*sind (THETA1);
x2= x1+L2*cosd(THETA2);
y2= y1+L2*sind (THETA2);
plot ([x0, x1], [y0, y1], [x1, x2], [y1, y2],'linewidth',3);
axis([-0.1 1.5 0 1.5])
pause (0.5)
M(ct)= getframe(gcf);
ct=ct+1
end
end
//
- Here we also created temporary variables THETA1, THETA2 so that when an element from arrays of theta1 and theta2 are called these values are assigned to temporary variables THETA1 and THETA2. So, with these temporary values the x1, x2, y1, y2 values are calculated.
- Now we use plot command to plot the positions of the links lines with obtained coordinates of x1, y1, x2, y2;
- So, every time when there is a change in variables i and j there will be new temporary variables THETA1 and THETA2 which will give us new coordinates x1, y1, x2, y2.
- We also select the axis range in both x and y directions.
- We use pause(time) so that the plot pauses for some time before going to the next frame.
- After that, we create an array M and use the function getframe to get all the frames and store them in M , so that we can have a complete animation. We created a variable ct and initialised it to 1. Ct is used to point out the index of the stored frame in the array.
movie(M)
videofile=VideoWriter('robotic_arm.avi');
open(videofile)
writeVideo(videofile,M);
close(videofile)
- Now we stitch all the frames together to make as a movie.
- Now using the command VideoWriter we create a videofile and give it a name, as well as the extension type like.AVI
- After creating we open the file and write the movie M into it and close the videofile.
Now in the same directory file of Matlab, the movie is created.
Program Code-
clear all
close all
clc
L1=1;
L2=0.5;
theta1=linspace(0,90,10);
theta2=linspace(0,90,10);
ct=1;
%cordinates
x0=0;
y0=0;
for i = 1:length(theta1)
THETA1=theta1(i);
for j = 1:length(theta2)
THETA2=theta2(j);
x1=L1*cosd(THETA1);
y1=L1*sind(THETA1);
x2=x1+L2*cosd(THETA2);
y2=y1+L2*sind(THETA2);
%Ploting
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3)
axis([-.1 1.5 0 1.5])
pause(.5)
M(ct)= getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile=VideoWriter('forword_kinemetics.avi');
open(videofile)
writeVideo(videofile,M);
close(videofile)
Output-
Conclusion-
The movement of 2R robotic arm is successfully simulated and in the output, the animations are displayed showing us the movement of the robotic arm from which its workspace can be tracked