Aim: To write a program in Matlab to simulate the forward kinematics of A 2d Robotic arm.
Introduction:
*The robotic arm consists of two links with lengths L1 and L2.
*Xo, Yo is the starting point of link 1 which is attached to the base with an angle theta1
*Link 2 starts from the ending point of link 1 at X1, Y1, therefore
X1 = L1*cosd(theta1)
Y1 = L1*sind(theta1)
*X2, Y2 is the endpoint of link2 which is at an angle theta2 with the link1
X2 = X1+L2*cosd(theta2)
Y2 = Y1+L2*sind(theta2)
Code:
clear all
close all
clc
%inputs
l1 = 2; %length of link1
l2 = 2.5; %length of link2
theta1 = linspace(0,90,10) %angle made by link1 with x_axis
theta2 = linspace(0,90,10) %angle made by link2 with x_axis
a = 1;
for i = 1:length(theta1);
THETA1 = theta1(i);
for j = 1:length(theta1);
THETA2 = theta2(j);
%coordinates
X0 = 0; %position of base of robotic arm
Y0 = 0;
X1 = l1*cosd(THETA1)%position of link1
Y1 = l1*sind(THETA1)
X2 = X1+l2*cosd(THETA2)%position of link2
Y2 = Y1+l2*sind(THETA2)
%plotting
plot([X0 X1],[Y0 Y1],[X1 X2],[Y1 Y2],'linewidth',5)
axis([0 10 0 15])
M(a) = getframe(gcf)
a = a+1
pause(0.5)
end
end
movie(M)
videofile = VideoWriter('forward_kinematics.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Code explanation:
1.Input parameters L1 &L2, theta1&theta2 are considered.
2. For multiple theta values we have created an array of theta values using the Linspace command.
3. To have a different value of theta2 for every value of theta1 the for loop command is used with an end command.
4.so "I" is the value that will store the number of values of theta1 and j will be the number of values that will store the values of theta2.
5.we will use the loop counter to access the values of Theta1 and Theta2 where we have different values of i and j.
6. now we have to define the positions of Xo, Yo, X1, Y1, X2, Y2.
7.we will now use the counter command which gets all the figures for different values of theta1&theta2.
8.we will use the plot command for seeing different lines of line1 &line2.
9.since the plot command shows the recent plot we will use the "holdon" command to stop that.
10.at this point of execution we will be able to see different figures without animation.
11. To get the animation we will replace the counter command with a "pause" command so that we will get a figure with different colors.
12. this will be avoided by removing holdon command.
13.to start the theta1 at the particular location we have to set the location of the x-axis and y-axis.
14.to create a movie we need to save the individual frame of each position of theta1&theta2 in an array M(a), where a represents each frame.
15.By using the "get frame" command we can get the current image that is executing.
16.the code for getting individual frame is M(a) = getframe(gfc).
17.At the end of for loop we need to use a command Movie(M) to create a movie with all the frames that we created.
18.All the frames need a file that to be stored so we can create a video file using the "videowriter" command
19.to open, write and close a vedio file we used the commands "open(vediofile)", write(vediofile,M) and close(vediofile).
Video link
https://drive.google.com/file/d/1X4ZFcX53JOcTN2I1SZeQvZ7KnvgtPBNq/view?usp=sharing