welcome: please sign in
location: Fuzzy Co-processor

Fuzzy co-processor

The following text is an ongoing work and is not complete yet. It is being refined collaboratively. Please come back to follow the enhancements or to help us finish it too.

fuzzy_cloud.png

Intro

The goal of this project is to implement a fuzzy generic controller, to be used as a "partner" or co-processor board to main MCU-based boards that have to control dynamical systems. The theory is of the generic discrete fuzzy controllers and the implementation goal is to make the fuzzy control process as fast as possible, for that a lookup table is built before the use and the control itself is turned into a simple search in an indexed array.

About generic discrete fuzzy controllers

Little intro

If you don`t know absolutely nothing about fuzzy logic, I recommend a little reading (depending on how comfortable you are about the idea of 'fuzzyness' - Try the wikipedia article).

Control using fuzzy theory was greatly promoted in the 80's in japan (sparked by hitachi engineers Seiji Yasunobu and Soji Miyamoto), they showed how fuzzy logic is efficient and do not require a precise mathematical representation of the system you are trying to control to work.

In this implementation the target was to create a full Mamdani inference system to build a generic controller. By generic discrete controller I mean:

[note 1] by "any" I mean linear (or, at least, locally linear) witch I can measure error and variation of error in time (those two are my real variables that permit calling it a "generic" controller).

Ok, so tell me more

A little bit deeper on this controllers:

Fisrt, a controller is something that tries to force a system into a desired state, this can be anything like keeping a car on the track or control the temperature of a boiler. The general representation is something like this:

So, to build a basic controller we just need to know the error (the difference between the current state to the desired state) and define what action must be executed on the system to reach the desired state. A quite simple idea. On fuzzy logic we don't need to know exactly the current state of the system, we trust on a fuzzy notion of it and we don't know exactly how much we have to actuate over it. Strange but it works! (Try to imagine the dynamics of it, not static frames and it turns quite clear).

Some key characteristics about this kind of controller:

singe.png

sets.png

How a Fuzzy Inference System Works

A fuzzy inference system is basically simple applications of if - then (inference) rules, but over fuzzy variables. So, there are some steps illustrated on the diagram bellow:

fuzzy_inf.png

This step converts a numeric input to a linguistic fuzzy variable, in our case the number is first quantized into our limited discrete universe ( [-6,6] ) according to scale factors and then the pertinence to each fuzzy set of the input is evaluated. In this discrete case this pertinence is quite easy do evaluate. Let's suppose a numeric input of 3, according to the representation of the fuzzy sets pertinence functions above, the fuzzy output will be 0.7 of pertinence to fuzzy set '2' and 0.7 of pertinence to fuzzy set '4'.

All rules in this controller are of the form If (I1 And I2) Then O1, this means the inputs are combined by an 'and' operator then there is an inference process to determine the output. In the fuzzy case, rules are represented in terms of the fuzzy sets, something like If 2 and 0 then 2 (Our fuzzy sets are named after their apex).

Representing all rules in a form of matrix[1] :

-6

-4

-2

0

2

4

6

-6

-4

-2

0

2

4

6

Having all the rules we can process the inputs and obtain the fuzzy outputs

This step is responsible for turning a fuzzy output into a numeric output that will be sent to the main processor and will be realized by an actuator. The deffuzyfication function can be user defined or the default (the implemented default is Mean of Maxima).

Can you show me?

Now a practical example of the process described above.

Implementation

code core

   1 #include "proto_controller.h"
   2 
   3 /* 
   4 Out is a structure to represent a fuzzy output 
   5 of a Mamdani inference process
   6 */
   7 typedef struct out{
   8   int group;
   9   double cut;
  10   struct out *next;
  11 }Out;
  12 
  13 Out* createOutPut(){
  14   return ((Out*)malloc(sizeof(Out)));
  15 }
  16 
  17 /* A sample defuzzyfication method (Mean of Maxima) */
  18 int MOMDefuzz(Out* out){
  19   Out* t = out;
  20   int mom = 0,k = 0;
  21   double maxCut = 0;
  22   while(t){
  23     maxCut = MAX(maxCut,t->cut);
  24     t = t->next;
  25   }
  26   t = out;
  27   while(t){
  28     if(t->cut == maxCut){
  29       mom+=t->group;
  30       k++;
  31     }
  32     t = t->next;
  33   }
  34   return ((int) round(mom/k));
  35 }
  36 
  37 /* A degree of pertinence evaluation function */
  38 double checkPert(int input,int group){
  39   int diff = abs(group-input);
  40   switch(diff){
  41     case 0: return 1.0;
  42     case 1: return 0.7;
  43     case 2: return 0.3;
  44     default: return 0.0;
  45   }
  46 }
  47 
  48 /* Inference function */
  49 int fuzzyInf(double err,double derr, double gx, double gy, int* rules,int ( * defuzzy ) ( Out * )){
  50   int xe,ye;
  51   int i = 0, j = 0, k = 0;
  52   double m1,m2;
  53   Out *outPut,*current;
  54   
  55   outPut = createOutPut();
  56   current = outPut;
  57   
  58   xe = QUANT(gx,err);
  59   ye = QUANT(gy,derr);
  60   
  61   for(i=0;i<NUMOFGROUPS;i++){
  62     for(j=0;j<NUMOFGROUPS;j++){'
  63       m1 = checkPert(xe,2*i-6);
  64       m2 = checkPert(ye,2*j-6);
  65       if(m1!=0 && m2!=0){
  66         k++;
  67         current->group = rules[i+NUMOFGROUPS*j];
  68         current->cut = MIN(m1,m2);
  69         current->next = createOutPut();
  70         current = current->next;
  71       }
  72     }
  73   }
  74   current = NULL;
  75   return defuzzy(outPut);
  76   
  77 }

Results

Tests

Credits

This paper has been presented on the CBSF - Brasilian Congress of Fuzzy Systems at Sorocaba, São Paulo, Nov 2010.

Fuzzy Co-processor (last edited 2010-12-02 15:46:27 by Bittencourt)