Project

Study of Forward Kinematics of Robotic Arm.

Aim: To study the forward kinematics of the robotic arm.

Formulae used:

Trignometric relations of Sin and Cos.

  1. Sin (X) = Opposite/Hypotenuse
  2. Cos (X) = Adjacent/Hypotenuse

Trigonometry, the branch of mathematics concerned with specific functions of angles and their application to calculations. There are six functions of an angle commonly used in trigonometry. Their names and abbreviations are sine (sin), cosine (cos), tangent (tan), cotangent (cot), secant (sec), and cosecant (csc). These six trigonometric functions in relation to a right triangle are displayed in the figure. For example, the triangle contains an angle A, and the ratio of the side opposite to A and the side opposite to the right angle (the hypotenuse) is called the sine of A, or sin A; the other trigonometry functions are defined similarly. These functions are properties of the angle A independent of the size of the triangle. Trigonometric functions are used in obtaining unknown angles and distances from known or measured angles in geometric figures.

Figure01: Right Angle Triangle for explaining Trignometric relations.

Sin A = a/c = Opposite/Hypotenuse

Where,

Sin A: Trignometric function (Sine) of angle A.

Opposite: Value of the length of the side opposite (a) to angle A.

Hypotenuse: Value of the length of the hypotenuse (c) of triangle ABC.

Cos A = b/c = Adjacent/Hypotenuse

Cos A: Trignometric function (Cosine) of angle A.

Adjacent: Value of the length of the side adjacent (b) to angle A.

Hypotenuse: Value of the length of the hypotenuse (c) of triangle ABC.

By using above formulae we can find the values of:

  1. Lengths of sides of triangle a, b and c.
  2. Angles (A, B and C) of the Triangle ABC.
  3. Coordinates of point A, B and C.

Objectives:

  1. Create an animation file of the plot.
  2. Step-wise explanation of your code.
  3. Mention the errors faced while programming.
  4. Steps taken to overcome those errors.
  5. Screenshots showing errors thrown in command window.

Problem Statement: Write a matlab program for the 2R robotic arm consisting of two links, link-01 and link-02. Link-01 and Link-02 are of length 3 cm and 2 cm respectively. Where angles made by link-01 and link-02 with X-axis are 45 degrees and 75 degrees respectively.

Solution:

First let us understand, how to create a Program for plotting the links in matlab.

Plotting links is as simple as plotting two different lines.

In order to draw a single line we require:

  1. Length of line. (Say length of link in this case.)
  2. Angular position of the line. (Angle at which the link is inclined.)
  3. Coordinates of line stating initial and final position of it. (Say coordinates of the link.)

We have Lengths both links and the angles at which both the links are inclined are also given.

L1 = 3 cm

L2 = 2 cm

theta1 = 45

theta2 = 75

We need to find the values of the coordinates of both links.

Refer the following figure to calculate the coordinates of the links.

By using the trignometric relations the values of the unknown coordinates of the links are calculated.

Figure02: Calculations for the coordinates of the links.

We can calculate following coordinates:

X0 = 0

Y0 = 0

X1 = L1*cosd(theta1)

Y1 = L1*sind(theta1)

X2 = X1 + L2*cosd(theta2)

Y2 = Y1 + L2*sind(theta2)

Now we can program the plot showing the link-01 and link-02.

Matlab Program for the 2R robotic arm:

1-	% Plotting 2R robotic arm
2-	 
3-	clear all
4-	close all
5-	clc
6-	 
7-	% Inputs
8-	 
9-	    L1 = 3;
10-	L2 = 2;
11-	 
12-	theta1 = 45
13-	theta2 = 75
14-	 
15-	% Coordinates
16-	 
17-	X0 = 0
18-	Y0 = 0
19-	 
20-	X1 = L1*cosd(theta1);
21-	Y1 = L1*sind(theta1);
22-	 
23-	X2 = X1 + L2*cosd(theta2)
24-	Y2 = Y1 + L2*sind(theta2)
25-	 
26-	% Plotting Links 1 and 2
27-	 
28-	hold on
29-	plot([X0 X1],[Y0 Y1],'linewidth',5,'color','g');
30-	plot([X1 X2],[Y1 Y2],'linewidth',5,'color','r');
31-	

Detailed Explanation of the program:

1-	Name of Program.
2-	
3-	Clears your workspace.
4-	Closes all the figures.
5-	Clears Command window.
6-	
7-	Inputs
8-	
9-	Length of link-01.
10-     Length of link-02.
11-	
12-      Angular position of link-01.
13-	 Angular position oflink-02.
14-	
15-	 Coordinates
16-	
17-	Value of coordinate X0 of link-01.
18-	Value of coordinate Y0 of link-01.
19-	
20-	Value of coordinate X1 of link-01 and link-02.
21-	Value of coordinate Y1 of link-01 and link-02.
        [Since the Start point of link-02 and End point of link-01 are same 
        coordinates. Therefore is no need of entering values of coordinates of the 
        link-02 separately.]
