Using Fuzzy Logic for a braking system

Hi all,

Currently, I'm doing on a project where i am required to program a braking system using Fuzzy logic. I'm new to fuzzy logic and therefore i need some advice.

This is how the system will work,
using a rotary encoder to detect the speed of the wheel > setting a deviation on the speed>using Fuzzy Logic to determine on the elevation of the brake (angle ranging from 0-90 deg)> servo motor will be executing the brake.

example:
IF wheels at 20 m/s THEN servo motor elevation at 50 deg.
IF wheels at 30 m/s THEN Servomotor elevation at 90 deg.

thanks..

Who says it's fuzzy? That looks like a very definite set of rules. Maybe you could call it "proportional" but there's nothing fuzzy about it.

You can call it Fuzzy if you like. We can't stop you from calling your Arduino "Andy" or anything else.

hi MorganS,

FYI on fuzzy logic

MorganS is right that the example you've written is not fuzzy logic. If you squint really hard you could maybe argue that it's the start of the creation of a membership set but there's a long way to go before it would be called a fuzzy controller.

You haven't really said what advice you would like. Do you need help with the mathematics? Do you need someone to write the code for you? Do you need help selecting components?

Using Fuzzy Logic for a braking system

  • No need to brake - we're miles away and he is a good driver.
  • OK, maybe slow down a little.
  • Sheesh - WTF - his brake lights aren't working!

...R

hi all,

Currently, i'm having trouble in the codes in getting the servo motor to the angle which is required. Is there any error or ways which i can correct my coding?

Thanks.


#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>
#include <Servo.h>

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

//encoder input
FuzzySet* stopped = new FuzzySet(0, 0, 0, 0);
FuzzySet* slow = new FuzzySet(1, 10, 10, 15);
FuzzySet* normal = new FuzzySet(13, 20, 20, 25);
FuzzySet* Fast = new FuzzySet(0.24, 30, 30, 35);

//elevation output
FuzzySet* None = new FuzzySet(0, 0, 0, 0);
FuzzySet* Low = new FuzzySet(30, 30, 30, 30);
FuzzySet* middle = new FuzzySet(70, 70, 70,70);
FuzzySet* Full = new FuzzySet(90, 90, 90, 90);

//encoder
double val = 0;
byte PinA = 2;
byte PinB = 3;
int ASet;
int BSet;

//servomotor
Servo myservo;
int d = val * 6 / 200;

//sensor
const int EchoPin =11;
const int trigPin = 12;
long duration,cm;

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

//servo
myservo.attach(9);

//encoder
pinMode(PinA, INPUT);
pinMode(PinB, INPUT);
ASet = digitalRead(PinA);
BSet = digitalRead(PinB);

// read the input pin
attachInterrupt(0, INCRE, CHANGE);
attachInterrupt(1, DECRE, CHANGE);
analogWrite(8, 100);

// FuzzyInput (speed)
FuzzyInput* inputSpeed = new FuzzyInput(1);
inputSpeed->addFuzzySet(stopped);
inputSpeed->addFuzzySet(slow);
inputSpeed->addFuzzySet(normal);
inputSpeed->addFuzzySet(Fast);

fuzzy->addFuzzyInput(inputSpeed);

//Fuzzy output 1
FuzzyOutput* elevation = new FuzzyOutput(1);
elevation->addFuzzySet(None);
elevation->addFuzzySet(Low);
elevation->addFuzzySet(middle);
elevation->addFuzzySet(Full);

fuzzy->addFuzzyOutput(elevation);

//-------------Building FuzzyRule-------------- //

//if speed = stopped, elevation = none (Rule 1)//
FuzzyRuleAntecedent* ifSpeedStopped = new FuzzyRuleAntecedent();
ifSpeedStopped->joinSingle(stopped);
FuzzyRuleConsequent* thenElevationNone = new FuzzyRuleConsequent();
thenElevationNone->addOutput(None);
FuzzyRule*fuzzyRule01 = new FuzzyRule(1, ifSpeedStopped, thenElevationNone);
fuzzy->addFuzzyRule(fuzzyRule01);

//if speed = slow, elevation = low (Rule 2)//
FuzzyRuleAntecedent* ifSpeedSlow = new FuzzyRuleAntecedent();
ifSpeedSlow->joinSingle(slow);
FuzzyRuleConsequent* thenElevationLow = new FuzzyRuleConsequent();
thenElevationLow->addOutput(Low);
FuzzyRule*fuzzyRule02 = new FuzzyRule(2, ifSpeedSlow, thenElevationLow);
fuzzy->addFuzzyRule(fuzzyRule02);

//if speed = normal, elevation = middle (Rule 3)//
FuzzyRuleAntecedent* ifSpeedNormal = new FuzzyRuleAntecedent();
ifSpeedNormal->joinSingle(middle);
FuzzyRuleConsequent* thenElevationMiddle = new FuzzyRuleConsequent();
thenElevationMiddle->addOutput(middle);
FuzzyRule*fuzzyRule03 = new FuzzyRule(3, ifSpeedNormal, thenElevationMiddle);
fuzzy->addFuzzyRule(fuzzyRule03);

//if speed = slow, elevation = low (Rule 4)//
FuzzyRuleAntecedent* ifSpeedFast = new FuzzyRuleAntecedent();
ifSpeedFast->joinSingle(Full);
FuzzyRuleConsequent* thenElevationFull = new FuzzyRuleConsequent();
thenElevationFull->addOutput(Full);
FuzzyRule*fuzzyRule04 = new FuzzyRule(4, ifSpeedFast, thenElevationFull);
fuzzy->addFuzzyRule(fuzzyRule04);
}

void loop()
{
encoder();
fuzzy1();
}

void encoder()
{
int time = millis() / 1000;
if (time % 1 == 0)
{
Serial.print(time);
Serial.print(" s \t ");
Serial.print(val * 6 / 200);
Serial.println(" rpm");
val = 0; delay(1000);
}
}

void INCRE()
{
ASet = digitalRead(PinA) == HIGH;
val += (ASet != BSet) ? +1 : -1;
}

void DECRE()
{
BSet = digitalRead(PinB) == HIGH;
val += (ASet == BSet) ? +1 : -1;
}

void elevation()
{

}

void fuzzy1()
{

//convert the time into a distnace
cm = microsecondsToCentimeters(duration);

fuzzy->setInput(1, val * 6 / 200);
fuzzy->setInput(2, cm);
fuzzy->fuzzify();

//speed
Serial.print("Speed: ");
Serial.print(stopped->getPertinence());
Serial.print(", ");
Serial.print(slow->getPertinence());
Serial.print(", ");
Serial.print(normal->getPertinence());
Serial.print(", ");
Serial.println(Fast->getPertinence());

//elevation
Serial.print("Elevation: ");
Serial.print(None->getPertinence());
Serial.print(",");
Serial.print(Low->getPertinence());
Serial.print(", ");
Serial.print(middle->getPertinence());
Serial.print(", ");
Serial.println(Full->getPertinence());

float output1 = fuzzy->defuzzify(1);

//print servo angle value
Serial.print("Servo angle: ");
Serial.print(output1);
Serial.println(" degrees");
myservo.write(output1);

delay(1);
}

long microsecondsToCentimeters(long microseconds)

{ return microseconds / 29 / 2;
}

Please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum Your code is too long for me to study quickly without copying to a text editor.

Also post a link to the documentation for the Fuzzy library - don't expect people to be familiar with it.

And tell us in detail what that program actually does and what would want it to do that is different.

...R