That code looks as if it is trying to power the motor in the target direction until it reaches the target, then reverses the motor direction and waits until the motor slows down, stops, accelerates in the reverse direction and reaches the other limit.
You don't say what it actually does, but this design is a recipe for a massively overshooting system which would be liable to be unstable and just overshoot further and further until it hits a mechanical limit somewhere.
I would have thought that a system with significant inertia as you're describing would benefit from using a PID algorithm to control the motor direction and speed. That way you could easily get it to move to the required position and stop.