Forward kinematics of a 2R- Robotic Arm
Aim - It is aimed at writing a program to simulate the movements of robotic arm to get an idea about its workspace.
Three coordinates are calculated to use them in our program. Let u1 and u2 be the length of links inclined at an angle θ1 and θ2 respectively.
(xo,yo)=(0,0) , (x1,y1)=(u1.cos(θ1),u1.sin(θ1)) , (x2,y2)=(x1+u2⋅cos(θ2),y1+u2⋅sin(θ2))
Initially ,We have written a program where one can take values of inclination of links manually which is vey cumbersome process. One has to change the values of theta1 and theta2 in program to get the different orientations of robotic arm.
Screen-shots of preliminary program, output and figure are attached below (final program will be written in insert -sample code):
Output : a single orientation of a robotic arm.
To make sure that program takes value of θ1 and θ2 automatically , we have used linspace command. Values of θ1 and θ2 are taken in between 0 and 90 degree.
FOR loop inside another for loop is used to ensure that at start we get a single value of θ1 . As program is executed further , inner FOR loop must be completed before we can get another fixed value of θ1 from starting FOR loop. From above example, for single value of θ1, we will get 10 different values of θ2. Total orientations of robotic arm = 100.
Line or plot command can be used to get the figure. Plot command allows us to pause the program which gives us different frames of the animation. Whenever only plot command is used , most recent plot command is displayed. To overcome this, Hold on command is used to ensure that plot is not overwritten.
Animation which we got from plot command and without plot command:
Increasing counter is being deployed to get different frames(figure window). Those different frame(figure window) we got from increasing counter is saved in an array M using the command getfeame(gcf). Please ensure that , frames are saved before counter is increased. Otherwise, it leads to error. Movie(M) command creates movie from different frames. Other command shown are self-explantory.
Error one get if counter is increased before frame(figure window is saved in array):
Please not movie is not created and whole program is not executed if counter is increased before saving the frames in array M.
Complete program:
%Forward kinematics of 2R robotic arm
clear all
close all
clc
%Known input parameters
u1 = 1;
u2= 0.3;
theta1 = linspace( 0 , 90, 10);
theta2 = linspace(0, 90, 10);
ct=1;
for m =1:length(theta1)
THETA1 =theta1(m)
for k= 1:length(theta2)
THETA2 = theta2(k)
%Three coordinates of robotic arm
x0 = 0;
y0= 0;
x1 = u1*cosd(THETA1);
y1= u1*sind(THETA1);
x2= x1+ u2*cosd(THETA2);
y2= y1+ u2*sind(THETA2);
%Using plot command to plot
plot([x0 x1] , [ y0 y1],[x1 x2],[y1 y2], 'linewidth' , 3)
axis([ -0.1 1.5 0 2] )
pause(0.07)
M(ct) = getframe(gcf)
ct= ct+1;
end
end
movie(M)
videofile = VideoWriter( 'forward_kinematics.avi', 'Uncompressed AVI')
open(videofile)
writeVideo(videofile, M)
close(videofile)
Link to the created animation on youtube :