This is very much an applied maths question. I'm having trouble with Euler angles in the context of smartphone apps. I've been working with Android, but I would guess that the same problem arises whatever operating system one uses.
In the context of it's 3D sensors, the Android operating system is set up to calculate a rotation matrix $\mathbf{R}$ which is defined by
$$ \mathbf{R}=\left[ \begin{array} [c]{ccc}% E_{x} & E_{y} & E_{z}\\ N_{x} & N_{y} & N_{z}\\ G_{x} & G_{y} & G_{z}% \end{array} \right] $$
where $x$, $y$ and $z$ are axes relative to the smartphone,
and where
$$ \begin{align*} \mathbf{E} & = \left( E_{x},E_{y},E_{z}\right) =\text{a unit vector which points East}\\ \mathbf{N} & = \left( N_{x},N_{y},N_{z}\right) =\text{a unit vector which points North}\\ \mathbf{G} & = \left( G_{x},G_{y},G_{z}\right) = \begin{array} [c]{l}% \text{a unit vector which points away from}\\ \text{the centre of the earth (Gravity vector)} \end{array} \text{ .} \end{align*} $$
Once $\mathbf{R}$ has been calculated, the Android operating system will then calculate for you the Euler angles $\phi$, $\theta$ and $\psi$ where
$$ \begin{array} [c]{lll}% \text{azimuth} & =\phi & =\text{rotation about }\mathbf{G}\\ \text{pitch} & =\theta & =\text{rotation about }\mathbf{E}\\ \text{roll} & =\psi & =\text{rotation about }\mathbf{N} \end{array} \text{ .} $$
The relationship between $\mathbf{R}$ and $(\phi$, $\theta$, $\psi)$ is given by
$$ \mathbf{R}=\left[ \begin{array} [c]{ccc}% \cos\phi\cos\psi-\sin\phi\sin\psi\sin\theta & \sin\phi\cos\theta & \cos \phi\sin\psi+\sin\phi\cos\psi\sin\theta\\ -\sin\phi\cos\psi-\cos\phi\sin\psi\sin\theta & \cos\phi\cos\theta & -\sin \phi\sin\psi+\cos\phi\cos\psi\sin\theta\\ -\sin\psi\cos\theta & -\sin\theta & \cos\psi\cos\theta \end{array} \right] \text{ .} $$
The azimuth $\phi$ is a particularly important result, because $\phi=0$ corresponds to the direction of north. However, there is a well known problem with these Euler angles, which is known as Gimbal lock. When $\theta=\pm\frac{\pi}{2}$ the co-ordinate system is denerate in the sense that the combination $\phi\pm\psi$ is defined, but the individual quantities $\phi$ and $\psi$ are not defined.
In the context of smartphone apps, this means that determining the direction of north is tricky when the device isn't lying flat on a table. The closer the device gets to being upright, the less reliable to co-ordinate system becomes. On stackoverflow.com some people recommend switching axes when $\theta$ moves away from $0$ so as to avoid Gimbal lock, but I find that that solution introduces other problems, such as jumps in $\phi$, $\theta$ and $\psi$ when the coordinate system is switched.
So my question is, instead of $\phi$, $\theta$ and $\psi$ as defined above, is there a better way of defining an angular co-ordinate system for expressing the orientation of a smartfone relative to the vectors $\mathbf{E}$, $\mathbf{N}$ and $\mathbf{G}$? In particular, is there a coordinate system that's better for defining the direction of north, that will work whether the device is upright or lying flat on a table.
Clarification Whatever the best methodology is, one result must be some kind of azimuth angle which can be explicitly calculated from the rotation matrix $R$, which represents the angle through which the device must be rotated so that it's pointing north.