Temperature Dance: PID Controllers for Perfect Fermentation
Your sourdough starter thrives at exactly 26°C. Too cold, and fermentation crawls; too hot, and the wild yeasts die. You’ve rigged a heating pad with a temperature probe, but manual adjustments are maddening—overshooting, undershooting, never quite right. What you need is what industrial bakers discovered decades ago: a PID controller.
PID stands for Proportional, Integral, Derivative—three mathematical terms for how humans naturally adjust things. When the temperature drops 2°C below target, you don’t just turn the heat up a little (proportional). You remember how long it’s been cold (integral) and notice it’s dropping fast (derivative). A PID controller codifies this intuition.
Object subclass: #PIDController
instanceVariableNames: 'kp ki kd setpoint lastError integral'
PIDController>>update: currentTemp
| error derivative output |
error := setpoint - currentTemp.
integral := integral + error.
derivative := error - lastError.
output := (kp * error) + (ki * integral) + (kd * derivative).
lastError := error.
^ output
Smalltalk’s object-oriented purity makes the control loop feel like a living thing—the controller maintains its own memory of past errors, learning from each measurement. In the 1980s, this was revolutionary: control systems as objects, not just equations.
struct PIDController {
kp: f64, ki: f64, kd: f64,
setpoint: f64,
last_error: f64,
integral: f64,
}
impl PIDController {
fn update(&mut self, current_temp: f64) -> f64 {
let error = self.setpoint - current_temp;
self.integral += error;
let derivative = error - self.last_error;
self.last_error = error;
self.kp * error + self.ki * self.integral + self.kd * derivative
}
}
Rust’s version emphasizes safety—no forgotten initialization, no memory leaks in long-running fermentation cycles. The mutable reference &mut self ensures only one thread can adjust the controller at once, crucial when multiple sensors might trigger updates.
Tuning the three constants (kp, ki, kd) is an art. Too aggressive, and your bread oven oscillates wildly. Too gentle, and temperature drifts for hours. The best bakers, like the best control engineers, develop an intuition for these numbers—knowing that perfect fermentation, like perfect control, emerges from the delicate balance between reaction and patience.