Week 2- 2R Robotic Arm Challenge
Aim:
1. To write a program in Matlab to simulate the forward kinematics of a 2R Robotic Arm.
2. Create an animation file of the plot.
1. Basic:-
1.1 Concept of kinematics: In Kinematics only Velocity and Acceleration Analysis of various parts is to be done. Forces are neglected.
1.2 The origin of Robotics and linking with humans:
The links are the smallest unit of the Machine, similar to the cell in the Human Body. By a combination of the link, pair is formed and the pair together forms a chain which further causes linkage formation, and at last, the combination of Linkage (also known as Mechanism) forms Machine. The Machine is the basis of the Robotics. The simplest explanation for the origin of Robotics/machines and linking with Humans is as follows.
1.3 Mechanical linkage:-
A mechanical linkage is an assembly of bodies connected to manage forces and movement. The movement of a body, or link, is studied using geometry so the link is considered to be rigid. The connections between links are modeled as providing ideal movement, pure rotation, or sliding for example, and are called joints. A linkage modeled as a network of rigid links and ideal joints is called a Kinematic chain. For example, The basic linkage mechanism in practice is a deployable mirror linkage constructed as shown below.

1.4 Approach:-
In our Project, we are going to use the basic arms of the robot to do that we are dealing with the linkage known as Forward kinematics of a 2R Robotic arm (where 2R means 2-rotating Link i.e 2 is a number of links and R is rotation). we are taking two links as a representative of the Robotic arm in which Link-1 is Hinged at (X0, Y0) and making an angle1(theta1), while Link-2 is hinged at the end of the Link-1 and making angle2(theta 2) with respect to Datum.

We need to Animate the Arm and find out its behavior under different conditions. Our Approach to Getting the final Program and a series of steps that we will perform during the entire Problem is shown below in the Step chart.

2. Detailed Procedure and Explanation:-
2.1 Procedure and Explanation for Matlab:
Step-1: Calculating the position of Link-1.
From the fixed Support, the initial value of X and Y for the Link-1 (X0, Y0) and the final value is (X1, Y1).
x0=0;
y0=0;
x1=l1*cosd(theta1);
y1=l1*sind(theta1);
Step-2: Calculating the position of the Link-2.
For the Link-2 starting point is the Ending point if the Link-1 ie (X1, Y1) and the Ending point is (X2, Y2).
x2=x1+l2*cosd(theta2);
y2=y1+l2*sind(theta2);
Step-3: Writing Basic Program:-
Basic Program represents the basic idea about the plot where we get only a 2D plot but no animation.
To get the basic plot first We are Providing the basic input as the length of link 1 L1= 1 and length of link 2 L2=0.5.
In meters also theta1 = 45 and theta2 = 30 in degree.
And providing basic conditions as above in step-1 and step-2, and for plotting we are using the command 'Line' not 'plot' because the plot command gives us an error
Line ( [ X0 X1 ], [ Y0 Y1 ] )
Similarly,
Line ( [ X1 X2], [Y1 Y2] )
close all
clear all
clc
% Program for the kinematices of robotices arm
%inputs
l1 = 1
l2 = 0.5
theta1=45
theta2=30
%coordinates
x0=0
y0=0
x1 = l1* cosd(theta1)
y1 = l1* sind(theta1)
x2 = x1+l2* cosd(theta2)
y2 = y1+l2* sind(theta2)
line([x0 x1], [y0 y1],'linewidth', 3, 'color' ,'r')
line([x1 x2], [y1 y2],'linewidth', 3, 'color' ,'b')
The output of the Programm is a simple plot as shown below.

