alvesoaj / eFLL

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

No Output Crisp #24

Closed LazuardiBox closed 3 years ago

LazuardiBox commented 4 years ago

Hi. This is a great library to use. It made simpler and efficient. I'm A newbie in Arduino. I'm triying to compile fuzzy of 3 inputs and 1 output. Input crisps shows no problem at all. But when it came to defuzzification, It Always shows zero. I'm using proteus to simulate, with potentio as the input data. Arduino UNO.

I used to check my code over and over but it looks no problem i think. dunno what happen.

Any solution? thanks :)

`
//------------------------------------------------------------------------------------ Library

include // eFLL - A Fuzzy Library for Arduino and Embedded Systems

include

include ;

//------------------------------------------------------------------------------------ Deklarasi Pin

define DHTPIN 12

define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE); int chk; float hum; float temp; int relay = 13; int soilpin = A1;

//------------------------------------------------------------------------------------ Membuat FIS

Fuzzy *fuzzy = new Fuzzy();

//------------------------------------------------------------------------------------ Membuat anggota MF dari MF input RH

// Anggota Fuzzy Input (Relative Humidity) FuzzySet low_RH = new FuzzySet(0, 25, 25, 50); FuzzySet average_RH = new FuzzySet(40, 50, 50, 60); FuzzySet *high_RH = new FuzzySet(50, 75, 75, 100);

//------------------------------------------------------------------------------------ Membuat anggota MF dari MF input SRH.

// Anggota Fuzzy Input (Soil Moisture Relative Humidity) FuzzySet low_SRH = new FuzzySet(0, 25, 25, 50); FuzzySet average_SRH = new FuzzySet(40, 50, 50, 60); FuzzySet *high_SRH = new FuzzySet(50, 75, 75, 100);

//------------------------------------------------------------------------------------ Membuat anggota MF dari MF input TEMP.

// Anggota Fuzzy Input (Temperature) FuzzySet low_temp = new FuzzySet(0, 11, 11, 22); FuzzySet average_temp = new FuzzySet(22, 24.5, 24.5, 27); FuzzySet *high_temp = new FuzzySet(27, 31, 31, 35);

//------------------------------------------------------------------------------------ Membuat anggota MF dari MF output actions.

// Anggota Fuzzy Output (Actions) FuzzySet noact = new FuzzySet(0, 0.175, 0.175, 0.35); FuzzySet duadetik = new FuzzySet(0.35, 0.4525, 0.4525, 0.55); FuzzySet limadetik = new FuzzySet(0.55, 0.65, 0.65, 0.75); FuzzySet tujuhdetik = new FuzzySet(0.75, 0.875, 0.875, 1);

//------------------------------------------------------------------------------------ void setup.

void setup(){

Serial.begin(9600); dht.begin(); Serial.begin(9600); pinMode(relay, OUTPUT); digitalWrite(relay, HIGH);

//------------------------------------------------------------------------------------ Memasukkan anggota MF ke MF masing-masing.

// FuzzyInput Relative Humidity

FuzzyInput *RH = new FuzzyInput(1);

RH->addFuzzySet(low_RH); RH->addFuzzySet(average_RH); RH->addFuzzySet(high_RH);

fuzzy->addFuzzyInput(RH);

//------------------------------------------------------------------------------------ Memasukkan anggota MF ke MF masing-masing.

// FuzzyInput Soil Moisture Relative Humidity

FuzzyInput *SRH = new FuzzyInput(2);

SRH->addFuzzySet(low_SRH); SRH->addFuzzySet(average_SRH); SRH->addFuzzySet(high_SRH);

fuzzy->addFuzzyInput(SRH);

//------------------------------------------------------------------------------------ Memasukkan anggota MF ke MF masing-masing.

// FuzzyInput Temperature

FuzzyInput *temp = new FuzzyInput(3);

temp->addFuzzySet(low_temp); temp->addFuzzySet(average_temp); temp->addFuzzySet(high_temp);

fuzzy->addFuzzyInput(temp);

//------------------------------------------------------------------------------------ Memasukkan anggota MF ke MF masing-masing.

// Fuzzy Output Actions

FuzzyOutput *actions = new FuzzyOutput(1);

actions->addFuzzySet(noact); actions->addFuzzySet(duadetik); actions->addFuzzySet(limadetik); actions->addFuzzySet(tujuhdetik);

fuzzy->addFuzzyOutput(actions);

//------------------------------------------------------------------------------------ Membuat aturan fuzzy (Fuzzy Rule)

// Antecedent = Penggabungan anggota MF dengan fungsi AND atau OR. // Consequent = Konsekuensi atau output yang akan dihasilkan dari Antecedent yang sudah dibuat. // Contoh = Jika Antecedent "Low1 AND Low2" maka Consequent adalah "min"

//------------------------------------------------------------------------------------ Deklarasi Antecedent

FuzzyRuleAntecedent *lowRHlowSRH = new FuzzyRuleAntecedent(); lowRHlowSRH->joinWithAND(low_RH, low_SRH);

FuzzyRuleAntecedent *averageRHaverageSRH = new FuzzyRuleAntecedent(); lowRHlowSRH->joinWithAND(average_RH, average_SRH);

FuzzyRuleAntecedent *highRHhighSRH = new FuzzyRuleAntecedent(); lowRHlowSRH->joinWithAND(high_RH, high_SRH);

FuzzyRuleAntecedent *lowTEMP = new FuzzyRuleAntecedent(); lowTEMP->joinSingle(low_temp);

FuzzyRuleAntecedent *averageTEMP = new FuzzyRuleAntecedent(); lowTEMP->joinSingle(average_temp);

FuzzyRuleAntecedent *highTEMP = new FuzzyRuleAntecedent(); lowTEMP->joinSingle(high_temp);

//------------------------------------------------------------------------------------ Deklarasi Consequent

FuzzyRuleConsequent *thenActduadetik = new FuzzyRuleConsequent(); thenActduadetik->addOutput(duadetik);

FuzzyRuleConsequent *thenActlimadetik = new FuzzyRuleConsequent(); thenActlimadetik->addOutput(limadetik);

FuzzyRuleConsequent *thenActtujuhdetik = new FuzzyRuleConsequent(); thenActlimadetik->addOutput(tujuhdetik);

FuzzyRuleConsequent *thenNoAct = new FuzzyRuleConsequent(); thenActduadetik->addOutput(noact);

//------------------------------------------------------------------------------------ Rule 1.

FuzzyRuleAntecedent *lowRHlowSRHlowTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(lowRHlowSRH, lowTEMP);

FuzzyRule *fuzzyRule1 = new FuzzyRule(1, lowRHlowSRHlowTEMP, thenActduadetik); fuzzy->addFuzzyRule(fuzzyRule1);

//------------------------------------------------------------------------------------ Rule 2.

FuzzyRuleAntecedent *lowRHlowSRHaverageTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(lowRHlowSRH, averageTEMP);

FuzzyRule *fuzzyRule2 = new FuzzyRule(2, lowRHlowSRHaverageTEMP, thenActlimadetik); fuzzy->addFuzzyRule(fuzzyRule2);

//------------------------------------------------------------------------------------ Rule 3.

FuzzyRuleAntecedent *lowRHlowSRHhighTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(lowRHlowSRH, highTEMP);

FuzzyRule *fuzzyRule3 = new FuzzyRule(3, lowRHlowSRHhighTEMP, thenActtujuhdetik); fuzzy->addFuzzyRule(fuzzyRule3);

//------------------------------------------------------------------------------------ Rule 4.

FuzzyRuleAntecedent *averageRHaverageSRHlowTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(averageRHaverageSRH, lowTEMP);

FuzzyRule *fuzzyRule4 = new FuzzyRule(4, averageRHaverageSRH, thenActduadetik); fuzzy->addFuzzyRule(fuzzyRule4);

//------------------------------------------------------------------------------------ Rule 5.

FuzzyRuleAntecedent *averageRHaverageSRHaverageTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(averageRHaverageSRH, averageTEMP);

FuzzyRule *fuzzyRule5 = new FuzzyRule(5, averageRHaverageSRHaverageTEMP, thenActlimadetik); fuzzy->addFuzzyRule(fuzzyRule5);

//------------------------------------------------------------------------------------ Rule 6.

FuzzyRuleAntecedent *averageRHaverageSRHhighTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(averageRHaverageSRH, highTEMP);

FuzzyRule *fuzzyRule6 = new FuzzyRule(6, averageRHaverageSRHhighTEMP, thenActtujuhdetik); fuzzy->addFuzzyRule(fuzzyRule6);

//------------------------------------------------------------------------------------ Rule 7.

FuzzyRuleAntecedent *highRHhighSRHlowTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(highRHhighSRH, lowTEMP);

FuzzyRule *fuzzyRule7 = new FuzzyRule(7, highRHhighSRHlowTEMP, thenNoAct); fuzzy->addFuzzyRule(fuzzyRule7);

//------------------------------------------------------------------------------------ Rule 8.

FuzzyRuleAntecedent *highRHhighSRHaverageTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(highRHhighSRH, averageTEMP);

FuzzyRule *fuzzyRule8 = new FuzzyRule(8, highRHhighSRHaverageTEMP, thenActduadetik); fuzzy->addFuzzyRule(fuzzyRule8);

//------------------------------------------------------------------------------------ Rule 9.

FuzzyRuleAntecedent *highRHhighSRHhighTEMP = new FuzzyRuleAntecedent(); lowRHlowSRHlowTEMP->joinWithAND(highRHhighSRH, highTEMP);

FuzzyRule *fuzzyRule9 = new FuzzyRule(9, highRHhighSRHhighTEMP, thenActlimadetik); fuzzy->addFuzzyRule(fuzzyRule9);

}

//------------------------------------------------------------------------------------ void loop

void loop() {

//------------------------------------------------------------------------------------ Membaca input data

float h = dht.readHumidity(); float t = dht.readTemperature();

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

int inputRH = (h); int inputSRH = map(analogRead(soilpin), 0, 1023, 10, 100); int inputTEMP = (t);

//------------------------------------------------------------------------------------ Menampilkan ke Serial

String humtext = "RH (%) : "; String humprint = humtext + inputRH ; //---------------------------------------------------------- Print DHT Relative Humidity Serial.println(humprint);

String soiltext = "Soil RH (%) : "; String soilprint = soiltext + inputSRH ; //---------------------------------------------------------- Print SOIL Relative Humidity Serial.println(soilprint);

String temptext = "TEMP (C) : "; String tempprint = temptext + inputTEMP ; //---------------------------------------------------------- Print DHT Temperature Serial.println(tempprint);

//------------------------------------------------------------------------------------ Deklarasi input fuzzy

fuzzy->setInput(1, inputRH); fuzzy->setInput(2, inputSRH); fuzzy->setInput(3, inputTEMP);

//------------------------------------------------------------------------------------ Fuzzifikasi dan Menampilkan ke serial monitor

fuzzy->fuzzify(); float outputfuzzy = fuzzy->defuzzify(1);

//------------------------------------------------------------------------------------ Menampilkan derajat keanggotaan dari anggota MF input RH

Serial.println("Input: "); Serial.print("\tRH: low-> "); Serial.print(low_RH->getPertinence()); Serial.print(", average-> "); Serial.print(average_RH->getPertinence()); Serial.print(", high-> "); Serial.println(high_RH->getPertinence());

//------------------------------------------------------------------------------------ Menampilkan derajat keanggotaan dari anggota MF input SRH

Serial.print("\tSRH: low-> "); Serial.print(low_SRH->getPertinence()); Serial.print(", average-> "); Serial.print(average_SRH->getPertinence()); Serial.print(", high-> "); Serial.println(high_SRH->getPertinence());

//------------------------------------------------------------------------------------ Menampilkan derajat keanggotaan dari anggota MF input TEMP

Serial.print("\tTEMP: low-> "); Serial.print(low_temp->getPertinence()); Serial.print(", average-> "); Serial.print(average_temp->getPertinence()); Serial.print(", high-> "); Serial.println(high_temp->getPertinence());

//------------------------------------------------------------------------------------ Proses Fuzzifikasi

//------------------------------------------------------------------------------------ Menampilkan derajat keanggotaan dari anggota MF output ACTIONS

Serial.println("Output: ");

Serial.println("\tACTIONS: "); Serial.print("noact->-> "); Serial.print(noact->getPertinence()); Serial.print(", duadetik-> "); Serial.print(duadetik->getPertinence()); Serial.print(", limadetik-> "); Serial.print(limadetik->getPertinence()); Serial.print(", tujuhdetik-> "); Serial.print(tujuhdetik->getPertinence());

Serial.println("Hasil Fuzzifikasi: "); Serial.print("\t\t\tACTIONS: "); Serial.print(outputfuzzy); delay(60000); }
`

IMG-20200601-WA0010

alvesoaj commented 4 years ago

Hey, did you check if the rules are being activated?

You can do that with

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

Check the rules that should be fired for certain inputs

LazuardiBox commented 4 years ago

Hello, I've added the code. And still got same result. Any ideas?

fuzzy->fuzzify(); bool Rule1 = fuzzy->isFiredRule(1); bool Rule2 = fuzzy->isFiredRule(2); bool Rule3 = fuzzy->isFiredRule(3); bool Rule4 = fuzzy->isFiredRule(4); bool Rule5 = fuzzy->isFiredRule(5); bool Rule6 = fuzzy->isFiredRule(6); bool Rule7 = fuzzy->isFiredRule(7); bool Rule8 = fuzzy->isFiredRule(8); bool Rule9 = fuzzy->isFiredRule(9); float outputfuzzy = fuzzy->defuzzify(1);

alvesoaj commented 4 years ago

Great, print the output of each fuzzyrule to check which one is being activated.

mamprat commented 2 years ago

sir i have some problem with the crisps shows, i try with your idea, but the result is the same. is there something wrong in the placement of the script?

image

alvesoaj commented 2 years ago

Hello @mamprat , did you double check the rules building? I mean, are all antecedent and consequent correct? Do you have this logic in somewhere else, for example, Matlab? Second, are you using the latest version of the library?

mamprat commented 2 years ago

hello sir, thank you for responding to my question. I built with 3 inputs and 4 outputs with the atecedent connective input "AND". I try combine "joinWithAND" and "joinSingle" to form a single rule on "FuzzyRuleAntecedent". In the output of "FuzzyRuleConsequent" I add a number at the end of each code to distinguish one rule with others, because with different inputs there are several conditions of the same output can this be the cause?. I have checked for the rules I think they are correct because shows the appropriate output only the conditions are not for numbers, I tried simulation using labview to compare them. for the library I have updated with the new one.

image

image

image

there is for all my code fuzzy_test.txt

arsalwildan commented 1 year ago

I have the same problem as you, have you found a solution?

mamprat commented 1 year ago

I have the same problem as you, have you found a solution?

repeat to check your rules, sometime the rules is wrong in write. or change the arduino with big memory if you makes many rules in your code

stdong24 commented 11 months ago

I have the same problem as you, have you found a solution?

repeat to check your rules, sometime the rules is wrong in write. or change the arduino with big memory if you makes many rules in your code

hello, can i see the code for the correct all of the project, i've got same problem like u.thx if u don't mind it.