Week 2- 2R Robotic Arm Challenge
% to simulate the forward kinematics of a 2R Robotic Arm
% to create an animation file of the plot (.avi)
% initiating the length and angle for the robotic arm
l1 = 1;
l2 = 0.5;
theta_1 = linspace(0,90,10);
theta_2 = linspace(0,90,10);
ct = 1; % frame counter
% to cover the whole workspace, we need 2 'for loops'
for i = 1:length(theta_1)
th1 = theta_1(i);
for j = 1:length(theta_2)
th2 = theta_2(j);
x0 = 0;
y0 = 0;
x1 = l1*cosd(th1);
y1 = l1*sind(th1);
x2 = x1 + l2*cosd(th2);
y2 = y1 + l2*sind(th2);
%plot graphs
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3);
axis([-0.1 1.5 0 1.5]); % fix the axis
pause(0.03); % pause time to help in generating video
M(ct) = getframe(gcf); % an array is made. each figure is saved here
ct = ct + 1; % frame counter updated after each saving of frame
end
end
% now we have all the respective images ready saved in the M(ct) array. The
% array was pre-defined for speedy operation of code.
movie(M) % we want to make a movie of M array elements or frames
videofile = VideoWriter('roboticarmmovie.avi', 'Uncompressed AVI'); % name and type is specified
open(videofile) % fiel is opened
writeVideo(videofile, M) % all the frames are written
close(videofile) % file is closed






errors:
struct to double error - solved after restart
using plot command (value not a numeric scalar)- typo error
typo error - movie(m), getframe(gcf)
animation stuck for theta values more than 20, so 10 values considered.
uninitialised frames - restart matlab to solve the problem