// https://github.com/alvesoaj/eFLL
// https://blog.zerokol.com/2012/09/arduinofuzzy-fuzzy-library-for-arduino.html
// wokwi lib eFLL
#include <Fuzzy.h>
Fuzzy *fuzzy = new Fuzzy(); // Instantiating a Fuzzy object
void setup()
{
Serial.begin(9600); // Set the Serial output
randomSeed(analogRead(0)); // Set a random seed
FuzzyInput *distance = new FuzzyInput(1); // Instantiating a FuzzyInput object
FuzzySet *small = new FuzzySet(0, 20, 20, 40); // Instantiating a FuzzySet object
distance->addFuzzySet(small); // Including the FuzzySet into FuzzyInput
FuzzySet *safe = new FuzzySet(30, 50, 50, 70);
distance->addFuzzySet(safe);
FuzzySet *big = new FuzzySet(60, 80, 80, 80);
distance->addFuzzySet(big);
fuzzy->addFuzzyInput(distance); // Including the FuzzyInput into Fuzzy
FuzzyOutput *speed = new FuzzyOutput(1); // Instantiating a FuzzyOutput objects
FuzzySet *slow = new FuzzySet(0, 10, 10, 20); // Instantiating a FuzzySet object
speed->addFuzzySet(slow); // Including the FuzzySet into FuzzyOutput
FuzzySet *average = new FuzzySet(10, 20, 30, 40);
speed->addFuzzySet(average);
FuzzySet *fast = new FuzzySet(30, 40, 40, 50);
speed->addFuzzySet(fast);
fuzzy->addFuzzyOutput(speed); // Including the FuzzyOutput into Fuzzy
// Building FuzzyRule "IF distance = small THEN speed = slow"
FuzzyRuleAntecedent *ifDistanceSmall = new FuzzyRuleAntecedent(); // Instantiating a FuzzyRuleAntecedent objects
ifDistanceSmall->joinSingle(small); // Creating a FuzzyRuleAntecedent with just a single FuzzySet
FuzzyRuleConsequent *thenSpeedSlow = new FuzzyRuleConsequent(); // Instantiating a FuzzyRuleConsequent objects
thenSpeedSlow->addOutput(slow); // Including a FuzzySet to this FuzzyRuleConsequent
FuzzyRule *fuzzyRule01 = new FuzzyRule(1, ifDistanceSmall, thenSpeedSlow); // Instantiating a FuzzyRule objects
fuzzy->addFuzzyRule(fuzzyRule01); // Including the FuzzyRule into Fuzzy
// Building FuzzyRule "IF distance = safe THEN speed = average"
FuzzyRuleAntecedent *ifDistanceSafe = new FuzzyRuleAntecedent();
ifDistanceSafe->joinSingle(safe);
FuzzyRuleConsequent *thenSpeedAverage = new FuzzyRuleConsequent();
thenSpeedAverage->addOutput(average);
FuzzyRule *fuzzyRule02 = new FuzzyRule(2, ifDistanceSafe, thenSpeedAverage);
fuzzy->addFuzzyRule(fuzzyRule02);
// Building FuzzyRule "IF distance = big THEN speed = high"
FuzzyRuleAntecedent *ifDistanceBig = new FuzzyRuleAntecedent();
ifDistanceBig->joinSingle(big);
FuzzyRuleConsequent *thenSpeedFast = new FuzzyRuleConsequent();
thenSpeedFast->addOutput(fast);
FuzzyRule *fuzzyRule03 = new FuzzyRule(3, ifDistanceBig, thenSpeedFast);
fuzzy->addFuzzyRule(fuzzyRule03);
}
void loop()
{
//int input = random(0, 80); // Getting a random value
for ( int input = 80; input > -1; input-- ) {
// Serial.print("\nEntrance: "); // Printing something
Serial.print("Distance: ");
Serial.print(input);
fuzzy->setInput(1, input); // Set the random value as an input
fuzzy->fuzzify(); // Running the Fuzzification
float output = fuzzy->defuzzify(1); // Running the Defuzzification
// Serial.print("\tResult: "); // Printing something
Serial.print("\tSpeed: ");
Serial.println(output);
delay(100); // wait 1 second
}
while(1){};
}