Pages: [1]   Go Down
Author Topic: Arduino code for fuzzy logic controller  (Read 1159 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you tried this?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

New England
Offline Offline
Sr. Member
****
Karma: 7
Posts: 295
Natural Semiinductor
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I am going to get going.

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 21
Posts: 1670
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 239
Posts: 24371
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and even money in the bank is just 0s and 1s
Mostly the former in mine, it seems.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11179
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Rapa Nui
Offline Offline
Edison Member
*
Karma: 53
Posts: 1990
Pukao hats cleaning services
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1067
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
and even money in the bank is just 0s and 1s
Mostly the former in mine, it seems.

Before or after the 1s?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
Logged

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 121
Posts: 8453
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: