I have a pendulum and an IMU sensor that is capable to give me Euler angles and Quaternions. I want to attach the sensor on pendulum and measure θ angle around x axis from the starting position (see the following image). The problem is that the pendulum rotates itself (around z axis) and this is a problem according to Euler singularities (gimbal lock). So when the pendulum is at 180 degrees and rotates around z-axis i get wrong θ angle.
How can i solve this issue and get the correct θ angle?
example of pendulum and sensor
Edit:
Lets say that we attach the sensor on pendulum such as x-axis is parallel to ceiling and z-axis parallel to pendulum's thread.