Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Controllers

Simulating a Fuzzy Thermostat in a Closed Loop

Close the loop on a fuzzy controller: simulate a heated room, run a two-input fuzzy rule base every step, and watch the steady-state error disappear with an incremental controller.

A controller table is not a controller until it is connected to something that reacts. In this lesson you will build a tiny plant (a room that heats up and leaks heat), connect a fuzzy controller to it and watch the temperature move toward a target. You will also meet the most common surprise in control: a controller that "works" but never quite reaches the setpoint.

Everything runs in the browser with numpy. The plant is a made-up model chosen so that numbers are easy to read; the ideas are the same for a real thermostat.

The plant: a leaky room

We use a first-order model. Each step (one minute) the temperature T changes because the heater adds heat in proportion to power P (0 to 100 percent) and the room loses heat toward the outside temperature of 10 degrees:

TEXT
T_next = T + 0.1 * (0.5 * P - (T - 10))

If the heater stays at power P, the room settles where 0.5 * P = T - 10. To hold 22 degrees the heater must supply P = 24. Keep this number in mind.

flowchart LR
  SP["Setpoint 22"] --> E["error e<br/>and change de"]
  E --> C["Fuzzy controller"]
  C -->|"power P"| R["Room model"]
  R -->|"temperature T"| E

The loop runs once per simulated minute.

Step 1: a fuzzy P controller with absolute output

First try the simplest design: two input terms from the error (cold, OK, warm) and a power output (low, medium, high) with Mamdani inference. The output is the absolute power.

Python Absolute-power fuzzy controller in the loop

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.

The room warms quickly, but it never stops at 22: it levels off near 28.2 degrees. When the room is exactly on target the error is zero, the rules ask for medium power (50 percent), and that is far more than the 24 percent needed to hold 22. The loop settles where the controller's output (a smooth function of the error) equals the heat the room loses, which needs a steady-state error of about -6 degrees. Any controller whose output depends only on the current error has this problem, fuzzy or not. Moving the sets or the output values would shift the offset but could not remove it.

Step 2: an incremental controller with two inputs

The fix is the same trick PI controllers use: let the controller decide how much to change the power, and add that change to the current power. The power then keeps adjusting until the error is truly zero.

We also use two inputs, the error e and the change in error de (current error minus the previous one), and a Sugeno output: each rule gives a number (how much to change the power per minute), and the result is the weighted average.

e \ de de negative de zero de positive
e positive (cold) 0 +3 +6
e zero -2 0 +2
e negative (warm) -6 -3 0

Read the top-right cell as: "if the room is colder than the setpoint and getting colder (the error is growing), raise the power by 6". Read the top-left as: "if it is cold but already warming fast, do not add power".

Python Two-input incremental fuzzy controller (Sugeno)

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.

The temperature overshoots slightly (about 22.3), then settles at 22.000 with the power at 24.0 percent, exactly the value the room needs. The incremental controller has an integrating effect: it keeps nudging the power as long as any error is left.

Reading the result

  • If the temperature overshoots 22 before settling, the rules push too hard when the room is far from the target: reduce the large output numbers (6 and -6).
  • If the controller is slow, the output numbers are too small, or the zero error set is too wide so the controller gives up early.
  • If the power flickers each minute, the de sets are too narrow compared with the sensor noise.

Disturbance test

A controller must also handle surprises. Open a window at minute 60 by dropping the outside temperature from 10 to 0 degrees and see how the loop recovers.

Python Opening a window at minute 60

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.

After the window opens the heater needs about 44 percent to hold 22 degrees (the room now loses more heat). The controller finds this by itself because it never relied on knowing the exact number; it only reacts to the error.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does an absolute-output fuzzy controller on this room end up with a steady-state error?

    The output depends only on the current error, so the room only settles where the controller's output equals the heat the room loses, which requires some error.
  2. 2. What does the incremental controller output?

    Adding changes to the current power keeps adjusting until the error is zero, which removes the steady-state error.
  3. 3. In the rule table, what should happen when the room is cold but the error is already falling quickly?

    A negative change of error means the room is approaching the target; adding more power would cause overshoot.
  4. 4. Which Sugeno step combines the rules into a single number?

    Sugeno defuzzification divides the sum of w times z by the sum of w.

Summary

  • Test a controller in a loop with a plant model; a static table is not enough.
  • A controller that outputs absolute power from the error alone has a steady-state error.
  • An incremental (integrating) fuzzy controller removes it by outputting changes in power.
  • Two inputs (error and change of error) let the rules anticipate overshoot.
  • Always try a disturbance, such as a window opening, before trusting a design.

Next: Fuzzy Control Compared with PID