C12 · Lesson 47 · Embedded lab
Battery budgets, control loops, flash wear and watchdogs, in Python and C
The last part of the course is about devices that keep working: for a year on a coin cell, through a power cut, with a loop that holds its set point and a watchdog that catches a hang. Each of those properties is a number you can compute before building anything: an average current, an overshoot, an erase count, a reset time. This lab writes the models in Python, and then runs the integer PI controller from the control lesson on the board to check that it behaves exactly like the model.
- The curve is a PI loop's step response on the control lesson's plant (τ = 20 ms, gain 1) with = 2, computed in continuous time: , with and .
- Raise : the loop settles on the set point faster at first but overshoots more.
- The lab simulates the same loop sampled every millisecond. Its overshoot is larger (9.6% against 7.9% here at = 200, 24.5% against 19.6% at 400): the sample delay costs phase, just as the control lesson predicted.
Predict
A sensor node draws 2 µA asleep and wakes for 50 ms every 10 s at 2.05 mA. How long does a 220 mAh coin cell last?
The average is mA, so hours, about 749 days. The waking adds 84% of the average; shortening it matters more than lowering the sleep current further. (Real cells also self-discharge, about 1% a year for lithium coin cells, and lose capacity in the cold.)
Name it
The models
| Property | Model |
|---|---|
| Battery life | average current = ; life = capacity ÷ average |
| Loop response | with , , under PI control with an output limit |
| Flash wear | a log moving through sectors, erasing each only when it's reused |
| Watchdog | a reset whenever the gap since the last kick exceeds the timeout |
Each takes a few lines, and each answers a question that would take days or months to answer by watching hardware.
Check
Write battery_days(states, capacity_mah): states is a list of (current in mA, seconds) making up one repeating cycle. Return (average current in mA, life in days).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write simulate_pi(kp, ki, tau, ts, n, umax=None): a unit step into the plant (, , y starting at 0) under PI control: , , limited to umax if given; then , except when the output is limited and . Return the n outputs. Then overshoot(ys) in percent (0 if none) and settle_index(ys, tol=0.02): the first index from which every output is within tol of 1, or None.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write log_wear(saves, sectors, per_sector): records are appended to a log that starts in sector 0, all sectors erased. When the current sector is full, move to the next (wrapping to 0), erasing it first if it holds old records. Return each sector's erase count after saves records.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write wdt_reset(kicks, timeout): a watchdog starts at time 0 and resets the device if timeout seconds pass without a kick. kicks are the times of kicks. Return the time of the first reset before the last kick, or None if every gap is short enough.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Run the integer PI controller on the board: kp_q8 = 192, ki_q16 = 131, output limits 0 and 1000, with the accumulator updated by ki_q16 * e only when that doesn't push a limited output further into its limit. Feed it 20 errors of 800, then 20 of 100, then 20 of −50, and print the outputs for the 20th, 40th and 60th.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Explain why
Why does a PI loop with an output limit and conditional integration overshoot less than the same loop with no limit at all?
Without a limit, the controller's large initial output drives the plant hard, and the integrator keeps accumulating the error during the rise, so by the time the output reaches the set point the integrator holds far more than it needs and pushes the output past it. With a limit, the output is held at the maximum during the rise, and conditional integration stops the integrator from growing while the output is limited and the error still pushes the same way. The integrator only starts to act once the output comes off the limit, close to the set point, so it holds much less excess and the overshoot is smaller, at the cost of a slightly slower rise.