Week 2- 2R Robotic Arm Challenge
https://drive.google.com/file/d/1ZA3N7WGmmGm72Qp8GrKSYxj6QNDU3TSB/view?usp=sharing
clear all
close all
clc
% forward kinematic of robotic arm
% inputs
l1= 1 (Length of link 1)
l2= 0.5 (Length of link 2)
theta1=45; (Angle of link 1 with x axis)
theta2=30; (Angle of link 2 with x axis)
% coordinates
x0=0; (Starting point of link 1)
y0=0; (Starting point of link 1)
x1=l1*cosd(theta1); (Ending point of link 1 & Starting point of link 2)
y1=l1*sind(theta1); (Ending point of link 1 & Starting point of link 2)
x2=x1+l2*cosd(theta2); (Ending point of link 2)
y2=y1+l2*sind(theta2); (Ending point of link 2)
%plotting
line([x0 x1],[y0 y1],'linewidth',3,'color','b'); (plot of link 1)
line([x1 x2],[y1 y2],'linewidth',3,'color','r'); (plot of link 2)
explanation:
First I done these coding and plotted a graph and checked how it came.That graph also I attaching with this .
Then I opned a new window for creating array of angles and creating a movie of the movements of links.
In that new link first I done coding like below:
clear all
close all
clc
% inputs
l1=1;
l2=0.5;
theta1=linspace(0,90,10); (Array of angles between 0 & 90deg with 10 intermediate
points)
theta2=linspace(0,90,10); (Array of angles between 0 & 90deg with 10 intermediate
points
for i=1:length(theta1) (opened for loop with loop operator 'i')
THETA1=theta1(i); ( Assigned that to a variable 'THETA 1')
for j=1:length(theta2) (Opned another for loop with loop operator 'j'))
THETA2=theta2(j); ( Assigned that to a variable 'THETA 2')
disp('hello')
end
end
Explanation:
Here I opened 2 for loops with loop with loop operators'i' &'j' beacuse here for each 'theta 1', 'theta 2' changes 10 times. So, total 100 outputs should come and that I checked by printing 'hello'.I got 'hello' printed 100 times and that screenshot also I am attaching along with this.
Then I removed hello command from there and pasted coordinate commmands from previous window and then it became'
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
line([x0 x1],[y0 y1],'linewidth',3,'color','b'); (Command for ploting link 1)
line([x1 x2],[y1 y2],'linewidth',3,'color','r'); (Command for ploting link 2)
end
end
Explanation:
here I plotted link 1 & link 2 with all angles and I got a plot with all agles for both links.I am attaching that graph along with this.Here for each angle of link1 , angle of link 2 varies 10 times.
But that plot was not good and we need to create a movie with the positions of both linlks. So, I bring some changes in that coding as given below:
Here I am writing only the changed portion of program and remaining portion of program remains the same as above.
%plotting
hold on (it will help to show both links togther in plot)
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3); (Command for plotting both links)
pause(0.03); (Here we are giving time for the pausing each frame)
ct=ct+1 ( this is for increasing each figure)
explanation:
After this change in coding I got a graph with all positions and plotting of that links we could see as an animation & that graph also I am attaching along with this.
But still I didn't get a perfect movie from that, so I tried to bring some another changes in that coding like below:
%plotting
hold on
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
pause(0.03);
M=getframes(gcf);
ct=ct+1
end
end
movie(M)
videofile=VideoWriter('forward_kinematic.AVI','Uncompressed avi');
open(videofile);
Writevideo=(videofile);
close(videofile);
Explanation:
Here I got an error like below:
Undefined function or variable 'getframes'.
Error in challenge_robotic_arm_final (line 31)
M=getframes(gcf);
Agai I tried to solve this error and finally I changed codes like below:
%plotting
hold on
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
pause(0.03);
M=getframe(gcf);
ct=ct+1
end
end
movie(M)
videofile=VideoWriter('forward_kinematic.AVI','Uncompressed avi');
open(videofile);
Writevideo=(videofile);
close(videofile);
Explanation:
Now, I got plot with all angles like a movie and that plot also I am attaching with this but still some shown as below:
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter/close (line 278)
In challenge_robotic_arm_final (line 41)
I thought my video files are not saving properly and again I tried to solve this and finally I end up with these changes
%plotting
hold on
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
pause(0.03);
M=getframe(gcf); (Here we are assigning each fram into a variable called 'M')
ct=ct+1
end
end
movie(M) (For making a movie with all of that frames)
videofile=VideoWriter('forward_kinematic.AVI','Uncompressed avi');
(Creating a videofile for dumping all that frames after execution)
open(videofile); (Open that videofile)
writeVideo(videofile,M); (Writing all frames into that videofile)
close(videofile); (Closing that videofile)
Explanation:
Now I got an animation video of position changing of both links and there is no error shown in the command window.So I am attaching that final plot also along with this.
Here 'forward_kinematic.AVI' is the file name & 'Uncompressed avi' is the profile also.
>> Link for the final video also attaching along with this