Week 3.2 - 2R Robotic Arm Challenge
AIM: Program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
OBJECTIVE: To write a codes to create an animation file of the plot by introducing commands like getframe(), movie(), open, write and close video file command in .avi extention. In this program we use nested loops to do the calculation and plotting.
THEORY: 2R Robotic Arm consists of base, two arms or kinematic links and two rotational joints. These links will have motion in them when they are programmed.
The link one will make make an angle theta1 eith respect to base, similarly link two will make an angle theta2.
FORMULA: For particular links starting from particular axis, the equation is as follows:
1) `x_0`=0, `y_0`=0 for initial point.
2) `x_1=l_1costheta_1` and `y_1=l_1sintheta_1`
3) `x_2=x_1+l_2costheta_2`and `y_2=y_1+l_2sintheta_2`
PROGRAM to create an animation file of the plot of 2R robotic arm challenge:
clear all
close all
clc
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);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'LineWidth',3)
axis([-0.1 1.5 0 1.5])
pause(0.03)
M(ct) = getframe(gcf);
ct = ct + 1;
end
end
movie(M)
videofile = VideoWriter('Robot.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
EXPLANATION:
This program has 30 lines. The line by line explanation is given below:
1) clear all: This is a command that is used to clear all workspace data.
2) close all: This is a command that is used to close any plot/graphs.
3) clc: This is a clear screen command. It clears command box.
4) l1 = 1;: The length of larger link stored as 1 units inside variable l1 (; is used terminate its operation so that the array does not appear in command window).
5) l2 = 0.5;: The length of smaller link stored as 0.5 units inside variable l2.
6) theta1 = linspace(0,90,10);: The angle of first link stores values 0, 10, 20,...., 90 in ten steps.
7) theta2 = linspace(0,90,10);: The angle of second link stores values 0, 10, 20,...., 90 in ten steps.
8) ct = 1;: Initial counter value is taken as 1. Counter is used to repeat the number of plots seperately for different outputs.
9) for i = 1 : length(theta1): First for loop of a nested loop that is to do iteration for link l1.
10) THETA1 = theta1(i);: Values of 'theta1' is stored one by one inside variable 'THETA1' inside first loop.
11) for j = 1 : length(theta2): Second for loop of a nested loop that is to do iteration for link l2. Thi nested loop is for following purpose-for 10 degree rotation of l1, we get complete 0 to 90 degree rotation of l2. Then for next 10 degree rotation of l1, we get complete 0 to 90 degree rotation of l2 until l1 has reached maximum limit of 90 degrees.
12) THETA2 = theta2(j);: Values of 'theta2' is stored one by one inside variable 'THETA2' inside second loop.
13) x0 = 0;: Value of X-axis of 1st point is fixed in variable 'x0'.
14) y0 = 0;: Value of Y-axis of 1st point is fixed in variable 'y0'.
15) x1 = l1*cosd(THETA1);: Value of X-axis for 2nd point for l1 is fixed in variable 'x1'.
16) y1 = l1*sind(THETA1);: Value of Y-axis for 2nd point for l1 is fixed in variable 'y1'.
17) x2 = x1 + l2*cosd(THETA2);: Value of X-axis for last point for l1 and l2 is fixed in variable 'x2'.
18) y2 = y1 + l2*sind(THETA2);: Value of Y-axis for last point for l1 and l2 is fixed in variable 'y2'.
19) plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'LineWidth',3): This is the step-wise plotting. It will plot from x0 to x1 in X-axis and y0 to y1 in Y-axis and so on till y2.
20) axis([-0.1 1.5 0 1.5]): An axis command used to fix the axis i.e. axis([xmin xmax ymin ymax]).
21) pause(0.03): The time interval between consecutive graphs are taken as 0.03 seconds.
22) M(ct) = getframe(gcf);: All the graphs are stored inside the array 'M' with the help of command getframe() which captures axes or figure as movie frame.
23) ct = ct + 1;: Increaments the counter 'ct' by +1 inside nested for loop which stores the graphs as a frame in array 'M(ct)'.
24) end: To end the second for loop.
25) end: To end the first for loop.
26) movie(M): It plays the movie frames in array 'M' after collecting the fram 'videofile' stores the name of movie as 'Robot.avi' as 'Uncompressed AVI' in VideoWriter() command.
28) open(videofile): Video mentioned in VideoWriter() command is opened with open() command.
29) writeVideo(videofile,M): It write video data to file stored in 'videofile'.
30) close(videofile): Created video closed after completion.
ERRORS FACED WHILE PROGRAMMING:
1) While NO errors were found in the command window, we had errors in the outputs.
2) In nested loop, the initial limit was not mentioned therefore the loops was'nt functioning later corrected.
3) The formulas mentioned inside the nested loop were initially mistyped as a result animation was not working.
4) The values of axis() was typed wrong and later corrected.
CONCLUSION:
1) The program ran succesfully.
2) The outputs shown below and as attachment as attachment given below saved as Robot.avi.
https://drive.google.com/file/d/1pWkWlNs-b4VUNW3wvj5ipFvdGzZbWKuF/view?usp=share_link