DC Motor with Magnetic Encoder Overrun problem


i don't write very often in forums, but this time i need your expert opinion. i have a Polulu gear motor that is driven by a M0. On the shaft there is a magnetic encoder disk attached which determines 3 steps per revolution. So I count with the hardware interrupt of the MCU the steps to get the position of the shaft.

The gear motor has a gear ratio of 298:1, that means 298*3 are 894 steps per revolution, in my console I can specify 's894' and the shaft rotates exactly 360 degrees.

Unfortunately due to inertia and charging current the motor continues to run for 2 more revolutions, so if I specify s1000, the motor will rotate up to 10 steps more, depending on the set speed.

I have uploaded a video on YouTube, when the LED goes off, the motor turns 1-2 more revolutions.
https://youtu.be/HRhcKACEZ58 ... just play it with 0.25 speed

How can I compensate this via code so that it really ends up at 1000 steps?
Code examples, links are welcome

Thanks for your help

You need a PID loop and closed-loop control for accurate motion-control. Otherwise the best you
can do is dead-reckoning, and your system cannot hold position actively either. For instance
dead-reckoning here would mean driving the motor for 6 fewer counts than you intend.