Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Controllers

The Architecture of a Fuzzy Controller

See how a fuzzy logic controller works end to end: inputs, fuzzification, rule base, inference engine and defuzzification, with a runnable Mamdani heater example in Python.

A fuzzy logic controller (FLC) is a program that turns sensor readings into an actuator command using human-style rules such as "if the room is much colder than the target, heat strongly". It does not need a mathematical model of the room. It needs a few sensible rules and a few overlapping labels like cold, OK and hot.

This chapter puts fuzzy sets and rules to work in a closed loop. In this lesson you will meet the four blocks every controller has, and build one that runs in your browser.

The four blocks

A classic fuzzy controller is a pipeline with one feedback path around it:

flowchart LR
  S["Sensor<br/>(crisp number)"] --> F["Fuzzification"]
  F --> I["Inference engine<br/>+ rule base"]
  I --> D["Defuzzification"]
  D --> A["Actuator<br/>(crisp number)"]
  A --> P["Plant<br/>(room, motor, tank)"]
  P --> S

A controller is a pure function from crisp inputs to a crisp output, wrapped in a loop through the plant.

  1. Fuzzification converts a crisp input (for example an error of 3.2 degrees) into degrees of membership in each linguistic term (Negative, Zero, Positive).
  2. Rule base holds the expert knowledge as IF-THEN rules.
  3. Inference engine evaluates every rule: it combines the antecedent memberships with AND/OR and clips or scales each rule's output set.
  4. Defuzzification turns the combined output fuzzy set back into one crisp number the actuator can use.

Inside a fuzzy controller

Fuzzify Number to memberships
Rules IF error is big THEN push
Infer Fire rules, clip outputs
Defuzzify Fuzzy set to one number

Same four steps on every cycle

1 / 2

Choosing inputs and outputs

Most controllers use the error e = setpoint - measurement and often its change de (how fast the error is moving). One input tells the controller where you are relative to the goal; two inputs add a sense of direction and speed. The output is usually a command (heater power, valve opening) or a change in command.

Each variable gets a universe of discourse (its range) and three to seven labels. Three to five labels per variable is the sweet spot: fewer labels cannot express enough behavior, more labels explode the number of rules (5 x 5 = 25 rules for two inputs).

A first controller in Python

The example below controls heater power from the temperature error. The inputs and output use trapezoidal sets (a triangle is a trapezoid whose plateau has width zero). We evaluate the rules with min for clipping and max to combine output sets (the Mamdani method), then take the centroid.

Python Heater power from temperature error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the output climbs smoothly as the error grows. There is no jump at the boundary between zero and positive, because those two sets overlap and both rules fire a little.

Seeing which rules fire

Debugging a fuzzy controller means looking at the firing strengths: which rules were active and by how much. If you ask for an error of 4 and the output feels wrong, print the memberships before you touch any shape.

Python Firing strengths for an error of 4

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

At e = 4 the zero set is still 60% active while pos is 40% active. The final heater power is a blend of medium and high, weighted by those strengths.

Mamdani versus Sugeno controllers

Mamdani Sugeno (Takagi-Sugeno)
Rule output A fuzzy set A number or a linear function of the inputs
Defuzzification Centroid of the aggregate Weighted average of rule outputs
Speed Slower (works over a curve) Fast, easy to compute on small chips
Readability Very natural Better for tuning and math analysis

Both give a smooth output. Use Mamdani when the rules should read like English and Sugeno when you need speed or want to fit rules from data.

Common mistakes

  • Gaps in coverage. If some input value has zero membership in every set, no rule fires and the output is undefined. Make neighboring sets overlap so that memberships add to about 1.
  • Too many labels. Seven labels on two inputs means 49 rules to write and test.
  • Forgetting the plant. A controller that looks right in a table can still oscillate in a loop. The next lesson, simulating a fuzzy thermostat, closes the loop.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which block converts a crisp sensor value into degrees of membership?

    Fuzzification maps a crisp input onto each linguistic term and returns a membership degree per term.
  2. 2. In a Mamdani controller, what does the inference engine do with a rule's output set?

    Each output set is clipped by how strongly its rule fired, and the clipped sets are combined (usually with max).
  3. 3. Why should neighboring input sets overlap?

    Overlap gives smooth blending between rules and prevents input values that activate nothing.
  4. 4. Which is a typical pair of controller inputs?

    Error says where you are relative to the goal and change of error says how quickly that is changing.

Summary

  • A fuzzy controller = fuzzification, rule base, inference engine, defuzzification, wrapped in a feedback loop.
  • Inputs are usually error and change of error; three to five labels per variable keep the rule base small.
  • Mamdani rules output fuzzy sets and use a centroid; Sugeno rules output numbers and use a weighted average.
  • Debug by printing memberships and firing strengths.

Next: Simulating a Fuzzy Thermostat