simulate the forward kinematics of a 2R Robotic Arm using MatLab
Project: simulate the forward kinematics of a 2R Robotic Arm using MatLab
Objective: 1. Generate MatLab code for 2R Robotic Arm.
2. Generate animation video fo movement of Robotic arm.
code
close all
clc
l1 = 1;
l2 = 0.5;
theta1 = linspace(0,90,20);
theta2 = linspace(0,90,20);
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);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
axis([-0.2 1.5 0 1.6]);
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 Description:
l1 = length of the first arm of a robot.
l2 = length of the second arm of a robot.
theta1 = Angle between 1st arm with a horizontal plane. linspace command is used to create angle array. here 20 angle points are created between 0 to 90 degree.
theta2 = Angle between 2nd arm with a horizontal plane. linspace command is used to create angle array. here 20 angle points are created between 0 to 90 degree.
ct = command for the count the figure number.
there are 2 for loop created for each theta. so that there is no need to separate angle input each time. the programme run in a loop for different angle values.
for each theta1 value, there are 20 theta2 values. so the total combination is 400.
x0 y0 = start point of arm 1.
x1 y1 = endpoint of arm 1 and start point of arm 2.
x2 y2 = endpoint of arm 2.
x1 x2 y1 y2 is calculated through trigonometric sin/cos relationships.
plot = command is used to plot 2 arms of a robot.
axis = command is used to adjust the axis length.
getframe = getframe command captures the current axes as it appears on the screen as a movie frame. Each frame is captured in the M variable.
ct = ct+1 frame count increased by 1 and again for loop run for different angle value.
VideoWriter(filename, profile) = creates a VideoWriter object to write video data to an AVI file additionally applies a set of properties tailored to a specific file format (such as 'MPEG-4' or 'Uncompressed AVI').
Animation: