Fuzzy Logic Code for Arduino

Sorry, I am not able to post the fuzzy file.

#include <FuzzyRule.h>
#include <FuzzyComposition.h>
#include <Fuzzy.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzyOutput.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzySet.h>
#include <FuzzyRuleAntecedent.h>

Fuzzy* fuzzy = new Fuzzy();

FuzzySet* S0 = new FuzzySet(0, 0, 0, 30);
FuzzySet* S1 = new FuzzySet(0, 30, 30, 70);
FuzzySet* S2 = new FuzzySet(30, 90, 90, 130);
FuzzySet* S3 = new FuzzySet(90, 150, 150, 190);
FuzzySet* S4 = new FuzzySet(150, 210, 210, 250);
FuzzySet* S5 = new FuzzySet(210, 270, 270, 310);
FuzzySet* S6 = new FuzzySet(270, 330, 330, 360);
FuzzySet* S7 = new FuzzySet(310, 360, 360, 360);

//FuzzySet* FL = new FuzzySet(0, 0, 0, 1);
//FuzzySet* FM = new FuzzySet(0, 50, 50, 100);
//FuzzySet* FH = new FuzzySet(50, 100, 100, 100);

//FuzzySet* RL = new FuzzySet(0, 0, 0, 1);
//FuzzySet* RM = new FuzzySet(0, 50, 50, 100);
//FuzzySet* RH = new FuzzySet(50, 100, 100, 100);

void setup()
{
Serial.begin(9600);

//FuzzyInput
FuzzyInput* imu = new FuzzyInput(1);
imu->addFuzzySet(S0);
imu->addFuzzySet(S1);
imu->addFuzzySet(S2);
imu->addFuzzySet(S3);
imu->addFuzzySet(S4);
imu->addFuzzySet(S5);
imu->addFuzzySet(S6);
imu->addFuzzySet(S7);

fuzzy->addFuzzyInput(imu);

//FuzzyOutput
//Motor 1 Forward
FuzzyOutput* m1f = new FuzzyOutput(1);

FuzzySet* FL1 = new FuzzySet(0, 0, 0, 1);
m1f->addFuzzySet(FL1);
FuzzySet* FM1 = new FuzzySet(0, 50, 50, 100);
m1f->addFuzzySet(FM1);
FuzzySet* FH1 = new FuzzySet(50, 100, 100, 100);
m1f->addFuzzySet(FH1);

fuzzy->addFuzzyOutput(m1f);

//Motor 1 Reverse
FuzzyOutput* m1r = new FuzzyOutput(2);

FuzzySet* RL1 = new FuzzySet(0, 0, 0, 1);
m1r->addFuzzySet(RL1);
FuzzySet* RM1 = new FuzzySet(0, 50, 50, 100);
m1r->addFuzzySet(RM1);
FuzzySet* RH1 = new FuzzySet(50, 100, 100, 100);
m1r->addFuzzySet(RH1);

fuzzy->addFuzzyOutput(m1r);

//Motor 2 Forward
FuzzyOutput* m2f = new FuzzyOutput(3);

FuzzySet* FL2 = new FuzzySet(0, 0, 0, 1);
m2f->addFuzzySet(FL2);
FuzzySet* FM2 = new FuzzySet(0, 50, 50, 100);
m2f->addFuzzySet(FM2);
FuzzySet* FH2 = new FuzzySet(50, 100, 100, 100);
m2f->addFuzzySet(FH2);

fuzzy->addFuzzyOutput(m2f);

//Motor 2 Reverse
FuzzyOutput* m2r = new FuzzyOutput(4);

FuzzySet* RL2 = new FuzzySet(0, 0, 0, 1);
m2r->addFuzzySet(RL2);
FuzzySet* RM2 = new FuzzySet(0, 50, 50, 100);
m2r->addFuzzySet(RM2);
FuzzySet* RH2 = new FuzzySet(50, 100, 100, 100);
m2r->addFuzzySet(RH2);

fuzzy->addFuzzyOutput(m2r);

//Motor 3 Forward
FuzzyOutput* m3f = new FuzzyOutput(5);

FuzzySet* FL3 = new FuzzySet(0, 0, 0, 1);
m3f->addFuzzySet(FL3);
FuzzySet* FM3 = new FuzzySet(0, 50, 50, 100);
m3f->addFuzzySet(FM3);
FuzzySet* FH3 = new FuzzySet(50, 100, 100, 100);
m3f->addFuzzySet(FH3);

fuzzy->addFuzzyOutput(m3f);

//Motor 3 Reverse
FuzzyOutput* m3r = new FuzzyOutput(6);

FuzzySet* RL3 = new FuzzySet(0, 0, 0, 1);
m3r->addFuzzySet(RL3);
FuzzySet* RM3 = new FuzzySet(0, 50, 50, 100);
m3r->addFuzzySet(RM3);
FuzzySet* RH3 = new FuzzySet(50, 100, 100, 100);
m3r->addFuzzySet(RH3);

fuzzy->addFuzzyOutput(m3r);

//Building FuzzyRule-1
FuzzyRuleAntecedent* ifimuS0 = new FuzzyRuleAntecedent(); //Instantiating an Antecedent to expression
ifimuS0->joinSingle(S0); //Adding corresponding FuzzySet to Antecedent object

FuzzyRuleConsequent* a = new FuzzyRuleConsequent(); //thenm1fFL1Andm1rRM1Andm2fFL2Andm2rRM2Andm3fFH3Andm3rRL3
a->addOutput(FL1);
a->addOutput(RM1);
a->addOutput(FL2);
a->addOutput(RM2);
a->addOutput(FH3);
a->addOutput(RL3);

FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifimuS0, a );
fuzzy->addFuzzyRule(fuzzyRule1);

