Simulation of forward kinematics of 2R Robotic Arm using MATLAB.
Title: Simulation of forward kinematics of 2R Robotic Arm using MATLAB.
Aim: To write a program to simulate forward kinematics of a 2R Robotic Arm using MATLAB.
Introduction: In today's simulation has become an imporatant way for research purposes. It is used for planning, designing, analysis,etc. Simulation plays an important role in case of robotics.The commonly used tool for such simulations involves MATLAB.So, the simulation includes modelmodel preparation of the product and estimating certain outputs. This helps in understanding how the product would work, what are the difficulties, and how they could be tackled.The following report focuses on the example of a 2R Robotic Arm, its coding and possible results.
Code:
%program for robotic arm simulation
clear all
close all
clc
% input parameters
l1 = 3
l2 = 1.5
theta1 = linspace(0,90,10)
theta2 = linspace(0,90,30)
ct = 1
for i = 1:length(theta1)
THETA1 = theta1(i)
for j = 1:length(theta2)
THETA2 = theta2(j)
%co-ordinates
x0 = 0
y0 = 0
x1 = l1*cosd(THETA1);
y1 = l1*sind(THETA1);
x2 = x1+l2*cosd(THETA2);
y2 = y1+l2*sind(THETA2);
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',2);
grid on
pause(0.01)
axis([0 5 0 5])
M(ct) = getframe(gcf);
ct=ct+1;
end
end
movie(M)
videofile = VideoWriter('Robotic_Arm_challenge.avi','uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)
Explanation : Starting with the input parameters, l1 and l2 are the lengths of link 1 and link 2. theta1 is the angle by which the link 1 tends to rotate about the base while theta2 is the angle by which the link 2 tends to totate about the base.
Both the values of theta are assigned with 'linspace' command. The 'linspace' command is used to get the values of angle theta so that these are equally divided in between starting and ending values. In this case, for theta1- values are equally divided between 0 to 90 for 10 instances. Similarly for theta2 - values are equally divided between 0 to 90 for 30 instances.
After, giving inputs 'for' loops are used. This command is required in order to get the values of theta2 for every change in the values of theta1.To achieve this, looping variable i and j are assigned. 'ct' represents counter for each frame.
Now coordinates of the links should be estimated. The base is considered to be fixed hence its cordinates are (0,0) i.e x0 = 0 and y0 = 0. This cordinate serves to be the starting point of link 1. Thus, the ending point cordinates for link 1 would be (l1*cosd(theta1),l1*sind(theta2)) here, x1 = l1*cosd(theta1) and y1 = l1*sind(theta2). Similarly, (x2,y2) = ((x1+l1*cosd(theta1)),(y1+l1*sind(theta2)).
In for loop, for i = 1:length(theta1) means the 'i' would cover all the values of theta1. THETA1 is a variable and it only consist one value whereas, theta contains 10 values and acts as an array. Same is in the case of j, THETA2 and theta2.Now, plot command is used for plotting the lines for the coordinates.In order to get the plots in a single window pause command is used and 0.01 represents seconds.
Axis is used for setting the required units on x and y axes.Then for getting individual frames an array 'M' is embedded M(ct) = getframe(gcf), where, gcf represents 'get current window'.Due to this a window named 'figure 1' will be displayed.
To generate a movie with all frames Mover command is used. Further, Videofile=(VideoWriter('Robotic_Arm_challenge.avi','uncompressed AVI') is used for crating a vido having extension as .avi and the name of the file created is Robotic_Arm_challenge.Further the file is opened, frames are written and closed by the codes open, writeVideo and close respectively.
Errors faced while programming and their corrected actions :
1) Linspace command was missing:
This showed an error in command window shown below.
Corrected action: The linspace command was inserted.
2) Axis command was not inserted:
As, the axis command was not inserted, the resulting plot was unsatisfactory i.e whole mechanism was not stable.The values on X and Y axis were continuously changing.
3) Wrong Syntax at line 35:
This showed the following error in comman window.
Corrected action:
Result: The values of every coordinate at every instance of theta1 and theta 2 are displayed in command window and animation is created.
Animation :