Arduino code for fuzzy logic controller

Respected Sir,

could u help me out with the arduino code for a fuzzy logic controller.
I am planning to implement a collision avoidance mobile robot using the fuzzy logic controller

Have you tried this?

Fuzzy logic is a marketing term. Technically, it is called "an algorithm". It uses '0' bits and '1' bits with no fuzziness in between.

If only my professors had accepted that definition I could have saved a lot of time in the past. Of course its the same with neural networks and genetic algorithms and even money in the bank is just 0s and 1s - we should all just give up and go home now.

Duane B

rcarduino.blogspot.com

and even money in the bank is just 0s and 1s

Mostly the former in mine, it seems.

mariathms28:
I am planning to implement a collision avoidance mobile robot using the fuzzy logic controller

In that case you need to design the control algorithm in sufficient for it to be represented in code, and then implement it, and then test and fix it until it meets your requirements.

Obviously you would also need to have a pretty clear idea of how the collision avoidance algorithm was intended to work, and if you're specifying an approach based on fuzzy logic then you would also need to thoroughly understand what fuzzy logic is and how it is applicable to this problem.

Did you even google "fuzzy logic collision avoidance"? Tons of papers there.

AWOL:

and even money in the bank is just 0s and 1s

Mostly the former in mine, it seems.

Before or after the 1s?

good morning i want to write a program to make collision avoidance with fuzzy logic. my problem is when i test my code, i have good result in my serial board and when i connect my two motors, the us sensor don't send good diwtance like before. can somebody help me? this is my sketch

#include <Servo.h>

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

Servo servo;
// variables to take x number of readings and then average them
// to remove the jitter/noise from the DYP-ME007 sonar readings
const int numOfReadings = 10;
// number of readings to take/ items in the array
int readings[numOfReadings];
// stores the distance readings in an array
int arrayIndex = 0;
// arrayIndex of the current item in the array
int total = 0;
// stores the cumlative total
int averageDistance = 0;
// DYP-ME007 echo pin (digital 5)
int echoPin = 8;
// DYP-ME007 trigger pin (digital 4)
int initPin = 10;
// stores the pulse in Micro Seconds
unsigned long pulseTime = 0;
// variable for storing the distance (cm)
unsigned long dist = 0;
// Instanciando um objeto da biblioteca
Fuzzy* fuzzy = new Fuzzy();

int ENA = 5; //M1 Speed Control
int ENB = 6; //M2 Speed Control
int IN1=2;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int IN3=4;//connected to Arduino's port 4
int IN4=7;

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

pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);

servo.attach(9);

// set init pin as output
pinMode(initPin, OUTPUT);
// set echo pin as input
pinMode(echoPin, INPUT);
// create array loop to iterate over every item in the array
for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {
readings[thisReading] = 0;
}

// Criando o FuzzyInput distancia
FuzzyInput* distance = new FuzzyInput(1);
// Criando os FuzzySet que compoem o FuzzyInput distancia
FuzzySet* small = new FuzzySet(0, 75, 75, 150); // Distancia pequena
distance->addFuzzySet(small); // Adicionando o FuzzySet small em distance
FuzzySet* safe = new FuzzySet(112, 187, 187, 262); // Distancia segura
distance->addFuzzySet(safe); // Adicionando o FuzzySet safe em distance
FuzzySet* big = new FuzzySet(225, 300, 300, 300); // Distancia grande
distance->addFuzzySet(big); // Adicionando o FuzzySet big em distance

fuzzy->addFuzzyInput(distance); // Adicionando o FuzzyInput no objeto Fuzzy

// Criando o FuzzyOutput velocidade
FuzzyOutput* velocitya = new FuzzyOutput(1);
// Criando os FuzzySet que compoem o FuzzyOutput velocidade
FuzzySet* slow = new FuzzySet(0, 60, 60, 96); // Velocidade lenta
velocitya->addFuzzySet(slow); // Adicionando o FuzzySet slow em velocity
FuzzySet* average = new FuzzySet(72, 150, 150, 210); // Velocidade normal
velocitya->addFuzzySet(average); // Adicionando o FuzzySet average em velocity
FuzzySet* fast = new FuzzySet(180, 240, 240, 240); // Velocidade alta
velocitya->addFuzzySet(fast); // Adicionando o FuzzySet fast em velocity

