Simulation of Forward kinematics of 2R robotic arm in MATLAB
The project involves the simulation of forward kinematics of a 2R robotic arm using MATLAB as the programming language.
Objective: To locate the points along with the forward motion in CCW direction of 2 links of varying lengths over a range of 0 to 180 deg and the corresponding visual representation of the results achieved.
Code:
% Forward kinematics of a 2R robotic arm challenge
close all
clear all
clc
%inputs
L1 = 2; % Assuming a length of link 1 as 2 m
L2 = 1; % Assuming a length of link 2 as 1 m
theta1= linspace(0,180,20); theta2= linspace(0,180,10) % consider a an angle 180 of the pick and place robot arm
%computation
ct= 1;
for i = 1:length(theta1)
THETA1 = theta1(i);
for k = 1:length(theta2)
THETA2 = theta2(k);
X0 = 0; % x coordinate of the start point of link 1
Y0 = 0; % y coordinate of the start point of link 1
X1 = L1*cosd(THETA1); % x coordinate of the end point of link 1
Y1 = L1*sind(THETA1); % y coordinate of the end point of link 1
X2 = X1 + L2*cosd(THETA2); % x coordinate of the end point of link 2
Y2 = Y1 + L2*sind(THETA2); % y coordinate of the end point of link 2
%plotting
plot([X0 X1],[Y0 Y1], [X1 X2], [Y1 Y2], 'linewidth',4); % plotting the x and y axis coordinates
axis([-3 3 0 3]) % axis to plot the values and display the limits
pause = (0.05) % pause time for each frame
xlabel('distance traversed along X axis'); ylabel('distance traversed along Y axis');
M(ct) = getframe(gcf); % for creating the animation
ct= ct+1; %frame of increment 1
end
end
movie(M)
videofile = VideoWriter( 'Forward_kinematics_challenge_week1.avi', 'uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)
A concise description of the code:
Here L1 and L2 represent the lengths of the links under consideration in the metric unit 2m and 1m respectively.
theta1 and theta2 represent the angles subtended with an equal interval of 20 and 10 respectively over a range of 0 to 180 deg.
The variable ct considers the count of each frame executed in the intervals of the angle subtended.
The for loop "i" is initiated for the first and subsequent values of theta1 for link 1 and defined. As for the variable THETA1, it represents the specific angle under consideration.
Similarly "k" takes in values for link 2 with theta2 and THETA2 for definition.
X0, X1, X2, Y0, Y1, Y2 identifies the coordinate points of the start and end points of the links 1 and 2 n the XY plane.
X0, Y0 - representing start points of link 1,
X1, Y1 - representing the endpoints of link 1/ start points of link 2.
X2, Y2 - representing the endpoints of link 2.
All these points varying for the length and angles under consideration.
Initiating the plot command represents the above-mentioned points X0 to Y2 on the graph for visual representation with a linewidth of 4.
The axis command assists one to limit the points of representation in the graph generated as output.
Command M(ct) captures each frame with the subsequent ct= ct+1 variable for the compilation of frames to generate the movie with the movie command.
The "videofile" defines the name and type of video to be created with VideoWriter for each frame and compiled.
VOILA!!!
The expected result is generated over the screen in avi format.
For the visual representation of the created program play the video attached below. Cheerio.
Kudos to the mentors at skill-lync.