Discretizing control for firmware
Translate continuous control ideas into timed, bounded firmware updates without losing units or stability margin.
- Implement discrete integration and differentiation with explicit sample interval and state.
- Diagnose jitter, missed deadlines, quantization, and saturation in a sampled control loop.
Related study — optional context
Understand
The working idea
A microcontroller acts at discrete instants. It reads sensors, updates controller state, and writes an actuator command. Continuous equations are useful designs, but firmware must choose a sample period, numerical approximation, state initialization, and behavior when execution is late. Those choices can change the closed loop.
Forward Euler integration uses x_{k+1} = x_k + Δt f(x_k, u_k). A discrete integral uses I_k = I_{k-1} + e_kΔt; a backward difference estimates derivative as (e_k - e_{k-1})/Δt. Omitting Δt makes gains depend accidentally on loop frequency. For the first-order system dx/dt = -x/τ, forward Euler becomes x_{k+1} = (1 - Δt/τ)x_k. The approximation decays without sign flipping when 0 < Δt/τ < 1; for larger steps it can oscillate numerically, and at Δt/τ ≥ 2 it is unstable even though the continuous system is stable.
Sampling should be comfortably faster than the dynamics the loop must control, but “as fast as possible” is not a design. ADC settling, sensor buses, computation, PWM update timing, and logging consume time. Use a monotonic timer and measured Δt when appropriate; separate slow communication or screen updates from a time-critical loop. The build decision may be to keep control at 100 Hz while updating a display at 10 Hz, preventing rendering delays from becoming motor-command jitter.
A common misconception is that a nominal delay call guarantees a fixed period. Work performed in the loop adds time, interrupts add jitter, and blocking I/O can miss deadlines. Another failure is changing sample rate without retuning or rescaling discrete gains. Quantization can create limit cycles near a setpoint, while saturation can wind up hidden state. Log timestamps, execution duration, missed-deadline count, raw input, state, and bounded output. The GPIO state-machine simulation, if used, is a bounded behavioral timing model, not MCU emulation.
Follow the reasoning
Worked example
Use forward Euler for dx/dt = -x/0.10 s from x₀ = 1.0. Compare one update with Δt = 0.02 s and Δt = 0.15 s.
- 1
For 0.02 s, multiplier is 1 - 0.02/0.10 = 0.8, so x₁ = 0.8.
- 2
For 0.15 s, multiplier is 1 - 0.15/0.10 = -0.5, so x₁ = -0.5.
- 3
The exact continuous value after 0.02 s is e⁻⁰·² = 0.819; after 0.15 s it is e⁻¹·⁵ = 0.223, always positive.
The 20 ms update is a reasonable first approximation; the 150 ms update creates an artificial sign reversal despite eventual numerical decay.
Sanity checkA decaying continuous state cannot cross zero in this model, so the negative discrete result exposes an overly coarse approximation.
Try it yourself
Independent check
Move to the bench
Measure loop timing and show how blocking work alters a discrete controller.
- Microcontroller
- Low-voltage LED or safely limited motor test rig
- Timer-capable firmware
- Serial logger
- Oscilloscope or logic analyzer optional
Experiment
- Implement a periodic state update using a monotonic timestamp and log actual Δt and execution duration.
- Run the loop with a bounded simulated first-order state or low-energy plant and compare 10 ms and 50 ms sample periods.
- Add a slow display/serial task on a separate schedule and confirm control timing remains within its deadline.
- Deliberate fault: insert a known blocking operation into the control path; diagnose missed deadlines and changed response from timestamps, then remove or reschedule it.
Observe
- Expected: coarser or jittery updates increase approximation and control error; separated slow tasks reduce interference.
- Measure: actual period distribution, deadline misses, state, and command; nominal configured period is not a measurement.
Specify control, sensor, logging, and screen update rates for a desk robot and justify which tasks may skip an update.
- Begin with a simulated state or LED; if using a motor, keep voltage, current, speed, and travel tightly limited.
Keep evidence
Bench note
Record what you expected, what you actually measured or observed, and one difference worth investigating. This is self-reported evidence, not a certification.