Week 3.2 - 2R Robotic Arm Challenge
Aim:
Write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm, as shown in the video.
Theory:
A robotic arm is a type of mechanical arm, usually programmable, 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 (such as in an articulated robot) or translational (linear) displacement. The links of the manipulator can be considered to form a kinematic chain. The terminus of the kinematic chain of the manipulator is called the end effector and it is analogous to the human hand.
In this project, we consider a 2R robotic arm. A 2R robotic arm represents a robotic arm with two rotations. I.e. it has two links which rotate or work over a two-dimensional plane with each of links having a single degree of freedom.
Also, both the links are restricted to a 90° rotation with respect to the axes. Fig below shows the 2R robotic arm having many parts as described.

- Let, l1 & l2 be length of the link -1 & link-2 respectively.
- Let, theta 1 > be the angle formed by link-1 with horizontal.
- Let, theta 2 > be the angle formed by link-2 with horizontal.
- Let, the starting and end co-ordinates of link - 1 to be (x0, y0) and (x1, y1).
- Let, the starting and end co-ordinates of link - 2 to be (x1, y1) and (x2, y2).
Code explanation;
- Providing the input variables like.
- Predefining the length / values of the robotic arms.
- Providing the values for theta – 1, theta – 2 using linspace command for theta = 0 to 90 degrees for 15 values.
- Providing the cr (compression ratio) value as a specific digit.
- Now using ‘for’ command for iterating the values of the desired variable.
- Again using ‘for’ command for providing movement to the second robotic arm.
- Providing the eqn for the x1, y1, x2, y2 or say defining the co-ordinates of both the robotic arms.
- Plotting all the co-ordinates values and providing the axis no.
- Using the pause command to make the plot look realistic.
- Incrementing the cr or compression ratio value by +1 each time it executes for loop.
- All figures are assigned to a variable M, one at a time using cr.
- Enabling the ‘for’ loop twice.
- At end saving the movie in .avi format.
MATLAB Code:
% forward kinematic of a 2R robotic arm manipulator
clear all
close all
clc
% inputs
l1 = 1.5; % length of link 1
l2 = 1; % length of link 2
theta_1 = linspace(0,90,15); % create a row vector from 0 to 90 with 15 evently spacedpoints
theta_2 = linspace(0,90,15); % create a row vector from 0 to 90 with 15 evently spacedpoints
ct = 1;
% for loop
for i = 1:length(theta_1);
THETA_1 = theta_1(i); % the value theta 1 when iterated is stored in THETA 1
for j = 1:length(theta_2);
THETA_2 = theta_2(j); % the value theta when iterated is stored in THETA 1
% co-ordinates
x0 = 0; % base of link 1 is fixed at original
y0 = 0; % base of link 1 is fixed at original
x1 = l1 *cosd(THETA_1); % find the final co-cordinates of line 1
y1 = l1 *sind(THETA_1); % find the final co-cordinates of line 1
x2 = x1 + l2*cosd(THETA_2); % find the final co-cordinates of line 2
y2 = y1 + l2*sind(THETA_2); % find the final co-cordinates of line 2
% plotting
plot([x0 x1],[y0 y1], [x1 x2],[y1 y2],'linewidth',3);
axis([-0.5,2.5,0,2.5]);
pause(0.03);
M(ct) = getframe(gcf);
ct = ct+1;
end
end
% Animation
movie(M)
videofile = VideoWriter('Forward_kinematic_robotic_arm.avi','Uncompressed AVI')
open(videofile)
writeVideo(videofile,M)
close(videofile)


Result:
Below is the link given for the program output for movie/animation of a 2R robotic arm
https://youtu.be/J4cJxiIpRDI
Conclusion:
Thus we have simulated 2R robotic arm by using the matlab programming and also created a Movie/Animation from the plots obtained from the program