Problem Fuzzy

i had problem about my defuzzification process.. when input1 & 2 =0, output should be 0. when i change input1 & 2 it give correct output value..but when i change back to input1&2 to 0, the output hold the previous value..it did not give 0 anymore.. :cold_sweat:

viva.txt (11.2 KB)

byte getvalue ()
{
  const byte returnVal [32] = {3, 6, 9, 13, 16, 19, 22, 25, 28, 31, 34,  38, 41, 44, 47,
                               50, 53, 56, ...etc };
  int index = (digitalRead (DS_L)<<4) | (digitalRead (DS_ML)<<3) | (digitalRead (DS_M)<< 2) | (digitalRead (DS_MR)<< 1) | digitalRead (DS_R);
  return returnVal [index];
}

Why write seven lines, when you can write the same thing in 140?

Why write seven lines, when you can write the same thing in 140?

LOL!

AWOL:

byte getvalue ()

{
 const byte returnVal [32] = {3, 6, 9, 13, 16, 19, 22, 25, 28, 31, 34,  38, 41, 44, 47,
                              50, 53, 56, ...etc };
 int index = (digitalRead (DS_L)<<4) | (digitalRead (DS_ML)<<3) | (digitalRead (DS_M)<< 2) | (digitalRead (DS_MR)<< 1) | digitalRead (DS_R);
 return returnVal [index];
}



Why write seven lines, when you can write the same thing in 140?

Or return map(index, 0, 31, 3, 100);

i had made changes using your solution but the output still had a same problem like previous programming.. i did not no where the wrong this program.. :cold_sweat:

#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>

define DS_L 13 //1

define DS_ML 12 //2

define DS_M 11 //3

define DS_MR 10 //4

define DS_R 9 //5

float val = 0;
float lastError = 0;

// Instantiating an object of library
Fuzzy* fuzzy = new Fuzzy();

FuzzySet* negative = new FuzzySet(-0.44, -0.44, -0.44, 0);
FuzzySet* zero = new FuzzySet(-0.44, 0, 0, 0.53);
FuzzySet* positive = new FuzzySet(0, 0.53, 0.53, 0.53);

FuzzySet* negatif = new FuzzySet(-0.97, -0.97, -0.97, 0);
FuzzySet* kosong = new FuzzySet(-0.97, 0, 0, 0.97);
FuzzySet* positif = new FuzzySet(0, 0.97, 0.97, 0.97);

void setup(){
pinMode(DS_L,INPUT);
pinMode(DS_ML,INPUT);
pinMode(DS_M,INPUT);
pinMode(DS_MR,INPUT);
pinMode(DS_R,INPUT);

Serial.begin(9600);

// FuzzyInput
FuzzyInput* error = new FuzzyInput(1);
error->addFuzzySet(negative);
error->addFuzzySet(zero);
error->addFuzzySet(positive);

fuzzy->addFuzzyInput(error);

// FuzzyInput
FuzzyInput* derror = new FuzzyInput(2);
derror->addFuzzySet(negatif);
derror->addFuzzySet(kosong);
derror->addFuzzySet(positif);

fuzzy->addFuzzyInput(derror);

// FuzzyOutput
FuzzyOutput* motors = new FuzzyOutput(1);

FuzzySet* HLeft = new FuzzySet(-1.50, -1.00, -1.00, -0.50);
motors->addFuzzySet(HLeft);
FuzzySet* SLeft = new FuzzySet(-1.00, -0.50, -0.50, 0);
motors->addFuzzySet(SLeft);
FuzzySet* Straight = new FuzzySet(-0.50, 0, 0, 0.50);
motors->addFuzzySet(Straight);
FuzzySet* SRight = new FuzzySet(0, 0.50, 0.50, 1.00);
motors->addFuzzySet(SRight);
FuzzySet* HRight = new FuzzySet(0.50, 1.00, 1.00, 1.50);
motors->addFuzzySet(HRight);

fuzzy->addFuzzyOutput(motors);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrornegativeAndderrornegatif = new FuzzyRuleAntecedent();
iferrornegativeAndderrornegatif->joinWithAND(negative, negatif);

FuzzyRuleConsequent* thenmotorsHRight = new FuzzyRuleConsequent();
thenmotorsHRight->addOutput(HRight);

FuzzyRule* fuzzyRule1 = new FuzzyRule(1, iferrornegativeAndderrornegatif, thenmotorsHRight);
fuzzy->addFuzzyRule(fuzzyRule1);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrornegativeAndderrorkosong = new FuzzyRuleAntecedent();
iferrornegativeAndderrorkosong->joinWithAND(negative, kosong);
FuzzyRuleAntecedent* iferrornegativeAndderrorpositif = new FuzzyRuleAntecedent();
iferrornegativeAndderrorpositif->joinWithAND(negative, positif);
FuzzyRuleAntecedent* iferrorzeroAndderrornegatif = new FuzzyRuleAntecedent();
iferrorzeroAndderrornegatif->joinWithAND(zero, negatif);

FuzzyRuleConsequent* thenmotorsSRight = new FuzzyRuleConsequent();
thenmotorsSRight->addOutput(SRight);

FuzzyRule* fuzzyRule2 = new FuzzyRule(2, iferrornegativeAndderrorkosong, thenmotorsSRight);
fuzzy->addFuzzyRule(fuzzyRule2);
FuzzyRule* fuzzyRule3 = new FuzzyRule(3, iferrornegativeAndderrorpositif, thenmotorsSRight);
fuzzy->addFuzzyRule(fuzzyRule3);
FuzzyRule* fuzzyRule4 = new FuzzyRule(4, iferrorzeroAndderrornegatif, thenmotorsSRight);
fuzzy->addFuzzyRule(fuzzyRule4);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrorzeroAndderrorkosong = new FuzzyRuleAntecedent();
iferrorzeroAndderrorkosong->joinWithAND(zero, kosong);

FuzzyRuleConsequent* thenmotorsStraight = new FuzzyRuleConsequent();
thenmotorsStraight->addOutput(Straight);

FuzzyRule* fuzzyRule5 = new FuzzyRule(5, iferrorzeroAndderrorkosong, thenmotorsStraight);
fuzzy->addFuzzyRule(fuzzyRule5);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrorzeroAndderrorpositif = new FuzzyRuleAntecedent();
iferrorzeroAndderrorpositif->joinWithAND(zero, positif);
FuzzyRuleAntecedent* iferrorpositiveAndderrornegatif = new FuzzyRuleAntecedent();
iferrorpositiveAndderrornegatif->joinWithAND(positive, negatif);
FuzzyRuleAntecedent* iferrorpositiveAndderrorkosong = new FuzzyRuleAntecedent();
iferrorpositiveAndderrorkosong->joinWithAND(positive, kosong);

FuzzyRuleConsequent* thenmotorsSLeft = new FuzzyRuleConsequent();
thenmotorsSLeft->addOutput(SLeft);

FuzzyRule* fuzzyRule6 = new FuzzyRule(6, iferrorzeroAndderrorpositif, thenmotorsSLeft);
fuzzy->addFuzzyRule(fuzzyRule6);
FuzzyRule* fuzzyRule7 = new FuzzyRule(7, iferrorpositiveAndderrornegatif, thenmotorsSLeft);
fuzzy->addFuzzyRule(fuzzyRule7);
FuzzyRule* fuzzyRule8 = new FuzzyRule(8, iferrorpositiveAndderrorkosong, thenmotorsSLeft);
fuzzy->addFuzzyRule(fuzzyRule8);

// Building FuzzyRule
FuzzyRuleAntecedent* iferrorpositiveAndderrorpositif = new FuzzyRuleAntecedent();
iferrorpositiveAndderrorpositif->joinWithAND(positive, positif);

FuzzyRuleConsequent* thenmotorsHLeft = new FuzzyRuleConsequent();
thenmotorsHLeft->addOutput(HLeft);

FuzzyRule* fuzzyRule9 = new FuzzyRule(9, iferrorpositiveAndderrorpositif, thenmotorsHLeft);
fuzzy->addFuzzyRule(fuzzyRule9);
}

void loop()
{
getvalue();
float error = val - 0.4688;
float derror = error - lastError;
lastError = error;

Serial.print("Value:");
Serial.println(val);
Serial.print("Error:");
Serial.println(error);
Serial.print("Derror:");
Serial.println(derror);

fuzzy->setInput(1,error);
fuzzy->setInput(2,derror);

fuzzy->fuzzify();

Serial.print("Error: ");
Serial.print(negative->getPertinence());
Serial.print(", ");
Serial.print(zero->getPertinence());
Serial.print(", ");
Serial.println(positive->getPertinence());

Serial.print("Derror: ");
Serial.print(negatif->getPertinence());
Serial.print(", ");
Serial.print(kosong->getPertinence());
Serial.print(", ");
Serial.println(positif->getPertinence());

float motors = fuzzy->defuzzify(1);

Serial.print("Motors: ");
Serial.println(motors);

delay(1000);
}

void getvalue()
{
float returnVal [32] = {0.0313, 0.0625, 0.0938, 0.1250, 0.1563, 0.1875, 0.2188, 0.2500, 0.2813, 0.3125, 0.3438, 0.3750, 0.4063, 0.4375, 0.4688,
0.5000, 0.5313, 0.5625, 0.5938, 0.6250, 0.6563, 0.6875, 0.7188, 0.7500, 0.7813, 0.8125, 0.8438, 0.8750, 0.9063, 0.9375, 0.9688, 1.0000};
int index = (digitalRead (DS_L)<<4) | (digitalRead (DS_ML)<<3) | (digitalRead (DS_M)<< 2) | (digitalRead (DS_MR)<< 1) | digitalRead (DS_R);
val = returnVal [index];
}

anybody know how to use this http://www.makeproto.com/projects/fuzzy/matlab_arduino_FIST/index.php i did not know how to put it into programming.. maybe some tutorial could be good for me..

Shorter still.
uncompiled, untested

float getvalue()
{
  int index = (digitalRead (DS_L)<<4) | (digitalRead (DS_ML)<<3) | (digitalRead (DS_M)<< 2) | (digitalRead (DS_MR)<< 1) | digitalRead (DS_R);
  return (float)(index + 1) / 32.0;
}