2R Robotic Arm
Aim:
To write a MATLAB program for forward Kinematic motion of a robortc arm as given in the video
CODE ATTACHEMENT :
close all
clc
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);
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',5);
axis([-0.5 2 0 2])
pause(0.3)
v(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(v)
Videofile = VideoWriter("nematic3.avi","Uncompressed AVI");
open(Videofile)
writeVideo(Videofile,v)
close(Videofile)
explining the above code in step by step manner
step 1: first clear the command window using the command clc.
step 2: second declear the length of the links and the continuous angle value as theta's as theta1 and theta2 and the counter values as 1.
step 3:here i have made two 'for' loops [with reference to the lecture video of the course].
step 4:In this first 'for' loop i have assigned a value i = 1 to the length of theta1 angle value and then i value is assigned to THETA1 = theta(i) and this same thing is assigned for second 'for' loop i have assigned j in the above same procedure and obtained THETA2.
step 5:Defining the co-ordinates of the link 1 and link 2
step 6:Then using 'plot' command plot the link in the graph as follow
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',5)
here [x0 x1],[y0 y1],[x1 x2],[y1 y2] this gives the co-ordinates of the links and the thickness of the line or link is given by ['linewidth',5] where 5 is the thickness value.
step 7: The axis command is used to define where the graph starts.
step 8: Using Pause command i set the time interval of the animation.
step 9:here [v(ct) = getframe(gcf)] this command gives the frame size and storing.
step 10: Then the counter is inceremented.
step 11: Then the for loop ends and using movie command the frames are collected
step 12: Then the file is saved in the formate AVI
step 13: Then using open command the saved file is saved
step 14: Then it is writen using writevideo command
ERROR:
I have faced only one error that the video file is not saved in the desigination and i have attached the error image and resolved it and i get the plot and animation avi file.
Final Plot:
Video link for the Simulation:
I have shared the video in my YouTube the channel name Mechons which made from the MATLAB as simulation and the link is given below ;
link : https://youtu.be/kbTXDCF88Q0
Result:
Hence I have solved the problem using MATLAB code and successfully executed .
[note : I have made one more code for getting the link 's initial position is standing and getting back to length and i have attached it ]
close all
clc
l1 = 1;
l2 = 0.5;
theta1 = linspace(90,0,10);
theta2 = linspace(90,0,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);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',5);
axis([-0.5 2 0 2])
pause(0.3)
a(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(v)
Videofile = VideoWriter('kinema1.avi','Uncompressed AVI');
open(Videofile)
writeVideo(Videofile,a)
close(Videofile)
in this code i dont even get any error code and the video link is given below
link : https://youtu.be/KLzMd70FSUk