Ece478 12es_final_report

41
ECE478 FINAL REPORT Applying Fuzzy Logic to Control a Submarine CLASS 12ES GROUP MEMBERS: Đinh Tuấn Anh Nguyễn Thanh Sang Đoàn Công Danh Tạ Đình Sang

Transcript of Ece478 12es_final_report

Page 1: Ece478 12es_final_report

ECE478FINAL REPORT

Applying Fuzzy Logic to Control a Submarine

CLASS 12ES

GROUP MEMBERS:

Đinh Tuấn Anh

Nguyễn Thanh Sang

Đoàn Công Danh

Tạ Đình Sang

Page 2: Ece478 12es_final_report

Table of ContentsI. Fuzzy logic and fuzzy controller....................................................................................................2

1. Fuzzy Logic....................................................................................................................................3

2. Working-Process..........................................................................................................................3

3. Fuzzy Logic Algorithm.................................................................................................................5

I. Fuzzy Logic Algorithm of Controlling a Submarine...................................................................5

1. Linguistic Variables.....................................................................................................................5

2. Membership functions................................................................................................................6

a. Popular Membership Functions:...............................................................................................6

b. Membership Functions in Cotrolling a Submarine..................................................................8

3. Fuzzy Rules.................................................................................................................................10

4. Convert the output data to non-fuzzy values (defuzzification).........................................13

1. main function.................................................................................................................................14

2.double triangular............................................................................................................................16

3.Assigning rules and returns the angles two blades rotate..............................................................18

4. max-min method...........................................................................................................................31

5. Result...........................................................................................................................................32

6. Conclusion...................................................................................................................................36

FiguresFigure 1: A Fuzzy Logic System (FLS).......................................................................................................4Figure 2:Examples of Popular Membership Functions............................................................................8Figure 3: Triangular Membership Function.............................................................................................9Figure 4: Depth Membership Function...................................................................................................10Figure 5: Left-Right Obstacle Membership Function.............................................................................10Figure 6: Velocity Membership Function...............................................................................................11Figure 7.Blade1-Blade2 Membership Function.....................................................................................11Figure 8: Weighted Average Method......................................................................................................14

Page 3: Ece478 12es_final_report

I. Fuzzy logic and fuzzy controller

1. Fuzzy Logic

Fuzzy logic is an approach to computing based on "degrees of truth" rather than the usual "true or false" (1 or 0) Boolean logic on which the modern computer is based. 

The idea of fuzzy logic was first advanced by Dr. Lotfi Zadeh of the University of California at Berkeley in the 1960s. Dr. Zadeh was working on the problem of computer understanding of natural language. Natural language (like most other activities in life and indeed the universe) is not easily translated into the absolute terms of 0 and 1. (Whether everything is ultimately describable in binary terms is a philosophical question worth pursuing, but in practice much data we might want to feed a computer is in some state in between and so, frequently, are the results of computing.) It may help to see fuzzy logic as the way reasoning really works and binary or Boolean logic is simply a special case of it.

Fuzzy logic includes 0 and 1 as extreme cases of truth (or "the state of matters" or "fact") but also includes the various states of truth in between so that, for example, the result of a comparison between two things could be not "tall" or "short" but ".38 of tallness."

Fuzzy logic seems closer to the way our brains work. We aggregate data and form a number of partial truths which we aggregate further into higher truths which in turn, when certain thresholds are exceeded, cause certain further results such as motor reaction. A similar kind of process is used in neural networks, expert systems and other artificial intelligence applications. Fuzzy logic is essential to the development of human-like capabilities for AI, sometimes referred to as artificial general intelligence: the representation of generalized human cognitive abilities in software so that, faced with an unfamiliar task, the AI system could find a solution. 

Page 4: Ece478 12es_final_report

2. Working-Process

Figure 1: A Fuzzy Logic System (FLS)

Firstly, a crisp set of input data are gathered and converted to a fuzzy set using fuzzy linguistic variables, fuzzy linguistic terms and membership functions. This step is known as fuzzification. Afterwards, an inference is made based on a set of rules. Lastly, the resulting fuzzy output is mapped to a crisp output using the membership functions, in the defuzzification step.

Fuzzy sets

