Pada kesempatan kali ini saya akan menjelaskan mengenai bagaimana cara membuat sebuah alat yang dapat digunakan untuk mengendalikan suhu pemanas sehingga suhunya sesuai setpoint atau fuzzy rule yang telah dibuat / diinginkan. alat ini menggunakan pemanas berupa peltier 5v dan sensor suhu Ds18b20 sebanyak 2 buah serta untuk pemprosesan datanya menggunakan Arduino Uno. untuk lebih jelasnya berikut adalah koding dan komponennya. untuk library eFLL bisa didownload pada link berikut.
#include <Fuzzy.h>
#include <Wire.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float tempobject;
float temppeltier;
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);
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 = sensors.getTempCByIndex(0);
temppeltier = sensors.getTempCByIndex(1);
Serial.print("Suhu Object: ");
Serial.print(tempobject);
Serial.print("|| Suhu Peltier: ");
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());
float output1 = fuzzy->defuzzify(1);
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,output1);
delay(1000);
}