Week 2- 2R Robotic Arm Challenge
%%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);
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_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Google drive link (Animation of the 2R robotic arm):
https://drive.google.com/file/d/1MrENv1Jw85aUD8KSKx0nk1WtEpBsVzsJ/view?usp=sharing
Step-wise explination of the code used in this program
1- Firstly I started off using the close all, clear all and the clc command. The 'close all' command deletes all the figures whose handles are not hidden, the 'clear all' command clears a lot of other things from memory, such as breakpoints, persistent variables and cached memory, and 'clc' command clears the command window.
2- Then, I provided the Inputs used in the following program. Such as, length of both the links l1 and l2 respectfully, along with theta 1 and theta 2. I used the linspace command because theta1 and theta 2 is a array,
3- In this program two for loop commands have been used because theta 2 has to be varried using another for loop.
4- for each loop the loop counter variable must be different.I used i and j respectively.
5- provided the coordinates of x0,y0,x1,y1,x2 and y2. and then using the plot command, plotted the graph assigning a certain linewidth of 3 and a fixed axis.
6- To get individual frames saved it in a array called M(ct)=getframe command.
7- go get a movie with all the frames used movie(M). followed by the command videofile to put all the frames and given the file name and its profile as 'forward_kinematics.avi' and 'uncompressed AVI' respectfully.
8- firstly used the open command to open the video file followed by writeVideo to write the frames we captured and finally close the video file.