eFLL
eFLL copied to clipboard
Strange Outptut
Hi AJ,
I'm using the Fuzzy.h code that you defined for a project, but my output is strange. Are you able to check my code?
#include <Fuzzy.h>
// 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 // all FuzzySet pertinence
// Fuzzy Fuzzy *fuzzy = new Fuzzy();
// setting up the values our input variables can take
//Inputs (hight, medium, Low) defined in Matlab // FuzzyInput - Ambient Temperature Inputs FuzzySet *lowAT = new FuzzySet(0, 20, 20, 40); FuzzySet *mediumAT = new FuzzySet(30, 50, 50, 70); FuzzySet *highAT = new FuzzySet(60, 80, 100, 100);
// FuzzyInput - Light Inputs FuzzySet *lowL = new FuzzySet(5, 15, 20, 25); FuzzySet *highL = new FuzzySet(20, 30, 30, 50);
// FuzzyInput - Time Inpits FuzzySet *lowT = new FuzzySet(0, 4, 8, 12); FuzzySet *highT = new FuzzySet(12, 16, 20, 24);
//Outpits (open or close) defined in Matlab // FuzzyOutput FuzzySet *closeBlinds = new FuzzySet(0, 20, 20, 40); FuzzySet *openBlinds = new FuzzySet(30, 50, 50, 70);
void setup() { // Set the Serial output Serial.begin(9600); // Set a random seed randomSeed(analogRead(0));
// Every setup must occur in the function setup()
// FuzzyInput - Declaring our input variables from above with the inputs FuzzyInput *ambientTemp = new FuzzyInput(1);
ambientTemp->addFuzzySet(lowAT); ambientTemp->addFuzzySet(mediumAT); ambientTemp->addFuzzySet(highAT); fuzzy->addFuzzyInput(ambientTemp);
// FuzzyInput FuzzyInput *light = new FuzzyInput(2);
light->addFuzzySet(lowL); light->addFuzzySet(highL); fuzzy->addFuzzyInput(light);
// FuzzyInput FuzzyInput *timeOfDay = new FuzzyInput(3);
timeOfDay->addFuzzySet(lowT); timeOfDay->addFuzzySet(highT); fuzzy->addFuzzyInput(timeOfDay);
// FuzzyOutput FuzzyOutput *resultBlinds = new FuzzyOutput(1);
resultBlinds->addFuzzySet(openBlinds); resultBlinds->addFuzzySet(closeBlinds);
fuzzy->addFuzzyOutput(resultBlinds);
// Building FuzzyRules - Defined on Matlab
//First Rule FuzzyRuleAntecedent *ifAmbientTempLowAndLightHigh = new FuzzyRuleAntecedent(); ifAmbientTempLowAndLightHigh->joinWithAND(lowAT, highL);
FuzzyRuleConsequent *thenOpenBlinds = new FuzzyRuleConsequent(); thenOpenBlinds->addOutput(openBlinds);
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifAmbientTempLowAndLightHigh, thenOpenBlinds); fuzzy->addFuzzyRule(fuzzyRule1);
//Second Rule FuzzyRuleAntecedent *ifAmbientTempHighAndLightLow = new FuzzyRuleAntecedent(); ifAmbientTempHighAndLightLow->joinWithAND(highAT, lowL);
FuzzyRuleConsequent *thenCloseBlinds = new FuzzyRuleConsequent(); thenCloseBlinds->addOutput(closeBlinds);
FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifAmbientTempHighAndLightLow, thenCloseBlinds); fuzzy->addFuzzyRule(fuzzyRule2);
//Third Rule FuzzyRuleAntecedent *ifAmbientTempMediumAndLightHigh = new FuzzyRuleAntecedent(); ifAmbientTempMediumAndLightHigh->joinWithAND(mediumAT, highL);
// FuzzyRuleConsequent *thenCloseBlinds = new FuzzyRuleConsequent(); thenCloseBlinds->addOutput(closeBlinds);
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifAmbientTempMediumAndLightHigh, thenCloseBlinds); fuzzy->addFuzzyRule(fuzzyRule3); }
void loop() { // get random entrances int input1 = random(0, 100); int input2 = random(0, 70); int input3 = random(0, 24);
Serial.println("\n\n\nEntrance: "); Serial.print("\t\t\tAmbient Temperature: "); Serial.print(input1); Serial.print(", Light: "); Serial.print(input2); Serial.print(", and Time: "); Serial.println(input3);
fuzzy->setInput(1, input1); fuzzy->setInput(2, input2); fuzzy->setInput(3, input3);
fuzzy->fuzzify();
Serial.println("Input: "); Serial.print("\tAmbient Temperature: low-> "); Serial.print(lowAT ->getPertinence()); Serial.print(", medium-> "); Serial.print(mediumAT->getPertinence()); Serial.print(", high-> "); Serial.println(highAT->getPertinence());
Serial.print("\tLight: low-> "); Serial.print(lowL->getPertinence()); Serial.print(", high-> "); Serial.print(highL->getPertinence());
Serial.print("\tTime: Cold-> "); Serial.print(highT->getPertinence()); Serial.print(", high-> "); Serial.print(lowT->getPertinence()); Serial.println(", low-> ");
float output1 = fuzzy->defuzzify(1);
Serial.println("Output: "); Serial.print("\tBlinds: Open-> "); Serial.print(openBlinds->getPertinence()); Serial.print(", Close-> "); Serial.println(closeBlinds->getPertinence());
Serial.println("Result: "); Serial.print("\t\t\tResult of Blinds: "); Serial.print(output1);
// wait 12 seconds delay(12000); }
Apparently your code is good, I strong recommend you to print and check which rules were activated and check if they are correct settled.