//Buliding FuzzyRule-2
FuzzyRuleAntecedent* ifimuS1 = new FuzzyRuleAntecedent();
ifimuS1->joinSingle(S1);

FuzzyRuleConsequent* b= new FuzzyRuleConsequent();
b->addOutput(FL1);
b->addOutput(RL1);
b->addOutput(FL2);
b->addOutput(RH2);
b->addOutput(FH3);
b->addOutput(RL3);

FuzzyRule*fuzzyRule2 = new FuzzyRule(2, ifimuS1, b);
fuzzy->addFuzzyRule(fuzzyRule2);

//Buliding FuzzyRule-3
FuzzyRuleAntecedent* ifimuS2 = new FuzzyRuleAntecedent();
ifimuS2->joinSingle(S2);

FuzzyRuleConsequent* c = new FuzzyRuleConsequent();
c->addOutput(FH1);
c->addOutput(RL1);
c->addOutput(FL2);
c->addOutput(RH2);
c->addOutput(FL3);
c->addOutput(RL3);

FuzzyRule*fuzzyRule3 = new FuzzyRule(3, ifimuS2, c);
fuzzy->addFuzzyRule(fuzzyRule3);

//Buliding FuzzyRule-4
FuzzyRuleAntecedent* ifimuS3 = new FuzzyRuleAntecedent();
ifimuS3->joinSingle(S3);

FuzzyRuleConsequent* d = new FuzzyRuleConsequent();
d->addOutput(FH1);
d->addOutput(RL1);
d->addOutput(FL2);
d->addOutput(RL2);
d->addOutput(FL3);
d->addOutput(RH3);

FuzzyRule*fuzzyRule4 = new FuzzyRule(4, ifimuS3, d);
fuzzy->addFuzzyRule(fuzzyRule4);

//Buliding FuzzyRule-5
FuzzyRuleAntecedent* ifimuS4 = new FuzzyRuleAntecedent();
ifimuS4->joinSingle(S4);

FuzzyRuleConsequent* thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3 = new FuzzyRuleConsequent();
thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3->addOutput(FL1);
thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3->addOutput(RL1);
thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3->addOutput(FH2);
thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3->addOutput(RL2);
thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3->addOutput(FL3);
thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3->addOutput(RH3);

FuzzyRule*fuzzyRule5 = new FuzzyRule(5, ifimuS4, thenm1fFL1Andm1rRL1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRH3);
fuzzy->addFuzzyRule(fuzzyRule5);

//Buliding FuzzyRule-6
FuzzyRuleAntecedent* ifimuS5 = new FuzzyRuleAntecedent();
ifimuS5->joinSingle(S5);

FuzzyRuleConsequent* thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3 = new FuzzyRuleConsequent();
thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3->addOutput(FL1);
thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3->addOutput(RH1);
thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3->addOutput(FH2);
thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3->addOutput(RL2);
thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3->addOutput(FL3);
thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3->addOutput(RL3);

FuzzyRule*fuzzyRule6 = new FuzzyRule(6, ifimuS5, thenm1fFL1Andm1rRH1Andm2fFH2Andm2rRL2Andm3fFL3Andm3rRL3);
fuzzy->addFuzzyRule(fuzzyRule6);

continued…

//Buliding FuzzyRule-7
FuzzyRuleAntecedent* ifimuS6 = new FuzzyRuleAntecedent();
ifimuS6->joinSingle(S6);

FuzzyRuleConsequent* thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3 = new FuzzyRuleConsequent();
thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3->addOutput(FL1);
thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3->addOutput(RH1);
thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3->addOutput(FL2);
thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3->addOutput(RL2);
thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3->addOutput(FH3);
thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3->addOutput(RL3);

FuzzyRule*fuzzyRule7 = new FuzzyRule(7, ifimuS6, thenm1fFL1Andm1rRH1Andm2fFL2Andm2rRL2Andm3fFH3Andm3rRL3);
fuzzy->addFuzzyRule(fuzzyRule7);

//Buliding FuzzyRule-8
//FuzzyRuleAntecedent* ifimuS7 = new FuzzyRuleAntecedent();
//ifimuS7->joinSingle(S7);

//FuzzyRuleConsequent* thenm1fFLAndm1rRHAndm2fFLAndm2rRLAndm3fFHAndm3rRL = new FuzzyRuleConsequent();
//thenm1fFLAndm1rRHAndm2fFLAndm2rRLAndm3fFHAndm3rRL->addOutput(FL);
//thenm1fFLAndm1rRHAndm2fFLAndm2rRLAndm3fFHAndm3rRL->addOutput(RH);
//thenm1fFLAndm1rRHAndm2fFLAndm2rRLAndm3fFHAndm3rRL->addOutput(FL);
//thenm1fFLAndm1rRHAndm2fFLAndm2rRLAndm3fFHAndm3rRL->addOutput(RL);
//thenm1fFLAndm1rRHAndm2fFLAndm2rRLAndm3fFHAndm3rRL->addOutput(FH);
//thenm1fFLAndm1rRHAndm2fFLAndm2rRLAndm3fFHAndm3rRL->addOutput(RL);

//FuzzyRule*fuzzyRule8 = new FuzzyRule(8, ifimuS7, thenm1fFLAndm1rRHAndm2fFHAndm2rRLAndm3fFLAndm3rRL);
//fuzzy->addFuzzyRule(fuzzyRule8);

}

void loop()
{
fuzzy->setInput(1, 0);

fuzzy->fuzzify();

Serial.print("imu:");
Serial.print(S0->getPertinence());
Serial.print("\n");
Serial.print(S1->getPertinence());
Serial.print("\n");
Serial.print(S2->getPertinence());
Serial.print("\n");
Serial.print(S3->getPertinence());
Serial.print("\n");
Serial.print(S4->getPertinence());
Serial.print("\n");
Serial.print(S5->getPertinence());
Serial.print("\n");

float output1 = fuzzy->defuzzify(1);
float output2 = fuzzy->defuzzify(2);
float output3 = fuzzy->defuzzify(3);
float output4 = fuzzy->defuzzify(4);
float output5 = fuzzy->defuzzify(5);
float output6 = fuzzy->defuzzify(6);

Serial.print("Motor 1 Forward:");
Serial.print(output1);
Serial.print("\n");

Serial.print("Motor 1 Reverse:");
Serial.print(output2);
Serial.print("\n");

Serial.print("Motor 2 Forward:");
Serial.print(output3);
Serial.print("\n");

Serial.print("Motor 2 Reverse:");
Serial.print(output4);
Serial.print("\n");

Serial.print("Motor 3 Forward:");
Serial.print(output5);
Serial.print("\n");

Serial.print("Motor 3 Reverse:");
Serial.print(output6);

delay(100000);
}

The output should be like this:

motor 1 forward= 0, motor 1 reverse= 50, motor 2 f/w= 0, motor 2 r/w= 50, motor 3 f/w= 83.7, motor 3 r/w= 0

Please modify your posts and use code tags

And what does the output looks like?

Hi,
Is this associated with this thread?

Tom… :o :o

I am trying to write arduino code for fuzzy logic. I have one input from IMU and 6 output to 3 motors. I am not able to get output in serial monitor. Can someone help?

See reply #3.

I am trying to write arduino code for fuzzy logic. I have one input from IMU and 6 output to 3 motors. I am not able to get output in serial monitor. Can someone help?

#include <FuzzyRule.h>
#include <FuzzyComposition.h>
#include <Fuzzy.h>
#include <FuzzyRuleConsequent.h>
#include <FuzzyOutput.h>
#include <FuzzyInput.h>
#include <FuzzyIO.h>
#include <FuzzySet.h>
#include <FuzzyRuleAntecedent.h>

Fuzzy* fuzzy = new Fuzzy();

FuzzySet* S0 = new FuzzySet(0, 0, 0, 30);
FuzzySet* S1 = new FuzzySet(0, 30, 30, 70);
FuzzySet* S2 = new FuzzySet(30, 90, 90, 130);
FuzzySet* S3 = new FuzzySet(90, 150, 150, 190);
FuzzySet* S4 = new FuzzySet(150, 210, 210, 250);
FuzzySet* S5 = new FuzzySet(210, 270, 270, 310);
FuzzySet* S6 = new FuzzySet(270, 330, 330, 360);
FuzzySet* S7 = new FuzzySet(310, 360, 360, 360);

//FuzzySet* FL = new FuzzySet(0, 0, 0, 1);
//FuzzySet* FM = new FuzzySet(0, 50, 50, 100);
//FuzzySet* FH = new FuzzySet(50, 100, 100, 100);

//FuzzySet* RL = new FuzzySet(0, 0, 0, 1);
//FuzzySet* RM = new FuzzySet(0, 50, 50, 100);
//FuzzySet* RH = new FuzzySet(50, 100, 100, 100);

void setup()
{
Serial.begin(9600);

//FuzzyInput
FuzzyInput* imu = new FuzzyInput(1);
imu->addFuzzySet(S0);
imu->addFuzzySet(S1);
imu->addFuzzySet(S2);
imu->addFuzzySet(S3);
imu->addFuzzySet(S4);
imu->addFuzzySet(S5);
imu->addFuzzySet(S6);
imu->addFuzzySet(S7);

fuzzy->addFuzzyInput(imu);

//FuzzyOutput
//Motor 1 Forward
FuzzyOutput* m1f = new FuzzyOutput(1);

FuzzySet* FL1 = new FuzzySet(0, 0, 0, 1);
m1f->addFuzzySet(FL1);
FuzzySet* FM1 = new FuzzySet(0, 50, 50, 100);
m1f->addFuzzySet(FM1);
FuzzySet* FH1 = new FuzzySet(50, 100, 100, 100);
m1f->addFuzzySet(FH1);

fuzzy->addFuzzyOutput(m1f);

//Motor 1 Reverse
FuzzyOutput* m1r = new FuzzyOutput(2);

FuzzySet* RL1 = new FuzzySet(0, 0, 0, 1);
m1r->addFuzzySet(RL1);
FuzzySet* RM1 = new FuzzySet(0, 50, 50, 100);
m1r->addFuzzySet(RM1);
FuzzySet* RH1 = new FuzzySet(50, 100, 100, 100);
m1r->addFuzzySet(RH1);

fuzzy->addFuzzyOutput(m1r);

//Motor 2 Forward
FuzzyOutput* m2f = new FuzzyOutput(3);

FuzzySet* FL2 = new FuzzySet(0, 0, 0, 1);
m2f->addFuzzySet(FL2);
FuzzySet* FM2 = new FuzzySet(0, 50, 50, 100);
m2f->addFuzzySet(FM2);
FuzzySet* FH2 = new FuzzySet(50, 100, 100, 100);
m2f->addFuzzySet(FH2);

fuzzy->addFuzzyOutput(m2f);

//Motor 2 Reverse
FuzzyOutput* m2r = new FuzzyOutput(4);

FuzzySet* RL2 = new FuzzySet(0, 0, 0, 1);
m2r->addFuzzySet(RL2);
FuzzySet* RM2 = new FuzzySet(0, 50, 50, 100);
m2r->addFuzzySet(RM2);
FuzzySet* RH2 = new FuzzySet(50, 100, 100, 100);
m2r->addFuzzySet(RH2);

(page 1)

See reply #7.

And reply 5

Hi,

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile:
PS. Please read post #5.