Chapter 12

Back to the Shower

Why did we oscillate, and how close to the edge are we?

Chapter 12: Back to the Shower ↗

The shower, with new eyes

June: I put our drone PID on the shower (same snappy feel, scaled to knob units). It swung hot, cold, hot… just like Mika in Chapter 0. The drone never did that!

Theo: The drone has no big delay. The shower has 2.5 seconds of pipe. Let's finally work out what a delay does.

The shower has two slow parts: the pipe, a pure 2.5 s delay, and the smoother, the head and pipe walls warming up with Chapter 3's time constant, τ=1\tau = 1 s (after 1 s it has closed 63 % of the gap to a new temperature).

First, a confession about Chapter 0's "normal hand". It turned the knob at a speed proportional to how wrong the water felt: knob speed =k⋅e= k\cdot\err{e}, with k=0.8 %k = 0.8\,\% of the knob per second per °C. A speed proportional to the error means the knob position is the pile of past error. That's an I controller, with Ki=kK_i = k.

Mika: Wait, so in Chapter 0 I was a PID controller? Well, an I controller. An over-eager one.

Delay is a lag in the wiggle

Theo: I still think a delay just makes everything slower. It shouldn't be able to make anything unstable.

Let's isolate the pipe's pure delay first. Imagine a sine wave at the pipe's input travelling to the showerhead with no smoothing: the output has the same shape, 2.5 s late. How late is that, compared with one wiggle?

Chapter 12: Back to the Shower ↗

Theo: Wait. At a 5-second wiggle the hot water arrives exactly when the knob asks for cold. That's not slower. That's backwards.

The pipe delay stays at 2.5 s, but its phase lag depends on how fast the wave wiggles. Call the lag ϕ\phi and the delay LL. If one wiggle takes TT seconds (here TT is a time, not a temperature), its angular speed is ω=2π/T\omega=2\pi/T radians per second. A positive lag means the output trails the input. Engineers write a lag as a negative angle, so the plots below go downwards.

One whole wiggle is one full turn of Chapter 5's spinner: 360°, or 2π radians. So what matters is what fraction of a wiggle the delay covers: L/TL/T.

ϕ=LT⏟share of a wiggle⋅360∘=ωL radians,ω=2πT\phi = \underbrace{\frac{L}{T}}_{\text{share of a wiggle}}\cdot 360^\circ = \omega L \text{ radians},\qquad \omega = \frac{2\pi}{T}

Chapter 12: Back to the Shower ↗

For a 5-second wiggle, 2.5 s is half a wiggle: 180°. In this pure-delay model, each output crest lines up with an input trough. The shower's smoother also shrinks fast wiggles; we'll add that next.

Mika: So the same 2.5 s is a nudge for a lazy wiggle and a full flip for a quick one. The pipe never changes. The wiggle does!

Measuring the shower one wiggle at a time