But in this, we are not getting the animated plot to do that we are using a separate program in the next step.
Step-4: Program for Arm Animation and Error Elimination.
Here we are taking multiple angles and multiple values of (X, Y).
Initially, we are providing the inputs for the angle with the help of linspace command as follows:
theta1=linspace(0,90,30);
theta2=linspace(0,90,60);
Where:-
In linspace command the first value represents the starting point of the angle, similarly, the second point represents the ending point of the angle and at last, the third point represents the number of values between them
To change the angle we are using the For-loop command as shown below.
for i = 1: length(theta1)
THETA1 = theta1(i)
for j=1:length(theta2)
THETA2=theta2(j)
Now, here is the value of 'i' & 'j' changes we are taking the values of theta1, theta2 from the original arrays, and assigning them to the variable called THETA1, THETA2.
We use the value of 'i' & 'j' to take particular values for theta1 and theta2 and then we are going to perform forward kinematics calculation hence, rearranging the Program in the following manner. for each value of theta1, each position of the link1, link2 can take multiple positions and which is depend’s on theta2.
Further, if it is THETA1 then THETA2 required another for-loop. for each value of theta1, we are looping the value of theta2, which is as shown above. For kinematic calculation, we are copying our first program below the line THETA2 = theta2(j) and changing the angle to THETA accordingly as follows.
x0=0;
y0=0;
x1 = l1* cosd(THETA1);
y1 = l1* sind(THETA1);
x2 = x1+l2* cosd(THETA2);
y2 = y1+l2* sind(THETA2);
%plotting
line([x0 x1],[y0 y1])
line([x1 x2],[y1 y2])
we got our first plot but not the animation as follows.

To eliminate this we are typing figure(ct) before plotting and outside for-loop ct=1
And after the plot command type ct=ct+1 (i.e we increment the value each time program gets executed in the for-loop) and executing the command we get 100 independent plots (because our angle1 value is 10 and angle2 value is also 10).
Therefore to get rid of this we are using plot command instead of line command.
line([x0 x1],[y0 y1])
line([x1 x2],[y1 y2])
Again we get the same result but the most recent link as a plot result. To prevent this we are using the hold on and pause command. Here we get the result as a 1st result (error) but animated.
Now we are going to remove the 2nd plot command and make the first plot as the below
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2])
we get an animated plot with random color changes as shown below.

Now removing the hold on command and including the axis command, adjusting the pause interval gives us a better result.
for axis([-0.1 1.5 0 1.5]) and for pause interval as pause (0.1). we get our Required Result but further, we need to make a movie of it, which is in the next step.
Step-5: Creating a movie.
To create movie type M(ct)=getframe(gcf) just below the pause(0.01) and at extreme end command type Movie(M)
Where:- ct represents every frame and getframe command represents getting the current figure in the gcf window. Further, we are going to save all the figures in the array called (M).
Further, we are creating avi file to get this we are using the following commands.
movie(M)
videofile = VideoWriter('forward_kinematic.avi');
open(videofile)
writeVideo(videofile, M)
close(videofile)
Note:- while using the writeVideo command use it as it is because it is case sensitive hence any mistake can give an error in the program execution. Also, we can use uncompress command but it gives us the large avi file. Hence in this program, we are not using it.

The final program is as shown below:
% Program for forward kinematics of a 2R robotic arm manipulation.
clear all % command use to clear workspace
close all % command use to close all plots, graphs, etc.
clc %comand use to clear command window.
% l1 =Length of 1st link
l1=1;
%l2 = Length of 2nd link
l2=0.5;
% theta1= Angle between 1st link and the base
theta1=linspace(0,90,40);
% theta= Angle between 1st link and the 2nd link
theta2=linspace(0,90,40);
% To take value of both theta auto we us 'for'loop.
for i=1:length(theta1)
THETA1=theta1(i);%this command is use get diffrent value of theta1
%every value of i theta will be change
ct=1;
for j=1:length(theta2)
THETA2=theta2(j);%this command is use get diffrent value of theta2
%every value of j theta will be change
%coordinate of starting point of link 1 is x0 y0
%coordinate of starting point of link 2 is x1 y1
%coordinate of ending point of link 2 is x2 y2
x0=0;
y0=0;
x1=l1*cosd(THETA1);
y1=l1*sind(THETA1);
x2=x1+l2*cosd(THETA2);
y2=y1+l2*sind(THETA2);
% Ploting the coordinates to check the robotic arm as line
plot([x0 x1],[y0 y1],[x1 x2],[y1 y2],'linewidth',4);
axis([-0.1 1.5 0 1.5])%this command is use to fix the axis
pause(0.03);
M(ct)=getframe(gcf);%this command is use to store alll the figure in a correctore call M
ct=ct+1;%every time theta value change ct will increase by 1.
end
end
% to make video we use video command
movie(M)
videofile = VideoWriter('forward_kinematic.avi');
open(videofile)
writeVideo(videofile, M)
close(videofile)