22-	
23-	Value of coordinate X2 of link-02.
24-	Value of coordinate Y2 of link-02.
25-	
26-     Plotting links 1 and 2
27-	
28-	hold on
       [We are using plot command to execute the program where after execution of plot 
       command only the plot of latest value will get executed. Therefore to get the 
       plot of both the links we must use hold on command.]
29-    Using plot command to plot the link-01 of required line width and color.
30-    Using plot command to plot the link-02 of required line width and color.

In the above program we just entered the values of all inputs such as lengths of the links and the angular positions of the links.

Now in this case we are not knowing the values of the coordinates of the links. But we had calculated the trigonometric equations to find the values of the coordinates of the end point of link -01 (start point of link-02) and end point of link-02.

So we are just entering the trigonometric equations as the values of the coordinates of end point of link -01 (start point of link-02) in line 20 (coordinate X1) and 21(coordinate Y1).

Doing same for the end point of link-02 in line 23(coordinate X2) and 24(coordinate Y2)

Now until this stage we had just marked the initial and final points coordinates of the both links.

Now in next step we need to plot the link-01 and link-02 by using plot command.

We need to use the separate plot commands for both links individually. So that it become simple to differentiate between the two links.

Therefore, we use line-29 to plot the link-01 using the plot command and coordinates required to plot the link-01 stating required color and the linewidth of the link.

For link-02 plot command and coordinates required to plot the link-02 stating required color and the linewidth of the link is programmed in line-30.

Now if we run the program we will get the plot as shown in figure03.

Figure03: Plot showing linl-01 and link-02.

Now we will have to make a video of the link-01 and link-02 for the different values of theta1 and theta2 respectively.

That means for each value of theta1 of link-01 we will have to assign the n number of values of theta2 of link-02. This can be done by using for loop in for loop.

Say n = 10

Now, here we had entered n = 10 as the number of intervals for the angular positions of the link-01 and link-02.

Therefore, we will get total 100 frames for the animation video of the different angular positions of the two links. That means link-02 will change the total ten number of positions for each position of the link-01.

Program for the video of different positions of link-01 and link-02:

01-	% Program for the video of different positions of link-01 and linl-02.
02-	 
03-	clear all
04-	close all
05-	clc
06-	 
07-	% inputs 
08-	L1 = 3
09-	L2 = 2
10-	 
11-	theta1 = linspace(0,90,40);
12-	theta2 = linspace(0,90,40);
13-	 
14-	% Generating for loop
15-	Ct=1;
16-	for i = 1:length(theta1)
17-	    THETA1 = theta1(i);
18-	    for j = 1:length(theta2)
19-	        THETA2 = theta2(j);
20-	        
21-	        X0 = 0
22-	        Y0 = 0
23-	        
24-	        X1 = L1*cosd(THETA1)
25-	        Y1 = L1*sind(THETA1)
26-	        
27-	        X2 = X1 + L2*cosd(THETA2)
28-	        Y2 = Y1 + L2*sind(THETA2)
29-	        
30-	        plot([X0 X1],[Y0 Y1],[X1 X2],[Y1 Y2],'linewidth',2);
31-	        axis([-1 5 0 4]);
32-	        pause(0.001);
33-	        M(Ct) = getframe(gcf);
34-	        Ct=Ct+1;
35-	    end
36-	end
37-	 
38-	        movie(M)
39-	        videofile = VideoWriter('C_forward_kinematics.avi','uncompressed avi');
40-	        open(videofile)
41-	        writeVideo(videofile,M);
42-	        close(videofile)

Detailed explanation of the program:

01-     Name of the program
02-	
03-	Clears your workspace.
04-	Closes all the figures.
05-	Clears Command window.
06-	
07-	Inputs
08-	Length of link-01.
09-     Length of link-02.
10-	
11-	Angular position of link-01.
        [Using linspace command (syntax: linspace(x,z,n);) to create ten different 
        values for the theta1]
12-	Angular position oflink-02.
        [Using linspace command (syntax: linspace(x,z,n);) to create ten different 
        values for the theta2]

13-	
14-     Generation of for loop.
15-	Ct=1; 
        [After execution of the program we will get the representation of the links for 
        specified values in single plot only, which is quite inconvenient and looks 
        ugly. In order to avoid it we must use the counter command (syntax: Ct=1;) to 
        obtain the each plot on completely different figure.]
16-	for i = 1:length(theta1)
        [First for loop, using for loop and entering the values of the loop counter 
        variable i using length command to take the specified number of values of the 
        theta1.]
