Translate

Membuat Alat Blood Warmer Arduino Fuzzy Logic Mamdani

Membuat Alat Blood Warmer Arduino Fuzzy Logic Mamdani


        Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat digunakan untuk Bloodwarmer yang mana menggunakan 2 buah sensor suhu yaitu sensor mlx90614 dan ds18b20. alat ini menggunakan metode Fuzzy Logic mamdani dan penampil LCD 16x2. untuk aktuator on off heater menggunakan solid state relay. untuk lebih jelasnya berikut adalah koding dan skemanya.


1. Skema Fuzzy





2. Program Arduino IDE

#include <Fuzzy.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 2  
#include <Adafruit_MLX90614.h>

Adafruit_MLX90614 mlx = Adafruit_MLX90614();

float TargetC;

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

float tempobject;
float temppeltier;
float output1;
int fuzzyx;

Fuzzy *fuzzy = new Fuzzy();

// FuzzyInput
FuzzySet *dingin = new FuzzySet(0, 0, 35, 36);
FuzzySet *normal = new FuzzySet(35, 36, 36, 37);
FuzzySet *panas = new FuzzySet(36, 37, 100, 100);

// FuzzyInput
FuzzySet *rendah = new FuzzySet(0, 0, 30, 40);
FuzzySet *sedang = new FuzzySet(30, 40, 40, 50);
FuzzySet *tinggi = new FuzzySet(40, 50, 100, 100);

// FuzzyOutput
FuzzySet *minimum = new FuzzySet(0, 0, 1, 2);
FuzzySet *average = new FuzzySet(2, 50, 50, 100);
FuzzySet *maximum = new FuzzySet(100, 200, 255, 255);

void setup()
{

  Serial.begin(9600);
  mlx.begin();
  lcd.begin();
  lcd.clear();
  lcd.noCursor();
  pinMode(9,OUTPUT);
  
  // FuzzyInput suhuobject
  FuzzyInput *suhuobject = new FuzzyInput(1);

  suhuobject->addFuzzySet(dingin);
  suhuobject->addFuzzySet(normal);
  suhuobject->addFuzzySet(panas);
  fuzzy->addFuzzyInput(suhuobject);

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

  suhupeltier->addFuzzySet(rendah);
  suhupeltier->addFuzzySet(sedang);
  suhupeltier->addFuzzySet(tinggi);
  fuzzy->addFuzzyInput(suhupeltier);

  // FuzzyOutput pwm
  FuzzyOutput *pwmout = new FuzzyOutput(1);

  pwmout->addFuzzySet(minimum);
  pwmout->addFuzzySet(average);
  pwmout->addFuzzySet(maximum);
  fuzzy->addFuzzyOutput(pwmout);

  // Building FuzzyRule 1
  FuzzyRuleAntecedent *ifsuhuobjectDinginAndsuhupeltierRendah = new FuzzyRuleAntecedent();
  ifsuhuobjectDinginAndsuhupeltierRendah->joinWithAND(dingin, rendah); 
  FuzzyRuleConsequent *thenPwmoutMaximum= new FuzzyRuleConsequent();
  thenPwmoutMaximum->addOutput(maximum);
  FuzzyRule *fuzzyRule1 = new FuzzyRule(1, ifsuhuobjectDinginAndsuhupeltierRendah, thenPwmoutMaximum);
  fuzzy->addFuzzyRule(fuzzyRule1);

  // Building FuzzyRule 2
  FuzzyRuleAntecedent *ifsuhuobjectDinginAndsuhupeltierSedang = new FuzzyRuleAntecedent();
  ifsuhuobjectDinginAndsuhupeltierSedang->joinWithAND(dingin, sedang); 
  //FuzzyRuleConsequent *thenPwmoutMaximum= new FuzzyRuleConsequent();
  thenPwmoutMaximum->addOutput(maximum);
  FuzzyRule *fuzzyRule2 = new FuzzyRule(2, ifsuhuobjectDinginAndsuhupeltierSedang, thenPwmoutMaximum);
  fuzzy->addFuzzyRule(fuzzyRule2);

  // Building FuzzyRule 3
  FuzzyRuleAntecedent *ifsuhuobjectDinginAndsuhupeltierTinggi = new FuzzyRuleAntecedent();
  ifsuhuobjectDinginAndsuhupeltierTinggi->joinWithAND(dingin, tinggi); 
  //FuzzyRuleConsequent *thenPwmoutMaximum= new FuzzyRuleConsequent();
  thenPwmoutMaximum->addOutput(maximum);
  FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifsuhuobjectDinginAndsuhupeltierTinggi, thenPwmoutMaximum);
  fuzzy->addFuzzyRule(fuzzyRule3);

  // Building FuzzyRule 4
  FuzzyRuleAntecedent *ifsuhuobjectNormalAndsuhupeltierRendah= new FuzzyRuleAntecedent();
  ifsuhuobjectNormalAndsuhupeltierRendah->joinWithAND(normal, rendah); 
  FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
  thenPwmoutAverage->addOutput(average);
  FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifsuhuobjectNormalAndsuhupeltierRendah, thenPwmoutAverage);
  fuzzy->addFuzzyRule(fuzzyRule4);

  // Building FuzzyRule 5
  FuzzyRuleAntecedent *ifsuhuobjectNormalAndsuhupeltierSedang= new FuzzyRuleAntecedent();
  ifsuhuobjectNormalAndsuhupeltierSedang->joinWithAND(normal, sedang); 
  //FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
  thenPwmoutAverage->addOutput(average);
  FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifsuhuobjectNormalAndsuhupeltierSedang, thenPwmoutAverage);
  fuzzy->addFuzzyRule(fuzzyRule5);

  // Building FuzzyRule 6
  FuzzyRuleAntecedent *ifsuhuobjectNormalAndsuhupeltierTinggi= new FuzzyRuleAntecedent();
  ifsuhuobjectNormalAndsuhupeltierTinggi->joinWithAND(normal, tinggi); 
  //FuzzyRuleConsequent *thenPwmoutAverage= new FuzzyRuleConsequent();
  thenPwmoutAverage->addOutput(average);
  FuzzyRule *fuzzyRule6 = new FuzzyRule(6, ifsuhuobjectNormalAndsuhupeltierTinggi, thenPwmoutAverage);
  fuzzy->addFuzzyRule(fuzzyRule6);

  // Building FuzzyRule 7
  FuzzyRuleAntecedent *ifsuhuobjectPanasAndsuhupeltierRendah= new FuzzyRuleAntecedent();
  ifsuhuobjectPanasAndsuhupeltierRendah->joinWithAND(panas, rendah); 
  FuzzyRuleConsequent *thenPwmoutMinimum= new FuzzyRuleConsequent();
  thenPwmoutMinimum->addOutput(minimum);
  FuzzyRule *fuzzyRule7 = new FuzzyRule(7, ifsuhuobjectPanasAndsuhupeltierRendah, thenPwmoutMinimum);
  fuzzy->addFuzzyRule(fuzzyRule7);

  // Building FuzzyRule 8
  FuzzyRuleAntecedent *ifsuhuobjectPanasAndsuhupeltierSedang= new FuzzyRuleAntecedent();
  ifsuhuobjectPanasAndsuhupeltierSedang->joinWithAND(panas, sedang); 
  //FuzzyRuleConsequent *thenPwmoutMinimum= new FuzzyRuleConsequent();
  thenPwmoutMinimum->addOutput(minimum);
  FuzzyRule *fuzzyRule8 = new FuzzyRule(8, ifsuhuobjectPanasAndsuhupeltierSedang, thenPwmoutMinimum);
  fuzzy->addFuzzyRule(fuzzyRule8);

  // Building FuzzyRule 9
  FuzzyRuleAntecedent *ifsuhuobjectPanasAndsuhupeltierTinggi= new FuzzyRuleAntecedent();
  ifsuhuobjectPanasAndsuhupeltierTinggi->joinWithAND(panas, tinggi); 
  //FuzzyRuleConsequent *thenPwmoutMinimum= new FuzzyRuleConsequent();
  thenPwmoutMinimum->addOutput(minimum);
  FuzzyRule *fuzzyRule9 = new FuzzyRule(9, ifsuhuobjectPanasAndsuhupeltierTinggi, thenPwmoutMinimum);
  fuzzy->addFuzzyRule(fuzzyRule9);
  
}

