eFLL - A Fuzzy Library for Arduino and Embedded Systems

Leia este artigo em Português

Fuzzy Logic is an extension of traditional Boolean logic, using linguistic variables allows to express logical values ​​intermediate between FALSE and TRUE, describing with greater efficiency the uncertainty principle in the real world.

Fuzzy Systems are practical applications that employ Fuzzy Logic in its decisions making on the basis of linguistic variables and terms, the robotics and the electronic engineering has a large utility room.

Developed by Robotic Research Group (RRG) at the State University of Piauí (UESPI-Teresina) the eFLL (Embedded Fuzzy Logic Library)
library is a versatile, lightweight and efficient option to work with Fuzzy Logic in embedded systems.



Please, report bugs and suggestions with comments or in the official code page on GitHub


How to install

Easy Way

Step 1: Open the Arduino IDE

Step 2: In main menu, go to SKETCH >> INCLUDE LIBRARY >> MANAGE LIBRARIES

Step 3: Search for "eFLL" or "Fuzzy"

Step 4: eFLL will appear in the list, to finish, just click in INSTALL, now you can include eFLL to your sketchs

Old Way

Step 1: Go to the official project page on GitHub: eFLL

Step 2: Make a clone of the project using Git or download it
Download on the button "Download as zip."

Step 3: Clone or unzip the files into Arduino libraries' folder:


Obs: Rename the folder from "eFLL-master" to "eFLL"

Ubuntu (/usr/share/arduino/libraries/) if installed via apt-get, if not, on Windows, Mac or Linux (where you downloaded the Arduino IDE, the Library folder is inside)

Ok! The library is ready to be used.


How to import

If the installation of the library has been successfully held,  to import the library is easy:

Step 1: Open your Arduino IDE, check out the tab on the top menu SKETCH 
LIBRARY  → Import eFLL

Características

Written in C++/C, uses only standard C language library "stdlib.h", so eFLL is a library designed not only to Arduino, but any Embedded System or not how have your commands written in C.

It has no explicit limitations on quantity of Fuzzy, Fuzzy Rules, Inputs or Outputs, these limited processing power and storage of each microcontroller

The library uses the process:

(MAX-MIN) and (Minimum Mamdani) for inference and composition and (CENTER OF AREA) to defuzzification in a continuous universe.


Tested with GTest for C, Google Inc.

Simple example:

Speed ​​control of a robotic,  entry: Frontal distance obstacle.



Brief documentation

Fuzzy object - This object includes all the Fuzzy System, through it, you can manipulate the Fuzzy Sets, Linguistic Rules, inputs and outputs.

FuzzyInput object - This object groups all entries Fuzzy Sets that belongs to the same domain.

FuzzyOutput object - This object is similar to FuzzyInput, is used to group all output Fuzzy Sets that belongs to the same domain.

FuzzySet object - This is one of the main objects of Fuzzy Library, with each set is possible to model the system in question. Currently the library supports triangular membership functions, trapezoidal and singleton, which are assembled based on points A, B, C and D, they are passed by parameter in its constructor FuzzySet(float a, float b, float c, float d) examples:

Triangular pertinence function:

FuzzySet* fs = FuzzySet(10, 20, 20, 30);

FuzzySet* fs = FuzzySet(10, 33, 33, 33);

FuzzySet* fs = FuzzySet(5, 5, 5, 30);

Trapezoidal pertinence function:
FuzzySet* fs = FuzzySet(10, 20, 30, 40);

FuzzySet* fs = FuzzySet(0, 0, 10, 20);
Any value below 10 will have pertinence = 1
FuzzySet* fs = FuzzySet(20, 30, 40, 40);
Any value above 30 will have pertinence = 1

Singleton pertinence function:
FuzzySet* fs = FuzzySet(20, 20, 20, 20);

FuzzyRule object - This object is used to mount the base rule of Fuzzy object, which contains one or more of this object. Instantiated with FuzzyRule fr = new FuzzyRule (ID, antecedent, consequent)

FuzzyRuleAntecedent object - This object is used to compound the object FuzzyRule, responsible for assembling the antecedent of the conditional expression of a FuzzyRule, examples:

"IF distance = small THEN velocity = slow"

FuzzyRuleAntecedent* ifDistanceSmall = new FuzzyRuleAntecedent(); ifDistanceSmall->joinSingle(small);

