Detecting pitch and roll with the accelerometer
In Chapter 12, IMU Programming with Python, we were getting a vector from the accelerometer, but we need to calculate angles to consider using it alongside the gyroscope and magnetometer. To use this to rotate things, we need to turn this vector into pitch-and-roll angles.
Getting pitch and roll from the accelerometer vector
The accelerometer describes what is going on in Cartesian coordinates. We need to convert these into a pair of pitch-and-roll angles perpendicular to each other. In Chapter 12, IMU Programming with Python, the Coordinate and rotation systems section shows roll as taking place around the x axis, and pitch as taking place around the y axis.
A crude but effective way to consider this is as two planes. When rotating around the x axis, you can take a vector in the yz plane and find its angle. When turning around the y axis, then you consider the xz plane instead. Take a look at the next diagram: