I wonder if it’s some kind of grounding issue or noise that’s getting induced when you open/close the relay?
Any suggestions how to troubleshoot? Or work around it with the squaring routine?
The only way to know for sure what’s going on is to check with a scope…
My guess, and these are only guesses is that it could be noise or it could be that you’re cutting the signal when the motor is getting a “high” signal and you’re dropping it low by cutting it off, causing it to take a step… I think the first theory is more likely.
If you don’t have a scope to see what’s going on you could just run a loop in your code to check a few times for squareness. My guess is that it will get into square at some point…