The method joinSingle(FuzzySet* fuzzySet) is used to build simple expressions IF/THEN. To compose more complex expressions there are other special methods.

"IF temperature = hot AND pressure = hight THEN rick = big"

FuzzyRuleAntecedent* ifTemperatureHotAndPressureHight = new FuzzyRuleAntecedent();
ifTemperatureHotAndPressureHight->joinWithAND(hot, hight); 

"IF temperature = hot OR pressure = hight THEN rick = big"

FuzzyRuleAntecedent* ifTemperatureHotAndPressureHight = new FuzzyRuleAntecedent();
ifTemperatureHotAndPressureHight->joinWithOR(hot, hight);

The methods joinWithAND(FuzzySet* fuzzySet1, FuzzySet* fuzzySet2) and joinWithOR(FuzzySet* fuzzySet1, FuzzySet* fuzzySet2) can make logical compositions between Fuzzy Sets. These methods also have more advanced variations that allow further enhance the expression, they are:

bool joinWithAND(FuzzySet* fuzzySet, FuzzyRuleAntecedent* fuzzyRuleAntecedent);
bool joinWithAND(FuzzyRuleAntecedent* fuzzyRuleAntecedent, FuzzySet* fuzzySet);
bool joinWithOR(FuzzySet* fuzzySet, FuzzyRuleAntecedent* fuzzyRuleAntecedent);
bool joinWithOR(FuzzyRuleAntecedent* fuzzyRuleAntecedent, FuzzySet* fuzzySet);
bool joinWithAND(FuzzyRuleAntecedent* fuzzyRuleAntecedent1, FuzzyRuleAntecedent* fuzzyRuleAntecedent2);
bool joinWithOR(FuzzyRuleAntecedent* fuzzyRuleAntecedent1, FuzzyRuleAntecedent* fuzzyRuleAntecedent2);

examples:

"IF (velocity = hight AND distance = small) OR fuel = low THEN velocity = small AND consumption = short"

FuzzyRuleAntecedent* speedHightAndDistanceSmall = new FuzzyRuleAntecedent();
speedHightAndDistanceSmall->joinWithAND(hight, small); 

FuzzyRuleAntecedent* fuelLow = new FuzzyRuleAntecedent();
fuelLow->joinSingle(low); 

// Este objeto FuzzyRuleAntecedente é que será usada para compor o objeto FuzzyRule 
FuzzyRuleAntecedent* ifSpeedHightAndDistanceSmallOrFuelLow = new FuzzyRuleAntecedent();
ifSpeedHightAndDistanceSmallOrFuelLow->joinWithOR(speedHightAndDistanceSmall, fuelLow); 

Using these methods, any expression can be mounted, FuzzyRuleAntecedent can be used to compose another object FuzzyRuleAntecedent, in many different ways. 
OBS:. in the previous example, the final antecedent was composed of  speedHightAndDistanceSmall and fuelLow objects, but the latter could be replaced without loss by the FuzzySet object low, since it is a simple expression, without any conditional operator:

FuzzyRuleAntecedent* speedHightAndDistanceSmall = new FuzzyRuleAntecedent();
speedHightAndDistanceSmall->joinWithAND(hight, small); 

// Este objeto FuzzyRuleAntecedente é que será usada para compor o objeto FuzzyRule 
FuzzyRuleAntecedent* ifSpeedHightAndDistanceSmallOrFuelLow = new FuzzyRuleAntecedent();
ifSpeedHightAndDistanceSmallOrFuelLow->joinWithOR(speedHightAndDistanceSmall, low); 

FuzzyRuleConsequente object - This object is used to render the object FuzzyRule, responsible for assembling the output expression of a FuzzyRule, examples:

"IF disctance = small THEN velocity = slow"

FuzzyRuleConsequent* thenSpeedSlow = new FuzzyRuleConsequent(); 
thenSpeedSlow->addOutput(slow);

What would result in an FuzzyRule object like:

FuzzyRule* fuzzyRule = new FuzzyRule(2, ifDistanceSmall, thenSpeedSlow);

"IF (velocity = hight AND distance = small) OR fuel = low THEN velocity = small AND consumption = short"

FuzzyRuleConsequent* thenSpeedSmallAndFeedTine = new FuzzyRuleConsequent(); thenSpeedSmallAndFeedSmall->addOutput(small);

thenSpeedSmallAndFeedSmall->addOutput(tine);  

For the object FuzzyRuleConsequent entire expression is mounted using the method addOutput(FuzzySet* fuzzySet);

What would result in an FuzzyRule object like:

FuzzyRule* fuzzyRule = new FuzzyRule(2, ifSpeedHightAndDistanceSmallOrFuelLow, thenSpeedSmallAndFeedTine);

After assembling an FuzzyRule object, use the method addFuzzyRule(FuzzyRule* fuzzyRule); to add it to Fuzzy object base rule, repeat the same process for all the rules.

Tip

These are all eFLL library objects that are used in the process. The next step, generally interactive is handled by three methods of the Fuzzy Class first:

bool setInput(int id, float value);

It is used to pass the
Crispe input value to the system note that the first parameter is the FuzzyInput object' ID which parameter value is intended.

bool
fuzzify();

It is used to start the fuzzification process, composition and inference.

And finally:


float defuzzify(int id);

It is used to finalize the fuzzification
process, notice that the param ID belongs to FuzzyOutput object which you want to get the defuzzification value.

Hint: Sometimes is necessary to know the pertinence with which some or each fuzzy set was activated. To do this, use the method float getPertinence();  of FuzzySet Class, eg:


FuzzySet* hot = new FuzzySet(30, 50, 50, 70);
...
... // After fuzzification with ->fuzzyfy();
...
float pertinenceOfHot = hot->getPertinence();

Or whether a particular rule was fired, use the method bool isFiredRule(int ruleId); of Fuzzy object.

FuzzyRule* fuzzyRule = new FuzzyRule(2, ifDistanceSmall, thenSpeedSlow);
...
... // After fuzzification with ->fuzzyfy();
...
bool wasTheRulleFired = fuzzy->isFiredRule(2);

Advanced example:

eFLL - A Fuzzy Library for Arduino and Embedded Systems eFLL - A Fuzzy Library for Arduino and Embedded Systems Reviewed by AJ Alves on sexta-feira, setembro 28, 2012 Rating: 5

43 comentários:

Arbie disse...

AJ O. Alves

Thanks for the team who want to share this library for arduino. what a great job's.. :) by the way i want to ask about the variabel get from ADC (example input LM35 & output relay) where do i place analogRead('variabel') as a fuzzy input and digitalWrite(13,HIGH/LOW) as a fuzzy output i'm still confuse it..

i want to apologize cause bad english (i'm from indonesia),I really appreciate your help and send me some tutorial
bimo.ardi.h@gmail.com

thanks before.. :)

AJ Alves disse...

Thanks Arbie!

As I replied to you via email.
But his doubts may be the same as others, then:
To see an example of how to use the library and make decisions across the state rules with Fuzzy::isFiredRule(int id); see https://gist.github.com/4110773

Pat disse...

Thank you very much for providing this great library to the Adruino community!
I'm currently using it in a "robotic fireman toy truck" project to drive the speed and direction of the truck.

I'm just beginning the implementation but thanks to your doc and samples it's going fine.

Patrice
http://breizhmakers.over-blog.com/

Arbie disse...

Hi AJ...

i've got trouble at result of defuzzyfy when i have 2 or more fuzzyrule using same fuzzyrule consequent but different fuzzyantecedent...

but when i have 1 fuzzyrule using different fuzzyrule consequent it works..

why..??
example i' have 2 input
1.temperature have 5 variable
2.moisture have 3 Variable

and 1 output
1.time have 7 variable

so i must building 3x5 fuzzyrule = 15 fuzzyrule, there 12 fuzzyrule have same fuzzyconsequent... any idea to solve my problem..??

thank you.. for helping me :)

thank

AJ Alves disse...

Thanks one more time Arbie!!!
This fix, was solved!
You're helping to improve our library!!!

AlanK disse...

Thank you so much for providing this fantastic library as an open source. I can finally finish something I started looking at many years ago but never quite got around to finishing!

Christophe disse...

Very interesting indeed. I don’t have a lot of experience with libraries, but I’d like to share two ideas :
- is it possible to have a “boolean” output with defuzzyfication ? Most outputs are boolean. I know you can arrange that with some “slow PWM”, but there should be a better way
- is it possible to reuse fuzzysets ? In the example I see the same set defined more than once.
- using some #defines could allow to make the usage a bit lighter. IMHO it would allow to reduce verbosity/hide the OO construction/links. For example

