Week 2- 2R Robotic Arm Challenge
clear all
close all
clc
%% PART 1: INPUTS
L1=1; % Length of link 1.
L2=0.6; % Length of link 2.
% Angle of rotation for each link
theta1=linspace(0,90,9); % for LINK 1.
theta2=linspace(0,120,12); % for LINK 2.
%% PART 2: Calculating coordinates of the endpoints of both the links using 'For' loop.
ct=1; % Program counter variable.
for i=1:length(theta1)
THETA1=theta1(i);
for j=1:length(theta2)
THETA2=theta2(j);
% Link 1 starting point coordinates.
X0=0;
Y0=0;
% Link 1 starting and Link 2 starting point coordinates.
X1=L1*cosd(THETA1);
Y1=L1*sind(THETA1);
% Link 1 endpoint coordinates.
X2=X1+L2*cosd(THETA2);
Y2=Y1+L2*sind(THETA2);
%plotting both links.
plot([X0 X1],[Y0 Y1],[X1 X2],[Y1 Y2],'linewidth',3)
axis([-0.6 1.8 0 1.8])
xlabel('X-Axis');
ylabel('Y-Axis');
legend('Link 1','Link 2');
M(ct)=getframe(gcf);
ct=ct+1;
end
end
%% PART 3: Animation of 2R Robotic Arm movement:
movie(M)
videofile = VideoWriter('Fd_Knmtcs.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
EXPLANATION OF THE CODE
Line 1: clears the workspace from all the variables.
Line 2: closes all the figure windows.
Line 3: Clear the command window.
After this, the code is divided into three parts:
PART 1: INPUTS:
In this part, input variables are assigned arbitrary values. Like:
- a) Length of Link 1(L1) = 1.
- b) Length of Link 1(L2) = 0.6.
In line 12, the range of movement of Link 1 is arbitrarily fixed using 'Linspace' command. This command will generate a row vector starting from '0' till '90' with exactly 9 elements in the row vector named 'theta1'.
The same is done in line 13, for Link 2 by creating row vector 'theta2'.
PART 2: Calculating coordinates of the endpoints of both the links using 'For' loop:
In this part, the coordinates of both the links are calculated corresponding to different values of theta1 and theta2. to do so, 'for' loops are used because in the present case the number for which the calculations must be repeated is known.
In line 19, The length(theta1) command will calculate the elements present in row vector theta1, and the for loop is defined, which will run from i=1 to i=length(theta1). After every iteration is completed the 'i' value will get incremented by one till it reaches the value equal to length(theta1), which is 9 in this case.
In line 20, THETA1 is another variable introduced, this variable stores the value of the angle of Link 1 stored in the ith element of the theta1 row vector.
In line 21, another 'for' is initiated which will have its loop counter(j) running from j=1 to j=length(theta2) similar to line 19. But, in this loop j will vary from 1 to 12 since there are 12 elements in theta2 row vector.
In line 22, THETA2 is similar to THETA1, this variable stores the value of the angle of Link 2 stored in the jth element of the theta2 row vector.
In lines 25 and 26, the start point of Link1 is defined by variables X0, Y0 and assigning them value '0' corresponding to the origin.
In lines 29 and 30, the endpoint of Link1 is defined by variables X1, Y1 and assigning their values are calculated using the expression mentioned. X1 and Y1 is also the start point of Link2.
In lines 33 and 34, the endpoint of Link2 is assigned and calculated as variable X2 and Y2.
In line 38, the links are plotted using the ’plot’ command. The links are plotted by joining the points calculated in the above lines. The command will be executed with correct syntax, which is:
- First, the X-coordinates of link 1, i.e. [X0 X1]
- Followed by, the Y-coordinates of link 1 i.e. [Y0 Y1]
- The X-coordinates of Link 2, [X1 X2]
- The X-coordinates of Link 2, [Y1 Y2]
- Labels to X and Y coordinate axes are assigned in the plot in lines 40 and 41.
- legend for both links is assigned in line 42.
‘Linewidth’ command is used to alter the thickness of the line joining these points, here it is increased to 3 for better observation.
Line 39, the span of the coordinate system is changed using ‘axis’ command. The values within square brackets are the extreme points corresponding to the negative X-axis, positive X-axis, negative Y-axis, positive Y-axis respectively.
In line 43, A new variable(M) is introduced. This variable is a row vector used to store the most recent plot created by the code (or the current plot) from the figure window with the help of ’getframe’ command. For each value of ’ct’ there is an element in ‘M’ which is a plot created in the previous ‘plot’ command.
In line 44, the value of ’ct’ is increased by 1.
This concludes one iteration of the loops. The loops will then repeat for new values of i and j, till both values reach their respective last values and then code will exit the loop.
When the code exits these loops the M will have 108 plots saved as frames. These frames will now be converted into an animation to see the real-time motion of the 2R Robotic arm.
PART 3: Animation of 2R Robotic Arm motion.
In this part, the plots stored in row vector M as frames will be converted into a video format. To do so:
- A function named ‘movie(M)’ is called, input to this function is the row vector M (Line 50).
- Line 51, ‘VideoWriter(filename, profile)’ function is called, with object name as ‘videofile’. This function will write video data to an AVI format. ‘filename’ specifies the name of the video file, here the file name is ‘Fd_Knmtcs.avi’. ‘Profile’ describes the type of file to create, which is ‘Uncompressed AVI’ for this case.
- Line 52, ‘open’ commands to open a VideoWriter for writing.
- Line 53, ‘WriteVideo( videofile, M)’, will write the frames stored in M into a video file. The first term in bracket(videofile) is the object where the frames will be added and the second term (M) is the place from where the frames will be picked up.
- Line 54, close(videofile), will close the video file created.
The link to the video of the animation is given below:
Error faced was once due to the wrong syntax input for function ‘movie(M)’. The solution was prompted by MATLAB itself (file name may be case sensitive) and was performed to rectify the error. A screenshot of the error is also added.
The output from this code is a video animation of the motion of the 2 links for the arbitrarily fixed range of motion. The screenshot after the complete execution of the code and playing the video file is also attached.