fuzzy->addFuzzyOutput(velocitya); // Adicionando o FuzzyOutput no objeto Fuzzy

// Criando o FuzzyOutput velocidade
FuzzyOutput* velocityb = new FuzzyOutput(2);
// Criando os FuzzySet que compoem o FuzzyOutput velocidade
FuzzySet* slowb = new FuzzySet(0, 60, 60, 60); // Velocidade lenta
velocityb->addFuzzySet(slowb); // Adicionando o FuzzySet slow em velocity
FuzzySet* averageb = new FuzzySet(72, 150, 150, 210); // Velocidade normal
velocityb->addFuzzySet(averageb); // Adicionando o FuzzySet average em velocity
FuzzySet* fastb = new FuzzySet(180, 240, 240, 240); // Velocidade alta
velocityb->addFuzzySet(fastb); // Adicionando o FuzzySet fast em velocity

fuzzy->addFuzzyOutput(velocityb); // Adicionando o FuzzyOutput no objeto Fuzzy

//-------------------- Montando as regras Fuzzy
// FuzzyRule "SE distancia = pequena ENTAO velocidade = lenta"
FuzzyRuleAntecedent* ifDistanceSmall = new FuzzyRuleAntecedent(); // Instanciando um Antecedente para a expresso
ifDistanceSmall->joinSingle(small); // Adicionando o FuzzySet correspondente ao objeto Antecedente
FuzzyRuleConsequent* thenVelocityaSlowAndVelocitybAverageb = new FuzzyRuleConsequent(); // Instancinado um Consequente para a expressao
thenVelocityaSlowAndVelocitybAverageb->addOutput(slow);// Adicionando o FuzzySet correspondente ao objeto Consequente
thenVelocityaSlowAndVelocitybAverageb->addOutput(averageb);
// Instanciando um objeto FuzzyRule
FuzzyRule* fuzzyRule1 = new FuzzyRule(1, ifDistanceSmall, thenVelocityaSlowAndVelocitybAverageb); // Passando o Antecedente e o Consequente da expressao
fuzzy->addFuzzyRule(fuzzyRule1); // Adicionando o FuzzyRule ao objeto Fuzzy

// FuzzyRule "SE distancia = segura ENTAO velocidade = normal"
FuzzyRuleAntecedent* ifDistanceSafe = new FuzzyRuleAntecedent(); // Instanciando um Antecedente para a expresso
ifDistanceSafe->joinSingle(safe); // Adicionando o FuzzySet correspondente ao objeto Antecedente
FuzzyRuleConsequent* thenVelocityaAverageAndVelocitybAverage = new FuzzyRuleConsequent(); // Instancinado um Consequente para a expressao
thenVelocityaAverageAndVelocitybAverage->addOutput(average);// Adicionando o FuzzySet correspondente ao objeto Consequente
thenVelocityaAverageAndVelocitybAverage->addOutput(averageb);
// Instanciando um objeto FuzzyRule
FuzzyRule* fuzzyRule2 = new FuzzyRule(2, ifDistanceSafe, thenVelocityaAverageAndVelocitybAverage); // Passando o Antecedente e o Consequente da expressao
fuzzy->addFuzzyRule(fuzzyRule2); // Adicionando o FuzzyRule ao objeto Fuzzy

// FuzzyRule "SE distancia = grande ENTAO velocidade = alta"
FuzzyRuleAntecedent* ifDistanceBig = new FuzzyRuleAntecedent(); // Instanciando um Antecedente para a expresso
ifDistanceBig->joinSingle(big); // Adicionando o FuzzySet correspondente ao objeto Antecedente
FuzzyRuleConsequent* thenVelocityaFastAndVelocitybFast = new FuzzyRuleConsequent(); // Instancinado um Consequente para a expressao
thenVelocityaFastAndVelocitybFast->addOutput(fast);// Adicionando o FuzzySet correspondente ao objeto Consequente
thenVelocityaFastAndVelocitybFast->addOutput(fastb);
// Instanciando um objeto FuzzyRule
FuzzyRule* fuzzyRule3 = new FuzzyRule(3, ifDistanceBig, thenVelocityaFastAndVelocitybFast); // Passando o Antecedente e o Consequente da expressao
fuzzy->addFuzzyRule(fuzzyRule3); // Adicionando o FuzzyRule ao objeto Fuzzy
}

void scan(){

// send 10 microsecond pulse
digitalWrite(initPin, HIGH);
// wait 10 microseconds before turning off
delayMicroseconds(10);
// stop sending the pulse
digitalWrite(initPin, LOW);

// Look for a return pulse, it should be high as the pulse goes low-high-low
pulseTime = pulseIn(echoPin, HIGH);
// Distance = pulse time / 58 to convert to cm.
dist = pulseTime/58;
// subtract the last distance
total = total - readings[arrayIndex];
// add distance reading to array
readings[arrayIndex] = dist;
// add the reading to the total
total = total + readings[arrayIndex];

arrayIndex = arrayIndex + 1;
// go to the next item in the array
// At the end of the array (10 items) then start again
if (arrayIndex >= numOfReadings) {
arrayIndex = 0;
}

}

void loop(){
servo.write(90);
scan();

// calculate the average distance
averageDistance = total / numOfReadings;
// print out the average distance to the debugger

fuzzy->setInput(1, averageDistance);

fuzzy->fuzzify();

float output1 = fuzzy->defuzzify(1);
float output2 = fuzzy->defuzzify(2);
output1=round(output1);
output2=round(output2);
if(averageDistance<300){
output1=output1;
output2=output2;
analogWrite(ENA,output1);
analogWrite(ENB,output2);
}
else{
output1=190;
output2=190;
analogWrite(ENA,output1);
analogWrite(ENB,output2);
}

Serial.print("Distancia: ");
Serial.print(averageDistance);
Serial.print("Velocidadea: ");
Serial.println(output1);
Serial.print("Velocidadeb: ");
Serial.print(output2);
// wait 100 milli seconds before looping again
delay(100);
}

Oh dear, CODE tags and proper formatting please, few people will be willing to read that.

To format use ^T in the IDE, then edit your post, highlight the code, and click the # button at the top of the editing area.


Rob

When I try your codes in my arduino ide, it seems there is another error aside from yours. The code " FuzzyRule* fuzzyRule3 = new FuzzyRule(3, ifDistanceBig, thenVelocityaFastAndVelocitybFast); // Passando o Antecedente e o Consequente da expressao" is showing an error that says the "ifdistance" is not declared in the scope. Do you have any tutorial/resources that I can use to improve my knowledge in using fuzzy logic controller in arduino?

I had successfully compile the code.

Sketch uses 14,534 bytes (45%) of program storage space. Maximum is 32,256 bytes.
Global variables use 339 bytes (16%) of dynamic memory, leaving 1,709 bytes for local variables. Maximum is 2,048 bytes.

Had you install the eFLL (Embedded Fuzzy Logic Library) library for Embedded Systems?

http://forum.arduino.cc/index.php?topic=124827.0

AmbiLobe:
Fuzzy logic is a marketing term. Technically, it is called "an algorithm". It uses '0' bits and '1' bits with no fuzziness in between.

The fuzziness in fuzzy logic has nothing to do with fuzz between 0s and 1s. It's to do with set membership. In typical crisp logic, Venn diagram stuff, an element is either "in" a set or "not in" a set; something's either "hot" or "cold", or "tall" or "short". In fuzzy logic, an element can be a member of both the "tall" and "short" sets.

The output is never fuzzy, in the sense that a set of inputs will give the same output each time, unless the algorithm or its parameters have changed in the meantime.

Can someone have a copy of english comments on the codes? Because only few programmers can understand the language of the said program especially the guide comments.