Abnormal positive and negative rotation of the motor

I used Magnetic encoder to do current control, and the command was forward rotation 5A. At first, the motor followed my command and turned forward, but after measuring for 10 times, the motor reversed. Each test: the motor was normal in the early period, but it reversed after a period of time. can you help me?

It is likely that the CPR of your encoder is slightly different than what you entered. For example encoder is 4000 CPR but you entered 4096. This is common error with AMS magnetic encoders: you expect it to be 4096 but they ship with 4000.

Thank you ! I will try to change cpr.

but How to determine the real cpr. My cpr is 16384 now.