eFLL - A Fuzzy Library For Arduino and Embedded Systems
eFLL - A Fuzzy Library For Arduino and Embedded Systems
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
Step 4: eFLL will appear in the list, to finish, just click in INSTALL, now you can include eFLL to
your sketchs
Old Way
1 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
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)
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 →
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.
2 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
It has no explicit limitations on quantity of Fuzzy, Fuzzy Rules, Inputs or Outputs, these limited
processing power and storage of each microcontroller
(MAX-MIN) and (Minimum Mamdani) for inference and composition and (CENTER OF AREA)
to defuzzification in a continuous universe.
Simple example:
#include <Fuzzy.h>
void setup()
3 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
Serial.begin(9600);
randomSeed(analogRead(0));
distance->addFuzzySet(small);
distance->addFuzzySet(safe);
4 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
distance->addFuzzySet(big);
fuzzy->addFuzzyInput(distance);
speed->addFuzzySet(slow);
speed->addFuzzySet(average);
5 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
speed->addFuzzySet(fast);
fuzzy->addFuzzyOutput(speed);
ifDistanceSmall->joinSingle(small);
thenSpeedSlow->addOutput(slow);
6 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
fuzzy->addFuzzyRule(fuzzyRule01);
ifDistanceSafe->joinSingle(safe);
thenSpeedAverage->addOutput(average);
7 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
fuzzy->addFuzzyRule(fuzzyRule02);
ifDistanceBig->joinSingle(big);
thenSpeedFast->addOutput(fast);
fuzzy->addFuzzyRule(fuzzyRule03);
8 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
void loop()
// Printing something
Serial.println("\n\n\nEntrance: ");
Serial.print("\t\t\tDistance: ");
Serial.println(input);
fuzzy->setInput(1, input);
fuzzy->fuzzify();
// Printing something
9 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
Serial.println("Result: ");
Serial.print("\t\t\tSpeed: ");
Serial.println(output);
// wait 12 seconds
delay(12000);
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:
10 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
11 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
12 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
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:
13 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
The method joinSingle(FuzzySet* fuzzySet) is used to build simple expressions IF/THEN. To compose
more complex expressions there are other special methods.
14 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
examples:
"IF (velocity = hight AND distance = small) OR fuel = low THEN velocity = small AND consumption
= short"
// 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:
15 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
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 (velocity = hight AND distance = small) OR fuel = low THEN velocity = small AND consumption
= short"
thenSpeedSmallAndFeedSmall->addOutput(tine);
16 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
For the object FuzzyRuleConsequent entire expression is mounted using the method
addOutput(FuzzySet* fuzzySet);
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:
Crispe input value to the system note that the first parameter is the FuzzyInput object' ID which
parameter value is intended.
bool fuzzify();
And finally:
17 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
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:
Or whether a particular rule was fired, use the method bool isFiredRule(int ruleId); of Fuzzy object.
Advanced example:
#include <Fuzzy.h>
18 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
// For scope, instantiate all objects you will need to access in loop()
// It may be just one Fuzzy, but for demonstration, this sample will print
// Fuzzy
// FuzzyInput
// FuzzyInput
19 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
// FuzzyInput
// FuzzyOutput
// FuzzyOutput
20 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
void setup()
Serial.begin(9600);
randomSeed(analogRead(0));
// FuzzyInput
distance->addFuzzySet(near);
distance->addFuzzySet(safe);
distance->addFuzzySet(distant);
fuzzy->addFuzzyInput(distance);
21 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
// FuzzyInput
speedInput->addFuzzySet(stopedInput);
speedInput->addFuzzySet(slowInput);
speedInput->addFuzzySet(normalInput);
speedInput->addFuzzySet(quickInput);
fuzzy->addFuzzyInput(speedInput);
// FuzzyInput
temperature->addFuzzySet(cold);
temperature->addFuzzySet(good);
temperature->addFuzzySet(hot);
fuzzy->addFuzzyInput(temperature);
22 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
// FuzzyOutput
risk->addFuzzySet(minimum);
risk->addFuzzySet(average);
risk->addFuzzySet(maximum);
fuzzy->addFuzzyOutput(risk);
// FuzzyOutput
speedOutput->addFuzzySet(stopedOutput);
speedOutput->addFuzzySet(slowOutput);
speedOutput->addFuzzySet(normalOutput);
speedOutput->addFuzzySet(quickOutput);
fuzzy->addFuzzyOutput(speedOutput);
23 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
// Building FuzzyRule
distanceNearAndSpeedQuick->joinWithAND(near, quickInput);
temperatureCold->joinSingle(cold);
ifDistanceNearAndSpeedQuickOrTemperatureCold->joinWithOR(distanceNearAndSpeedQuick,
temperatureCold);
thenRisMaximumAndSpeedSlow->addOutput(maximum);
thenRisMaximumAndSpeedSlow->addOutput(slowOutput);
fuzzy->addFuzzyRule(fuzzyRule1);
24 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
// Building FuzzyRule
distanceSafeAndSpeedNormal->joinWithAND(safe, normalInput);
ifDistanceSafeAndSpeedNormalOrTemperatureGood->joinWithOR(distanceSafeAndSpeedNormal,
good);
thenRiskAverageAndSpeedNormal->addOutput(average);
thenRiskAverageAndSpeedNormal->addOutput(normalOutput);
fuzzy->addFuzzyRule(fuzzyRule2);
// Building FuzzyRule
25 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
distanceDistantAndSpeedSlow->joinWithAND(distant, slowInput);
ifDistanceDistantAndSpeedSlowOrTemperatureHot->joinWithOR(distanceDistantAndSpeedSlow,
hot);
thenRiskMinimumSpeedQuick->addOutput(minimum);
thenRiskMinimumSpeedQuick->addOutput(quickOutput);
fuzzy->addFuzzyRule(fuzzyRule3);
void loop()
26 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
Serial.println("\n\n\nEntrance: ");
Serial.print("\t\t\tDistance: ");
Serial.print(input1);
Serial.print(input2);
Serial.println(input3);
fuzzy->setInput(1, input1);
fuzzy->setInput(2, input2);
fuzzy->setInput(3, input3);
fuzzy->fuzzify();
27 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
Serial.println("Input: ");
Serial.print(near->getPertinence());
Serial.print(safe->getPertinence());
Serial.println(distant->getPertinence());
Serial.print(stopedInput->getPertinence());
Serial.print(slowInput->getPertinence());
Serial.print(normalInput->getPertinence());
28 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
Serial.println(quickInput->getPertinence());
Serial.print(cold->getPertinence());
Serial.print(good->getPertinence());
Serial.println(hot->getPertinence());
Serial.println("Output: ");
Serial.print(minimum->getPertinence());
Serial.print(average->getPertinence());
29 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
Serial.println(maximum->getPertinence());
Serial.print(stopedOutput->getPertinence());
Serial.print(slowOutput->getPertinence());
Serial.print(normalOutput->getPertinence());
Serial.println(quickOutput->getPertinence());
Serial.println("Result: ");
Serial.print("\t\t\tRisk: ");
Serial.print(output1);
30 of 31 5/19/21, 1:39 AM
eFLL - A Fuzzy Library for Arduino and Embedded Systems https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino...
Serial.println(output2);
// wait 12 seconds
delay(12000);
31 of 31 5/19/21, 1:39 AM