Week 3.2 - 2R Robotic Arm Challenge
MATLAB-CODE
close all %Closes all the open windows
clear all %Clears the workspace
clc %Clears the command window
%Length of first arm of robot
l1 = 8;
%Length of second arm of robot
l2 = 5;
%Angle of first arm with respect to horizontal axis
theta1 = 0:10:90;
%Angle of second of arm with respect to horizontal axis
theta2 = 0:10:90;
%Initialising the x0 and y0 as the origin or the base of the first arm
x0 = 0;
y0 = 0;
%Initialising ct - counter variable as 1 for storing of each frame of
%figure for converting the same to movie.
ct = 1;
%For each instance of theta1
for i = 1:length(theta1)
%Calculating the x1 and y1 i.e. the top of first arm
%Cosd and sind is used because the angle is in degrees
x1 = l1 * cosd(theta1(i));
y1 = l1 * sind(theta1(i));
%for Each isntace of theta2 at every instance of theta1
for j = 1:length(theta2)
%Calculating the x2 and y2 i.e. the top of second arm
%over here the second arm base is connected with the top of first
%arm
x2 = x1 + l2 * cosd(theta2(j));
y2 = y1 + l2 * sind(theta2(j));
%Plots two lines i.e. the first arm and second arm of robot on the
%x and y plot at particular angle of theta1 and theta 2
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'Linewidth',3);
%Defines the limits of x and y axis to keep the plot uniform over
%all the instances of theta1 and theta2
axis([0 15 0 15]);
%The program / code pauses for 0.1 second or hold the execution for
%few second as to visualise this properly
pause(0.1);
%getframe command helps to capture the current frameof figure
%inside of the axis
%gcf - stands for Get Current Frame i.e. the current or active
%frame is captured and stored in the M - array
M(ct)=getframe(gcf);
%Increments the ct - counter variable by 1 for next frame storing
ct = ct + 1;
end
end
%Creates the movie out of frames in M Array
% movie(M);
%videofile is the variable declared in matlab to store the video file
%VideoWriter command is used to define the name of the file with extension
%and the format of the videofile
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
%Opens the videofile i.e. enabling the matlab to write to the file
open(videofile);
%WriteVideo command is used for writing the video frames to the file
%videofile is the file variable on which the frames are to be written
%And M is the movie file or the frames of the figure from which the movie
%is to be prepared.
writeVideo(videofile,M);
%Closes the file so no tempering or other data gets stored on to the file
close(videofile);
---------------------------------------------------------------------------------------------------------------------------------------------
OUTPUT
The link to videofile uploaded on youtube is shared in the below link.
This video helps us to understand how the change in angles of the robotic arm works as there are 2 arms of different length.
It also helps us to understand the underlying the mechanics of the motion of the robotic arm.