Project

2R Robotic Arm Simulation

Objective:

In this report, we shall look at the simulation of a 2-R robotic arm using MATLAB.

Theory:

A robotic arm is a type of mechanical with similar functions to a human arm; the arm may be the sum total of the mechanism or may be part of a more complex robot. The links of such a manipulator are connected by joints allowing either rotational motion or translational (linear) displacement. As robots are used widely these days ranging from the industries to automation tasks, it is important to study their kinematics, i.e. how the robots move and what areas it can reach( also called as Workspace ).

Consider the basic 2-R robotic arm with two rotational joints and 2 links so that it can move around in the 2-D cartesian plane.

As can be seen from the figure the link 1 is fixed on the base with the help of a rotational joint. Because of this, it restricts the motion of link 1 only in the circle with the center at the joint of the base. Whereas link 2 is fixed to link 1 via a rotational joint and hence it is free to rotate in a circular plane with the center at the endpoint of link 1. To study the kinematics of this robotic arm it is imperative to perform some calculations as to where the coordinates of each point of the link are. This task may seem daunting at first, but it is rather a simple trigonometry based calculation.

Consider only the first link( link 1 ).

As discussed already the link can only move in a circular plane with respect to the base where it is attached. So if we consider the base as the origin of the 2-D cartesian System, we can find that the coordinates of the endpoints of the link 1 are :

`x_1 = l_1cos(theta_1)` and `y_1 = l_1sin(theta_1)`

Now consider the link 2 which is attached to the endpoint of link 1:

Now relative to the endpoint of link 1 link 2 is going to move in a circular plane and hence the coordinates of link 2 with respect to link 1 can be found as:

`x_2 = l_2cos(theta_2)` and `y_2 = l_2sin(theta_2)`

So overall the coordinates of the endpoint of link 2 with respect to origin are given as :

`x_2 =x_1+ l_2cos(theta_2)` and `y_2 = y_1 + l_2sin(theta_2)`

Now after having found the coordinates of all the points of the 2-R robotic arm we can now plot them in MATLAB. The following code is implemented in MATLAB for the same:

clear all;
close all;
clc;

% 
% in this program I shall be simulating the 2-R robotic arm with 2 links
% and 2 joints rotating in the 2-D Cartesian plane
%

% The length of the link attached to base
l1 = 1;

% The length of the link attached to the manipulator
l2 = 0.5;

% Range of theta values for link 1
theta1 = linspace(0,90,20);

% Range of theta values for link 2
theta2 = linspace(0,90,20);

% Setting the initial loop counter to store the figures
ct = 0;

% The main simulation loop
for i = 1:numel(theta1)
    
    % Extracting the ith element from the theta1 array
    THETA1 = theta1(i);
    
    % for link 2 rotation at constant theta1
    for j = 1:numel(theta2)
        
        % We shall find all the possible positions of link 2 wrt link 1
        THETA2 = theta2(j);
        
        % Origin co-ordinates/ Begin link 1 co-ordinates
        x0 = 0;
        y0 = 0;
        
        % End link 1 co-ordinates / Begin link 2 co-ordinates
        x1 = l1*cosd(THETA1);
        y1 = l1*sind(THETA1);
        
        % End link 2 co-ordinates
        x2 = x1 + l2*cosd(THETA2);
        y2 = y1 + l2*sind(THETA2);
        
        % Updating the loop counter
        ct = ct + 1;
        
        % Now with the co-ordinates found out we can now plot them
        figure(1);clf;
        plot([x0 x1],[y0 y1],'b-','linew',3);
        hold on;
        plot([x1 x2],[y1 y2],'r-','linew',3);
        
        % Changing the axis limit to make the simulation uniform
        axis([-0.5 1.6 -0.5 1.6]);
        
        % Adding the lables
        xlabel('X-Axis');
        ylabel('Y-Axis');
        
        % Removing the tick labels
        set(gca,'XTick',[]);
        set(gca,'YTick',[]);
        
        % Adding a legend
        legend('Link 1','Link 2');
        grid off;
        set(gca,'Fontsize',15);
        title(['\theta_1 = ' num2str(THETA1) ' and  \theta_2 = ' num2str(THETA2) ]);
        
        % Pausing for some time to give the effect of simulation
        pause(0.001);
        
        % Writing this figure information in a matrix M
        M(ct) = getframe(gcf);
        
    end
    
end


% Now we can write a movie with this
videofile = VideoWriter('2-R robotic arm Simulation.avi');

% Changing the frame count so as to visualize the angles properly
videofile.FrameRate = 10;
open(videofile);
writeVideo(videofile,M);
close(videofile);


Explanation of the code:

The MATLAB program first creates a linearly spaced vector starting from 0 all the way up to 90 for both the angles. Using the for loop the positions of the links are calculated at each pair of theta's and is stored in the matrix M according to the loop counter. Finally, a movie is written out with the frames stores in M.

Some of the screenshots of the outputs at individual angles :

With the help of the MATLAB code, the following animation was obtained:

The youtube link: https://youtu.be/gMSuZthh3Gk

Errors Faced While Solving the Challenge and how to tackle them:

1.) One major error was that of the use of the linspace command:

% Range of theta values for link 1
theta1 = linspace(0:20:90);

% Range of theta values for link 2
theta2 = linspace(0:20:90);

As can be seen, the user can make a small mistake in defining the inputs of the linspace. In the above code, the inputs defined are vectors that should not be done by using the linspace command. The following error pops up in the MATLAB Command Window :

This can be easily fixed by giving the right set of inputs as scalars.

