Quaternion and gravity vector

I tried to get informed about quaternions and rotation matrices. I needed a method to track the orientation of a rigid body in the space. Then I understood that my examination rigid body is not moving, but only swinging. This is the reason why I tried to get inside the gravity vector but I would like to listen to the opinion of some expert before doing any task.

My case: a pendulum that is swinging with the same force, for more time. An imu placed on the pendulum sphere stores the angle distant from the vertical.

The code:

# Rotation matrix (half)
  (R11 R12 R13)
 =(R21 R22 R23)
  (R31 R32 R33)
  
  R11 = q0 * q0 + q1 * q1 -0.5f; // = 0.5f - q2 * q2 - q3 * q3;
  R21 = q1 * q2 - q0 * q3;
  R31 = q1 * q3 + q0 * q2;
  R12 = q1 * q2 + q0 * q3;
  R22 = q0 * q0 + q2 * q2 -0.5; 
  R32 = q2 * q3 - q0 * q1;
  R13 = q1 * q3 - q0 * q2;                                     
  R23 = q2 * q3 + q0 * q1;                                   
  R33 = q0 * q0 + q3 * q3 -0.5f; 

// assuming gravity vector as G= (0,0,1), then gx=R13, gy=R23, gz=R33
 

My request: considering this system as in the picture:

is it right to compute the angle between the sensor and the vertical vector (that should point always at the center of Earth) using the below formula? Is there someone, with more math fundamentals, who can help me to interpret the behaviour and the solution?

  gx = q1 * q3 - q0 * q2;
  gy = q0 * q1 + q2 * q3;
  gz = q0 * q0 + q3 * q3 - 0.5f;
  norm = sqrt(gx * gx + gy * gy + gz * gz);
  norm = 1.0 / norm;
  gx *= norm;
  thetha = acos(gx)*180/PI;

Thank You all

You forgot to mention all the important details, like where the quaternion came from, and what is it supposed to represent. How was "the below formula" derived?

For informed help, please read and follow the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.

I used the quaternion generated by an algorithm sensor fusion that is inside my sensor and that uses ACC, gyro, and mag to compute the quaternion. The sensor is attached to the pendulum. Thus, the reference system of the IMU is fixed with the pendulum arm, and the quaternion is the result of sensor fusion during the swing of itself.
I don't know if the math behind is correct in my application but I found very interesting this paper https://core.ac.uk/download/pdf/36738189.pdf and this How to Convert a Quaternion to a Rotation Matrix – Automatic Addison. Can You help me to understand better or give some useful tips?

WHICH sensor? WHICH algorithm sensor fusion?

WHAT is the assumed relationship between the sensor orientation, the body orientation and Earth frame of reference?

If the pendulum is hanging at rest, what are the values of the quaternion and what is the (x, y, z) direction of "gravity" as reported by the sensor?

I only follow the task that the imu permits. There is a command to extract the quaternion. I don't know what the sensor fusion does inside the imu. I think that the relationship is that the pendulum moves only on a plane yz of Earth. With this assumption, I thought about the gravity vector to detect the inclination. In the picture, there is the IMU colored in red and its reference system.

To do anything useful with a quaternion, you must know how it is defined.

However, if it is too much trouble to identify the sensor you are using and how it is mounted on the pendulum, there is little hope for getting help on a forum.

Good luck with your project.

It is a BNO055. Shall this do so much difference? I am asking help to understand quaternions, and maybe my best solution

Look @jremington i followed something like this https://www.allaboutcircuits.com/projects/bosch-absolute-orientation-sensor-bno055/ or Convert quaternion to rotation vector (radians) - MATLAB rotvec - MathWorks Italia , and then the formula was derived by only experiments. But i have no idea if It Is useable

Just try it out and see if the results make sense. If not, debug from there.

Also be aware that jremington, while provides good inputs, is generally a dick. Whenever you see "Good luck with your project", it means he's given up on you

Correct. And, by the way, your suggestion to the OP on how to proceed is extremely insightful and helpful!