#define IFINPUTSET(name,set) FuzzyRuleAntecedent* name = new FuzzyRuleAntecedent(); name->joinSingle(set);

Anônimo disse...

can the system read negative value? i try to feed the system with negative input, but the output is wrong, any solution. thanks

AJ Alves disse...

Christophe, thanks so much for your notes!

AJ Alves disse...

Hey Unknow, it is possible! One of my tests is using only negative numbers in FuzzySets and Inputs, and always i get a good result.

Please, can you explain better what you are trying to do, how are you building your FuzzySets. Take a good look in "Advanced Example" in this post, i use a negative FuzzySet called "cold" and after i pass a negative value in the third input.

Anônimo disse...
Este comentário foi removido pelo autor.
Anônimo disse...
Este comentário foi removido pelo autor.
Anônimo disse...

unknow here, the library works, i'm sory about that

AJ Alves disse...

Ok Sam, thanks to use our library. any doubt, aj.alves@zerokol.com

sreeram disse...

i've got trouble at result of defuzzyfy when i have 2 or more fuzzyrule using same fuzzyrule consequent but different fuzzyantecedent...

but when i have 1 fuzzyrule using different fuzzyrule consequent it works..

why..??
example i' have 2 input
1.temperature have 4 variable
2.humidity have 4 Variable

AJ Alves disse...

Sreeram, shuld you send me a email (aj.alves@zerokol.com) with the source code?

LectorCompulsivo disse...

Just for those who have the same problem: The library cannot be imported to Arduino unless that the folder name will be changed from eFLL-master to eFLLmaster.

Thanks to all who disinterestedly contribute!

Anônimo disse...

Hi, i have same problem like Sreeram, i have 2 inputs, and one output.
"when i have 2 or more fuzzyrule using same fuzzyrule consequent but different fuzzyantecedent"

can someone help?
my email ketcchman@gmail.com
or skype pente_

sreeram disse...

I had change the library name eFLL-master to eFLL and still it doesn't works.

Unknown disse...

Hi AJ...

i've got trouble at result of defuzzyfy when i have 2 or more fuzzyrule using same fuzzyrule consequent but different fuzzyantecedent...

why..??
example i' have 3 input from gyro sensor
1.X-axis have 4 variable
2.Y-axis have 4 Variable
3.Z-axis have 4 Variable

and 1 output
1.move have 6 variable

so i must building 4x4x4 fuzzyrule??

I really appreciate your help and send me some tutorial
rezazulfan@gmail.com
thanks before.. for helping me :)

Unknown disse...

how i know which desfuzzy method im using ?
And by the way fuzzy->setInput(1, dist); how i know the correct value for Dist ?

Unknown disse...

Boa Tarde meu amigo. Estou tentando utilizar a sua biblioteca para desenvolver um robô que desvia de obstáculos. Ele possui 4 sensores infravermelhos da Sharp ( 3 frontais e um traseiro ). Estou com um pouco de dificuldade para saber quais dados coloco na função "FuzzySet(0, 20, 20, 40)". As fotos do seu site não estão carregando. Sabe dizer quando voltará ao normal? Você poderia me enviar esse manual?

John S disse...

Hi, I'm working in a system to control a dc/dc converter, and I have two inputs, 1 output and 16 rules. I designed the controller in Matlab and tested on arduino. The problem is that the same controller has different values for the output when tested in Matlab and Arduino. When one rule is fired, can I know their value at the output membership?, the thing is that I have 4 active rules and the output in Matlab is the average of the memberships, but in Arduino seems to be the maximum of memberships. I was wondering what does minimum-mandani mean at the output?
Thanks

amruta disse...

Thank you so much for the library

Unknown disse...

Thanks for such a library! is excellent!
I Have a question. When you are controlling the velocity ¿are you using PWM values?
Thanks!

AJ Alves disse...

Yes! PWM

Unknown disse...

obviously pwm power

Unknown disse...

