Hi mmurray70,
The original MPGSmooth.c program counts the encoder pulses using software. In order to handle encoder counting higher than what could normally be handled in software (~5Hz) it uses a trick that assumes the velocity can not change instantly. Based on the previous velocity it can predict how many counts it should/must have missed. This trick can normally increase the count rate about 10X and works very well. Unfortunately if the velocity does appear to change instantly due to noise or jitter in sampling time the algorithm can get confused and think the encoder is moving rapidly (and aliasing) when it is actually still. I suspect the time to do all the AdjustLimits calculations is slowing down and/or causing jitter in the MPG sampling.
If you don't intend to move the MPG faster than 5KHz you can defeat the "trick" by changing line:
DiffX2 = 2*(Pos-PosNoWrap) + (Change2+Change1);
to
DiffX2 = 2*(Pos-PosNoWrap);
In this case there should never be any run-away no matter how instantly the MPG changes speed. If you move the MPG too fast it should result in improper counting and jerky motion but never run-away.
Another option is to use a hardware encoder counter that always counts reliably up to 1MHz. See the example: MPGSmoothHardwareEnc.c
Another option might be to run the MPG and AdjustLimits in different threads so they would have no effect on one another.
Regards