Week 2- 2R Robotic Arm Challenge
%program to understand forloopend
%2R Roboticarm
clear ;
close all;
clc ;
L1=1; %length of frist link
L2=0.7; %length of second link
the1 = linspace(0,90,10) ;
the2 = linspace(0,90,10);
ct=1;
for i=1:length(the1)
Theta1=the1(i);
for j=1:length(the2)
Theta2=the2(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 0 2]);
pause (0.45)
M(ct) = getframe(gcf);
ct = ct+1;
end
end
movie(M)
videofile = VideoWriter('2d.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
here first i submitted the result of this programe as a youtube link.
this programe showcase diffrent position of 2link or 2 seprate arm position in the 2D plane, the step to plot the point or position of the link we need some details as starting point, length of link and angle between two link and angle between surfce.
initial or staring point for link 1 is(0,0) and it have length=1mtr,second link having length=0.7mtr, now to find the end point of 1st link we use cos and sin to find x and y axis point. x axis point x1 is find using cos thita and y axis point for link 1's end point y1 by using sin thita.
x1=length of link1 *cos(thita) and y1=length of link 1 *sin(thita) in programe we use cosd(thita) and sind(thita) where d is for angle in degree if we use only cos and sin than matlab consider the value in radian.
now from that we get the end point of link 1 at any angle. the end point of link 1 is starting point for link 2 which is having length l2=0.7.
for getting the end point of link 2 we use x2 for x axis and y2 for y axis. x2=x1+l2cosd(thita) and for y2=y1+l2sind(thita)
from this we get the point but for perticular thita(angle in degree).for genrating motion we use forloop function to work with multiple array. for diffrent value of thita for both links. we use linspace for geeting value of angle. we use ct for count variable.
first we use for loop for thta 1 and feed the value of thita 1 which is array now we start second loop for 2nd link with thith 2.in that we add all value and equation for x1,y1,x2,y2. and plot the graph using plot command.foe gating each result as frame we use getframe with count variable for each result. here we use ct=ct+1 after getframe so that we can get each result in graph if we dont use it we get only end point or end positiion of the link. it dont reflect as video. here you can see i use pause command its for slow the motion in plot it is not effecting the motion of video if we dont use pause the plot is sowing quick or fast motion.it not change the speed of motion or video that we created using movie command. we use videoWriter command for giving name and format to the video. frist thing we write in bracket is name of the file and second thing we write is the format or extention of the file in matlab we have uncompressed avi and mpge. we use open and colse command to open and colse the videofile that we creat. and we use writefile in between open and colse to run tha file.