void loop()
{

  sensors.requestTemperatures();
  tempobject = mlx.readObjectTempC();
  temppeltier = sensors.getTempCByIndex(0);

  lcd.setCursor(0,0);
  lcd.print("To/b= ");
  lcd.print(tempobject,1);
  lcd.print("/");
  lcd.print(temppeltier,1);
  lcd.print("   ");
  lcd.setCursor(0,1);
  lcd.print("COG=");
  lcd.print(output1);
  lcd.print("/");
  lcd.print(fuzzyx);
  lcd.print("     ");
     
  Serial.print("Suhu Orang: ");
  Serial.print(tempobject);
  Serial.print("|| Suhu Object: ");
  Serial.println(temppeltier);

  fuzzy->setInput(1, tempobject);
  fuzzy->setInput(2, temppeltier);

  fuzzy->fuzzify();

  Serial.print("Suhu Object: Dingin-> ");
  Serial.print(dingin->getPertinence());
  Serial.print(", Normal-> ");
  Serial.print(normal->getPertinence());
  Serial.print(", Panas-> ");
  Serial.println(panas->getPertinence());

  Serial.print("Suhu Peltier: Rendah-> ");
  Serial.print(rendah->getPertinence());
  Serial.print(",  Sedang-> ");
  Serial.print(sedang->getPertinence());
  Serial.print(",  Tinggi-> ");
  Serial.print(tinggi->getPertinence());

  output1 = fuzzy->defuzzify(1);
  fuzzyx = map(output1,191,100,255,0);

  if(fuzzyx < 0){
    fuzzyx = 0;
  }
    
  Serial.println("Output: ");
  Serial.print("PWMOUT: Minimum-> ");
  Serial.print(minimum->getPertinence());
  Serial.print(", Average-> ");
  Serial.print(average->getPertinence());
  Serial.print(", Maximum-> ");
  Serial.println(maximum->getPertinence());

  Serial.print("HASIL: ");
  Serial.print("PWM: ");
  Serial.println(output1);
  analogWrite(9,fuzzyx);
  
  delay(1000);
}


3. VIDEO HASILNYA



No comments:

Post a Comment