Step-wise explanation of the code:
1. Matlab code is started with clear , close , clc command
clear all : This command is used to clear all the data stored in the workspace.
close all : This command is used close all figures and close plots.
clc : This command clears the command window.
2. The input value for θ1 and θ2 is determined with a command linspace in this we create a degree of freedom from 0 to 90 with 10 values between the degree of freedom.
3. Then initialize the counter command using ct.
4. To move the robotic arm continuously we are using for loop.
for i = 1:length(theta1)
THETA1 = theta1(i)
for j = 1:length(theta2)
THETA2 = theta2(j)
The value of 'i' is going to change from 1 to length of θ1
and value of 'j' is going to change from 1 to length of θ2. Here, as the values of 'i' & 'j' changes θ1 and θ2
are taken from original arrays and are assigned to variables 'THETA1' and 'THETA2' which contains only one value and not an array, loop counter variables are used to access particular value. For each value of θ1 i.e each position of link 1, link 2 can take multiple positions depends on θ2.
5. Then enter the values of co-ordinates ([X0 X1], [Y0 Y1],[X1 X2], [Y1 Y2]).
6. Then plot the values of ([X0 X1], [Y0 Y1],[X1 X2], [Y1 Y2]) with the 'linewidth',3.
7. Then axis command is used for fixing axes - changing x-limits and y-limits.
8. Then we use getframe command to get individual frame and stored in variable M .
09. Then incrementing the counter value by 1.
10. The end command is used to end the for loops, this terminates the set of instructions.
11. using movie command we can create a movie with the help of individual frame.
12. Using the different individual frames a new uncompressed avi file is created using videofile command.
13. Then we are opening the videofile using the open command and closing the videofile using the close command.
% Forward kinematics of a 2R Robotic Arm Manipulator
clear
close
clc
% Inputs
% Link lengths
l1 = 2; % Assuming a length of link 1 as 2m
l2 = 1.5; % Assuming a length of link 2 as 1.5m
% Angle between the links
theta1 = linspace(0,90,40); % Angle of traverse of first link
theta2 = linspace(0,90,40); % Angle of traverse of second link
ct=1;
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
X0 = 0; % x coordinate of the start point of link 1
Y0 = 0; % y coordinate of the start point of link 1
X1 = l1*cosd(THETA1); % x coordinate of the end point of link 1
Y1 = l1*sind(THETA1); % y coordinate of the end point of link 1
X2 = X1+l2*cosd(THETA2); % x coordinate of the end point of link 2
Y2 = Y1+l2*sind(THETA2); % y coordinate of the end point of link 2
% Plotting
plot([X0 X1], [Y0 Y1],[X1 X2], [Y1 Y2],'linewidth',3) % plotting the x and y axis coordinates
axis([0 4 0 4]) % plotting the x and y axis coordinates
M(ct) = getframe(gcf); % for creating the animation
ct=ct+1; %frame of increment 1
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)