Week 2- 2R Robotic Arm Challenge
program for simulate the forward kinematics of a 2R Robotic Arm
- lets start the program with clear all ,close all ,clc.so that every time i run the code my plots ,command window , work space should be clean.
- the codes which is written in percent is to express what is written in the code for the person who is seeing it. (for understanding it is not mandatory to use %) .
value of theta1 and theta2 i used linspace comand in the form of start digit to end digit and no of value between example linspace(0,90,10) where o is starting 90 is ending and 10 is the no of values between them.
- now using first for loop comand so that the plots that are going to marg on graph multipal time through which a video can created. second for loop is inserted so that for each position for link 1 link 2 can take multipal position. comand is inserted for the the theta2 this is because we need the arm 1 and arm 2 be independent show it angle .
- [note that alphabet once used in first loop should not should not repeat in 2nd loop this will not provide you an desirable output].
- then provide the value of x y cordinates which is 0,0.
- values on the x1, y1 should have the THETA1 in same as written in the for loop which is to be displayed as you can see in my code { if not putting proper display values can cause false digram }.
- for x2,y2 hould have the THETA2 in same as written in the for loop which is to be displayed as you can see in my code { if not putting proper display values can cause false digram }.
- important [sind and cosd is been used by me this is because the d represent the value in degree i dind put (d) after the sin,cos ill be getting the values in radian]example sind(theta1) where d represent the value in degree.
- now using of plot command to plot the values use iam using the [] bracket because the x,x1 are in the form array and an array should be in [] bracket. not using the bracket will cause you an error . plot command to plot the graph instade of line comand cause plot comand allows to plot all the values in one graph or else we will be getting multiple graph in line comand
- using axis command so that the axis should be fixed and easy to understand am using axis of 180 degree cause it look cool.
- now providing pause comand so tha you can provide desirable time pause between the each graph .
- important ct=1 provided at start of command to capture frames of the plots.
- to convert our plots in movie use m(ct)=getframe(gcf)
- where ,
- m is movie ,ct is frame of each plot , getframe comand ,gct is get current figure.
- important ,ct=ct +1 should be after the m(ct)=getframe(gcf) comand so that first frame should be complet then series of in the loop.
- end the loop 2 and then loop 1 make sure of using 2 end or els it will show you an error.
- movie(m) comand to generate movie.
- to save the movie videofile=VideoWriter('name.avi',"Uncompressed AVI)
- i faced a lot of error in the saving of videos like writevideo(m) instade of writeVideo(videofile,m)
- videofile=VideoWriter('name.avi',"Uncompressed AVI) instade of videofile=('name.avi',"Uncompressed AVI)
- important ,ct=ct +1 should be after the m(ct)=getframe(gcf) comand so that first frame should be complet then series of in the loop.i had faced lot of error in this part after watching video i got it understand.
my code
- https://drive.google.com/file/d/1VteVrp0tOaALB4x1GG6zt-3g_jpPVNHR/view?usp=sharing
clear all close all clc % program for simulation of 2r robotic arm % input % length of arms l1=10; l2=5; % angle of arm 1 and arm 2 theta1=linspace(0,180,10); theta2=linspace(0,90,10); ct=1; for i=1:length(theta1); THETA1=theta1(i); for j=1:length(theta2); THETA2=theta2(j); % X,Y IS FIXED AXIS x=0; y=0; %X1,Y1 IS START AND END OF FIRST ARM 1 x1=l1*cosd(THETA1); y1=l1*sind(THETA1); %X2,Y2 IS START AND END OF ARM 2 x2=x1 + l2*cosd(THETA2); y2=y1 + l2*sind(THETA2); % plotting the arm plot([x,x1],[y,y1],[x1,x2],[y1,y2],'LineWidth',5); axis([-15 15 0 15]); pause(0.1); m(ct)=getframe(gcf); ct=ct+1; end end movie(m) videofile=VideoWriter('week_2_robotic_arm.avi','Uncompressed AVI') open(videofile) writeVideo(videofile,m) close(videofile)
thank you