I was dealing with a similar problem. (See: [url]simDrift issues in 3d])
I’m very grateful I found this post as I was getting no end of headaches.
This may be obvious, but in any case, here is a slightly more generic solution to the same problem. (See the link I posted for the context.)
#Get the difference between quat_cycle and the previous value of quat_cycle
diff_quat=Quat(quat_cycle - self.previous_quat)
#If the difference is abnormally large, reverse the quat to an equivalent quat.
if abs(diff_quat.getR())>1.0 or abs(diff_quat.getI())>1.0 or \
abs(diff_quat.getJ())>1.0 or abs(diff_quat.getK())>1.0:
quat_cycle.setR(-quat_cycle.getR())
quat_cycle.setI(-quat_cycle.getI())
quat_cycle.setJ(-quat_cycle.getJ())
quat_cycle.setK(-quat_cycle.getK())
#Update previous quat
self.previous_quat = quat_cycle