Simulation of Forward kinematics of a 2R Robotic arm using Matlab
AIM : Create a simulation of forward kinematics of 2R robotic arm using Matlab.
Theory:
Robotic arm is a mechanical arm which works on the basis of the code or program used in it.It uses the program to do the required action.Robotic arms are used in several industrial applications like casting ,machining,welding etc...Here we are dealing with a 2R robotc arm .This arm is having two rotational joints thats why it is called a 2R robotic arm.
Here the robotic arm is having two links.Link 1 is attached to the ground using a rotational joint.Let the link is at an angle of Theta1 with the ground and link1 is having a length of l1.The link2 is attached to the tip of link1 by another rotational joint,Let the link2 be at an angle oF Theta2 with the ground and the length of link2 is l2.At the tip of the link2 there is a manipulator is fitted, which may be a welding torch,machine tool or anything like that.
STEP 1
We need to calculate the position of link1
Let the starting point of link1 fixed to base of robot be (0,0).So (x0,y0) = (0,0).Since the link is at an angle of theta1 with the ground the co-ordinates of end points of link 1 is (l1cos(theta1),l1sin(theta1)).So (x1,y1) = ( l1cos(theta1),l1sin(theta1))
STEP 2
Calculation of position of link 2
The starting point of link 2 is the end point of link1.And the end point of link 2 is thus can be calculated as (x2,y2) = ((x1+l1cos(theta),(y1+l2sin(theta)).
We will plot the postions of link 1 and link 2 at various positions ,that means at differant theta values ,and then we will stitch those plots together to make an animation.
Step 3
We need to create a plot of link1 and link2 at a specific angles of theta1 and theta2.For that an intial program must be done.
Intial Program
clear all
close all
clc
% forward kinematics of 2R robotic arm simulator
%inputs
L1 = 1
L2 = .5
theta1 = 45;
theta2 = 30;
%coordinates
x0 = 0;
y0 = 0;
x1 = L1*cosd(theta1);
y1 = L1*sind(theta2);
x2 = x1+L2*cosd(theta2);
y2 = y1+L2*sind(theta2);
%plotting
line([x0 x1],[y0 y1],'linewidth',3,'color','b')
line([x1 x2],[y1 y2],'linewidth',3,'color','r')
As usual the matlab program starting with clear all,close all,clc to close all plots,clear the workspace and empty the command window.Then we use % symbol to mention the objective of the program,which is the forward kinematics of 2R robotic arm.Then we should give the inputs of the program such as length of links 1 and 2 which is equals to 1 and 0.5 meter respectively.Here an arbitrary values of theta 1 and theta 2 is taken,which is 45 and 30 degrees respectively.Since the cos and sin takes the radian values of theta,we should use cosd and sind in matlab to get values of theta in degress.For using the plot command we should input the x and y coordinates in the program.We already find out the values of (x0,y0 x2,y2 x1,y1).We need to inputs the values in the program.Then we can use line command to plot the two links.We can't use plot command since plot command only outputs the latest plot.So link 1 will go missed if we use plot command here.
Simulation Program
Here the Theta will span over a range of values.Theta 1 will be ranging from 0 to 110 and theta 2 will be ranging from 0 to 80.So we need to create plots of links at certain set of theta values and we will stitch those plots together to create an animation.We will be taking 10 values of theta between 0 to 110 and between 0 and 80 as theta 1 and theta 2 respectively and we will be creating the plots of these 10 sets of theta values.We will be executing the same set of inputs for every set of values of thetas ,so we can use for loop to make the program short.Actually we need to change the value of Theta2 from 0 to 80 for every single value of theta 1 .So we can make use of a nested for loop here to make that work.
Program
clear all
close all
clc
%inputs
L1 = 1;
L2 = .5;
theta1 = linspace(0,110,10);
theta2 = linspace(0,80,10);
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);
axis([-1 1.5 0 1.5])
pause(0.1)
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 EXPLAINATION
- Matlab program is starting with clear all,close all,clc.
- Then we need to provide the inputs such as l1,l2,theta1,theta2.Here theta will be given as range of values and we need to select some values at a particular interval,So we make use of linspace command.
- Then we need to repeat a same set of actions.So we can start with for loop here.At first,let i be varing from 1 to length of theta1(10),because we need to repeat the loop till the theta 1 completes its range.Then we specify THETA1 which is equal to values of theta at an i value.As i changes value of THETA1 is changing.
- Another for loop must be used then.Here j is varying from 1 to length of theta2 . we need to fix a value of theta1 and then repeat the same actions for range of values of theta2.Then specify the THETA2 which is equal to theta2(j).
- Then we copy and paste the same program from intial program which we need to execute inside the nested for loop.
- While plotting use plot command instead of line command.Since with hold on command the line command gives you an ugly looking animation.While using plot command make sure to use cordinates in pairs.(x0,x1) is given first then (y0,y1) then (x1,x2),(y1,y2).We will get the link 1 and link 2 is same color if we use the cordinates in a single [].So we must clearly specify the cordinates of both links in program.This must be given in single plot command since the plot command outputs only the latest value.
- Then axis is defined using axis command.Since here the link 1 will be rotaing beyond 90 degrees.x axis must be from negative side.
- Pause command is used to take a pause in between figures to give a smooth animation.
- M command is used to save the figure in an array.So we use counter to count the number of figures obtained after the execution of program each time.ct = 1 at first and each time ct must be incremented by 1.M(ct) will save the figure in an array each time ct incremented.
- Then we must gives the end for the two for loops.
- movie(M) will create a movie for the program.
- Then we need to save that movie in aVI file.This is by code videofile = VideoWriter(forward_kinematics.aVI,'Uncompressed aVI').
- Then write the file.Then close video file using close(videofile).
ERRORS FACED
matlab is case sensitive and here changing V with v solves the error.
when you use line command with hold on ,in place of plot command this will happen to the animation.This can be rectified by changing line command to plot command.
Even changing to plot command will not solve the issue completely,because the plot command only outputs the latest plot.This can be solved by using 1 plot command and input all the coordinates in pairs inside the plot command.
RESULT
The animation is created using MATLAB programming.Increasing the number of intervals in linspace command in theta value can give us a slow and smooth animation.The youtube link is provided below: