Project

Simulation of forward kinematics of a 2R Robotic Arm

SIMULATION OF A 2R ROBOTIC ARM

NOTE: There are 2 simulations in the project report; 1st linked simulation is the general multi-body simulation of a robotic arm, the second linked simulation is the MATLAB obtained simulation.

A 2R (2 revolute joints) robotic arm consists of 2 links, connected with each other by a revolute joint and the first link connected to the base with the help of another revolute joint. A manipulator (wrist assembly) is atteched to the second link usually with the help of a fixed joint. Consider a robotic arm with linkage lenghts L1 and L2, angles with respect to the X-Axis `underset(1)(theta)` and `underset(2)(theta)` and link coordinates (x0,y0), (x1,y1), (x2,y2). Below is a schematic of the said robotic arm:

Determining the forward kinematics of the robotic arm i.e. understanding the motion of the linkages and the work envelope/space of the arm is the first step when using any robot, especially the ones with manipulators.

This project deals with simulating the movement (forward kinematics) of a particular robotic arm with a particular set of input dimensions.

To essentially understand how a 2R robotic arm should move, below is a multi-body simulation of one:

Google Drive Link:

Simulation, 2R Robotic arm

Youtube Link:

2R Robotic arm Simulation

The two cylinders in the above simulation are the linkages and the blue box like structure attached to the end is the manipulator.

In the above mutli-body simulation, the robotic arm has the following parameters:
L1= 4
L2= 3
`underset(1)(theta)` range (range of movement of the first link)= 45deg to 135deg (90 deg)
`underset(2)(theta)` range (range of movement of the second link)= 0deg to 180deg (180 deg)

Now, we will try to obtain this type of simulation in MATLAB.

Below is the code in MATLAB with respective elucidation:

%input variables
L1= 4; %length of first link
L2= 3; %length of second link
x0 = rand; %X-Coordinate of the first revolute joint 
y0 = rand; %Y-Coordinate of the first revolute joint
%the angle that second link makes with X-axis ranges from 0 deg to 180 deg
theta1 = linspace(45,135,10); %total 10 angles in the range of theta1
%the angle that second link makes with X-axis ranges from 0 deg to 180 deg
theta2 = linspace(0,180,30); %total 30 angles in the range of theta2

The lengths of the links are taken as 4 and 3 units. x0 and y0 denote the corrdinates of the first revolute joint in the XY plane. In this code, both of these coordinates will be randomly generated numbers between 0 and 1 ('rand').

`underset(1)(theta)` is the angle the first link makes with the X-axis,`underset(2)(theta)`is the angle the second link makes with the X-axis. As mentioned above, `underset(1)(theta)` varies from 45deg to 135deg and has a total of ten angle values in its range.
`underset(2)(theta)` varies from 0deg to 180deg and has a total of thirty angle values in its range.

Since a number of combinations between `underset(1)(theta)` and `underset(2)(theta)` are possible, there will be multiple configurations of the robotic arm i.e. multiple frames of the plot obtained. We can store each configuration/frame in a variable called ct.

ct=1; %ct denotes each frame/configuration of the robotic arm

Since for each of the 10 angles of `underset(1)(theta)`, there are 30 angles of `underset(2)(theta)`, thus we need to introduce two for loops in order to get all the possible combinations of the angles. For each combination, we need to calculate the value of coordinates (x1,y1)(coordinate of the second revolute joint) and (x2,y2)(coordinate of the manipulator).

By simple trigonometry, we can ascertain the values of these coordinates.

`x1 = x0 + L1*cos(underset(1)(theta))`
`y1 = y0 + L1*sin(underset(1)(theta))`
`x2 = x1 + L2*cos(underset(2)(theta))`
`y2 = y1 + L2*sin(underset(2)(theta))`

We will be plotting all the obtained values of the coordinates for each combination within the for loop only.

%for each of the 10 angles of theta1, there are 30 angles of theta2
for i=1:length(theta1)
    THETA1=theta1(i); %varying the values of theta1
    for j=1:length(theta2)
        THETA2=theta2(j); %varying the values of theta2
        %coordinates of the second revolute joint
        x1 = x0 + L1*cosd(THETA1); %X-Coordinate
        y1 = y0 + L1*sind(THETA1); %Y-Coordinate
        %coordinates of the manipulator
        x2 = x1 + L2*cosd(THETA2); %X-Coordinate
        y2 = y1 + L2*sind(THETA2); %Y-Coordinate
        %plotting the link coordinates
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2], 'linewidth', 3)
        axis([-10 10 -10 10])
        pause(0.05)
        xlabel('X-AXIS')
        ylabel('Y-AXIS')
        %introducing an array containing all the frames (ct) of the arm
        M(ct) = getframe(gcf)
        ct = ct+1; %moving onto the next frame/configuration after each loop
    end
end

In order to obtain a video file for the animation, we have to make an array which contains all the frames/configurations of the arm/plot. In the above code, 'M' is one such array which contains all the values of 'ct' (the variable denoting each frame/configuration).

We will have to make a video file which contains this array 'M'. For playing the animation video, we have to access this video file only.

%introducing a video file with name and type of file
videofile = VideoWriter('forward-kiematics of 2R Robot.avi', 'Uncompressed AVI')
%opening , writing and closing the video file
open(videofile) %opening the video file
writeVideo(videofile,M) %writing the frames array into the video file
close(videofile) %closing the videofile

It is found out from the command window that M is a 1x300 row matrix i.e. there are a total of 300 frames/configurations possible for the robotic arm.

Below is the animation video generated for the robotic arm

Youtube Link:
Simulation of a 2R Robotic Arm

Google Drive Link:
MATLAB simulation, 2R Robotic Arm

Below is the full code with appropriate comments:

%input variables
L1= 4; %length of first link
L2= 3; %length of second link
x0 = rand; %X-Coordinate of the first revolute joint 
y0 = rand; %Y-Coordinate of the first revolute joint
%the angle that second link makes with X-axis ranges from 0 deg to 180 deg
theta1 = linspace(45,135,10); %total 10 angles in the range of theta1
%the angle that second link makes with X-axis ranges from 0 deg to 180 deg
theta2 = linspace(0,180,30); %total 30 angles in the range of theta2
ct=1; %ct denotes each frame/configuration of the robotic arm
%for each of the 10 angles of theta1, there are 30 angles of theta2
for i=1:length(theta1)
    THETA1=theta1(i); %varying the values of theta1
    for j=1:length(theta2)
        THETA2=theta2(j); %varying the values of theta2
        %coordinates of the second revolute joint
        x1 = x0 + L1*cosd(THETA1); %X-Coordinate
        y1 = y0 + L1*sind(THETA1); %Y-Coordinate
        %coordinates of the manipulator
        x2 = x1 + L2*cosd(THETA2); %X-Coordinate
        y2 = y1 + L2*sind(THETA2); %Y-Coordinate
        %plotting the link coordinates
        plot([x0 x1],[y0 y1],[x1 x2],[y1 y2], 'linewidth', 3)
        axis([-10 10 -10 10])
        pause(0.05)
        xlabel('X-AXIS')
        ylabel('Y-AXIS')
        %introducing an array containing all the frames (ct) of the arm
        M(ct) = getframe(gcf)
        ct = ct+1; %moving onto the next frame/configuration after each loop
    end
end
%introducing a video file with name and type of file
videofile = VideoWriter('forward-kiematics of 2R Robot.avi', 'Uncompressed AVI')
%opening , writing and closing the video file
open(videofile) %opening the video file
writeVideo(videofile,M) %writing the frames array into the video file
close(videofile) %closing the videofile
        
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.