If r represents this vector in the rotated device body frame xyz, then r = 0 0 -1

The transformation of r due to the rotation about the z axis can be represented by the following rotation matrix.

A = cos(α) -sin(α) 0 sin(α) cos(α) 0 0 0 1

The transformation of r due to the rotation about the x axis can be represented by the following rotation matrix.

B = 1 0 0 0 cos(β) -sin(β) 0 sin(β) cos(β)

The transformation of r due to the rotation about the y axis can be represented by the following rotation matrix.

C = cos(γ) 0 sin(γ) 0 1 0 -sin(γ) 0 cos(γ)

If R resresents the vector r in the earth frame XYZ, then since the inital body frame is aligned with the earth, R is as follows.

R = A B C r

R = cos(α) -sin(α) 0 sin(α) cos(α) 0 0 0 1 1 0 0 0 cos(β) -sin(β) 0 sin(β) cos(β) cos(γ) 0 sin(γ) 0 1 0 -sin(γ) 0 cos(γ) 0 0 -1

R = cos(α) -sin(α) 0 sin(α) cos(α) 0 0 0 1 1 0 0 0 cos(β) -sin(β) 0 sin(β) cos(β) -sin(γ) 0 -cos(γ)

R = cos(α) -sin(α) 0 sin(α) cos(α) 0 0 0 1 -sin(γ) sin(β) cos(γ) -cos(β) cos(γ)

R = - cos(α) sin(γ) - sin(α) sin(β) cos(γ) - sin(α) sin(γ) + cos(α) sin(β) cos(γ) -cos(β) cos(γ)

The compass heading θ is given by

θ = tan - 1 ( R x R y ) = tan - 1 ( - cos ( α ) sin ( γ ) - sin ( α ) sin ( β ) cos ( γ ) - sin ( α ) sin ( γ ) + cos ( α ) sin ( β ) cos ( γ ) )

provided that β and γ are not both zero.

As a consistency check, if we set γ=0, then

θ = tan - 1 ( - sin ( α ) sin ( β ) cos ( α ) sin ( β ) ) = -α

as expected.