Week 2- 2R Robotic Arm Challenge
Our objective is to:-Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.
"I have written programme for rotation of link 1(attached to ground) about 180' & rotation of link 2(attached to link1) about 360'."
First of all I started with
clear all {this command enables to clear workspace}
close all {this command enables to close if plot is opened in background}
clc {this command enables to clear command windows}
then i have taken
a=1.2; {here "a" is link1 which i have taken 1.2 m}
b=0.7; {here "b" is link2 which i have taken 0.7 m}
fi1=linspace(0,180,20); {fi1 is matrix of 20 numbers range from 0 to 180 of equal interval by using linspace command}
fi2=linspace(0,360,20); {fi2 is matrix of 20 numbers range from 0 to 360 of equal interval by using linspace command}
then i used
counter=1; {I have assigned "counter" as 1}
for h=1:length(fi1) {In for loop i have taken h value from 1 to length(fi1) this command gives total no. of array i.e 20}
FI1=fi1(h); {Assigned each single value of fi1(h) to FI1}
for k=1:length(fi2) {Within loop 1st i have taken loop k value from 1 to length(fi2) this command gives total no. of array i.e 20}
FI2=fi2(k); {Assigned each single value of fi2(h) to FI2}
so here with one value of FI1 we have 20 value of FI2 and so on.
after that i written
x0=0; {x co-ordinate at base which is 0}
y0=0; {y co-ordinate at base which is 0}
x1=a*cosd(FI1); {x co-ordinate of link1 end & I have used "cosd" bec. link1 rotates which changes its value at every angle}
y1=a*sind(FI1); {x co-ordinate of link1 end & I have used "cosd" bec. link1 rotates which changes its value at every angle}
x2=x1+b*cosd(FI2); {same here for link 2 but its initial is end of link 1 for that i have to add link1 x co-ordinate}
y2=y1+b*sind(FI2); {same here for link 2 but its initial is end of link 1 for that i have to add link1 y co-ordinate}
then i used
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3) {this commands enable to plot datas}
axis([-2 2 -1 2]) {fixing the axes}
grid on {turn on grid lines}
pause (0.03) {wait for 0.03 seconds}
MOVIE(counter)=getframe(gcf); {MOVIE is the name within it is counter & getframe(gcf) command collect all the frames which appeared as figure1,figure2,figure3....so on and make movie of it}
counter=counter+1; {repeatation of counter}
end {end of loop k}
end {end of loop h }
finally i wrote
movie(MOVIE) {''movie'' is command and ''MOVIE'' is name of all collected plots}
videocreated=VideoWriter('360_rotation.avi','Uncompressed AVI'); {VideoWriter is command within it "360_rotation.avi" is file name & "Uncompressed AVI" is option in matlab to be selected & stored it in videocreated}
open(videocreated) {opening of videocreated}
writeVideo(videocreated,MOVIE) {this command writes MOVIE in videocreated}
close(videocreated) {closes the videocreated}
"hit run command to run the code"
% Program in Matlab to simulate the forward kinematics of a 2R Robotic Arm
% As we know 2R robotic arm contains 2-link,2-joint & base
%link1=a, link2=b, Angle w.r.t ground(for link 1) measured from horizontal=fi1,
% Angle w.r.t link1(for link 2) measured from horizontal=fi2
%Co-ordinates are x0,y0,x1,y1,x2,y2
clear all
close all
clc
a=1.2;
b=0.7;
fi1=linspace(0,180,20);
fi2=linspace(0,360,20);
counter=1;
for h=1:length(fi1)
FI1=fi1(h);
for k=1:length(fi2)
FI2=fi2(k);
x0=0;
y0=0;
x1=a*cosd(FI1);
y1=a*sind(FI1);
x2=x1+b*cosd(FI2);
y2=y1+b*sind(FI2);
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3)
axis([-2 2 -1 2])
grid on
pause (0.03)
MOVIE(counter)=getframe(gcf);
counter=counter+1;
end
end
movie(MOVIE)
videocreated=VideoWriter('360_rotation.avi','Uncompressed AVI');
open(videocreated)
writeVideo(videocreated,MOVIE)
close(videocreated)
Animation link:-https://drive.google.com/file/d/1rVwCyEL_-jYpQJaRFOkrRGXmq9eZiQxn/view?usp=sharing
{kindly let me know if link is not working please}
Errors faced while programming & steps taken to overcome those errors.
NOTE:- [for error evaluating purpose i used fi1=linspace(0,180,5); & fi2=linspace(0,180,5);]
Rise of error-1
I had written
figure(counter)
line([x0,x1],[y0,y1],'linewidth',3,'color','r')
line([x1,x2],[y1,y2],'linewidth',3,'color','b')
counter=counter+1;
"It gives 25 distinct plot each having different axes"
Solution of 1 & Rise of error-2
To overcome it I made changes as given below
figure(counter)
plot([x0,x1],[y0,y1],'linewidth',3,'color','r')
plot([x1,x2],[y1,y2],'linewidth',3,'color','b')
counter=counter+1;
"It gives 25 distinct plot each having different axes and same color of link1 and link 2" \\ can't able to distinguish
Solution of 2 & Rise of error-3
To overcome it I made changes as given below
plot([x0,x1],[y0,y1],'linewidth',3,'color','r')
plot([x1,x2],[y1,y2],'linewidth',3,'color','b')
counter=counter+1;
"It gives only 1 plot with only one graph on it with both link as same color"\\ can't able to distinguish & no animation
Solution of 3 & Rise of error-4
To overcome it I made changes as given below
hold on
plot([x0,x1],[y0,y1],'linewidth',3,'color','r')
plot([x1,x2],[y1,y2],'linewidth',3,'color','b')
pause (0.03)
counter=counter+1;
"this give me animation but all position of all links are shown only in one plot without removing previous traced path"\\plz refer error4.png file attached
Solution of 4 & Rise of error-5
To overcome it I finally made changes as given below
plot([x0,x1],[y0,y1],[x1,x2],[y1,y2],'linewidth',3)
axis([-2 2 -1 2])
grid on
pause (0.03)
counter=counter+1;
MOVIE(counter)=getframe(gcf);
end
end
movie(MOVIE)
videocreated=VideoWriter('360_rotation.avi','Uncompressed AVI');
open(videocreated)
writeVideo(videocreated,MOVIE)
close(videocreated)
"this gives me error shown in command window"\\plz refer error5.png file attached
"Finally I make change to this error to get animation"