A fuzzy set is represented by a membership function defined on the universe of discourse. The universe of discourse is the space where the fuzzy variables are defined. The membership function gives the grade, or degree, of membership within the set, of any element of the universe of discourse. The membership function maps the elements of the universe onto numerical values in the interval [0, 1]. A membership function value of zero implies that the corresponding element is definitely not an element of the fuzzy set, while a value of unity means that the element fully belongs to the set. A grade of membership in between corresponds to the fuzzy membership to set. In crisp set theory, if someone is taller than 1.8 meters, we can state that such person belongs to the “set of tall people”. However, such

Page 5: Ece478 12es_final_report

sharp change from 1.7999 meters of a “short person” to 1.8001 meters of a “tall person” is against the common sense.

Fuzzifcation

Fuzzification is the process of decomposing a system input and/or output into one or more fuzzy sets. Many types of curves can be used, but triangular or trapezoidal shaped membership functions are the most common because they are easier to represent in embedded controllers. Fig 1 shows a system of fuzzy sets for an input with trapezoidal and triangular membership functions. Each fuzzy set spans a region of input (or output) value graphed with the membership. Any particular input is interpreted from this fuzzy set and a degree of membership is interpreted. The membership functions should overlap to allow smooth mapping of the system. The process of fuzzification allows the system inputs and outputs to be expressed in linguistic terms so that rules can be applied in a simple manner to express a complex system.

Defuzzification

After fuzzy reasoning we have a linguistic output variable which needs to be translated into a crisp value. The objective is to derive a single crisp numeric value that best represents the inferred fuzzy values of the linguistic output variable. Defuzzification is such inverse transformation which maps the output from the fuzzy domain back into the crisp domain. Some defuzzification methods tend to produce an integral output considering all the elements of the resulting fuzzy set with the corresponding weights. Other methods take into account just the elements corresponding to the maximum points of the resulting membership functions.

3. Fuzzy Logic Algorithm

1. Define the linguistic variables and terms (initialization) 2. Construct the membership functions (initialization) 3. Construct the rule base (initialization) 4. Convert crisp input data to fuzzy values using the membership functions

(fuzzification) 5. Evaluate the rules in the rule base (inference) 6. Combine the results of each rule (inference) 7. Convert the output data to non-fuzzy values (defuzzification)

Page 6: Ece478 12es_final_report

I. Fuzzy Logic Algorithm of Controlling a Submarine

1. Linguistic Variables

Linguistic variables are the input or output variables of the system whose values are words or sentences from a natural language, instead of numerical values. A linguistic variable is generally decomposed into a set of linguistic terms.

Fuzzy sets for depth:

Depth = {Low, Medium, High}

Fuzzy sets for left obstacles:

Left_Obstacles = {Close, Near, Far}

Fuzzy sets for right obstacles:

Right_Obstacles = {Close, Near, Far}

Fuzzy sets for velocity:

Velocity = {Slow, Medium, Fast}

Fuzzy sets for blade 1:

Blade1 = {Lowlow, Low, Zero, High, Highhigh};

Fuzzy sets for blade 2:

Blade2 = {Lowlow, Low, Zero, High, Highhigh};

2. Membership functions

Membership functions are used in the fuzzification and defuzzification steps of a FLS, to map the non-fuzzy input values to fuzzy linguistic terms and vice versa. A membership function is used to quantify a linguistic term.

Page 7: Ece478 12es_final_report

a. Popular Membership Functions:

Figure 2:Examples of Popular Membership Functions

The simplest membership functions are formed using straight lines. Of these, the simplest is the triangular membership function, and it has the function name trimf. This function is nothing more than a collection of three points forming a triangle. The trapezoidal membership function, trapmf, has a flat top and really is just a truncated triangle curve. These straight line membership functions have the advantage of simplicity.

Two membership functions are built on the Gaussian distribution curve: a simple Gaussian curve and a two-sided composite of two different Gaussian curves. The two functions aregaussmf and gauss2mf.

The generalized bell membership function is specified by three parameters and has the function name gbellmf. The bell membership function has one more parameter than the Gaussian membership function, so it can approach a non-fuzzy set if the free parameter is tuned. Because of their smoothness and concise notation, Gaussian and bell membership functions are popular methods for specifying fuzzy sets. Both of these curves have the advantage of being smooth and nonzero at all points.

Page 8: Ece478 12es_final_report

Although the Gaussian membership functions and bell membership functions achieve smoothness, they are unable to specify asymmetric membership functions, which are important in certain applications. Next, you define the sigmoidal membership function, which is either open left or right. Asymmetric and closed (i.e. not open to the left or right) membership functions can be synthesized using two sigmoidal functions, so in addition to the basic sigmf, you also have the difference between two sigmoidal functions, dsigmf, and the product of two sigmoidal functions psigmf.