I just wanna say Thank you so much.
I used EFFL for programing of a line follower robot and it works very good and fast.(It is fast because most of the code is written in void setup not void loop unlike http://www.makeproto.com/projects/fuzzy/matlab_arduino_FIST/index.php)

thanks a lot it was exellent.

Unknown disse...

Hello good day

It makes me very interesting, but I am very new to it and I want to add 5 sounds but I do not know how to do it.
Try adding more fuzzy objects but no results
Would be of great help if you could answer us

Unknown disse...

Hola buen día su código me parece excelente pero tengo una duda al respecto de cómo usar la librería fuzzy ya que quiero incorporar en mi proyecto 5 sensores ultrasónicos los cuales no he podido porque no reaccionan bien. Sólo me ha salido con un sensor ultrasónico pero me sería de gran ayuda el orientarme como implementar en 5 sensores

Stefan disse...

I have aerror:

exit status 1
'getDistanceFromSonar' was not declared in this scope

AJ Alves disse...

Hello Stefan.

Sorry, 'getDistanceFromSonar' and 'setRobotSpeed' was removed, thise function are out of Fuzzy scope, you must to implement they following your own model.

Unknown disse...

disculpe como se puede implementar 5 sensores ultrasónicos con esta librería? logré que funcionara con dos pero con 5 no funciona que debo de hacer? ya intenté de todo y desconozco bastante de la lógica difusa.

AJ Alves disse...

Daniela Harrison, vejo que seu problema é mais voltado ao entendimento da técnica. O ideial que você leia sobre Lógica Fuzzy. MAs, para fazer funcionar com 5 sensores, basta acrescentar mais conjuntos de entrada, e de saída se for necessário, regras, e passar os valores. Como você está montando?

Unknown disse...

How to create 2 Instantiating an object library ?

Ari Sant disse...

Saudações, meu nome é Ari Santoso, com idade de 59 anos, um instrutor em engenharia elétrica politécnico Semarang, Java Central, na Indonésia. Eletrônica hoby, comunicações (rádio amador) e Mecatrônica.
Estou interessado na Lógica Fuzzy Controller, e são feitas no sistema microcontrolador Arduino 101.
Estou muito interessado em escrever você, sobre FLC em Arduino, eu quero aprender e buscar informações sobre ele de você.
Thanks.
Saudações da Indonésia, se um período de férias para Bali, por favor mande Semarang,

Ari Santoso

washito disse...

Dear AJ O. Alves
how we write the code if we had 2 or more outputs from different between Error & DError, and how to write at fuzzy rules then how to activate output at main program.
Thanks a lot for the usefull library

Unknown disse...

Hello AJ O. Alves, Thank you for your work on creating this fantastic library, i have only one important question, Fuzzy controller Needs The Error and The Error Rate, your Simple example we can calculate The Error (reference Distance-Actual Distance) but i also Need The error Rate to build My Fuzzy table(experience Table). where is the Error rate ? is it calculated inside your library ? or i should know my error rate And add it as a fuzzyInput ? Thank you again very very very Much for your Hard work on This Library. <3

encuestas online disse...

Hola amigo soy de Venezuela, Muchas gracias por el desarrollo de la libreria , nos ayudo a desarrollar nuestro prototipo agradecido eternamente

Unknown disse...

Hello AJ O. Alves, thanks for the powerfull lib that you share..i have some problem here, when i try to add more output (2) there is get zero output, i just following every step that you share on the example. Is there have any limitation regarding the maximum input, rule or output for use ?

Unknown disse...

Hello sir AJ O. Alves, Your library solved a lots of problem of mine..Im really really greatfull to you sir..I have a question about the library..can it process more than 50 rules in arduino?? I'm having some issues...I have 54 rules, and Its not showing me the output..can you please help me giving a idea why its not...Thanks in advance sir

AJ Alves disse...

Hello Bahtiar Yoga and Farhat Shahir; I should work with more outputs, review all steps, test code in pieces; And the hardware is a limitation, test in a powerfull Arduino, like Mega or Due

Unknown disse...

Hello sir AJ O. Alves,
i have a problem with my sketch.. i have two input fuzzy : input fuzzy (1) = velocity1 & input fuzzy (2) = velocity 2. the input fuzzy have 5 membership and output fuzzy is time.. the output fuzzy have 7 membership.. when input fuzzy (1) is 13 cm/s and input fuzzy (2) is 0 cm/s then the result of output fuzzy is 7,63 second then the result change be 5,5 second then change be 5,5 again and then 0 second.. the result 7,63 is right,, but the result fuzzy after 7,63 that 5,5 second.. i dont know what problem about it.. i'm very confused about it.

I want to apologize cause bad english (i'm from indonesia),I really appreciate your help and send me some tutorial
ardi.hermanto95@gmail.com

Thanks before..

Tecnologia do Blogger.