localOrientation on rotary inverted pendulum
I think this is the result of the conversion to Euler coordinates. 

Your values are too close to the pole of the "coordinate sphere". Little precision errors have large influence on the calculation of the rotation between angles around X and Y. It behaves like a compass near one of the magnetic poles. The direction can't be determined exactly due to the near pole.

You might need a different way to determine the axis around Z. I'm not that familiar with Euler. I suggest to use a different order.