Metatransformer report
Discretizing control for firmware
Advanced · 70 minutes · Translate continuous control ideas into timed, bounded firmware updates without losing units or stability margin.
Text report: images and diagrams are omitted, and links to project files are inactive. Keep the exported project alongside this PDF for wiring diagrams, models, and linked files.
Learning outcomes
- Implement discrete integration and differentiation with explicit sample interval and state.
- Diagnose jitter, missed deadlines, quantization, and saturation in a sampled control loop.
Prerequisites
- feedback-pid
- rates-integration
- timers-pwm
Concepts
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.
Worked example
Problem
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.
Steps
- For 0.02 s, multiplier is 1 - 0.02/0.10 = 0.8, so x₁ = 0.8.
- For 0.15 s, multiplier is 1 - 0.15/0.10 = -0.5, so x₁ = -0.5.
- The exact continuous value after 0.02 s is e⁻⁰·² = 0.819; after 0.15 s it is e⁻¹·⁵ = 0.223, always positive.
Result
The 20 ms update is a reasonable first approximation; the 150 ms update creates an artificial sign reversal despite eventual numerical decay.
Sanity check
A decaying continuous state cannot cross zero in this model, so the negative discrete result exposes an overly coarse approximation.
Check your understanding
Firmware doubles its control-loop rate, but its integral update adds Ki×error each iteration with no Δt. What happens to integral action per real second?
A. It approximately doubles B. It halves C. It stays identical D. It becomes derivative action
Your answer: ______________________________
Reasoning:
Answer key
ANSWER KEY — check only after attempting the question.
A. It approximately doubles
Twice as many unscaled additions occur each second, so effective integral strength roughly doubles. Including Δt preserves units and rate independence. It neither halves nor changes into a derivative.
Hands-on lab
Goal
Measure loop timing and show how blocking work alters a discrete controller.
Equipment
- Microcontroller
- Low-voltage LED or safely limited motor test rig
- Timer-capable firmware
- Serial logger
- Oscilloscope or logic analyzer optional
Procedure
- 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.
What to 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.
Transfer challenge
Specify control, sensor, logging, and screen update rates for a desk robot and justify which tasks may skip an update.
Cautions
- Begin with a simulated state or LED; if using a motor, keep voltage, current, speed, and travel tightly limited.
Evidence record
Learner: ____________________ Date: ____________________
Evidence artifact or file: ____________________________________________
| Item | Record |
|---|---|
| Prediction or expected result | |
| Actual observation or measurement | |
| Conditions and tools used | |
| Difference from expectation | |
| Next decision or follow-up |
Notes: