alvesoaj / eFLL

eFLL (Embedded Fuzzy Logic Library) is a standard library for Embedded Systems
MIT License
209 stars 91 forks source link

Fuzzy rules not executed when using input from sensors #32

Closed ritz89 closed 2 years ago

ritz89 commented 2 years ago

I'm doing some simple project by following sample code, up until testing using a random number as input everything is going as it should be. However after I integrated input from DHT22 sensor, the rules is not working at all. image

then I try to use manual input with the exactly same value from the sensors, and then the rules is working. image

is there any solution for this case?

here is my code:

include

include "DHT.h"

define DHTPIN 2

define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

// Fuzzy Fuzzy *fuzzy = new Fuzzy();

// FuzzyInput FuzzySet dingin = new FuzzySet(0, 0, 20, 22); //trap FuzzySet ideal = new FuzzySet(22, 24, 24, 28); // tri FuzzySet toleransi = new FuzzySet(26, 31, 31, 31); //tri FuzzySet panas = new FuzzySet(30, 31, 32, 32); // trap

// FuzzyInput FuzzySet kering = new FuzzySet(0, 0, 50, 65); //trap FuzzySet kideal = new FuzzySet(65, 70, 70, 75); // tri FuzzySet *lembab = new FuzzySet(73, 74, 75, 75); //tri

//output 1 fan out, 2 cooling, 3 cooling, 4 heating // FuzzyOutput FuzzySet fanOn = new FuzzySet(1, 1, 1, 1); FuzzySet fanOff = new FuzzySet(0, 0, 0, 0);

FuzzySet cooling0 = new FuzzySet(0, 0, 0, 0); FuzzySet cooling1 = new FuzzySet(0, 2, 2, 2); FuzzySet *cooling2 = new FuzzySet(1, 2 , 3, 3);

FuzzySet heatingOn = new FuzzySet(1, 1, 1, 1); FuzzySet heatingOff = new FuzzySet(0, 0, 0, 0);

//Relay and Sensors const int relayKipas = 4; //pin3 const int relayPendingin1 = 5; //pin3 const int relayPendingin2 = 6; //pin3 const int relayLampuPemanas = 7; //pin3 int relayON = LOW; //relay nyala int relayOFF = HIGH; //relay mati

