FORWARD KINEMATICS OF A 2-R ROBOTIC ARM
Program Explaination-
Define the program objective by comment
Define Inputs-
- length of link 1(AB) = L1, length of link 2(BC) = L2.
- Angle made by link 1 with x-axis = theta1, Angle made by link 2 with x-axis = theta2 .
- To define angles, we use linspace command because for a particular range of motion links angles vary w.r.t x-axis.
- To create animation we req. n frames. So, in order to create a frame we define ct.
Now, as we want to lift our robotic arm from 0 drgree to 90 degrees. It could be accomplished in n steps & for each step we want same set of instructions to be performed, therefore we use 'for loop'.
for loop-
- i- defines the total range of steps in which link1 moves from initial position(0 degree) to final position (90 degree).
- IN 1:length(theta1), 1 means step1 & length (theta1) means value of i upto total values of theta1 (in our case 10).
- For each step, value of theta1 changes & to store it we create another variable 'alpha1'.
- For link2 we create another 'for loop' with variable j to store steps.
- For each step, value of theta2 also changes & to store it we create another variable 'alpha2'.
- Now, we define the coordinates of link1 as [A(x0,y0) & B(x1,y1) and the coordinates of link2 as [(x1,y1)& C (x2,y2)].
- As we want each & every position of the link to be in the frames, therefore we use plot command inside the for loop.
Plotting-
- Both the links are plotted under single plot command with desired line (link) width.
- Now we adjust the plot-axis, so that we get sufficient space to make the whole link visible.
- To see the each frame of motion from 0 to 90 degree, we use pause command, to pause the program (for desired time).
To create the animation-
- To save these n frames, we define a variable M(ct) and get the current fig. into the frame by using getframe(gcf) command. Here ct shows the nth frame.
- Every time link approches to new position, we req. new frame to capture it. Therefore, no. of frames increases one by one. (ct=ct+1).
Here, our for loop completes, Therefore we use end commond two times for two for loops each.
Saving the animation-
- To play the animation in matlab itself we use movie function along with the variable in which all frames are saved, "movie(M)".
- To create a video file that can be accessed outside the MATLAB, we create an empty videofile and save it in a variable.
- Empty videofile can be created by using VideoWriter command followed by videofile name with format extension & its type under single quote in paranthesis.
- Robotic_arms = VideoWriter ('Forward_Kinematics.avi' , 'uncompressed AVI').
- Now, we will open the empty video file by open command.
- open (Robotic_arms)
- Now, we will write the previously created animation into this videofile by writeVideo command.
- writeVideo (Robotic_arms,M)
- Now, to close the videowriter we use close command.
- close (Robotics_arms)
Error solutions-
error1 = In movie function 'm' should be of lower-case.
error2 = In VideoWriter function 'v' & 'W' should be of upper-case.
error3 = In writeVideo function 'w' should be of lower-case and 'V' should be of upper-case.
Video_link-https://youtu.be/Ay5KBRIkiFo
%To animate the 2-R robotic arm
clear all
close all
clc
%Inputs
L1 = 2;
L2 = 1;
theta1 = linspace(0,90,10);
theta2 = linspace(0,90,10);
ct = 1;
for i= 1:length(theta1)
alpha1 = theta1(i);
for j= 1:length(theta2)
alpha2 = theta2(j);
x0 = 0;
y0 = 0;
x1 = L1*cosd(alpha1);
y1 = L1*sind(alpha1);
x2 = x1 + L2*cosd(alpha2);
y2 = y1 + L2*sind(alpha2);
%Plotting
plot([x0 x1], [y0 y1],[x1 x2], [y1 y2], 'linewidth',3)
axis ([-1 4 0 4])
pause (0.03)
M(ct) = getframe (gcf);
ct = ct+1;
end
end
% To_create & save videofile.
movie (M)
Robotic_arms = VideoWriter ('forward_kinematics.avi','uncompressed AVI')
open (Robotic_arms)
writeVideo (Robotic_arms,M)
close (Robotic_arms)