Simulating Forward Kinematics of a 2D Robotic Arm
The objective of this project is to write a program in MATLAB to simulate the forward kinematics of a 2R Robotic Arm.
Forward Kinematics
Kinematics is something where we study about motion of any object. In two joint robotic arm we use given joint values i.e., angles and length of arms, and kinematic equation to determine the end position of the tip of the robotic arm. This is referred as forward kinematics. The possible locations of the tip of robotic arm, where it can reach is termed as its workspace.
In this project we used trigonometric equations to determine the workspace of the robotic arm.
The above figure shows the pictorial representation of a 2D Robotic Arm.
Let the origin be (x0,y0). Let the length of the two links be L1 and L2 respectively, and let the links make angels ɵ1 and ɵ2 with the x-axis at first and second joints respectively.
Then,
x1 = L1 cos ɵ1; y1 = L1 sin ɵ1, and
x2 = x1 +L2 cos ɵ2; y2 = y1 + L2 sin ɵ2
Hence, for different combination of ɵ1 and ɵ2 we can get different location of the tip of the robotic arm.
Code:
% Forward Kinematic of a 2R Robotic Arm
clear all
close all
clc
% Defining Inputs
l1 = 1;
l2 = 0.8;
theta1 = linspace(0,90,15);
theta2 = linspace(0,90,15);
x0 = 0;
y0 = 0;
ct = 1;
for i=1:length(theta1)
THETA1 =theta1(i);
x1 = l1 .*cosd(THETA1);
y1 = l1 .*sind(THETA1);
for j = 1:length(theta2)
THETA2 = theta2(j);
x2 = x1 + (l2 *cosd(THETA2));
y2 = y1 + (l2 *sind(THETA2));
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2], "LineWidth",3)
axis([-1 2 0 2])
pause(0.03)
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)
Coding Steps:
Line 1-4 : Command to clear the command space as well as workspace.
Line 6-13: defining inputs, Length of links, possible values of angles of ɵ1 and ɵ2 , coordinates of origin, counter (ct)
Line 14- 17: Executing ‘for’ loop to get different individual value for ɵ1 and calculating x1 and y1
Line 20-23: Executing another ‘for’ loop to get different individual value of ɵ2 for each value of ɵ1 and calculating x2 and y2
Line 24-26 : Using ‘plot’ function to plot the lines for each individual values of angles under ‘for’ loop. Defining axis limits of the graph and pausing the each plot for 0.03 seconds.
Line 27-28: Using ‘getframe(gcf)’ to store the frame in M and increasing the value of ct each time loop executes.
Line 34- 38: Command used to make video from the captured frames in M and specifying the name and type of video file formed. Then writing the video in the video file formed using ‘writevideo’.
Video :
Errors :
No errors faced during programming except some typos which was overcome after careful observation.