2.) Another trivial mistake that can be made by the programmer is that of mislabelling of the loop counters

% The main simulation loop
for i = 1:numel(theta1)
    
    % Extracting the ith element from the theta1 array
    THETA1 = theta1(i);
    
    % for link 2 rotation at constant theta1
    for i = 1:numel(theta2)
        
        % We shall find all the possible positions of link 2 wrt link 1
        THETA2 = theta2(j);
        
        % Origin co-ordinates/ Begin link 1 co-ordinates
        x0 = 0;
        y0 = 0;

As can be seen, the user has set the loop counter of both the loops as the same variable i. This type of practice should be avoided as it won't produce any error and it can lead to wrong calculation of the results. Though here it will not matter if both the loops have the same counter it will play a crucial role when the size of theta arrays isn't the same. This error is very small and will be hard to find for millions of lines of code. Though the practice of using the same loop counter is used for different loops ( if they are not nested ), a beginner should try to use different loop counter for each loop so it is easy to debug the code.

3.) Another common mistake is plotting the graphs with inputs in the incorrect order

Consider the MATLAB code below illustrating the same:

% Now with the co-ordinates found out we can now plot them
figure(1);clf;
plot([x0 y0],[x1 y1],'b-','linew',3);
hold on;
plot([x1 y1],[x2 y2],'r-','linew',3);

% Changing the axis limit to make the simulation uniform
axis([-0.5 1.6 -0.5 1.6]);

% Adding the lables
xlabel('X-Axis');
ylabel('Y-Axis');

% Removing the tick labels
set(gca,'XTick',[]);
set(gca,'YTick',[]);

% Adding a legend
legend('Link 1','Link 2');
grid off;
set(gca,'Fontsize',15);
title(['\theta_1 = ' num2str(THETA1) ' and  \theta_2 = ' num2str(THETA2) ]);

As it can be seen the user has thought the input to the plot will be the co-ordinates of the first point and then the co-ordinate of the second point. however, this will produce a weird plot with no physical significance. The plot obtained using this code is shown below for the reference:

4.) One more common mistake is the wrong plotting of the results:

The Beginners tend to make a mistake of plotting the results in the wrong loop. This does not produce any error, however it does not give a full idea of the simulation as the data is lost.

Consider the code as shown below:

% The main simulation loop
for i = 1:numel(theta1)
    
    % Extracting the ith element from the theta1 array
    THETA1 = theta1(i);
    
    % for link 2 rotation at constant theta1
    for j = 1:numel(theta2)
        
        % We shall find all the possible positions of link 2 wrt link 1
        THETA2 = theta2(j);
        
        % Origin co-ordinates/ Begin link 1 co-ordinates
        x0 = 0;
        y0 = 0;
        
        % End link 1 co-ordinates / Begin link 2 co-ordinates
        x1 = l1*cosd(THETA1);
        y1 = l1*sind(THETA1);
        
        % End link 2 co-ordinates
        x2 = x1 + l2*cosd(THETA2);
        y2 = y1 + l2*sind(THETA2);
        
        % Updating the loop counter
        ct = ct + 1;
        
    end
    
    % Now with the co-ordinates found out we can now plot them
    figure(1);clf;
    plot([x0 x1],[y0 y1],'b-','linew',3);
    hold on;
    plot([x1 x2],[y1 y2],'r-','linew',3);
    
    % Changing the axis limit to make the simulation uniform
    axis([-0.5 1.6 -0.5 1.6]);
    
    % Adding the lables
    xlabel('X-Axis');
    ylabel('Y-Axis');
    
    % Removing the tick labels
    set(gca,'XTick',[]);
    set(gca,'YTick',[]);
    
    % Adding a legend
    legend('Link 1','Link 2');
    grid off;
    set(gca,'Fontsize',15);
    title(['\theta_1 = ' num2str(THETA1) ' and  \theta_2 = ' num2str(THETA2) ]);
    
    % Pausing for some time to give the effect of simulation
    pause(0.001);
    
    % Writing this figure information in a matrix M
    M(ct) = getframe(gcf);
    
end

The following simulation is obtained :

The Youtube Link: https://youtu.be/9ncUN_snfrs

As can be seen, the link 2 appears as if stationary. This happens because of the fact that the plotting is done outside the inner loop. Because of this, the inner loop runs completely first without any plotting and then plots the result of that loop which is always at 90 degrees.

5.) One more mistake is not updating the loop counter to write the video file properly

Sometimes a mistake can be made in which the loop counter is not updated which doesn't cause the frames to be written and causes an error as shown below:

6.) One more very subtle error can be made :

        % Origin co-ordinates/ Begin link 1 co-ordinates
        x0 = 0;
        y0 = 0;
        
        % End link 1 co-ordinates / Begin link 2 co-ordinates
        x1 = l1*cos(THETA1);
        y1 = l1*sin(THETA1);
        
        % End link 2 co-ordinates
        x2 = x1 + l2*cos(THETA2);
        y2 = y1 + l2*sin(THETA2);

As is seen from the code, the code sin and cos are used to calculate the position of the robotic arm. This code won't produce any error however it will give unphysical results as is seen below in the animation :

The youtube link: https://youtu.be/juJrxG2ImFg

Though the animation seems okay, it doesn't justify the inputs provided. In the actual animation, we want `theta_1` to vary from 0 to 90 whereas here we see clearly it varies much more over the entire plane. This mistake can be avoided by taking proper care of the units of the angle. Either change the angles to radians or the sin to find for degrees.

Conclusion: MATLAB is a powerful tool and can provide several physical insights into the experience of the real-world phenomenon.

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.