Week 2- 2R Robotic Arm Challenge
The Forward kinematics of a 2R Robotic Arm
AIM: Write a program in MATLAB to simulate the forward kinematics of a 2R Robotic Arm.
OBJECTIVE:
1. Write a program to simulate forward kinematics of a 2R Robotic Arm.
2. To create a Movie/ Animation from the plots obtained from the program.
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 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 create a 2R robotic arm. A 2R robotic arm represents a robotic arm with two rotations. (it has two links which rotates or work over a 2D plane with each of links having a single degree of freedom. Also ,both the links are restricted to a 90 degree rotation with respect to the axis.)

- Let, L1 & L2 be the lengths of the link 1 & link 2 respectively.
- Let , ‘Theta1’ be the angle formed by link 1 and ‘Theta2’ 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)
PROGRAM EXPLANATION:
Inputs:
- Start programming in MATLAB with these three commands, as all the previous calculations are erased.
Clear all: for clears all the data stored in the workspace previously.
Close all: for closes all the previous figures and plots.
clc : for clears the command window .
- Now give inputs for the length of link 1( l1 ) as 1 meter and link 2( l2 ) as 0.5 meter, theta 1 and theta 2 as a range from 0 to 90 degrees, ‘linspace’ command is used for providing equal gap points within the range for 22 values.
- Now ‘for’ loop is used for calculating the x and y coordinates of link 1 and link 2. As ‘theta 1’ and ‘theta 2’ are arrays and contain multiple values of angle with the x-axis, therefore, we need another variable which stores only one value at a time so that we can get a set of x and y co-ordinates that are different that’s why two variables are defined for theta 1 and theta 2 that are ‘THETA1’ and ‘THETA2’.
- Now co-ordinates for the starting point of link 1 is defined and then the co-ordinates of endpoints of link 1 or the co-ordinates of the starting point of link 2 are defined. Here ‘cosd’ and ‘sind’ is used so that the values are stored in degrees.
- Now the co-ordinates of endpoint of link 2 is defined in which there is a use of co-ordinates of endpoints of link 1.
- Now plotting of all the points is done using the ‘plot’ command having a linewidth of 4 units. The labelling of the x-axis and y-axis is done with the use of ‘xlabel’ command and ‘ylabel’ command. A pause command is used for pausing the program for a certain timeframe before processing a new set of values.
- Now for creating a movie or an animation of the results a counter variable is defined before the first ‘for’ loop command as ‘ct’ and then a variable ‘M’ for capturing the current frame every time the ‘for’ command runs. Then for the increment of the frame ‘+1’ is added to the variable ‘ct’. After all this, both the ‘for’ loop are ended. Now for creating a complete movie a command ‘movie’ is used and then for assigning the file name and file format ‘VideoWriter’ command is used, the format used here is ‘ .avi’ . Then for opening the video file ‘open’ command is used and then ‘writeVideo’ command is used which writes the video with plot frames. After this, with the use of ‘close’ command the video file closed.
- Save the program in the particular location and then run the program . After the successful running of the program, an animation can also be seen running and this animation is saved in the format that assigned in the folder in which the program file is saved.
CODE FOR FORWARD KINEMATICS OF A 2R ROBOTIC ARM:
%Forward kinematics of a 2R Robotic Arm Manipulator
clear all
close all
clc
%inputs
%Link lengths
l1=1; %length of link1 as 1m
l2=0.5; %length of link2 as 0.5m
%Angle between the Links
theta1=linspace(0,90,22); %angle between x0-axis and Link1
theta2=linspace(0,90,22); %angle between x1-axis and Link2
ct=1;
for i=1:length(theta1)
THETA1=theta1(i);
for j=1:length(theta2)
THETA2=theta2(j);
x0=0; %x coordinate of the start point of Link1
y0=0; %y coordinate of the start point of Link1
x1=l1*cosd(THETA1); %x coordinate of the end point of Link1
y1=l1*sind(THETA1); %y coordinate of the end point of Link1
x2=x1+l2*cosd(THETA2); %x coordinate of the end point of Link2
y2=y1+l2*sind(THETA2); %y coordinate of the end point of Link2
%plotting
plot ([x0 x1],[y0 y1],[x1 x2],[y1 y2],'LineWidth',4) %plotting the x and y axis coordinates
axis([-0.1 2 0 2]) %plotting the value of x and y coordinates
xlabel('x_axis')
ylabel('y_axis')
pause(0.04) %pause timeframe
M(ct)=getframe(gcf); %for creating the animation
ct=ct+1;
end
end
movie(M) %creates a movie
videofile=VideoWriter('forward_kinematics.avi','Uncompressed AVI'); %assigns the file name and file form
open(videofile) %opens the video file
writeVideo(videofile,M) %writes the video with plot frame
close(videofile) %closes the videofile
ERRORS & SOLUTIONS:
1.In line 30 missed a comma(,) in between [x1 x2]&[y1 y2].

Solution: analysis it from command window and put a comma(,) in between " [x1 x2],[y1 y2] ".
2.Typing mistake in line 18 use capital (THETA2) instead of (theta2).

Solution: analysis from command window and replace (THETA2) to (theta2).
Conclusions / Results:
Thus we have simulated 2R robotic arm by using the Matlab programming and also created a Movie / Animation of a 2R robotic arm and we can see the workspace of it.
Output:
The link is given below for the program output for movie / animation of a 2R robotic arm.
https://drive.google.com/file/d/15GQXGMcC-QXKGYkpPJefVHcVBqHStnpg/view?usp=sharing
https://drive.google.com/file/d/15GQXGMcC-QXKGYkpPJefVHcVBqHStnpg/view?usp=sharing