Forward kinematics of a simple 2r robotic arm using matlab
- forward kinematics and simulation of a simple robotic arm using matlab
in this project the kinematics a simple robot with two linkages whose arm angles with respect to each other and base is caluculated and also the final position of the manipulator is caluculated and an animation is generated using matlab
% programme to simulate forward kinematics of a 2r robotic arm
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);
%co-ordinates
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([-0.1 1.5 0 1.5])
pause(0.03)
M(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(M)
videofile = VideoWriter('forward_kinematics2.avi','uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
REPORT:
ANIMATIONLINK:
The aim of the above programme is to simulate a 2r robotic arm and to know the position of the manipulator when the values of theta 1 and theta 2 are known.this programme also creates an animation of the robotic arm and saves it in avi format in the working directory.
- STEP WISE EXPLAINATION OF CODE
- clear all is basically used to clear/delete the variables created in Workspace
- close all is used to close all the plots
- clc clears the command window
inputs that are required to write the programme
to simulate a robotic arm the length of each link has to be known in this we have two links l1 and l2
so length of these two are considered l1=1&l2=0.5
the angle of l1 with respect to the base is taken as theta1
the angle of l2 with respect to l1 is taken as theta 2
so theta1 = linspace(0,90,10)
theta2 = linspace(0,90,10)
now for theta 1&2 linspace command is used so that theta 1&2 has 10 values each in between the angles 0 to 90 degrees.so for different values of theta 1 theta 2 also varies which gives the final position of the manipulator .
ct=1;-counter variable to show the starting iteration so that it updates its value every time the for loop is executed
for i= 1:length(theta1)
- here for loop is used so that the code inside the loop is re iterated for different values of theta and generates a plot with different positions of the arms with respect to the different values of theta
- here i value is taken as 1:length(theta1).so basically it means the variable i contains all the values of theta i.e 10 values
THETA1 = theta1(i);
- a condition is given stating that the values contained in theta1 should be multiplied with the value of i.and all these are stored in a variable called THETA1
for j= 1:length(theta2)
- here j value is taken as 1:length(theta2).so basically it means the variable i contains all the values of theta2 i.e 10 values
THETA2= theta2(j);
- a condition is given stating that the values contained in theta2 should be multiplied with the value of j.and all these are stored in a variable called THETA2
the co ordinates or the positions for various values of x and y are taken
%co-ordinates -basically it is a statement
starting co-ordinates of arm1
x0=0;
y0=0;
ending co-ordinates of arm 1
x1= l1*cosd(THETA1);
y1= l1*sind(THETA1);
- here x1 is taken as l1 times of cos multiplied with the variable THETA1,here cosd is given because by default matlab expects radian values in terms of cos so cosd basically accepts values in degrees so it is considered
- the same repeats for y1 also but here instead of cosd sind is taken
note:the ending point of arm1 will be the starting of arm 2
ending co-ordinates of arm2
x2= x1+ l2*cosd(THETA2);
y2= y1+ l2*sind(THETA2);
- the value of x2 is taken as sum of x1 and l2 times of cos of the variable THETA2.
- and y2 will also be the same replacing cos with sin
%plotting-statement
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
- plot command is used to tell matlab that you want to generate a plot.and it expects the co-ordinates of the plot in parenthesis and line width command is used to determine the thickness of the plot line in this case 3.
axis([-0.1 1.5 0 1.5])
- axis command fixes the axis of xand y with the given limits,
pause(0.03)
- pause command is used to slow down the animation for a brief time interval of 0.03 seconds
M(ct) = getframe(gcf);
- to get individual frames getframe command used and current figure window is gcf all the values are stored in an array called M(ct) where ct is the current frame
ct = ct+1;
for each iteration of for loop the counter ct value gets updated with addition of 1
end of for loop
movie(M)-this command generates a movie with all the frames
videofile = VideoWriter('forward_kinematics2.avi','uncompressed AVI');
to generate a video the videofile command is used consisting of the file name and format it needs to be is declared in the code enclosed with parenthesis
open(videofile)-opens the video file
writeVideo(videofile,M)-writes the captured frames in to the video
close(videofile)-closes the video file