A few days ago someone casually dropped the line online:
„f%$# PID, that’s good for boomers.”
PID regulation has been the daily bread of process automation for decades. Has something really changed, or are the boomers just stuck in the previous century…?
I’ve been doing electronics for a long time, so that jab hit home. I raised my hand and said: „Challenge accepted. Let’s see.”
test setup
I was already working on a PID regulator for a commercial project. Since commercial projects live and die by price, I picked the cheapest possible MCU that could still do the job: STM32F051 (Cortex-M0, 16 kB Flash). The rest of the hardware: DRV8842 H-bridge driver chip.
Hardware setup: DRV8842 driver chip + STM32F051
I wrote a complete C library for the DRV8842 that handles all its features. Because the chosen MCU has no hardware FPU, I implemented the PID using 64-bit integer arithmetic. The regulation sum consists of four terms: P, I, D and Feed Forward based on measured current.
the challenge begins: 16 kB Flash
Before I soldered the final target board, I developed the code on a prototype board with Cortex-M4 (hardware FPU + much more Flash/RAM). When I tried porting to the target Cortex-M0, the nicely polished library (which used some float for $%R_{SENSE}$% current measurement) didn’t fit. I had to squeeze it hard.
But in the end - not a big problem. The code can run on raw, unscaled values.
In 14 kB I managed to pack not only everything I needed, but also simple UART procedures dumping x and Y values needed for model training.
PID tuning
With USART available, I can send data via DMA - zero CPU load. 3 Mbps transfer speed is more than enough to read process parameters updated every 1 ms (no risk of data loss). Tuning in these conditions is actually pleasant. With simple Python scripts I could watch live values of P, I, D and Feed Forward terms.
At one point I even disconnected the MCU code and let Python’s simple_pid library take over the speed regulation - it did quite well (pity it doesn’t have Feed Forward parameter).
data collection for model training
Instead of doing classic Hardware-In-the-Loop (where the model learns to regulate speed by itself in real conditions), I took a shortcut. With simple Python scripts I logged several hundred thousand records of the working PID regulator in different conditions - no load, constant load, increasing load, decreasing load, etc.
This dataset was enough to train a basic ML model and compare its performance head-to-head with the classic PID.
Spoiler: the numbers speak for themselves.