void setup() { pinMode(relayKipas, OUTPUT); pinMode(relayPendingin1, OUTPUT); pinMode(relayPendingin2, OUTPUT); pinMode(relayLampuPemanas, OUTPUT); dht.begin(); // Set the Serial output Serial.begin(9600); // Set a random seed randomSeed(analogRead(0));

// Every setup must occur in the function setup()

// FuzzyInput FuzzyInput *temperatur = new FuzzyInput(1);

temperatur->addFuzzySet(dingin); temperatur->addFuzzySet(ideal); temperatur->addFuzzySet(toleransi); temperatur->addFuzzySet(panas); fuzzy->addFuzzyInput(temperatur);

// FuzzyInput FuzzyInput *kelembapan = new FuzzyInput(2);

kelembapan->addFuzzySet(kering); kelembapan->addFuzzySet(kideal); kelembapan->addFuzzySet(lembab); fuzzy->addFuzzyInput(kelembapan);

// FuzzyOutput fan mengurangi kelembapan, mengurangi panas sedikit, FuzzyOutput *fan = new FuzzyOutput(1); fan->addFuzzySet(fanOn); fan->addFuzzySet(fanOff); fuzzy->addFuzzyOutput(fan);

// FuzzyOutput FuzzyOutput *cooling = new FuzzyOutput(2); cooling->addFuzzySet(cooling0); cooling->addFuzzySet(cooling1); cooling->addFuzzySet(cooling2); fuzzy->addFuzzyOutput(cooling);

// fuzzy output FuzzyOutput * heating = new FuzzyOutput(3); heating->addFuzzySet(heatingOn); heating->addFuzzySet(heatingOff); fuzzy->addFuzzyOutput(heating);

// rule 1 FuzzyRuleAntecedent *keringDingin = new FuzzyRuleAntecedent(); keringDingin->joinWithAND(kering, dingin);

FuzzyRuleConsequent *hangatkanSedikit = new FuzzyRuleConsequent(); hangatkanSedikit->addOutput(fanOff); hangatkanSedikit->addOutput(cooling0); hangatkanSedikit->addOutput(heatingOn);

FuzzyRule *fuzzyRule01 = new FuzzyRule(1, keringDingin, hangatkanSedikit); fuzzy->addFuzzyRule(fuzzyRule01);

// rule 2 FuzzyRuleAntecedent *keringIdeal = new FuzzyRuleAntecedent(); keringIdeal->joinWithAND(kering, ideal);

FuzzyRuleConsequent *tahanKondisi = new FuzzyRuleConsequent(); tahanKondisi->addOutput(fanOff); tahanKondisi->addOutput(cooling0); tahanKondisi->addOutput(heatingOff);

FuzzyRule *fuzzyRule02 = new FuzzyRule(2, keringDingin, tahanKondisi); fuzzy->addFuzzyRule(fuzzyRule02);

// rule 3 FuzzyRuleAntecedent *keringtoleransi = new FuzzyRuleAntecedent(); keringtoleransi->joinWithAND(kering, toleransi);

FuzzyRuleConsequent *turunkanSedikit = new FuzzyRuleConsequent(); turunkanSedikit->addOutput(fanOn); turunkanSedikit->addOutput(cooling1); turunkanSedikit->addOutput(heatingOff);

FuzzyRule *fuzzyRule03 = new FuzzyRule(3, keringtoleransi, turunkanSedikit); fuzzy->addFuzzyRule(fuzzyRule03);

// rule 4 FuzzyRuleAntecedent *keringPanas = new FuzzyRuleAntecedent(); keringPanas->joinWithAND(kering, panas);

FuzzyRuleConsequent *dinginkan = new FuzzyRuleConsequent(); dinginkan->addOutput(fanOff); dinginkan->addOutput(cooling2); dinginkan->addOutput(heatingOff);

FuzzyRule *fuzzyRule04 = new FuzzyRule(4, keringPanas, dinginkan); fuzzy->addFuzzyRule(fuzzyRule04);

// rule 5 FuzzyRuleAntecedent *idealDingin = new FuzzyRuleAntecedent(); idealDingin->joinWithAND(kideal, dingin);

FuzzyRule *fuzzyRule05 = new FuzzyRule(5, idealDingin, hangatkanSedikit); fuzzy->addFuzzyRule(fuzzyRule05);

// rule 6 FuzzyRuleAntecedent *idealIdeal = new FuzzyRuleAntecedent(); idealIdeal->joinWithAND(kideal, ideal);

FuzzyRuleConsequent *tahanDingin = new FuzzyRuleConsequent(); tahanDingin->addOutput(fanOn); tahanDingin->addOutput(cooling1); tahanDingin->addOutput(heatingOff);

FuzzyRule *fuzzyRule06 = new FuzzyRule(6, idealIdeal, dinginkan); fuzzy->addFuzzyRule(fuzzyRule06);

// rule 7-8 FuzzyRuleAntecedent *kidealToleransi = new FuzzyRuleAntecedent(); idealIdeal->joinWithAND(kideal, toleransi);

FuzzyRuleAntecedent *kidealPanas = new FuzzyRuleAntecedent(); kidealPanas->joinWithAND(kideal, panas);

FuzzyRuleConsequent *dinginkan2 = new FuzzyRuleConsequent(); dinginkan2->addOutput(fanOn); dinginkan2->addOutput(cooling2); dinginkan2->addOutput(heatingOff);

FuzzyRule fuzzyRule07 = new FuzzyRule(7, kidealToleransi, dinginkan2); fuzzy->addFuzzyRule(fuzzyRule07); FuzzyRule fuzzyRule08 = new FuzzyRule(8, kidealPanas, dinginkan2); fuzzy->addFuzzyRule(fuzzyRule08);

FuzzyRuleAntecedent lembabDingin = new FuzzyRuleAntecedent(); lembabDingin->joinWithAND(lembab, dingin); FuzzyRuleConsequent keringHangatkan = new FuzzyRuleConsequent(); keringHangatkan->addOutput(fanOn); keringHangatkan->addOutput(cooling0); keringHangatkan->addOutput(heatingOn); FuzzyRule *fuzzyRule09 = new FuzzyRule(9, lembabDingin, keringHangatkan); fuzzy->addFuzzyRule(fuzzyRule09);

FuzzyRuleAntecedent lembabIdeal = new FuzzyRuleAntecedent(); lembabIdeal->joinWithAND(lembab, ideal); FuzzyRuleConsequent kurangiKelambapan = new FuzzyRuleConsequent(); kurangiKelambapan->addOutput(fanOn); kurangiKelambapan->addOutput(cooling1); kurangiKelambapan->addOutput(heatingOn); FuzzyRule *fuzzyRule10 = new FuzzyRule(10, lembabIdeal, kurangiKelambapan); fuzzy->addFuzzyRule(fuzzyRule10);

FuzzyRuleAntecedent lembabtoleransi = new FuzzyRuleAntecedent(); lembabtoleransi->joinWithAND(lembab, toleransi); FuzzyRuleAntecedent lembabpanas = new FuzzyRuleAntecedent(); lembabpanas->joinWithAND(lembab, panas);

FuzzyRuleConsequent *kurangiKelambapanDanDinginkan = new FuzzyRuleConsequent(); kurangiKelambapanDanDinginkan->addOutput(fanOn); kurangiKelambapanDanDinginkan->addOutput(cooling2); kurangiKelambapanDanDinginkan->addOutput(heatingOn);

FuzzyRule *fuzzyRule11 = new FuzzyRule(11, lembabtoleransi, kurangiKelambapanDanDinginkan); fuzzy->addFuzzyRule(fuzzyRule11);

FuzzyRule *fuzzyRule12 = new FuzzyRule(12, lembabpanas, kurangiKelambapanDanDinginkan); fuzzy->addFuzzyRule(fuzzyRule12); }

