Introduction to control#
Control makes a measurement follow a command. This page compares three ways to hold a block at 50 °C with a heater, from no feedback to proportional feedback.
Download this page as a notebook to run its code in JupyterLab. Running the notebooks explains how to set it up.
Open loop and closed loop#
An open-loop system sends a command and doesn’t check the result. A closed loop measures the process variable, the quantity it controls, compares it with the setpoint, and adjusts its output, over and over at a fixed rate.[1] The difference between setpoint and measurement is the error. The three controllers below differ only in how they turn that error into heater power:
Open loop. Sets a fixed power, the one that should hold 50 °C, and never looks at the temperature.
On/off. Sets full power when the block is below the setpoint, and none when it’s above.
Proportional. Sets a power proportional to the error, with a gain of 10 % per °C, limited to 0 to 100 %.
A simulated heater#
The block warms with the heater’s power and cools toward the room’s temperature, with a time constant of 8 s. Halfway through, at 20 s, the room cools from 20 °C to 10 °C. This is a made-up process for the simulation, not a measured one:
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from learning.style import set_matplotlib_style
set_matplotlib_style()
dt = 0.05 # s per control cycle
t = np.arange(0, 40, dt)
setpoint = 50.0 # °C
def simulate(controller):
"""Run the heater with a controller, and return the temperature and power of each cycle."""
temperature = 20.0
temps, powers = [], []
for time in t:
room = 20.0 if time < 20 else 10.0 # °C: the disturbance
power = float(np.clip(controller(setpoint, temperature), 0, 100)) # %
temperature += dt * (-(temperature - room) + 0.8 * power) / 8.0
temps.append(temperature)
powers.append(power)
return np.array(temps), np.array(powers)
controllers = {
"Open loop": lambda sp, measured: (
37.5
), # the power that holds 50 °C in a 20 °C room
"On/off": lambda sp, measured: 100 if measured < sp else 0,
"Proportional": lambda sp, measured: 10 * (sp - measured),
}
results = {name: simulate(c) for name, c in controllers.items()}
fig, ax = plt.subplots()
ax.axhline(setpoint, color="0.5", linestyle="--", label="Setpoint")
ax.axvline(20, color="0.7", linestyle=":")
for name, (temps, _) in results.items():
ax.plot(t, temps, label=name)
ax.set_xlabel("Time (s)")
ax.set_ylabel("Block temperature (°C)")
ax.legend()
plt.show()
pd.DataFrame(
{
name: {
"temperature at 40 s (°C)": temps[-1],
"error at 40 s (°C)": setpoint - temps[-1],
"switches between 0 and 100 % in the last 5 s": int(
(np.abs(np.diff(powers[-int(5 / dt) :])) == 100).sum()
),
}
for name, (temps, powers) in results.items()
}
).round(1)
| Open loop | On/off | Proportional | |
|---|---|---|---|
| temperature at 40 s (°C) | 40.6 | 49.9 | 45.6 |
| error at 40 s (°C) | 9.4 | 0.1 | 4.4 |
| switches between 0 and 100 % in the last 5 s | 0.0 | 99.0 | 0.0 |
Each controller shows a different trade-off:
Open loop is correct only while the room stays at the temperature it assumed. When the room cools, the block drifts away from 50 °C, and nothing corrects it.
On/off holds the setpoint, but its output switches on and off over and over, as the table’s last row shows. A deadband, a range of error in which the output doesn’t change, reduces the switching.
Proportional feedback follows the setpoint without chattering, but it settles short of it. A proportional controller’s output depends on the error, so it needs an error to produce any power. Raising the gain shrinks that offset, but too much gain makes the process variable oscillate.[1]
The integral term removes the offset.[1] PID control adds it, and the derivative term, to the same heater.
In Nominal#
To review a control loop, record its setpoint and process variable on the same time axis, and plot them together in a workbook. This code writes both, and the controller’s output, to a dataset as the loop runs:
from datetime import datetime, timezone
from nominal.core import NominalClient
client = NominalClient.from_profile("default")
dataset = client.get_dataset("<dataset RID>")
with dataset.get_write_stream() as stream:
for _ in range(100): # your control loop
now = datetime.now(timezone.utc)
stream.enqueue("heater.setpoint", now, setpoint)
stream.enqueue("heater.temperature", now, read_temperature())
stream.enqueue("heater.power", now, power)
get_write_stream queues each value and sends it in batches.[2] A slow network can then slow the program that writes the data, so keep the writes out of a loop that has to keep time. Network performance explains why, and what to do instead.
Further reading#
Control, on PID tuning, step responses and loop rates, with a dynamometer as the worked example.
K. J. Åström and R. M. Murray, Feedback Systems: An Introduction for Scientists and Engineers, a free textbook.