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

attachInterrupt(0, INCRE, CHANGE);
attachInterrupt(1, DECRE, CHANGE);
analogWrite(8, 100);

// FuzzyInput (speed)
FuzzyInput* inputSpeed = new FuzzyInput(1);

//Fuzzy output 1
FuzzyOutput* elevation = new FuzzyOutput(1);

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

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

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

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

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

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()
{
val += (ASet != BSet) ? +1 : -1;
}

void DECRE()
{
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;
}

``````so your code looks like this