void loop() { int input1 = 29;// dht.readTemperature(); int input2 = 77;// dht.readHumidity();

if (isnan(input1) || isnan(input2)) { Serial.println("Failed to read from DHT sensor!"); return; }

Serial.println("\n\n\tinput: "); Serial.print("\t\t\tsuhu: "); Serial.print(input1); Serial.print(", kelembapan: "); Serial.println(input2);

fuzzy->setInput(1, input1); fuzzy->setInput(2, input2);

fuzzy->fuzzify(); cekFuzzyRule(); Serial.println("\nInput Fuzzy: "); Serial.print("Suhu: dingin-> "); Serial.print(dingin->getPertinence()); Serial.print(", ideal-> "); Serial.print(ideal->getPertinence()); Serial.print(", toleransi-> "); Serial.println(toleransi->getPertinence()); Serial.print(", panas-> "); Serial.println(panas->getPertinence());

Serial.print("\nKelembapan: kering-> "); Serial.print(kering->getPertinence()); Serial.print(", ideal-> "); Serial.print(kideal->getPertinence()); Serial.print(", lembap-> "); Serial.print(lembab->getPertinence());

float output1 = fuzzy->defuzzify(1); float output2 = fuzzy->defuzzify(2); float output3 = fuzzy->defuzzify(3);

Serial.println("\nOutput: "); Serial.print("\n\tKipas: Mati-> "); Serial.print(fanOff->getPertinence()); Serial.print(", nyala-> "); Serial.print(fanOn->getPertinence());

Serial.print("\n\tPendingin: 0-> "); Serial.print(cooling0->getPertinence()); Serial.print(", 1-> "); Serial.print(cooling1->getPertinence()); Serial.print(", 2-> "); Serial.print(cooling2->getPertinence());

Serial.print("\n\tHeating: mati-> "); Serial.print(heatingOff->getPertinence()); Serial.print(", nyala-> "); Serial.print(heatingOn->getPertinence());

Serial.println("\nResult: "); Serial.print("\tKipas: "); Serial.print(output1); Serial.print(", Pendingin: "); Serial.println(output2); Serial.print(", pemanas: "); Serial.println(output3);

if(output1>0){ digitalWrite(relayKipas,relayON); Serial.println("kps nyala"); }else{ digitalWrite(relayKipas,relayOFF); Serial.println("kps mati"); } if(output2>1){ digitalWrite(relayPendingin2,relayON); digitalWrite(relayPendingin1,relayON); Serial.println("pendingin 1, 2 nyala"); }else if(output2>1){ digitalWrite(relayPendingin2,relayOFF); digitalWrite(relayPendingin1,relayON); Serial.println("pendingin 1 nyala, 2 mati"); }else{ digitalWrite(relayPendingin2,relayOFF); digitalWrite(relayPendingin1,relayOFF); Serial.println("pendingin mati"); } if(output3>0){ digitalWrite(relayLampuPemanas,relayON); Serial.println("pemanas nyala"); }else{ digitalWrite(relayLampuPemanas,relayOFF); Serial.println("pemanas mati"); }

// wait 12 seconds delay(2000); }

void cekFuzzyRule(){ for(int i=1; i<=12; i++){ Serial.print("Fuzzy rule "); Serial.print(i); Serial.print(": "); Serial.println(fuzzy->isFiredRule(i)); }

}

andytkomskt commented 2 years ago

Hey, you've solved this?

ritz89 commented 2 years ago

not yet

andytkomskt commented 2 years ago

Which arduino do use for testing?

ritz89 commented 2 years ago

i'm using atmega328

andytkomskt commented 2 years ago

i'm using atmega328

Same here. I've been test code using nano and uno

alvesoaj commented 2 years ago

It is very strange!

Both of your are trying the same code?

I will find a free time to do a try on this code.

andytkomskt commented 2 years ago

It is very strange!

Both of your are trying the same code?

I will find a free time to do a try on this code.

No sir. I try my own code, but i've solved the problem. Problem in my code is simple, i've reduce rules decalration for antecedent and consequent. Thank you sir

alvesoaj commented 2 years ago

It is very strange! Both of your are trying the same code? I will find a free time to do a try on this code.

No sir. I try my own code, but i've solved the problem. Problem in my code is simple, i've reduce rules decalration for antecedent and consequent. Thank you sir

I see, the hardware was your limitation