C1247 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 Kp = 2, computed in continuous time: y=1−e−σt(cos⁡ωdt+σ−Kp/τωdsin⁡ωdt), with σ=(1+Kp)/2τ and ωd=Ki/τ−σ2.
  • Raise Ki: 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 Ki = 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?

Name it

The models

PropertyModel
Battery lifeaverage current = ∑Iktk/∑tk; life = capacity ÷ average
Loop responseyn+1=ayn+bun with a=e−Ts/τ, b=1−a, under PI control with an output limit
Flash weara log moving through sectors, erasing each only when it's reused
Watchdoga 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 yn+1=ayn+bun (a=e−Ts/τ, b=1−a, y starting at 0) under PI control: e=1−y, u=Kpe+I, limited to umax if given; then I+=KiTse, except when the output is limited and e>0. 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?