tl;dr:
How to calculate the roll and pitch angle of a device using 3DoF accelerometer measurements.
Accelerometer
A 3DoF accelerometer is a device that measures acceleration along three orthogonal axes (x,y,z). They are used in robotics, fitness trackers, spacecraft, and many other applications.
Attitude
Attitude refers to the orientation of an entity in a 3D space. It’s usually represented by three angles: roll, pitch, and yaw. Roll is the rotation around the x-axis, pitch is the rotation around the y-axis, and yaw is the rotation around the z-axis.
Typically, a gyroscope is used to measure angular rates which is then integrated to determine the attitude.
But, we can also calculate the roll and pitch angles using accelerometer readings alone. But with one caveat:
The accelerometer must not be accelerating i.e. it must be at rest or move with a constant velocity.
Working Principle
The principle behind this being possible is that gravity is the only accelerating force acting on the sensor when it’s at rest or moving at a constant velocity.
Deriving the Formula
Let the gravity vector in the inertial frame (reference frame with 0 angles in all axes) be:
G=00−g where g=9.81m/s2
The accelerometer readings in the body frame are given by the vector:
A=axayaz
To find the acceleration on each axis due to the gravity vector, we need to convert it from the inertial frame into the body frame.
The rotation matrices for each axes are given by:
Roll:
M1(ϕ)=1000cϕ−sϕ0sϕcϕ
Pitch:
M2(θ)=cθ0sθ010−sθ0cθ
Yaw:
M3(ψ)=cψ−sψ0sψcψ0001
The rotation matrix from the inertial frame to the body frame is given by: