alvesoaj / eFLL

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

Two fuzzies #23

Closed daveHyder closed 2 years ago

daveHyder commented 4 years ago

Is it just for one fuzzy? I mean, I want do twice defuzzification. I've tried to use 2 fuzzy it can compile perfectly but cant work in real, Im really need helpp.

I need two defuzzifications because. I build a line follower robot, and the speed is from PWM = BasePWM +/- PWM1. BasePWM is the result from defuzzification from keypad input. and PWM1 is result from defuuzzification from line tracking sensors.

And please give me examples of using two defuzzification at the same time , if this library could calculate 2 fuzzies at once. thankss

alvesoaj commented 4 years ago

Hello, yes, you can have more then one fuzzy. Just check the variable names and be aware to not reuse objects!

Let me know what is happening.

daveHyder commented 4 years ago

I've tried, but still cant work, and here below is my code. maybe theres some mistakes but i don't know where the mistakes are. every sugestions will very helpfull, thanks

`#include // I2C Keypad library by Joe Young https://github.com/joeyoung/arduino_keypads

include // I2C LCD Library by Francisco Malpartida https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

include

include

include

DFPlayerMini_Fast myMP3;

define ARDUINO_TX 13//should connect to TX of the Serial MP3 Player module

define ARDUINO_RX 12//connect to RX of the module

SoftwareSerial mySerial(ARDUINO_RX, ARDUINO_TX);

int X1; int X2; int X3; int X4; int X5; int state = 0; int Sensor1 = 0; int Sensor2 = 0; int Sensor3 = 0; int Sensor4 = 0; int input; float PWM1; float PWM2; float out;

int MotorA_PWM = 9; //motor kiri int MotorB_PWM = 10; // motor kanan int CWatauCCW_1 = 11; //abu abu int CWatauCCW_2 = 12; //abu abu int Brake = 8; //Putih x2 int InfraredA = 6;// pin 6 of Arduino is connected to the output of the sensor inrared Kiri int InfraredB = 7;// pin 6 of Arduino is connected to the output of the sensor inrared Kanan float PWM;

Fuzzy *fuzzy1 = new Fuzzy();

//KEYPAD CYCLE TIME FuzzySet Cepat1 = new FuzzySet(0, 1, 20, 50); FuzzySet Sedang1 = new FuzzySet(20, 50, 50, 90); FuzzySet *Lama1 = new FuzzySet(50, 120, 150, 150);

//OUTPUT CYCCLE TIME FuzzySet Lama2 = new FuzzySet(0, 0, 70, 150); FuzzySet Sedang2 = new FuzzySet(70, 150, 150, 180); FuzzySet *Cepat2 = new FuzzySet(150, 180, 180, 200);

Fuzzy *fuzzy2 = new Fuzzy();

// FuzzyInputJarak1 FuzzySet Dekat1 = new FuzzySet(0, 0, 0.6, 1.5); FuzzySet Sedang3 = new FuzzySet(0.6, 1.4, 1.6, 2.4); FuzzySet *Jauh1 = new FuzzySet(1.5, 2.4, 3, 3);

// FuzzyInputJarak2 FuzzySet Dekat2 = new FuzzySet(0, 0, 0.6, 1.5); FuzzySet Sedang4 = new FuzzySet(0.6, 1.4, 1.6, 2.4); FuzzySet *Jauh2 = new FuzzySet(1.5, 2.4, 3, 3);

// FuzzyOutputPWM1 FuzzySet Berkurang1 = new FuzzySet(-100, -100, -50, 0); FuzzySet Normal1 = new FuzzySet(-10, 0, 0, 10); FuzzySet *Bertambah1 = new FuzzySet(0, 10, 20, 20);

// FuzzyOutputPWM2 FuzzySet Berkurang2 = new FuzzySet(-100, -100, -50, 0); FuzzySet Normal2 = new FuzzySet(-10, 0, 0, 10); FuzzySet *Bertambah2 = new FuzzySet(0, 10, 20, 20);

define lcd_addr 0x27 // I2C address of typical I2C LCD Backpack

define keypad_addr 0x20 // I2C address of I2C Expander module (A0-A1-A2 dip switch to off position)

// LCD Pins to I2C LCD Backpack - These are default for HD44780 LCD's

define Rs_pin 0

define Rw_pin 1

define En_pin 2

define BACKLIGHT_PIN 3

define D4_pin 4

define D5_pin 5

define D6_pin 6

define D7_pin 7

// Create instance for LCD called: i2c_lcd LiquidCrystal_I2C i2c_lcd(lcd_addr, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

// Define the keypad pins const byte ROWS = 4; const byte COLS = 3; char keys[ROWS][COLS] = { {'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'} };

// Keypad pins connected to the I2C-Expander pins P0-P6 byte rowPins[ROWS] = {6, 5, 4, 3}; //connect to the row pinouts of the keypad. baris pinnya = 3,4,5,6 byte colPins[COLS] = {2, 1, 0 }; //connect to the column pinouts of the keypad. colom pinya = 0,1,2 byte value = 0;

// Create instance of the Keypad name I2C_Keypad and using the PCF8574 chip Keypad_I2C I2C_Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS, keypad_addr, PCF8574 );

void setup() {

Serial.begin(115200); i2c_lcd.begin (16, 2); // our LCD is a 16x2, change for your LCD if needed

// LCD Backlight ON i2c_lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); i2c_lcd.setBacklight(HIGH); i2c_lcd.clear(); // Clear the LCD screen

I2C_Keypad.begin(); mySerial.begin(9600); myMP3.begin(mySerial); myMP3.volume(30); delay(10); pinMode(3, OUTPUT); //Spare Relay Add Merah pinMode(4, OUTPUT); //Spare Relay Add Kuning pinMode(5, OUTPUT); //Spare Relay Add Hijau pinMode(6, INPUT_PULLUP); //setting Arduino pin 6 as output Sensor infrared Kiri pinMode(7, INPUT_PULLUP); //setting Arduino pin 7 as output Sensor infrared kanan pinMode (2, INPUT_PULLUP ); //arduino push ON pinMode (A0, INPUT); // sensor kiri 1 pinMode (A1, INPUT); // sensor kiri 2 pinMode (A2, INPUT); // sensor kanan 1 pinMode (A3, INPUT); // sensor Kanan 2 pinMode (11, INPUT); // Sensor buat berhenti pinMode (9, OUTPUT); //pwmA. kabel servo = hijau pinMode (10, OUTPUT); //pwmB. kabel servo = hijau pinMode (8, OUTPUT); //brake. kabel servo = putih

FuzzyInput *CycleTime = new FuzzyInput(1); CycleTime->addFuzzySet(Cepat1); CycleTime->addFuzzySet(Sedang1); CycleTime->addFuzzySet(Lama1); fuzzy1->addFuzzyInput(CycleTime);

FuzzyOutput *PWM = new FuzzyOutput(1); PWM->addFuzzySet(Lama2); PWM->addFuzzySet(Sedang2); PWM->addFuzzySet(Cepat2); fuzzy1->addFuzzyOutput(PWM);

FuzzyRuleAntecedent ifCycleTimecepat = new FuzzyRuleAntecedent(); ifCycleTimecepat->joinSingle(Cepat1); FuzzyRuleConsequent thenSpeedCepat = new FuzzyRuleConsequent(); thenSpeedCepat ->addOutput(Cepat2); FuzzyRule *fuzzyRule01 = new FuzzyRule(1, ifCycleTimecepat, thenSpeedCepat); fuzzy1->addFuzzyRule(fuzzyRule01);

FuzzyRuleAntecedent ifCycleTimesedang = new FuzzyRuleAntecedent(); ifCycleTimesedang->joinSingle(Sedang1); FuzzyRuleConsequent thenSpeedSedang = new FuzzyRuleConsequent(); thenSpeedSedang->addOutput(Sedang2); FuzzyRule *fuzzyRule02 = new FuzzyRule(2, ifCycleTimesedang, thenSpeedSedang); fuzzy1->addFuzzyRule(fuzzyRule02);

FuzzyRuleAntecedent ifCycleTimelama = new FuzzyRuleAntecedent(); ifCycleTimelama->joinSingle(Lama1); FuzzyRuleConsequent thenSpeedLambat = new FuzzyRuleConsequent(); thenSpeedLambat->addOutput(Lama2); FuzzyRule *fuzzyRule03 = new FuzzyRule(3, ifCycleTimelama, thenSpeedLambat); fuzzy1->addFuzzyRule(fuzzyRule03);

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

Jarak1->addFuzzySet(Dekat1); Jarak1->addFuzzySet(Sedang3); Jarak1->addFuzzySet(Jauh1); fuzzy2->addFuzzyInput(Jarak1);

FuzzyInput *Jarak2 = new FuzzyInput(3);

Jarak2->addFuzzySet(Dekat2); Jarak2->addFuzzySet(Sedang4); Jarak2->addFuzzySet(Jauh2); fuzzy2->addFuzzyInput(Jarak2);

// FuzzyOutput FuzzyOutput *PWM1 = new FuzzyOutput(2);

PWM1->addFuzzySet(Berkurang1); PWM1->addFuzzySet(Normal1); PWM1->addFuzzySet(Bertambah1); fuzzy2->addFuzzyOutput(PWM1);

// FuzzyOutput FuzzyOutput *PWM2 = new FuzzyOutput(3);

PWM2->addFuzzySet(Berkurang2); PWM2->addFuzzySet(Normal2); PWM2->addFuzzySet(Bertambah2); fuzzy2->addFuzzyOutput(PWM2);

// Building FuzzyRule // FuzzyRuleAntecedent ifJarak1Jauh1AndJarak2Jauh2 = new FuzzyRuleAntecedent(); // ifJarak1Jauh1AndJarak2Jauh2->joinWithAND(Jauh1, Jauh2); // FuzzyRuleConsequent thenPWM1Normal1PWM2Normal2 = new FuzzyRuleConsequent(); // thenPWM1Normal1PWM2Normal2->addOutput(Normal1); // thenPWM1Normal1PWM2Normal2->addOutput(Normal2); // FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifJarak1Jauh1AndJarak2Jauh2, thenPWM1Normal1PWM2Normal2);// Output coba hapus AND nya nanti // fuzzy2->addFuzzyRule(fuzzyRule4);

// Building FuzzyRule FuzzyRuleAntecedent ifJarak1Sedang1AndJarak2Jauh2 = new FuzzyRuleAntecedent(); ifJarak1Sedang1AndJarak2Jauh2->joinWithAND(Sedang3, Jauh2); FuzzyRuleConsequent thenPWM1Normal1PWM2Bertambah2 = new FuzzyRuleConsequent(); thenPWM1Normal1PWM2Bertambah2->addOutput(Normal1); thenPWM1Normal1PWM2Bertambah2->addOutput(Bertambah2); FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifJarak1Sedang1AndJarak2Jauh2, thenPWM1Normal1PWM2Bertambah2);// Output coba hapus AND nya nanti fuzzy2->addFuzzyRule(fuzzyRule5);

// Building FuzzyRule FuzzyRuleAntecedent ifJarak1Dekat1AndJarak2Jauh2 = new FuzzyRuleAntecedent(); ifJarak1Dekat1AndJarak2Jauh2->joinWithAND(Dekat1, Jauh2); FuzzyRuleConsequent thenPWM1Berkurang1PWM2Bertambah2 = new FuzzyRuleConsequent(); thenPWM1Berkurang1PWM2Bertambah2->addOutput(Berkurang1); thenPWM1Berkurang1PWM2Bertambah2->addOutput(Bertambah2); FuzzyRule *fuzzyRule6 = new FuzzyRule(6, ifJarak1Dekat1AndJarak2Jauh2, thenPWM1Berkurang1PWM2Bertambah2);// Output coba hapus AND nya nanti fuzzy2->addFuzzyRule(fuzzyRule6);

// Rules 4 FuzzyRuleAntecedent ifJarak1Jauh1AndJarak2Sedang2 = new FuzzyRuleAntecedent(); ifJarak1Jauh1AndJarak2Sedang2->joinWithAND(Jauh1, Sedang4); FuzzyRuleConsequent thenPWM1Bertambah1PWM2Normal2 = new FuzzyRuleConsequent(); thenPWM1Bertambah1PWM2Normal2->addOutput(Bertambah1); thenPWM1Bertambah1PWM2Normal2->addOutput(Normal2); FuzzyRule *fuzzyRule7 = new FuzzyRule(7, ifJarak1Jauh1AndJarak2Sedang2, thenPWM1Bertambah1PWM2Normal2);// Output coba hapus AND nya nanti fuzzy2->addFuzzyRule(fuzzyRule7);

//Rules5 FuzzyRuleAntecedent ifJarak1Jauh1AndJarak2Dekat2 = new FuzzyRuleAntecedent(); ifJarak1Jauh1AndJarak2Dekat2->joinWithAND(Jauh1, Dekat2); FuzzyRuleConsequent thenPWM1Bertambah1PWM2Berkurang2 = new FuzzyRuleConsequent(); thenPWM1Bertambah1PWM2Berkurang2->addOutput(Bertambah1); thenPWM1Bertambah1PWM2Berkurang2->addOutput(Berkurang2); FuzzyRule *fuzzyRule8 = new FuzzyRule(8, ifJarak1Jauh1AndJarak2Dekat2, thenPWM1Bertambah1PWM2Berkurang2);// Output coba hapus AND nya nanti fuzzy2->addFuzzyRule(fuzzyRule8);

}

void loop() { MP3(); char key = I2C_Keypad.getKey(); i2c_lcd.setCursor(0, 0); i2c_lcd.print("cycle time :"); i2c_lcd.setCursor(0, 1); i2c_lcd.print("speed :");

X2 = analogRead ( A1); X3 = analogRead ( A2); float input2 = mapPecahan (X2, 0, 1023, 3, 0); float input3 = mapPecahan (X3, 0, 1023, 3, 0);

fuzzy2->setInput(2, input2); fuzzy2->setInput(3, input3); fuzzy2->fuzzify(); float pwm1 = fuzzy2->defuzzify(2); float pwm2 = fuzzy2->defuzzify(3); PWM1 = out + (pwm1); PWM2 = out + (pwm2);

Serial.print("CycleTime : "); Serial.print("\t"); Serial.print(input); Serial.print("\t"); Serial.print("PWMawal :"); Serial.print(out); Serial.print(" \t"); Serial.print("Sensor2:"); Serial.print("\t"); Serial.print(input2); Serial.print("\t"); Serial.print("Sensor3:"); Serial.print("\t"); Serial.print(input3); Serial.print("\t"); Serial.print(" pwmKiri: "); Serial.print("\t"); Serial.print(pwm1); Serial.print("\t"); Serial.print(" pwmKanan: "); Serial.print("\t"); Serial.print(pwm2); Serial.print("\t"); Serial.print("\t"); Serial.print("totalPWMkiri:"); Serial.print("\t"); Serial.print(PWM1); Serial.print("\t"); Serial.print("\t"); Serial.print("totalPWMkanan: "); Serial.print("\t"); Serial.print(PWM2); Serial.println();

int input = value;

if (key != NO_KEY) { if ( (key >= '0') && (key <= '9') ) { value = value * 10; value = value + key - '0'; i2c_lcd.setCursor(12, 0); i2c_lcd.print(value); if ( value > 150) { i2c_lcd.clear(); value = 0; } } if ( key == '#' ) {

  fuzzy1->setInput(1, input);
  fuzzy1->fuzzify();
  float out = fuzzy1->defuzzify(1);

  i2c_lcd.setCursor(12, 0);
  i2c_lcd.print(value);

  i2c_lcd.setCursor(8, 1);

  i2c_lcd.print(out);

  value = 0;

}

if ( key == '*' ) {
  i2c_lcd.clear();
  value = 0;
}

}

if (digitalRead(2) == HIGH) { sistem(); } else { Maju(); delay(500); }

}

// coba nanti keluarin digital read(2) dari if = no key!

void sistem() { if (digitalRead(6) == HIGH && digitalRead(7) == HIGH) { linefollower(); } if (digitalRead(6) == LOW | digitalRead(7) == LOW) { Berhenti() ; }

} void linefollower() {

X1 = analogRead (A0); // X2 = analogRead (A1);// gak ikut, buat fuzzy //X3 = analogRead (A2);// gak ikut, buat fuzzy X4 = analogRead (A3); X5 = digitalRead(11);

if (X1 > 500 ) { Kiri(); digitalWrite(3, LOW); }

if ( X4 > 500) { Kanan(); digitalWrite(3, LOW); }

if ( X1 < 500 && X4 < 500 ) { Maju (); digitalWrite(3, LOW); }

if (X5 == HIGH ) // sensor follower { Berhenti(); digitalWrite(3, HIGH); }

}

void Maju() { analogWrite (9, PWM1); //motor kiri, kecepatan 75, untuk ubah speed ganti saja angka 75 nya. angka maximal 255. kabel servo = hijau analogWrite (10, PWM2); //motor kanan, kecepatan 75, untuk ubah speed ganti saja angka 75 nya. angka maximal 255. kabel servo = hijau digitalWrite (8, LOW); //pin nomor 8 untuk brake

}

void Kanan() { analogWrite (9, PWM1); //motor kiri, kecepatan 50, untuk ubah speed ganti saja angka 50 nya. angka maximal 255. kabel servo= hijau analogWrite (10, 0); //motor kanan, kecepatan 0, untuk ubah speed ganti saja angka 0 nya. angka maximal 255. kabel servo = hijau digitalWrite (Brake, LOW);

}

void Kiri()

{ analogWrite (9, 0); //motor kiri, kecepatan 0, untuk ubah speed ganti saja angka 0 nya. angka maximal 255. kabel servo = hijau analogWrite (10, PWM2); //motor kanan, kecepatan 50, untuk ubah speed ganti saja angka 50 nya. angka maximal 255. kabel servo = hijau digitalWrite (Brake, LOW);

}

void Berhenti() { analogWrite (9, 0); //motor kiri, kecepatan 0, untuk ubah speed ganti saja angka 0 nya. angka maximal 255. kabel servo = hijau analogWrite (10, 0); //motor kanan, kecepatan 0, untuk ubah speed ganti saja angka 0 nya. angka maximal 255. kabel servo = hijau digitalWrite (Brake, HIGH);

}

void MP3 () {

X5 = digitalRead(11);

switch ( state ) { case 0 : if (X5 == LOW ) { myMP3.loop(1); state = 1; } break;

case 1 :
  if (X5 == HIGH) {
    myMP3.loop(2);
    state = 0;
  }
  break;

} }

float mapPecahan( long x, long fromLow, long fromHigh, float toLow, float toHigh) { return (x - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow ;

}

`

alvesoaj commented 3 years ago

Hey, did you find a solution?

Zaki23madrid commented 2 years ago

I am facing the same problem, did you solve it ?

alvesoaj commented 2 years ago

@Zaki23madrid did you checked the variable names? And, is the erros exactly the same?