Week 2- 2R Robotic Arm Challenge
% FORWARD KINEMATICS OF 2R ROBOTIC ARM
AIM : To Analyze the Movement of links of a Robotic arm at different positions by animation
clear
close all
clc
%Inputs
%length of link1
l1=1;
%length of link2
l2=1;
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);
%End coordinates of links
x0=0;
y0=0;
x1=l1*cosd(THETA1);
y1=l1*sind(THETA1);
x2=x1+l2*cosd(THETA2);
y2=y1+l2*sind(THETA2);
%plot
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',5)
axis([-0.1 2 0 2])
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)
EXPLANATION :
required data : length of two links (l1,l2)
end coordinates of two links
inclination of first link with respect to reference axis (theta1)& inclination of 2nd link with horizontal(theta2)
for link1 : starting coordinates (xo,yo)= (0,0)
end coordinates (x1,y1)= [ l1*cosd(theta1), l1*sind(theta1) ]
for link2 : starting coordinates =end cordinates of link 1
end coordinates = [ (x2,y2)=x1+l2*cosd(theta2),y1+l2*sind(theta2) ]
to analyze movement of links at different positions ,take an array of theta1 and theta2 and use for loop since we have multiples values of theta1 &theta2 and we need to repeat the programme for each and every value of theta1 and theta2 . store all the values of theta1 &theta2 for each iteration in variables THETA1 & THETA2 respectively
put the command counter(ct) to execute the for loop again and again
plot the both end coordinates of two links using "plot " command as per their format and provide linewidth to it
fix the axis by "axis"command and give the 'x' limits and 'y' limits
pause the code by using pause command
to save all individual frames in an array M use "getframes " command
to generate all frames in movie type in matlab type Movie(M)
to create a video using "VideoWriter" and as per its format ,type filename as "forward_kinematics.avi" and profile as ' Uncompressed AVI ' and save this in videofile
type open(videofile) to open it
type writeVideo(videofile,M) -----------> to write that the frames are captured in array M
close(videofile)-------> to close
ANIMATION FILE PLOT :
https://drive.google.com/file/d/11jHezVlaX22vGrdLNwxYzeOKrv5YDd_Z/view?usp=drivesdk
Errors found in command window :
No errors are appeared in command window.