Subsection 14.3.3 The Tracker’s Update Rule
In Lab 8’s notation, with \(V_0\) and \(V_1\) the two ADC readings, the error is \(e = V_1 - V_0\) and the proportional update is
\begin{gather*}
\text{PWM}(t + T) = \text{PWM}(t) + K\,e(t)
\end{gather*}
evaluated every \(T\) milliseconds and bounded before it is written to
CCR1. Lab 8’s remark that the proportionality constant \(V_\text{REF}/4096\) can be lumped into \(K\) means this: an ADC count is \(3.3/4096\) V, so an error in volts is the error in counts times that constant, and a gain that was chosen to multiply volts becomes, when it multiplies counts instead, that gain times the same constant. Lumping \(V_\text{REF}/4096\) into \(K\) is that multiplication. Since \(K\) is chosen by experiment, there is no reason to convert to volts at all; the counts are used directly, and \(K\) is chosen by trying values on your own build.In integer arithmetic a gain below 1 is a division of the error by a constant, with \(K = 1/\text{K\_DIV}\text{.}\) Because
/ on integers discards the remainder, every error with \(|e| < \text{K\_DIV}\) produces a step of 0, a dead zone of \(\pm\text{K\_DIV}\) counts around the balance point, about \(0.8\text{ mV} \times \text{K\_DIV}\text{.}\) A smaller K_DIV is a larger gain and a narrower dead zone, and past some value the loop overshoots and hunts. A float \(K\) removes the dead zone only if the PWM value is accumulated in a float too and converted to an integer at the write, since assigning 300.5 to an integer discards the same half count; the STM32C031C6 has no floating-point unit, so that arithmetic runs in software. The error can be negative, so e and the PWM value are signed integers, int32_t, and the bounded value is converted back to uint16_t only when it is written. The sampling interval \(T\) is kept with milliseconds() (Subsection 11.5.4); the photocell’s rise and fall times, 55 ms and 20 ms, and the servo’s 20 ms command frame put no useful \(T\) below a few tens of milliseconds.
