I don’t know why it wasn’t possible to redefine the direction of the accelerometer axis before, but now everything works.
As usual, if the acceleration axis is facing UP, then the force of gravity is directed DOWN and you just need to put a “-” in front of the acceleration value in the code:
I also found two options for calculating the roll angle from quaternion, in the source code there was the first option
and in the code you specified, there was a slightly different one.
But the result is not much different.
I'm looking for algorithms that will deal with changes in roll or pitch angles during sudden movements of the device. Video1 and video2
And this video3 shows the operation of DCM from open source
The video shows that the algorithm successfully compensates for sideways jerks of the sensor, while the orientation angles remain stable.