MTBuilder AcademyLearn it. Test it. Build it.
Feedback & motionLesson 40 of 48
Advanced70 minutes3 project connections

Discretizing control for firmware

Translate continuous control ideas into timed, bounded firmware updates without losing units or stability margin.

After this lesson, you can
  • Implement discrete integration and differentiation with explicit sample interval and state.
  • Diagnose jitter, missed deadlines, quantization, and saturation in a sampled control loop.
ReadPracticedBench demonstrated
Related study — optional context
01

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.

02

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. 1

    For 0.02 s, multiplier is 1 - 0.02/0.10 = 0.8, so x₁ = 0.8.

  2. 2

    For 0.15 s, multiplier is 1 - 0.15/0.10 = -0.5, so x₁ = -0.5.

  3. 3

    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 checkA decaying continuous state cannot cross zero in this model, so the negative discrete result exposes an overly coarse approximation.

03

Try it yourself

Independent check

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?
04

Move to the bench

Measure loop timing and show how blocking work alters a discrete controller.

Gather
  • Microcontroller
  • Low-voltage LED or safely limited motor test rig
  • Timer-capable firmware
  • Serial logger
  • Oscilloscope or logic analyzer optional

Experiment

  1. Implement a periodic state update using a monotonic timestamp and log actual Δt and execution duration.
  2. Run the loop with a bounded simulated first-order state or low-energy plant and compare 10 ms and 50 ms sample periods.
  3. Add a slow display/serial task on a separate schedule and confirm control timing remains within its deadline.
  4. 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.
Transfer it

Specify control, sensor, logging, and screen update rates for a desk robot and justify which tasks may skip an update.

Limits and cautions
  • Begin with a simulated state or LED; if using a motor, keep voltage, current, speed, and travel tightly limited.
05

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.

0 / 5,000A bench demonstration needs a concrete note of at least 20 characters.
Terms in this lessonBrowse the advanced glossary

Sources and further reading

  1. Control Tutorials for MATLAB and Simulink
  2. Arduino: Blink Without Delay