Project

Simulation of a 2R robotic arm manipulator

Aim: Robotics is an emerging multidisciplinary field with a lot and lot of applications in the various fields, be it industrial use, research purposes or education. So the author aims to develop a tool to study a planar robotic arm with 2 links.

Governing Equations: This project does not have any specific or complex governing equations. It only uses little trigonometry to calculate the coordinates of the ends of the 2 links.

That is, `x1= l1*cos(theta1)`

`y1=l1*sin(theta1)`

`x2= x1+l2*cos(theta2)`

`y2=y1+l2*sin(theta2)`

Objective: This project targets to study the workspace of a simple planar 2R robotic arm manipulator by developing a Matlab script. This project provides a base to study the highly advanced 3-dimensional robotic arm with many degree of freedom.

For simulating the 2R robotic arm in MATLAB, following is the code:

% Forward Kinematics Animation of a 2R (having 2 links) robotic arm

% Inputs
l1=1;
l2=0.5;

theta1=linspace(0,90,20);
theta2=linspace(0,90,20);
ct=1;
% Loop is necessary if we need simulation
for i=1:length(theta1)
    THETA1=theta1(i);
    for j=1:length(theta2)
        THETA2=theta2(j);
        x0=0; y0=0; % Origin
        x1=l1*cosd(THETA1); % x coordinate of End of 1st link
        y1=l1*sind(THETA1); % y coordinate of End of 1st link

        x2=x1+l2*cosd(THETA2); % x coordinate of End of 2nd link
        y2=y1+l2*sind(THETA2); % y coordinate of End of 2nd link
        
        % Plotting
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',3)
        axis([-0.1 1.5 0 1.5])
        pause(0.03)
        
        M(ct)=getframe(gcf);
        ct=ct+1;
    end
end

% For making animation from the plot
movie(M)
videofile=VideoWriter('forward_kinematics.avi','Uncompressed AVI');
open(videofile)
writeVideo(videofile,M)
close(videofile)

For the animation of the simulation: Video link to simulation video

Explanation of code: To begin with the problem, we have to define the known constants. These are: lengths of the links of the robotic arm, it is assumed that the links can have angles between 0 and 90 degrees, and the loop counter, ct initialised to 1. We take theta1 and theta2 as row vectors representing angle values of link1 and link 2 respectively. Both these variables contain 20 identical values from 0 to 90. Now, we could have just plotted the line representing the links of the arm. But that would not change continuously and automatically. So to do this, we need a loop. 'i' is the loop variable which runs 20 times as is clear from the code. THETA1 and THETA2 are 1x1 matrices which are retrieving values from the vectors theta1 and theta2 respectively. We have defined second loop for theta2 because ew need to see all the angles orientation of link2 on a particular angle orientation of link1. Now coordinates definition. x0 and y0 is origin x1 and y1 are the coordinates of the end of link1 and start of link2; x2 and y2 are the coordinates of the end of link2. x1, y1, x2 and y2 are calculated using simple trigonometry. Now Plotting. Simple plot function is used to plot the 2 links. Range of visible axis has to be set to clearly see the simulation. This has been done by the axis function. Pause represents the time (in seconds) of waiting (to properly visualize) from one plot to other. 'M' is a matrix for frames of the resulting video/ animation. gcf- get current figure. So basically, 'M' stores all the frames of the animation with the help of 'ct'. 'ct' gets incremented next. Now the last step is to make an animation from the series of plots stored in 'M'. The 'movie' function plays the movie defined by a matrix whose columns are movie frames. VideoWriter function creates a file with the given name. Then we opened it by open function. Then we write the video stored in 'M' into the just created file. And lastly, close the created file.

Errors encountered during the execution of code: As everything was pretty much clear from the video lectures, I didn't face many errors.

I faced an error in the execution of movie function:

Then after using help toolbox, I figured out that it was just a typo. And I was able to execute the whole code by replacing "Movie()" by "movie()"

Conclusions: After looking at the results obtained from the Matlab script, further scope of the study can be identified:

  • The locus of the end of the link2 could be traced to exactly map the path of the manipulator
  • This study can be extended to more no. of links and can be extended to 3-dimension
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.