Polynomial based curves account for several of the membership functions in the toolbox. Three related membership functions are the Z, S, and Pi curves, all named because of their shape. The function zmf is the asymmetrical polynomial curve open to the left, smf is the mirror-image function that opens to the right, and pimf is zero on both extremes with a rise in the middle.

There is a very wide selection to choose from when you're selecting a membership function. You can also create your own membership functions with the toolbox. However, if a list based on expanded membership functions seems too complicated, just remember that you could probably get along very well with just one or two types of membership functions, for example the triangle and trapezoid functions. The selection is wide for those who want to explore the possibilities, but expansive membership functions are not necessary for good fuzzy inference systems. Finally, remember that more details are available on all these functions in the reference section.

b. Membership Functions in Cotrolling a Submarine

μA ( x )=¿ {0 if x≤a

¿x−ab−a if a<x≤b

¿ c−xc−b

if b<x<c

¿ 0if x ≥ c

Figure 3: Triangular Membership Function

Page 9: Ece478 12es_final_report

Figure 4: Depth Membership Function

Figure 5: Left-Right Obstacle Membership Function

Figure 6: Velocity Membership Function

Page 10: Ece478 12es_final_report

Figure 7.Blade1-Blade2 Membership Function

3. Fuzzy Rules

In a FLS, a rule base is constructed to control the output variable. A fuzzy rule is a simple IF-THEN rule with a condition and a conclusion.

Let:

L1: depth is low L2: left obstacle is close L3: right obstacle is close L4: velocity is slow M1: depth is medium M2: left obstacle is near M3: right obstacle is near M4: velocity is medium H1: depth is high H2: left obstacle is far H3: right obstacle is far H4: velocity is fast Z/Z or Z/L … stands for blade1/blade2.

Then, we have the rule table below.

Table 1. Rule Table of Fuzzy Logic

L1 L2 L1 M2 L1 H2 M1 L2 M1 M2 M1 H2 H1 L2 H1 M2 H1 H2

Page 11: Ece478 12es_final_report

L3 L4 Z/Z L/H L/H Z/Z L/H L/H Z/Z L/L L/L

L3 M4 Z/Z L/H LL/H Z/Z L/H LL/H Z/Z L/L LL/L

L3 H4 Z/Z LL/H LL/H Z/Z LL/H LL/H Z/Z LL/L LL/L

M3 L4 H/H Z/H L/HH H/H Z/H L/H H/L Z/L L/LL

M3 M4 H/H Z/H L/HH H/H Z/H L/HH H/L Z/L L/LL

M3 H4 HH/H Z/HH L/HH HH/H Z/H L/HH HH/L H/LL L/LL

H3 L4 HH/H Z/HH Z/HH HH/H H/HH Z/HH HH/L HH/LL Z/LL

H3 M4 HH/H Z/HH Z/HH HH/H HH/H Z/HH HH/L H/L Z/LL

H3 H4 HH/H Z/HH Z/HH HH/H H/H Z/HH HH/L H/L Z/LL

According to table 1, we have 81 rules for two output blade1 and blade2

(1) if (Depth(Low) and Left_Obstacle(Far) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Zero and Blade2 is Zero

(2) if (Depth(Low) and Left_Obstacle(Near) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Low and Blade2 is High

(3) if (Depth(Low) and Left_Obstacle(Close) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Low and Blade2 is High

Page 12: Ece478 12es_final_report

(4) if (Depth(Medium) and Left_Obstacle(Far) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Zero and Blade2 is Zero

(5) if (Depth(Medium) and Left_Obstacle(Near) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Low and Blade2 is High

(6) if (Depth(Medium) and Left_Obstacle(Close) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Low and Blade2 is High

(7) if (Depth(High) and Left_Obstacle(Far) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Zero and Blade2 is Zero

(8) if (Depth(High) and Left_Obstacle(Near) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Low and Blade2 is Low

(9) if (Depth(High) and Left_Obstacle(Close) and Right_Obstacle(Far) and Velocity(Slow)) then Blade1 is Low and Blade2 is Low

In the C codes we discuss in next section, we will have full of Rules.

4. Convert the output data to non-fuzzy values (defuzzification)

After the inference step, the overall result is a fuzzy value. This result should be defuzzified to obtain a final crisp output. This is the purpose of the defuzzifier component of a FLS. Defuzzification is performed according to the membership function of the output variable.

In this project, we choose Weighted Average Method because this method is valid for symmetrical output membership functions.

Page 13: Ece478 12es_final_report

The follwing codes based on C programming language run on CodeBlocks platform.

#include <stdio.h>

#include <stdlib.h>

void assignRule_Blade (double L1, double L2, double L3, double L4, double M1, double M2, double M3, double M4, double H1, double H2, double H3, double H4, double blade1, double blade2);

double triangular (double low, double mid, double high, double x);

void Depth (double x, double a[]);

void Left_Obstacles (double x, double a[]);

void Right_Obstacles (double x, double a[]);

void Velocity (double x, double a[]);

double min4 (double x, double y, double m, double n);

/* The C sub-functions:

void assignRule_Blade returns values of two blades (1 and 2)

double triangular returns the probability of number x in the triangle.

Page 14: Ece478 12es_final_report

void Depth builds Depth membership function based on triangular function.

void Left_Obstacles builds Left_Obstacles membership function based on

triangular function.

void Right_Obstacles builds Right_Obstacles membership function based on

triangular function.

void Velocity builds Velocity membership function based on triangular

function.

double min4 returns the minimum value of four given numbers.

*/

1. main functionint main()

{

double D[3], Le[3], Ri[3], V[3];

double L1, L2, L3, L4, M1, M2, M3, M4, H1, H2, H3, H4;

Depth(52.4,D);

Left_Obstacles (3.91,Le);

Right_Obstacles(3.91,Ri);

Velocity(10.2,V);

L1 = D[0];

M1 = D[1];

H1 = D[2];

L2 = Le[2];

M2 = Le[1];

Page 15: Ece478 12es_final_report

H2 = Le[0];

L3 = Ri[2];

M3 = Ri[1];

H3 = Ri[0];

L4 = V[0];

M4 = V[1];

H4 = V[2];

double out_blade1, out_blade2;

assignRule_Blade(L1, L2, L3, L4, M1, M2, M3, M4, H1, H2, H3, H4, out_blade1, out_blade2);

return 0;

}

The way sub-functions implement

2. double triangular double triangular (double low, double mid, double high, double x)

{

double u;

Page 16: Ece478 12es_final_report

if (x<= low) u = 0;

else if (x > low && x <= mid ) u = (x-low)/(mid-low);

else if (x > mid && x <high) u = (high-x)/(high-mid);

else if (x >= high) u = 0;

return u;

}

void Depth (double x, double a[])

{

a[0] = triangular (0, 20, 40, x);

a[1] = triangular (20, 50, 80, x);

a[2] = triangular (60, 80, 100, x);

}

void Left_Obstacles (double x, double a[])

{

a[0] = triangular (0, 2, 4, x);

a[1] = triangular (3, 5, 7, x);

a[2] = triangular (6, 8, 10, x);

}

void Right_Obstacles (double x, double a[])

{

a[0] = triangular (0, 2, 4, x);

a[1] = triangular (3, 5, 7, x);

a[2] = triangular (6, 8, 10, x);

Page 17: Ece478 12es_final_report

}

void Velocity (double x, double a[])

{

a[0] = triangular (0, 4, 8, x);

a[1] = triangular (5, 10, 15, x);

a[2] = triangular (12, 16, 20, x);

}

double min4 (double x, double y, double m, double n)

{

return (((x<y)? x:y) < ((m<n)? m:n)) ? ((x<y)? x:y) : ((m<n)? m:n) ;

}

3.Assigning rules and returns the angles two blades rotate

void assignRule_Blade(double L1, double L2, double L3, double L4, double M1, double M2, double M3, double M4, double H1, double H2, double H3, double H4, double blade1, double blade2)

{

Page 18: Ece478 12es_final_report

Double ll1[25],ll2[25],l1[25],l2[25],z1[25],z2[25],h1[25],h2[25],hh1[25],hh2[25];

int idx;

for(idx=0;idx<25;idx++)

{

ll1[idx]=0;

ll2[idx]=0;

l1[idx]=0;

l2[idx]=0;

z1[idx]=0;

z2[idx]=0;

h1[idx]=0;

h2[idx]=0;

hh1[idx]=0;

hh2[idx]=0;

}

if (L1 && L2 && L3)

{

z1[0] = min4(L1, L2, L3, 1);

z2[0] = min4(L1, L2, L3, 1);

}

if (L1 && L2 && M3 && L4)

{

h1[0] = min4(L1,L2, M3, L4);

Page 19: Ece478 12es_final_report

h2[0] = min4(L1,L2, M3, L4);

}

if (L1 && L2 && M3 && M4)

{

h1[1] = min4(L1,L2, M3, M4);

h2[1] = min4(L1,L2, M3, M4);

}

if (L1 && L2 && M3 && H4)

{

hh1[0] = min4(L1,L2,M3,H4);

h2[2] = min4(L1,L2,M3,H4);

}

if (L1 && L2 && H3 && L4)

{

hh1[1] = min4(L1,L2,H3,L4);

h2[3] = min4(L1,L2,H3,L4);

}

if (L1 && L2 && H3 && M4)

{

hh1[2] = min4(L1,L2,H3,M4);

h2[4] = min4(L1,L2,H3,M4);

}

if (L1 && L2 && H3 && H4)

{

hh1[3] = min4(L1,L2,H3,H4);

h2[5] = min4(L1,L2,H3,H4);

}

Page 20: Ece478 12es_final_report

if (L1 && M2 && L3 && L4)

{

l1[0] = min4(L1,M2,L3,L4);

h2[6] = min4(L1,M2,L3,L4);

}

if (L1 && M2 && L3 && M4)

{

l1[1] = min4(L1,M2,L3,M4);

h2[7] = min4(L1,M2,L3,L4);

}

if (L1 && M2 && L3 && H4)

{

ll1[0] = min4(L1,M2,L3,H4);

h2[8] = min4(L1,M2,L3,H4);

}

if (L1 && M2 && M3 && L4)

{

z1[1] = min4(L1,M2,M3,L4);

h2[9] = min4(L1,M2,M3,L4);

}

if (L1 && M2 && M3 && M4)

{

z1[2] = min4(L1,M2,M3,M4);

h2[10] = min4(L1,M2,M3,M4);

}

if (L1 && M2 && M3 && H4)

{

Page 21: Ece478 12es_final_report

z1[3] = min4(L1,M2,M3,H4);

hh2[0] = min4(L1,M2,M3,M4);

}

if (L1 && M2 && H3 && L4)

{

h1[2] = min4(L1,M2,H3,L4);

hh2[1] = min4(L1,M2,H3,L4);

}

if (L1 && M2 && H3 && M4)

{

hh1[3] = min4(L1,M2,H3,M4);

hh2[2] = min4(L1,M2,H3,L4);

}

if (L1 && M2 && H3 && H4)

{

h1[3] = min4(L1,M2,H3,H4);

h2[11] = min4(L1,M2,H3,H4);

}

if (L1 && H2 && L3 && L4)

{

l1[2] = min4(L1,H2,L3,L4);

h2[12] = min4(L1,H2,L3,L4);

}

if (L1 && H2 && L3 && M4)

{

ll1[1] = min4(L1,H2,L3,M4);

h2[13] = min4(L1,H2,L3,M4);

Page 22: Ece478 12es_final_report

}

if (L1 && H2 && L3 && H4)

{

l1[3] = min4(L1,H2,L3,H4);

h2[14] = min4(L1,H2,L3,L4);

}

if (L1 && H2 && M3 && L4)

{

l1[4] = min4(L1,H2,M3,L4);

hh2[3] = min4(L1,H2,M3,L4);

}

if (L1 && H2 && M3 && M4)

{

l1[5] = min4(L1,H2,M3,M4);

hh2[4] = min4(L1,H2,M3,M4);

}

if (L1 && H2 && M3 && H4)

{

l1[6] = min4(L1,H2,M3,H4);

hh2[5] = min4(L1,H2,M3,H4);

}

if (L1 && H2 && H3 && L4)

{

z1[4] = min4(L1,H2,H3,L4);

hh2[6] = min4(L1,H2,H3,L4);

}

if (L1 && H2 && H3 && M4)

Page 23: Ece478 12es_final_report

{

z1[5] = min4(L1,H2,H3,M4);

hh2[7] = min4(L1,H2,H3,M4);

}

if (L1 && H2 && H3 && H4)

{

z1[6] = min4(L1,H2,H3,H4);

hh2[8] = min4(L1,H2,H3,H4);

}

if (M1 && L2 && L3)

{

z1[7] = min4(M1, L2, L3, 1);

z2[1] = min4(M1, L2, L3, 1);

}

if (M1 && L2 && M3 && L4)

{

h1[4] = min4(M1, L2, L3, L4);

h2[15] = min4(M1, L2, L3, L4);

}

if (M1 && L2 && M3 && M4)

{

h1[5] = min4(M1, L2, L3, M4);

h2[16] = min4(M1, L2, L3, M4);

}

if (M1 && L2 && M3 && H4)

{

Page 24: Ece478 12es_final_report

hh1[4] = min4(M1,L2,M3,H4);

hh2[9] = min4(M1,L2,M3,H4);

}

if (M1 && L2 && H3 )

{

hh1[5] = min4(M1,L2,H3,1);

hh2[10] = min4(M1,L2,H3,1);

}

if (M1 && M2 && L3 && L4)

{

l1[7] = min4(M1,M2,L3,L4);

h2[17] = min4(M1,M2,L3,L4);

}

if (M1 && M2 && L3 && M4)

{

l1[8] = min4(M1,M2,L3,M4);

h2[18] = min4(M1,M2,L3,M4);

}

if (M1 && M2 && L3 && H4)

{

ll1[2] = min4(M1,M2,L3,H4);

h2[19] = min4(M1,M2,L3,H4);

}

if (M1 && M2 && M3 )

{

z1[8] = min4(M1,M2,M3,1);

h2[20] = min4(M1,M2,M3,1);

Page 25: Ece478 12es_final_report

}

if (M1 && M2 && H3 && L4)

{

h1[6] = min4(M1,M2,H3,L4);

hh2[11] = min4(M1,M2,H3,L4);

}

if (M1 && M2 && H3 && M4)

{

hh1[6] = min4(M1,M2,H3,M4);

hh2[12] = min4(M1,M2,H3,M4);

}

if (M1 && M2 && H3 && H4)

{

h1[7] = min4(M1,M2,H3,H4);

h2[21] = min4(M1,M2,H3,H4);

}

if (M1 && H2 && L3 && L4)

{

l1[9] = min4(M1,H2,L3,L4);

h2[22] = min4(M1,H2,L3,L4);

}

if (M1 && H2 && L3 && M4)

{

ll1[3] = min4(M1,H2,L3,M4);

h2[23] = min4(M1,H2,L3,M4);

}

if (M1 && H2 && L3 && H4)

Page 26: Ece478 12es_final_report

{

ll1[4] = min4(M1,H2,L3,H4);

h2[24] = min4(M1,H2,L3,H4);

}

if (M1 && H2 && M3)

{

l1[10] = min4(M1,H2,M3,1);

hh2[13] = min4(M1,H2,M3,1);

}

if (M1 && H2 && H3)

{

z1[9] = min4(M1,H2,H3,1);

hh2[14] = min4(M1,H2,H3,1);

}

if (H1 && L2 && L3)

{

z1[10] = min4(H1,L2,L3,1);

z2[2] = min4(H1,L2,L3,1);

}

if (H1 && L2 && M3 && L4)

{

h1[8] = min4(H1,L2,M3,L4);

l2[0] = min4(H1,L2,M3,L4);

}

if (H1 && L2 && M3 && M4)

{

Page 27: Ece478 12es_final_report

h1[9] = min4(H1,L2,M3,M4);

l2[1] = min4(H1,L2,M3,M4);

}

if (H1 && L2 && M3 && H4)

{

hh1[7] = min4(H1,L2,M3,H4);

l2[2] = min4(H1,L2,M3,H4);

}

if (H1 && L2 && H3 )

{

hh1[8] = min4(H1,L2,H3,1);

l2[3] = min4(H1,L2,H3,1);

}

if (H1 && M2 && L3 && L4)

{

l1[11] = min4(H1,M2,L3,L4);

l2[4] = min4(H1,M2,L3,L4);

}

if (H1 && M2 && L3 && M4)

{

l1[12] = min4(H1,M2,L3,M4);

l2[5] = min4(H1,M2,L3,M4);

}

if (H1 && M2 && L3 && H4)

{

ll1[5] = min4(H1,M2,L3,H4);

l2[6] = min4(H1,M2,L3,H4);

Page 28: Ece478 12es_final_report

}

if (H1 && M2 && M3)

{

z1[11] = min4(H1,M2,M3,1);

l2[7] = min4(H1,M2,M3,1);

}

if (H1 && M2 && H3 && L4)

{

h1[10] = min4(H1,M2,H3,L4);

ll2[3] = min4(H1,M2,H3,L4);

}

if (H1 && M2 && H3 && M4)

{

hh1[9] = min4(H1,M2,H3,M4);

ll2[2] = min4(H1,M2,H3,M4);

}

if (H1 && M2 && H3 && H4)

{

h1[11] = min4(H1,M2,H3,H4);

l2[8] = min4(H1,M2,H3,H4);

}

if (H1 && H2 && L3 && L4)

{

l1[13] = min4(H1,H2,L3,L4);

l2[9] = min4(H1,H2,L3,L4);

}

if (H1 && H2 && L3 && M4)

Page 29: Ece478 12es_final_report

{

ll1[6] = min4(H1,H2,L3,M4);

l2[10] = min4(H1,H2,L3,M4);

}

if (H1 && H2 && L3 && H4)

{

ll1[7] = min4(H1,H2,L3,H4);

l2[11] = min4(H1,H2,L3,H4);

}

if (H1 && H2 && M3)

{

l1[14] = min4(H1,H2,M3,1);

ll2[1] = min4(H1,H2,M3,1);

}

if (H1 && H2 && H3)

{

z1[12] = min4(H1,H2,H3,1);

ll2[0] = min4(H1,H2,H3,1);

}

4. max-min method

double ll1max = ll1[0],ll2max=ll2[0],l1max=l1[0],l2max=l2[0],z1max=z1[0],z2max=z2[0],h1max=h1[0],h2max=h2[0],hh1max=hh1[0],hh2max=hh2[0];

for(idx=1;idx<25;idx++)

Page 30: Ece478 12es_final_report

{

if(ll1max<ll1[idx]) ll1max=ll1[idx];

if(ll2max<ll2[idx]) ll2max=ll2[idx];

if(l1max<l1[idx]) l1max=l1[idx];

if(l2max<l2[idx]) l2max=l2[idx];

if(z1max<z1[idx]) z1max=z1[idx];

if(z2max<z2[idx]) z2max=z2[idx];

if(h1max<h1[idx]) h1max=h1[idx];

if(h2max<h2[idx]) h2max=h2[idx];

if(hh1max<hh1[idx]) hh1max=hh1[idx];

if(hh2max<hh2[idx]) hh2max=hh2[idx];

}

printf("ll1 = %f l1 = %f z1=%f h1 = %f hh1 = %f\n", ll1max,l1max,z1max,h1max,hh1max);

printf("ll2 = %f l2 = %f z2=%f h2 = %f hh2 = %f\n", ll2max,l2max,z2max,h2max,hh2max);

// weighted average equation to find out the angles two blades rotate.

blade1 = (ll1max*15 + l1max*52 + z1max*92 + h1max*130 + hh1max*165 )/(ll1max+l1max+z1max+h1max+hh1max);

blade2 = (ll2max*15 + l2max*50 + z2max*90 + h2max*130 + hh2max*165 )/(ll2max+l2max+z2max+h2max+hh2max);

printf("blade1 = %f \nblade2 = %f ", blade1,blade2);

}

Page 31: Ece478 12es_final_report

5. Result

The following figures show the results of simulating fuzzy logic on Matlab (above) and implementing C programming on CodeBlocks (below).

Page 32: Ece478 12es_final_report
Page 33: Ece478 12es_final_report
Page 34: Ece478 12es_final_report
Page 35: Ece478 12es_final_report

After testing many times, we get other results recorded in table 2

Table 2. The input parameters and results comparison

DepthLeft

ObstacleRight

ObstacleVelocity

Matlab’s results

Blade1/Blade2

CodeBlock’s results

Blade1/Blade275 5 5 75 90/62 92/64.5580 6 9 18 13/53 15/5010 6 9 18 17/129 15/130

29.89 8.97 5.3 6.17 129/133 130/13025.62 3.42 5.32 17.17 68/150 68.8/151.264

15.8452 8.472 2.32 7.127 159/135 162/13090 9.472 9.32 9.127 90/90 92/907 9 9.32 9.13 90/90 92/90

35 4.6 9.5 9.7 53/127 52/130

6. ConclusionThere are some little differences between Matlab and CodeBlocks. This comes

from the difference of processing input and defuzzification method.If time permits, we think we can improve the C codes to enhance the quality of

results efficiently.