/**
fuzzy_controller.ino
**/
// eFLL includes
#include <Fuzzy.h>
#include <FuzzyComposition.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzyOutput.h>
#include <FuzzyRule.h>
#include <FuzzyRuleAntecedent.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzySet.h>
// pins
#define LDR A0
#define TRIGGER 4
#define ECHO 5
#define LED 6
// object library
Fuzzy *fuzzy = new Fuzzy();
void setup() {
// set console and pins
Serial.begin(9600);
pinMode(LDR, INPUT);
pinMode(TRIGGER, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(LED, OUTPUT);
// fuzzy sets
// Input1 temp
FuzzySet *cool = new FuzzySet(0, 0, 15, 25); //trapizoid MF (a,b,c,d)
FuzzySet *midt = new FuzzySet(15, 25, 25, 32);
FuzzySet *hot = new FuzzySet(25, 32, 50, 50);
// Input2 pop
FuzzySet *low = new FuzzySet(0, 0, 15, 25);
FuzzySet *mid= new FuzzySet(15, 25, 25, 35);
FuzzySet *high= new FuzzySet(25, 35, 50, 50);
// output air comp
FuzzySet *off = new FuzzySet(0, 0, 0, 0);
FuzzySet *lowb = new FuzzySet(0, 0, 25, 40);
FuzzySet *midb = new FuzzySet(25, 40, 60, 70);
FuzzySet *highb = new FuzzySet(60, 75, 100, 100);
// variables
// add input1 to fuzzy Input1
FuzzyInput *distance = new FuzzyInput(1);
temp->addFuzzySet(cool);
temp->addFuzzySet(midt);
temp->addFuzzySet(hot);
fuzzy->addFuzzyInput(temp)
// add input2 to fuzzy Input2
FuzzyInput *ldr = new FuzzyInput(2);
pop->addFuzzySet(low);
pop->addFuzzySet(mid);
pop->addFuzzySet(high);
fuzzy->addFuzzyInput(pop)
// add output to fuzzy output
FuzzyOutput *brightness = new FuzzyOutput(1);
airCom->addFuzzySet(off);
airCom->addFuzzySet(lowb);
airCom->addFuzzySet(midb);
airCom->addFuzzySet(highb);
fuzzy->addFuzzyOutput(airCom);
// Rules 1
// if distance is coll and pop is low then low is low
FuzzyRuleConsequent *thenAirComMidb = new FuzzyRuleConsequent();
thenAirComMidb->addOutput(midb); ////for Potentiometer lowb , for actual ldr => high
FuzzyRule *fuzzyRule1 = new FuzzyRule(1, if tempIscoolAndpopIsLow, then airComoff);
fuzzy->addFuzzyRule(fuzzyRule1);
//Rules2
// if distance is cool and pop is midt then airCom is midt
FuzzyRuleAntecedent *ifTempIsCoolAndPopIsmidt = new FuzzyRuleAntecedent();
ifTempIsCoolAndPopIsmidt->joinWithAND(cool, midt); //for Potentiometer highldr , for actual ldr => lowldr
FuzzyRuleConsequent *thenAirCommid = new FuzzyRuleConsequent();
thenAirCommidt->addOutput(midt); ////for Potentiometer lowb , for actual ldr => highb
FuzzyRule *fuzzyRule2= new FuzzyRule(2, ifTempIsCoolAndPopIsmidt, thenAirCommidt);
fuzzy->addFuzzyRule(fuzzyRule2);
//Rules3
// If temp is cool and pop is high then aircom is high
FuzzyRuleAntecedent *ifTempIsCoolAndPopIshigh = new FuzzyRuleAntecedent();
ifTempIsCoolAndPopIshigh->joinWithAND(cool, high); //for Potentiometer highldr , for actual ldr => lowldr
FuzzyRuleConsequent *thenAirComhigh = new FuzzyRuleConsequent();
thenAirComhigh->addOutput(high); ////for Potentiometer lowb , for actual ldr => highb
FuzzyRule *fuzzyRule3 = new FuzzyRule(3, ifTempIsCoolAndPopIshigh, thenAirComhigh);
fuzzy->addFuzzyRule(fuzzyRule3);
// Rules 4
// if temp is mid and pop is low then airCom is low
FuzzyRuleAntecedent *ifTempIsCoolAndPopIslow = new FuzzyRuleAntecedent();
ifTempIsCoolAndPopIslow->joinWithAND(mid,low); //for Potentiometer highldr , for actual ldr => lowldr
FuzzyRuleConsequent *thenAirComlow = new FuzzyRuleConsequent();
thenAirComlow->addOutput(low); ////for Potentiometer lowb , for actual ldr => highb
FuzzyRule *fuzzyRule4 = new FuzzyRule(4, ifTempIsCoolAndPopIslow, thenAirComlow);
fuzzy->addFuzzyRule(fuzzyRule4);
// Rules 5
// if temp is midt and pop is mid then airCom is mid
FuzzyRuleAntecedent *ifTempIsCoolAndPopIsMidldr = new FuzzyRuleAntecedent();
ifTempIsCoolAndPopIsMid->joinWithAND(midt,mid); //for Potentiometer highldr , for actual ldr => lowldr
FuzzyRule *fuzzyRule5 = new FuzzyRule(5, ifTempIsCoolAndPopIsMid, thenAirComMid);
fuzzy->addFuzzyRule(fuzzyRule5);
}
// returns the distance
int distance() {
digitalWrite(TRIGGER, LOW);
delayMicroseconds(5);
digitalWrite(TRIGGER, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER, LOW);
long pulse = pulseIn(ECHO, HIGH) / 2;
return pulse * 10 / 292;
}
// returns the brightness
int brightness() {
return analogRead(LDR);
}
/*
// prints in serial monitor
void print(int dist, int light, int output) {
Serial.print("distance: ");
Serial.print(dist);
Serial.print(" light: ");
Serial.print(light);
Serial.print(" => output: ");
Serial.print(output);
Serial.println();
}
*/
// main method
void loop() {
// get distance and light
int dist = distance();
int light = brightness();
// if the inputs are weird, ignore them Miximum value
if (dist < 0 || dist > 400 || light > 1023) return;
// fuzzyfication
fuzzy->setInput(1, dist); // dist as fuzzy input 1 (distance)
fuzzy->setInput(2, light); // light as fuzzy input 2 (ldr)
fuzzy->fuzzify();
// defuzzyfication
int output = fuzzy->defuzzify(1); // defuzzify fuzzy output 1 (brightness)
analogWrite(LED, output);
//void print(int dist, int light, int output) {
Serial.print("distance: ");
Serial.print(dist);
Serial.print(" light: ");
Serial.print(light);
Serial.print(" => output: ");
Serial.print(output);
Serial.println();
delay(100);
}