A Matlab Program to simulate the forward kinematics of a 2R Robotic Arm
Aim:- A Matlab Program to simulate the forward kinematics of a 2R Robotic Arm.
Objectives:-
- To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
- Creating an animation file of the plot.
- In this, we consider a 2R Robotic Arm and we need to calculate the position of link 1 and link 2.
Programming using MATLAB:-
clear all
close all
clc
% A matlab program to simulate the forward kinematics of a 2R Robotic Arm
% Creating an animation file of the plot
% So In this we will consider a 2R Robotic arm which has two links with
% two lengths L1 & L2 and angles theta1 & theta2.
% The calculations and inputs are as follows:-
% The inputs are
% Lenghts
L1 = 1;
L2 = 0.5;
% Angles
theta1 = linspace(0,75,10);
theta2 = linspace(0,75,10);
ct=1;
% Creating the loop
for i = 1:length(theta1)
THETA1 = theta1(i);
for j = 1:length(theta2)
THETA2 = theta2(j);
% Co-ordinates
x0 = 0;
y0 = 0;
% To Calculate the position of the Robotic arm
x1 = L1*cosd(THETA1);
y1 = L1*sind(THETA1);
x2 = x1 + L2*cosd(THETA2);
y2 = y1 + L2*sind(THETA2);
% Plotting
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
axis([-0.1 1.5 0 1.5])
pause(0.04)
M(ct) = getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile = VideoWriter('Forward_Kinematics_using_Far_Loop.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
Output:-
Explanation:-
- Forward kinematics refers to the use of the kinematic equations of a robot to compute the position of the end-effector from specified values for the joint parameters.
- So First we have taken the required inputs, lengths, angles and Co-ordinates.
- Then we used a counter command for incrementing or decrementing by a specific step value in every iteration of a loop.
- Then we created a loop for making the plot animate.
- After that we created a plot for the Robotic Arm.
- And then we paused the code and created frames in order to animate the plot according to the frames.
- Then we wrote a program to create a movie.
Errors and how to overcome them:-
- I got an error with the plot and I got only one link showing in the plot because I gave larger values to the L1 & L2. So the value must be smaller in order to get the graph.
- I got an error in the plotting after typing 'linewidth' because I forgot to avoid apostrophe after typing 'linewidth'.
Screenshot of my errors:-
Conclusion:-
So, In this we successfully calculated and simulated the forward kinematics of a 2R Robotic Arm and created an animation file of the plot.