This is a step-by-step tutorial on how to create a Fuzzy logic Controller for an Automatic Braking System using MATLAB(R2015a). Below is a typical automotive braking system and we shall discuss how the system can be automated based on a set of input parameters.

*Source: www.carparts.com*

The problem at hand has been adopted from **Artificial Intelligence Illuminated** – by **Ben Coppin** (1st Edition, Jones & Bartlett) – p.516-522.

Before we look at the problem, we need to understand the four key steps in creating a Fuzzy Logic System (FLS):

- Define inputs and outputs
- Create a membership function
- Create rules
- Simulate the fuzzy logic system / Fuzzy Inference System (FIS)

To get clarity on the above steps, we have to analyze the problem at hand. The problem given in the text book may look trivial but the actual scenario might be quite complicated and may comprise of thousands of rules.

As per the above problem, there are three inputs:

- Pressure on the brake pedal (brake pressure)
- Car speed
- Wheel speed

And one output:

- Brake (which has two decision parameters- Apply Brake or Release Brake)

To apply these rules, using Mamdani inference, the first step is to fuzzify the crisp input values. To do this, we need first to define the fuzzy sets for the various linguistic variables we are using. For this simple example, we will assume that brake pressure is measured from 0 (no pressure) to 100 (brake fully applied).

We will define brake pressure as having three linguistic values: high (H), medium (M), and low (L), which we will define as follows:

** H = {(50, 0), (100, 1)}**

** M = {(30, 0), (50, 1), (70, 0)}**

** L = {(0, 1), (50, 0)}**

Even though the author has considered the the membership function for the input parameter “brake_pressure” as triangular, I would opt for gaussian membership function (which suits the input requirement here)

You can create FIS using MATLAB or sciLab. Both have built-in fuzzy logic toolkits for this. I’m using MATLAB.

Step 1 : Open Matlab and type *fuzzy* on the command line to invoke the FLT as shown:

This will open the fuzzy logic designer console as follows:

Step 2 : Click on the “inputs” section (in yellow) to add inputs. Note that an input is already defined here, we need to add two more for our problem. For this goto**–>edit–>add variable–>input**

Step 3: Double click on “BrakePressure” and click on the red-curve. Rename it from “mf1” to “low” and change the Type from ” trimf to gaussmf” as shown below. Similarly, do it for medium and high as well by clicking over the curves:

Step 4: Now let’s fuzzify the car speed input. This can be trapezoidal and triangular since we have a speedometer that can objectively tell the speed (eg. 55.5 mph). Double click on the rectangle that has CarSpeed variable. We will define the car speed as also having three linguistic values: slow, medium, and fast.We will define the membership functions for these values for a universe of discourse of values from 0 to 100:

** S = {(0, 1), (60, 0)}**

** M = {(20, 0), (50, 1), (80, 0)}**

** F = {(40, 0), (100, 1)}**

If the wheel speed is in fact 55, then this gives us membership values as follows:

** M[slow(55)] = 0.083**

** M[medium(55)]= 0.833**

** M[fast(55)] = 0.25**

**M[] for “membership of”

Step 5 : Set the wheel speed on the same parameters as car speed and based on three linguistic variables: Slow, Medium and Fast.

**Slow**

**medium**

**fast**

Step 6 : Now that we have defined the inputs, let’s define the output- that is break (Apply_break and Release Break). Both are trapmfs:

Source: AI Illuminated, Ben Coppin, p.520

By default, the system uses “Mamdani” model. Here the defuzzification is centroid method

The formula for centroid method is:

How the fuzzy values for the antecedents of the rules are applied to the consequents is being shown below:

**Source: AI Illuminated, Ben Coppin, p.521**

Step 7 : Remember the rules? Yeah… now since we defined the fuzzy variables and their membership attributes, we need to create the rule base by feeding it into the system. For this go to **–> edit –>rules **to get the Rules Editor. You can add-edit-delete rules as shown below. Once done, click ‘close’ to return to the fuzzy logic designer.

Step 8 : You can save it to the hard disk by exporting it as shown below:

You can export it to your workspace as well so that it’s instance is available throughout the session and you can invoke it whenever you want. To do this goto –> **file–>export to the workspace**:

click “OK”

Step 9 : Now your FIS (Fuzzy Inference System) for the automatic Breaking System is ready.

Click on your Fuzzy Logic Designer window- * view–>rules* to see all the rules sets as shown below:

You can drag the red line (**|**) to left (**<–**) or right (**–>**) and see how the crisp output varies for various inputs as below:elow :

Step 10 : Plotting the surface. If you are using sciLab, you need to use a function called “plotsurf* ()*” to plot the surface of the plane created by the FIS you have just created, but MATLAB makes things easier. You just have to click –>

*to view the surface as shown below:*

**view –>surface**You can rotate it by dragging the surface :

You can aslo view the output in terms of just one parameter by varying the selection criteria :

The above plot shows how the output (brake) varies with respect to Car_Speed. You can try it out for other input parameters like Wheel_Speed and Brake_Pressure.

References:

- Artificial Intelligence Illuminated, Ben Coppin, First Edition, Jones and Bartlett.
- Artificial Intelligence – A Guide to Intelligence Systems, Michael Negnevitsky, 3rd Edition
- http://www.mathworks.com

If you have any queries, you can e-mail me at : srinath.krishnamoorthy@villacollege.edu.mv

About me :

I m Srinath Krishnamoorthy. I m an MTech in Computer Science and Engineering from MA College of Engineering, Kothamangalam, and BTech in Information Technology from Government Engineering College, Palakkad. I m teaching Artificial Intelligence for the University of West of England (BSc Computer Science) in Male`, Maldives. My area of interests is AI, Data Analytics, Computational Intelligence and Theory of Computation.