Fuzzy coprocessor
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.
Intro
The goal of this project is to implement a fuzzy generic controller, to be used as a "partner" or coprocessor board to main MCUbased 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:
 It may control "any"[note 1] dynamical system
 It can be scaled up to complex systems
 We have a finite number of possible entries
[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:
 The numeric universe of inputs and output is quantized (limited and discrete) into {6,5,4,3,2,1,0,1,2,3,4,5,6}
 Any input value can only have one of those four pertinence levels to a set: {0, 0.3, 0.7, 1}. An example of a pertinence function to the set 0 (zero) illustrated bellow:
 There are only seven fuzzy sets for each input and for the output, distributes as shown bellow:
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:
 'Fuzzyfication'
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'.
 Inference
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
 'Defuzzyfication'
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(groupinput);
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*i6);
64 m2 = checkPert(ye,2*j6);
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.