17-     THETA1 =theta1(i);
        [Second line of for loop is the command executed by the for loop where THETA1 
        used as output variable which will take the value of theta(i).]
18-	for j = 1:length(theta2)
        [Second for loop, using for loop and entering the values of the loop counter 
        variable j using length command to take the specified number of values of the 
        theta2.]
19-	THETA2 =theta2(j);
        [Second line of for loop is the command executed by the for loop where THETA2 
        used as output variable which will take the value of theta(j).]
20-	 
21-	Value of coordinate X0 of link-01.
22-	Value of coordinate Y0 of link-01.
23-	
24-	Value of coordinate X1 of link-01 and link-02.
        [For link-01 value of THETA1 will change as per loop counter variable i, 
        therefore THETA1 which is also a output variable of for loop 1 is used]
25-	Value of coordinate Y1 of link-01 and link-02.
        [For link-01 value of THETA1 will change as per loop counter variable i, 
        therefore THETA1 which is also a output variable of for loop 1 is used]
        [Since the Start point of link-02 and End point of link-01 are same 
        coordinates. Therefore there is no need of entering values of coordinates of 
        the link-02 separately.]
26-	
27-	Value of coordinate X2 of link-02.
28-	Value of coordinate Y2 of link-02.
        [For link-02 value of THETA2 will change as per loop counter variable j, 
        therefore THETA2 which is also a output variable of for loop 2 is used]
29-	
30-	Using plot command to plot the link-01 and link-02 in same figure of required 
        line width and color.
31-     Executing axis (syntax:axis([X0 X1 Y0 Y1]) command for proper presentation  of  
        the plot w. r. t. X-axis and Y-axis.
32-	Pause command for the interval between two successive plots during 
        presentation.
33-	Using getframe command (syntax:getframe(gcf);)  to get the frames from the 
        counter to create video of it.
34-	Preparing conter for the next plot.
35-	End of for loop-01.
36-	End of for loop-02.
37-	
38-     movie(M)
        [Using movie command to create the video of plots.]
39-	Syntax of videofile.
        [videofile = VideoWriter(‘Name_of_a_file.avi’,’uncompressed avi’);] 
40-	Open(videofile)
41-	WriteVideo(videofile,M)
42-	Close(videofile)
 

Errors committed during programming the animation:

  1. Error:

Repetitions of the definition of the coordinates.

Figure04: Showing error in line-26.

Correction:

Once we gave the trigonometric relations of the coordinates of the points, matlab will find the start point and end point of both links after the execution of plot command.

There is no need to specify the start point and end point of both links separately.

Deleted the extra and unwanted definition of the coordinates.

02. Error:

Wrong Syntax of plot command.

Figure05: Showing error in line-26.

Correction:

Syntax:

plot ([X0 X1], [Y0 Y1], ‘linewidth’,5,’color’,’r’);

Round brackets {()} were replaced by square brackets {[]} and the comma (,) between two coordinates was removed to get the syntax coded.

03. Error:

Wrong syntax of the for loop

Figure06: Showing error in line-14.

Correction:

Syntax:

for i = 1:length(theta1);

THETA1 = theta1(i);

end

The first line of for loop was discontinued after ‘for’. In simple words the for loop and loop counter variable must be in same line.

Hence place the cursor before loop counter variable ‘i’ and press backspace to get the syntax right.

04. Error:

Not closing the for loop.

Figure07: Showing error in line-14

Correction:

Its mandatory to close the ‘for’ loop to execute the program.

To close the for loop roll the cursor down program and type ’end’ to end the for loop.

05. Error:

Wrong syntax of video file.

Figure08: Showing error in line-38.

Correction:

Syntax:

movie(m)

videofile = VideoWriter(‘Name_of_file.avi’,’uncompressed avi’)

open(videofile)

writeVideo(videofile,m)

close(videofile)

The comma (,) after ‘uncompressed avi’ was removed to get the syntax right

Also the upper case alphabet ‘M’ was replaced by lower case ‘m’.

‘videowriter’ was replaced by ‘VideoWriter;.

‘writevideo’ was replaed by ‘writeVideo’.

Conclusion:

As we can see in the animation video two links: link-01 and link-02 are moving from 0 degrees to 90 degrees in counter clockwise direction. For each angular position of link-01, link-02 will change it's ten angular position. This is achieved by using for loop in for loop. We can also say that for each input (theta1) of counter variable 'i', counter vaiable 'j' will take ten values of theta2. So it's clear that here we are getting total 100 frames for the video. The workspace of the robotic arm is a six-dimensional entity, generally impossible to represent it in two dimensional plot. Here we can only study the different angular positions of the link-01 and link-02 by using matlab.

(link:https://drive.google.com/file/d/1cLzVEAHypHxjF7gwoOchirXTrcr_7u5k/view?usp=sharing,)

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.