Hello,

Background: I am trying to implement a position tracking loop or speed estimator respectively. There is an quadrature encoder, from which the pulses are decoded and counted using a timer. A 16 bit unsigned position value in the range od 0 to 2Pi is then derived from it. (0=0 rad, 65535 = 2Pi rad). Or this value can be whatever suitable form, even signed 16bit, i.e. scaled to -Pi to +Pi rad range. I need to estimate the instantaneous value of rotational speed (using a standard 2nd order loop using a PI regulator and integrator) .

The principle is described here:

https://www.embeddedrelated.com/showarticle/530.php, see chapter "Tracking Loops".

The simple continuous time model assumes the position value increments (or decrements) infinitely and does not have bounds on the value, unlike the fixed point arithmetic I need to implement it in. Therefore I found the best solution to scale the full rotation on the full scale range of the unsigned (or signed) integer variable and then to "somehow magically" deal with the wrapping problem.

The problem with the wrapping of the Position and PositionEstimation was found not to be easy to be solved, if at all in this shape or form of the implementation. When the Position wraps, the PositionError value goes of course haywire, as it suddenly changes sign and saturates. Please see below the source code and the plot.

Please note regulator output is scaled to +-597, which effectively brings the PI reg output on the range of +-8192 RPM, as the calculation period is 66.7us (15kHz frequency). The number 597 is derived from how many 0-2pi wraps (full revolutions) are needed for the required +-8192 range this way: (8192 * 2^16) / (60 * 15000) = 596.5.

The estimated speed seems to be correct in that part of the waveform, where the value settles, however gets periodically disrupted by the unpleasant overflow (or underflow) occurring at the Position signal.

What do I do wrong? How to handle the wrap?

Thank you for you help,

Y.

//EDIT: I might have finaly found the root cause of the issue. Will test thoroughly tomorrow. Now looking good, still a lot of things to be tweaked here and there.