Now add the smoother to the pipe delay. The head and the pipe walls take time to warm up, so the full shower both delays and smooths knob changes. Here's the plan: wiggle the knob at one speed, wait for a steady rhythm, and measure two numbers:

  • Gain (the same word as the controller's gain: size out ÷ size in): how big the temperature wiggle is, compared with the wiggle in the water mix we asked for.
  • Phase: how far behind it is, as an angle: its share of a whole wiggle, times 360°.

Each measurement drops a dot on two plots. Measure at least five speeds, slow and fast.

Chapter 12: Back to the Shower ↗

The smoother shrinks fast wiggles, while the pipe keeps adding phase lag. These gain and phase curves against wiggle speed form a Bode plot. Together they are the shower's frequency response: how it treats each wiggle speed. Here the input is the temperature of the mixed water, so slow wiggles have gain 1. If we used knob fraction as input, the gain would be 45 times larger. To get the frequency response from the formula, set s=iωs = i\omega: the same trick that found Chapter 11's edge. (It works because the shower on its own is stable, so its wiggles settle into a steady rhythm we can measure.)

G(iω)=e−iωL1+iωτ,∣G∣=11+ω2τ2,∠G=−arctan⁡(ωτ)−ωLG(i\omega) = \frac{e^{-i\omega L}}{1 + i\omega\tau}, \qquad |G| = \frac{1}{\sqrt{1+\omega^2\tau^2}}, \qquad \angle G = -\arctan(\omega\tau) - \omega L

Chapter 12: Back to the Shower ↗

June: Found it! Near 0.95 rad/s the shower on its own turns a wiggle upside down. Remember that speed.

How close to the cliff?

Now close the loop: hand → knob → pipe → skin → hand. Follow a wiggle all the way around. For the Chapter 0 hand, the trip around the loop is G∘G_{\circ} (a little circle for loop, because LL is already the delay):

G∘(s)=ks⏟hand⋅45 e−2.5ss+1⏟showerG_{\circ}(s) = \underbrace{\frac{k}{s}}_{\text{hand}}\cdot\underbrace{\frac{45\,e^{-2.5s}}{s+1}}_{\text{shower}}

Feedback subtracts (error = target − temperature). That's a ×(−1), Chapter 5's half-turn, so the wave flips once. If the trip around the loop also delays it by exactly 180°, the two flips cancel, and every correction arrives perfectly in step with the swing, like pushing a playground swing at just the right moment. If the loop returns the wave at full size (gain 1), the swing keeps itself going; bigger than 1, it grows. That's Chapter 10's 1+C P=01 + C\,P = 0 again, on the line s=iωs = i\omega: a lap that comes back flipped and full size is G∘(iω)=−1G_{\circ}(i\omega) = -1.

June: For this simple shower loop, the phase falls through −180° at one relevant wiggle speed. Find that speed on the phase plot, then read the loop gain at the same speed on the gain plot. Is it below or above 1?

Chapter 12: Back to the Shower ↗

Slide until the total passes 180°, between 0.45 and 0.46 rad/s. At the exact crossing, about 0.457 rad/s, the swing comes back 0.72 times as big, so the hand could be about 1 ÷ 0.72 ≈ 1.4 times stronger before reaching the edge.

New words

Gain margin
How many times you could multiply the controller's gain before the loop gain reaches 1 at the −180° speed. Below 1 = over the cliff.
Phase margin
At the speed where the loop gain is exactly 1, how much extra lag it would take to reach −180°. More is safer; 45°–60° is a common target (the side trip below says why).

For our shower, the phase of G∘G_{\circ} hits −180° at ω≈0.457\omega \approx 0.457 rad/s, a wiggle period of about 13.8 s. The normal hand (k=0.008k = 0.008) has a gain margin of only about 1.4, and a phase margin of about 22° (where its loop gain is 1, at 0.34 rad/s, the phase is −158°). Close to the edge! The harder hand doubles the gain: 1.4 ÷ 2 = 0.7, over the cliff. That's exactly why it never settled.

Predict first! Suppose the pipe were twice as long (a 5 s delay). What happens to the largest hand gain that's still stable?

Chapter 12: Back to the Shower ↗

Chapter 12: Back to the Shower ↗

Mika: Twice the pipe, about half the hand. And the swings at the edge get slower too: almost 24 seconds!

Theo: Okay, I take it back. A delay doesn't just make things slower: it eats phase margin, and phase margin is the only thing between you and an oscillation.

Your Chapter 0 shower, explained

Here's your own run from Chapter 0. If your swings were near the edge, their period should be close to what the loop predicts. There's a twist: people are not all the same kind of controller.

Chapter 12: Back to the Shower ↗

A speed hand (turn the knob while it feels wrong) is an I controller: its edge is at 13.8 s swings. A position hand ("it's too hot, put the knob here") acts like a P controller: G∘(s)=45K e−2.5s/(s+1)G_{\circ}(s) = 45K\,e^{-2.5s}/(s+1). A position hand has no pile, so no 90° of its own: the pipe and smoother alone must reach 180°, which happens at a faster wiggle, ω≈0.95\omega \approx 0.95 rad/s (June's upside-down speed). That means 6.6 s swings, for any KK above about 0.031 knob per °C. Most people are a mix. (The normal robot hand swings a bit slower than 13.8 s, about 15.5 s, because it sits just inside the edge; the harder hand's swings, about 11.6 s, are squashed by the knob hitting its ends.)

Chapter 12: Back to the Shower ↗

Mika: So when I jerked the knob straight to a spot, I was a position hand. My six-second swings were the pipe and the smoother, not clumsy fingers.

Design a robot shower

Now let’s beat our own hands. The robot uses PI: P reacts to the current temperature error, and I remembers accumulated error. Together they let the knob respond promptly while I finds the right lasting setting.

June: Easy. I'll use big gains, like on the drone: fast and snappy. They're already dialled in. Watch this!

Chapter 12: Back to the Shower ↗

June: …it hunts, hot–cold–hot, every 6 seconds, like a position hand in Chapter 0. Big gains pushed the gain-of-1 speed past the −180° speed. No phase margin at all.

Theo: The drone could take big gains because it had almost no lag. Here, robust means gentle: leave margin for the delay. Try my gentle gains, about Kp≈1K_p \approx 1 and Ki≈0.6K_i \approx 0.6.

P reacts to now, I to the pile. Adding a little now moves the knob a little earlier, which gives back phase: phase lead, about 37° at the old −180° speed with Theo's gains.

A well-margined PI gets comfortable in about 9.4 seconds, without repeated large swings. That beats the normal hand (which spent most of a minute swinging before it calmed down) and most humans. It can't beat physics, though: nothing can react to water that hasn't arrived yet. Engineers do have a trick for long delays, a Smith predictor, which runs a little simulation of the pipe inside the controller. That's a story for another notebook.

Robustness is the real lesson. Your model of the shower is never perfect: the pipe might be longer, the heater weaker. A margin says how wrong the model may be, as a number you can feel. The phase margin (in radians) divided by the gain-of-1 speed is a delay margin: how many seconds of extra pipe the loop can take.

Chapter 12: Back to the Shower ↗

Theo: The normal hand survives only about 1.1 s of extra pipe. My gentle robot survives 3.5 s. That's what robust means.

What we understand now

What we figured out

  • A delay LL lags a wiggle by ωL\omega L: nothing for slow wiggles, a lot for fast ones.
  • A pile (I) lags every wiggle by 90°; a smoother by up to 90°; a pipe by ωL\omega L, without limit.
  • Feeding in sine waves and measuring gain and phase gives the frequency response; plotted, it's a Bode plot, and it's just G(iω)G(i\omega).
  • For this simple shower loop, self-sustained oscillation begins when the loop reaches −180° phase with gain 1; larger gain makes the swings grow until limits intervene.
  • Gain and phase margin measure the distance to that cliff. Longer delay means less margin, so you need gentler control.
  • Our Chapter 0 swings were a controller (us) with too little margin.

We have every tool now: feedback, PID, poles, margins. Time for the real test: wind, a package drop, and a noisy sensor. Can you keep the drone at 2 m?

Chapter 12: Back to the Shower ↗