How to calculate the orientation of a IMU sensor?
As you've discovered, a three-axis accelerometer will only tell you the orientation when something is basically stable. Or, stable on average if you can tolerate a slow response due to the filtering required to filter out the vibrating component of the signal. Instead, to find sensor orientation in such conditions, you can use a 3-axis gyroscope, which is not very sensitive to vibration, and take the integral of the rate of turn output that it gives to find the orientation, and correct the gyroscope drift using some kind of extra information, typically from accelerometers and magnetometers, which give you the vectors of gravity and the earth's magnetic field. Kalman filter schemes are common here.

Nowadays you can buy sensors that do this for you and that output the Euler angles or a quaternion representation of the sensor's orientation relative to being flat.  Look for Inertial Measurement Unit (IMU) sensors that advertise that they have Sensor Fusion.

Your code example above is doing this, using the other sensors present in your CPro sensor with a Kalman filter to come up with a better estimate of the sensor's orientation as a quaternion, which is then converted to a transformation matrix by the to_matrix() method.  You can find more on how to convert a quaternion to Euler angles, which is what I think you want, from this article: Conversion Between Quaternions and Euler Angles