Tilt Sensing Using Linear Accelerometers

Freescale Semiconductor
Application Note
Document Number: AN3461
Rev. 6, 03/2013
Tilt Sensing Using a Three-Axis
Accelerometer
by: Mark Pedley
1
Introduction
Contents
1
Accelerometers are sensitive to both linear acceleration
and the local gravitational field. The former provides
information on taps and other handset motions allowing
the development of 'gesture' user interfaces while the
latter provides information on the accelerometer
orientation which allows a smartphone or tablet display
to automatically switch between portrait and landscape
settings.
2
3
4
5
6
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Key Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Accelerometer Output Under Gravity and Acceleration. . 3
Pitch and Roll Estimation. . . . . . . . . . . . . . . . . . . . . . . . . 6
Calculating the Angle Between Two Accelerometer
Readings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Calculating the Tilt Angle. . . . . . . . . . . . . . . . . . . . . . . . 19
Selecting Portrait and Landscape Modes . . . . . . . . . . . 21
This application note documents the mathematics of
orientation determination using a three-axis
accelerometer. The techniques are applicable to both
digital accelerometers and, after signal digitization, to
analog accelerometers. For convenience, it is assumed
that the accelerometer is mounted in a smartphone or
tablet but the arguments apply to any product with an
embedded three-axis accelerometer.
© 2007-2009, 2012-2013 Freescale Semiconductor, Inc. All rights reserved.
The following Freescale application notes cover related topics:
• AN4399 "High Precision Calibration of a Three Axis Accelerometer" describes how a product
containing a consumer grade accelerometer can be re-calibrated after manufacture to achieve a
high level of accuracy.
• AN4248 "Implementing a Tilt-Compensated eCompass using Accelerometer and Magnetometer
Sensors" contains the mathematics and reference source code for a tilt-compensated eCompass
where the accelerometer is used to correct for the magnetometer tilt from horizontal.
• AN4249 "Accuracy of Angle Estimation in eCompass and 3-D Pointer Applications" documents
the effect of accelerometer sensor errors on roll and pitch angle accuracy and ultimately on the
eCompass heading accuracy.
1.1
Key Words
Accelerometer, Tilt, Roll, Pitch, Portrait, Landscape.
1.2
Summary
1. Accelerometer sensors measure the difference between any linear acceleration in the
accelerometer’s reference frame and the earth's gravitational field vector.
2. In the absence of linear acceleration, the accelerometer output is a measurement of the rotated
gravitational field vector and can be used to determine the accelerometer pitch and roll orientation
angles.
3. The orientation angles are dependent on the order in which the rotations are applied. The most
common order is the aerospace sequence of yaw then pitch and finally a roll rotation.
4. Accelerometer sensors are insensitive to rotation about the earth's gravitational field vector. The
equations for the roll and pitch angles therefore have mathematical instabilities when rotation axes
happen to become aligned with gravity and point upwards or downwards. A workaround is
presented to prevent this instability occurring.
5. Simple vector algebra expressions are derived for computing the tilt of the accelerometer from
vertical or the rotation angle between any two accelerometer readings.
6. The most common application of accelerometers in consumer electronics is switching between
portrait or landscape display modes. An algorithm is presented for controlling a tablet PC’s display
orientation.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
2
Freescale Semiconductor, Inc.
2
Accelerometer Output Under Gravity and Acceleration
Accelerometers are sensitive to the difference between the linear acceleration of the sensor and the local
gravitational field. The data sheet for any accelerometer will denote the positive x, y, and z axes on the
sensor package and, by convention, these are defined so that a linear acceleration aligned in the direction
of these axes will give a positive accelerometer output.
A gravitational field component aligned along the same axes directions will, however, result in a negative
reading on the accelerometer. This can be understood by looking at an electron microscope image of a
MEMS accelerometer. The upper proof mass is suspended by the restoring springs. Both a gravitational
field directed to the left and a linear acceleration of the package to the right will deflect the proof mass to
the left.
Movable
Proof Mass
with fingers
Sensing
plates
Restoring
springs
Figure 1. Electron Microscope Image of MEMS Accelerometer Proof Mass and Sensing Plates
The deflection of the proof mass is measured from the change in capacitance between the fingers of the
proof mass and the sensing plates. A simplified transducer model and equivalent electrical circuit are
shown in Figure 2. Circuitry internal to the accelerometer sensor converts the tiny capacitance to a voltage
signal which, in digital accelerometers, is then digitized and output as a digital word over a serial bus.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
3
Simplified Model
Equivalent Circuit
Proof Mass
Figure 2. Simplified Transducer Model and Equivalent Electrical Circuit
The 'native' coordinate system of the accelerometer is not a convenient system to use when discussing the
orientation of a consumer product, such as a smartphone, since the accelerometer can be mounted at any
orientation on the circuit board which can, in turn, be mounted at an arbitrary angle in the final product. It
is more sensible to use a coordinate system aligned with the smartphone product axes. Figure 3 shows the
coordinate system which will be used in this document:
• The x-axis is aligned along the body axis of the smartphone
• The z-axis points downwards so that it is aligned with gravity when the smartphone is flat on a table
• The y-axis is aligned at right angles to both the x and z axes so that the three axes form a right
handed coordinate system.
Changes in orientation are described by rotations in roll φ, pitch θ and yaw ψ about the x, y and z axes
respectively.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
4
Freescale Semiconductor, Inc.
φ
Roll ∪
x axis
Yaw ψ
∪
θ
Pitch ∪
Figure 3. Definition of Coordinate System and Rotation Axes
In addition, since this document is concerned with measuring orientation in the earth's gravitational field,
the convention will be adopted that the accelerometer output is negated to give value +1g in any axis
aligned with the earth's downward gravitational field.
With this assumption, a three-axis accelerometer mounted in a smartphone oriented in the earth's
gravitational field g and undergoing linear acceleration ar measured in the earth's reference frame r, will
have output Gp given by:
⎛ G px ⎞
⎜
⎟
G p = ⎜ G py ⎟ = R ( g – a r )
Eqn. 1
⎜
⎟
⎝ G pz ⎠
where R is the rotation matrix describing the orientation of the smartphone relative to the earth’s
coordinate frame.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
5
It is further assumed that:
• The accelerometer has no linear acceleration a r ≈ 0 . This assumption is needed to solve Equation 1
for the rotation matrix R and, in consequence, any linear acceleration from handshake or other
sources will introduce errors into the orientation estimate.
• The initial orientation of the smartphone is lying flat with the earth's gravitational field aligned with
the smartphone z-axis:
With these additional assumptions, the smartphone accelerometer output Gp (measured in the native
accelerometer units of g) is:
⎛ G px ⎞
⎛ 0⎞
⎜
⎟
⎜ ⎟
G p = ⎜ G py ⎟ = Rg = R ⎜ 0 ⎟
Eqn. 2
⎜
⎟
⎜ ⎟
⎝ G pz ⎠
⎝ 1⎠
The next section introduces the components of the rotation matrix R and describes how to determine the
roll and pitch angles from the accelerometer reading.
3
Pitch and Roll Estimation
The orientation of the smartphone can be defined by its roll, pitch and yaw rotations from an initial
position. The roll, pitch and yaw rotation matrices, which transform a vector (such as the earth's
gravitational field vector g) under a rotation of the coordinate system of Figure 3 by angles φ in roll, θ in
pitch and ψ in yaw about the x, y and z axes respectively, are:
⎛ 1 0
0
⎜
R x ( φ ) = ⎜ 0 cos φ sin φ
⎜
⎝ 0 – sin φ cos φ
⎞
⎟
⎟
⎟
⎠
Eqn. 3
⎛ cos θ 0 – sin θ
⎜
Ry ( θ ) = ⎜ 0 1 0
⎜
⎝ sin θ 0 cos θ
⎞
⎟
⎟
⎟
⎠
Eqn. 4
⎛ cos ψ sin ψ 0 ⎞
⎜
⎟
R z ( ψ ) = ⎜ – sin ψ cos ψ 0 ⎟
⎜
⎟
0
0 1⎠
⎝
Eqn. 5
There are six possible orderings of these three rotation matrices and, in principle, all are equally valid. The
rotation matrices do not, however, commute meaning that the composite rotation matrix R depends on the
order in which the roll, pitch and yaw rotations are applied. It is instructive to compute the values of the
six possible composite rotation matrices R and to determine their effect on the earth's gravitational field of
1g initially aligned downwards along the z-axis.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
6
Freescale Semiconductor, Inc.
⎛ 0⎞
⎛ 0⎞
⎜ ⎟
⎜ ⎟
R xyz ⎜ 0 ⎟ = R x ( φ )R y ( θ )R z ( ψ ) ⎜ 0 ⎟
⎜ ⎟
⎜ ⎟
⎝ 1⎠
⎝ 1⎠
⎛
cos θ cos ψ
⎜
= ⎜ cos ψ sin θ sin φ – cos φ sin ψ
⎜
⎝ cos φ cos ψ sin θ + sin φ sin ψ
cos θ sin ψ
cos φ cos ψ + sin θ sin φ sin ψ
cos φ sin θ sin ψ – cos ψ sin φ
⎛ – sin θ
⎜
= ⎜ cos θ sin φ
⎜
⎝ cos θ cos φ
Eqn. 6
– sin θ
cos θ sin φ
cos θ cos φ
⎞⎛ 0 ⎞
⎟⎜ ⎟
⎟⎜ 0 ⎟
⎟⎜ ⎟
⎠⎝ 1 ⎠
⎞
⎟
⎟
⎟
⎠
Eqn. 8
⎛ 0⎞
⎛ 0⎞
⎜ ⎟
⎜ ⎟
R yxz ⎜ 0 ⎟ = R y ( θ )R x ( φ )R z ( ψ ) ⎜ 0 ⎟
⎜ ⎟
⎜ ⎟
⎝ 1⎠
⎝ 1⎠
⎛ cos ψ cos θ – sin θ sin φ sin ψ
⎜
= ⎜
– cos φ sin ψ
⎜
⎝ cos θ sin φ sin ψ + sin θ cos ψ
sin ψ cos θ + sin θ sin φ cos ψ
cos φ cos ψ
– cos ψ cos θ sin φ + sin ψ sin θ
⎛ – sin θ cos φ
⎜
= ⎜
sin φ
⎜
⎝ cos θ cos φ
Eqn. 9
– sin θ cos φ
sin φ
cos θ cos φ
⎞⎛ 0 ⎞
⎟⎜ ⎟
⎟⎜ 0 ⎟
⎟⎜ ⎟
⎠⎝ 1 ⎠
⎞
⎟
⎟
⎟
⎠
sin ψ
cos φ cos ψ
– cos ψ sin φ
– cos ψ sin θ
cos θ sin φ + cos φ sin θ sin ψ
cos θ cos φ – sin θ sin φ sin ψ
Eqn. 10
Eqn. 11
⎛ 0⎞
⎛ 0⎞
⎜ ⎟
⎜ ⎟
R xzy ⎜ 0 ⎟ = R x ( φ )R z ( ψ )R y ( θ ) ⎜ 0 ⎟
⎜ ⎟
⎜ ⎟
⎝ 1⎠
⎝ 1⎠
⎛
cos θ cos ψ
⎜
= ⎜ – cos φ cos θ sin ψ + sin φ sin θ
⎜
⎝ cos θ sin ψ sin φ + cos φ sin θ
Eqn. 7
Eqn. 12
⎞⎛ 0 ⎞
⎟⎜ ⎟
⎟⎜ 0 ⎟
⎟⎜ ⎟
⎠⎝ 1 ⎠
Eqn. 13
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
7
⎛
– cos ψ sin θ
⎜
= ⎜ cos θ sin φ + cos φ sin ψ sin θ
⎜
⎝ cos φ cos θ – sin θ sin φ sin ψ
⎞
⎟
⎟
⎟
⎠
Eqn. 14
⎛ 0⎞
⎛ 0⎞
⎜ ⎟
⎜ ⎟
R yzx ⎜ 0 ⎟ = R y ( θ )R z ( ψ )R x ( φ ) ⎜ 0 ⎟
⎜ ⎟
⎜ ⎟
⎝ 1⎠
⎝ 1⎠
Eqn. 15
⎛ cos ψ cos θ cos φ cos θ sin ψ + sin θ sin φ cos θ sin φ sin ψ – sin θ cos φ
⎜
= ⎜ – sin ψ
cos φ cos ψ
cos ψ sin φ
⎜
⎝ cos ψ sin θ – cos θ sin φ + cos φ sin ψ sin θ cos θ cos φ + sin θ sin φ sin ψ
⎛ cos θ sin φ sin ψ – cos φ sin θ
⎜
= ⎜
cos ψ sin φ
⎜
⎝ cos θ cos φ + sin θ sin φ sin ψ
⎞⎛ 0 ⎞
⎟⎜ ⎟
⎟⎜ 0 ⎟
⎟⎜ ⎟
⎠⎝ 1 ⎠
⎞
⎟
⎟
⎟
⎠
Eqn. 17
⎛ 0⎞
⎛ 0⎞
⎜ ⎟
⎜ ⎟
R zxy ⎜ 0 ⎟ = R z ( ψ )R x ( φ )R y ( θ ) ⎜ 0 ⎟
⎜ ⎟
⎜ ⎟
⎝ 1⎠
⎝ 1⎠
⎛ cos ψ cos θ + sin θ sin φ sin ψ
⎜
= ⎜ – cos θ sin ψ + cos ψ sin φ sin θ
⎜
cos φ sin θ
⎝
cos φ sin ψ
cos φ cos ψ
– sin φ
Eqn. 18
cos θ sin φ sin ψ – sin θ cos ψ
cos ψ cos θ sin φ + sin θ sin ψ
cos θ cos φ
⎛ cos θ sin φ sin ψ – cos ψ sin θ
⎜
= ⎜ cos ψ cos θ sin φ + sin θ sin ψ
⎜
cos θ cos φ
⎝
⎞
⎟
⎟
⎟
⎠
Eqn. 16
⎞⎛ 0 ⎞
⎟⎜ ⎟
⎟⎜ 0 ⎟
⎟⎜ ⎟
⎠⎝ 1 ⎠
Eqn. 19
Eqn. 20
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
8
Freescale Semiconductor, Inc.
⎛ 0⎞
⎛ 0⎞
⎜ ⎟
⎜ ⎟
R zyx ⎜ 0 ⎟ = R z ( ψ )R y ( θ )R x ( φ ) ⎜ 0 ⎟
⎜ ⎟
⎜ ⎟
⎝ 1⎠
⎝ 1⎠
⎛ cos ψ cos θ
⎜
= ⎜ – cos θ sin ψ
⎜
sin θ
⎝
cos φ sin ψ + cos ψ sin φ sin θ sin φ sin ψ – cos φ cos ψ sin θ
cos ψ cos φ – sin θ sin φ sin ψ cos ψ sin φ + cos φ sin ψ sin θ
– cos θ sin φ
cos θ cos φ
⎛ sin φ sin ψ – cos φ cos ψ sin θ
⎜
= ⎜ cos ψ sin φ + cos φ sin ψ sin θ
⎜
cos θ cos φ
⎝
⎞
⎟
⎟
⎟
⎠
Eqn. 21
⎞⎛ 0 ⎞
⎟⎜ ⎟
⎟⎜ 0 ⎟
⎟⎜ ⎟
⎠⎝ 1 ⎠
Eqn. 22
Eqn. 23
It can be readily seen from Equations 6 to 23 that the six composite rotation matrices and the six values of
the measured gravitational vector are all different. A consequence is that roll, pitch and yaw rotation angles
are meaningless without first defining the order in which these rotations are to be applied.
Four of these rotation sequences can be immediately rejected as being unsuitable for determining the
smartphone orientation. The accelerometer output has three components but, since the vector magnitude
must always equal 1g in the absence of linear acceleration, has just two degrees of freedom. The
accelerometer vector lies on the surface of a sphere with radius 1g. It is not therefore possible to solve for
three unique values of the roll φ, pitch θ and yaw ψ angles. The four rotation sequences in Equations 12
to 23 result in the accelerometer output being a function of all three rotation angles and cannot therefore
be solved.
In contrast, the two rotation sequences in Equations 6 to 11 depend only on the roll φ and pitch θ angles
and can be solved. The lack of any dependence on the yaw rotation angle ψ is easy to understand physically
since the first rotation is in yaw ψ around the smartphone z-axis which is initially aligned with the
gravitational field and pointing downwards. All accelerometers are completely insensitive to rotations
about the gravitational field vector and cannot be used to determine such a rotation.
It is conventional therefore to select either the rotation sequence Rxyz of Equations 6 to 8 or the sequence
Ryxz of Equations 9 to 11 to eliminate the yaw rotation ψ and allow solution for the roll φ and pitch θ
angles. The unknown yaw angle ψ represents the smartphone rotation from north but its determination
requires the addition of a magnetometer sensor to create an eCompass. Further details of the operation of
a tilt-compensated eCompass can be found in application note AN4248.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
9
Solving Rxyz (Equations 6 to 8) for the Pitch and Roll Angles
Equation 8 can be rewritten in the form of Equation 24 relating the roll φ and pitch θ angles to the
normalized accelerometer reading Gp:
⎛ – sin θ
⎜
Gp
----------- = ⎜ cos θ sin φ
Gp
⎜
⎝ cos θ cos φ
⎞
⎛ G px
⎟
⎜
1
⎟ ⇒ -------------------------------------------------- ⎜ G py
2
2
2
⎟
G px + G py + G pz ⎜ G
⎠
⎝ pz
⎞
⎛ – sin θ
⎟
⎜
⎟ = ⎜ cos θ sin φ
⎟
⎜
⎠
⎝ cos θ cos φ
⎞
⎟
⎟
⎟
⎠
Eqn. 24
Solving for the roll and pitch angles from Equation 24, and using the subscript xyz to denote that the roll
and pitch angles are computed according to the rotation sequence Rxyz, gives:
G py
tan φ xyz = ⎛⎝ ---------⎞⎠
G pz
Eqn. 25
– G px
– G px
tan θ xyz = ⎛ ------------------------------------------------⎞ = -------------------------------⎝ G py sin φ + G pz cos φ⎠
2
2
G py + G pz
Eqn. 26
The rotation sequence Rxyz is widely used in the aerospace industry and is termed the 'aerospace rotation
sequence'.
Solving Ryxz (Equations 9 to 11) for the Pitch and Roll angles
Equation 11 can be similarly solved for the roll and pitch angles from an arbitrary accelerometer reading
Gp:
⎛ – sin θ cos φ
⎜
Gp
----------- = ⎜
sin φ
Gp
⎜
⎝ cos θ cos φ
⎞
⎛ G px
⎟
⎜
1
⎟ ⇒ -------------------------------------------------- ⎜ G py
2
2
2
⎟
G px + G py + G pz ⎜ G
⎠
⎝ pz
⎞
⎛ – sin θ cos φ
⎟
⎜
⎟ = ⎜
sin φ
⎟
⎜
⎠
⎝ cos θ cos φ
⎞
⎟
⎟
⎟
⎠
Eqn. 27
Solving for the roll and pitch angles from Equation 27, and using the subscript yxz to denote that the angles
are computed according to the rotation sequence Ryxz, gives:
G py
tan φ yxz = ------------------------------2
2
G px + G pz
– G px
tan θ yxz = ⎛ ------------⎞
⎝ G pz ⎠
Eqn. 28
Eqn. 29
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
10
Freescale Semiconductor, Inc.
Equations 25 and 26 and Equations 28 and 29 therefore give different results for the roll and pitch angles
from the same accelerometer reading. As mentioned earlier, this is a simple consequence of the fact that
rotation matrices do not commute. The order of rotations is important and must always be specified when
referring to specific orientation angles.
Eliminating Duplicate Solutions by Limiting the Roll and Pitch Ranges
The next complication to be addressed is that the expressions for the roll φ and pitch θ angles in
Equations 25 and 26 and Equations 28 and 29 have an infinite number of solutions at multiples of 360°.
Restricting the range of the roll and pitch angles to lie in the range -180° to 180° helps somewhat but the
next paragraph shows that this still leads to two unique solutions for the roll and pitch angles.
Evaluating Equation 8 for pitch angle π − θ and roll angle φ + π and applying standard trigonometric
identities shows that the accelerometer measurement is the same as that resulting from rotations θ and φ.
⎛
– sin ( π – θ )
⎜
⎜ cos ( π – θ ) sin ( φ + π )
⎜
⎝ cos ( π – θ ) cos ( φ + π )
⎞
⎛ – sin θ
⎟
⎜
⎟ = ⎜ cos θ sin φ
⎟
⎜
⎠
⎝ cos θ cos φ
⎞
⎟
⎟
⎟
⎠
Eqn. 30
Similarly, evaluating Equation 11 for pitch angle θ + π and roll angle π − φ also shows that the
accelerometer measurement is identical to that resulting from rotations θ and φ.
⎛ – sin ( θ + π ) cos ( π – φ )
⎜
⎜
sin ( π – φ )
⎜
⎝ cos ( θ + π ) cos ( π – φ )
⎞
⎛ – sin θ cos φ
⎟
⎜
⎟ = ⎜
sin φ
⎟
⎜
⎠
⎝ cos θ cos φ
⎞
⎟
⎟
⎟
⎠
Eqn. 31
The solution is to restrict either the roll or the pitch angle (but not both) to lie between -90° and +90°. The
convention used in the aerospace sequence is that the roll angle can range between -180° to +180° but the
pitch angle is restricted to -90° to +90°. The convention used by Android™ smartphones and by Microsoft
for its Windows 8 sensor platform is the reverse with the roll angle restricted between -90° and 90° but the
pitch angle able to range between -180° and 180°. Whichever convention is used, the net result is to
eliminate one of the two duplicate solutions and give a single solution except in the very specific
orientations discussed later.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
11
Worked Example 1
Determine the roll and pitch angles defining the orientation of the smartphone relative to the starting
position with the smartphone flat on the table assuming the aerospace rotation sequence Rxyz for the
smartphone accelerometer reading (in native units of g) below:
⎛ 0.461105 ⎞
⎜
⎟
G p = ⎜ 0.082198 ⎟ , G p = 1.006910
⎜
⎟
⎝ – 0.887432 ⎠
Eqn. 32
Substituting the accelerometer reading into Equations 25 and 26 gives:
0.082198- = – 0.092625 ⇒ φ = – 5.2922°or 174.7081 °
tan φ xyz = -----------------------xyz
– 0.887432
Eqn. 33
– 0.461105
tan θ xyz = ----------------------------------------------------------- = – 0.517380 ⇒ θ xyz = – 27.3561°
2
2
0.082198 + 0.887432
Eqn. 34
The range of θxyz is -90° to 90° so there is a unique solution for θxyz in Equation 34. In Equation 33,
however, φxyz can vary between -180° and 180° giving two possible solutions of -5.29° and 174.71°.
Comparison with Equation 24 shows that the required solution which gives positive Gpy and negative Gpz
is 174.71°. In practice, this is not a problem for a software implementation provided that the ATAN2
function is used as φxyz = ATAN2(Gpy, Gpz). The ATAN2 function automatically returns the angle (in
radians) in the correct quadrant based on the signs of the two arguments.
Regions of Instability
The final complication is that both Equations 25 and 29 have a region where the calculation of the roll
angle φ and pitch θ angle respectively become unstable.
The inverse tangent function for the aerospace rotation sequence in Equation 25 is mathematically defined
for all values of Gpy and Gpz with the one exception where both Gpy and Gpz equal zero. This condition
occurs when the smartphone is aligned with its x-axis pointing vertically upwards or downwards. Even if
the smartphone is not exactly vertical, the inverse tangent calculation is dominated by accelerometer noise
in the numerator and denominator of Equation 25 and produces an unstable and essentially random
estimate of the roll angle. The physics is straightforward to understand: when the smartphone is vertical,
the x-axis is vertical so that a rotation in roll is a rotation about the gravitational field vector which cannot
be detected.
Similarly Equation 29 is defined for all values of Gpx and Gpz with the exception where both Gpx and Gpz
are zero. This occurs when the smartphone is oriented on edge with the y-axis vertical and parallel to the
earth's gravitational field vector so that any pitch θ rotation cannot be detected.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
12
Freescale Semiconductor, Inc.
Equations 26 and 28, in contrast, are always defined and stable when the accelerometer is held in the earth's
gravitational field. Applying the constraint that Gpx2 + Gpy2 + Gpz2 = 1g2 in Equations 26 and 28 shows
that it is not possible for both numerator and denominator to be simultaneously zero and give an unstable
angle estimate. When the numerator is zero, the denominator equals 1 and vice versa.
– G px
tan θ xyz = ---------------------- (Caution: do not use)
2
1 – G px
Eqn. 35
G py
tan φ yxz = ---------------------- (Caution: do not use)
2
1 – G py
Eqn. 36
Equations 35 and 36 should not, however, be used as alternatives to Equations 26 and 28 because any
handshake, linear acceleration, sensor noise or simply using the accelerometer in a region of the world
where the local gravitational field exceeds that at the original accelerometer calibration site can lead to Gpx
or Gpy exceeding 1g and the square root and angle calculation having no real-valued solution.
There is no perfect solution to the problem of the roll and pitch angle singularities in Equations 25 and 29.
One workaround, which is commonly used for the aerospace rotation sequence Rxyz, is to use the
numerically stable Equation 26 for calculation of the pitch angle θ but to modify Equation 25 for the roll
angle φ by mixing a fraction μ of the square of the accelerometer x-axis reading Gpx2 into the denominator
to prevent the denominator ever being zero. The resulting equations are:
– G px
tan θ xyz = ---------------------------2
2
G py + G pz
Eqn. 37
G py
tan φ xyz = -------------------------------------------------------2
2
sign ( G pz ) G pz + μ G px
Eqn. 38
sign(Gpz) has a value +1 if Gpz is non-negative and -1 if Gpz is negative. This term is present to recover the
loss sign of sign of Gpz when taking the square root. Obviously Equation 37 (which is identical to
Equation 26) is mathematically correct for the aerospace sequence Rxyz and therefore always gives the
correct pitch angle θ. Equation 38 for the roll angle φ is an approximation but has several characteristics
that make it attractive:
• It is impossible for both numerator and denominator of Equation 38 to be simultaneously zero and
give an undefined or unstable estimate of the roll angle.
• In the absence of any rotation in pitch θ, Gpx equals zero and Equation 38 reduces to Equation 25
giving the correct roll angle φ.
• When the true roll angle is zero, Gpy is also zero according to Equation 8. Equation 38 therefore
correctly gives zero roll angle φ for all pitch angles θ.
• Equation 38 smoothly drives the roll angle φ to zero as the smartphone becomes oriented vertically
upwards or downwards since Gpx2 approaches 1g2 and Gpy and Gpz approach zero. This is
reasonable behavior since the accelerometer reading is only dependent on the pitch angle θ when
the smartphone is vertical and the roll angle is superfluous.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
13
The roll angle error Δφ resulting from using the stable approximation of Equation 38 instead of the correct
(but potentially unstable) Equation 25 is given by:
G py⎞
G py
⎞
–1 ⎛
-⎟ – tan –1 ⎛ -------Δφ = tan ⎜ ---------------------------------⎝ G pz⎠
⎝ G 2+ μ G 2 ⎠
pz
px
Eqn. 39
Using the trigonometric identity:
–1
–1
–1 a – b
tan a – tan b = tan ⎛⎝ ----------------⎞⎠
1 + ab
Eqn. 40
and Equation 8 allows Equation 39 to be simplified to:
⎛
2
2
(
G pz – G pz + μ G px )⎞⎟
---------------------------------------------------------------
– 1 ⎜ G py
Δφ = tan ⎜
2
⎝ G py + G pz
2
2
2
– 1 ⎛ sin φ ( cos θ cos φ – cos θ cos φ + μ sin θ )⎞
= tan ⎜ -----------------------------------------------------------------------------------------------------------⎟ Eqn. 41
2
2⎟
⎝ cos θ sin 2 φ + cos φ cos 2 θ cos 2 φ + μ sin 2 θ ⎠
G pz + μ G px ⎠
Figures 4 and 5 plot the absolute value of the resulting error |Δφ| in degrees computed from Equation 41
as a function of roll φ and pitch angles θ between -90° and 90° and with the contour mesh set to interval
of 3° and μ equal to 0.01 and 0.1. It's clear that the stable approximation of Equation 38 gives an accurate
estimate except at orientations approaching 90° where the roll angle error rises as its value is forced to zero
for stability.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
14
Freescale Semiconductor, Inc.
Figure 4. Roll Angle Error Surface |Δφ| for μ = 0.01
Figure 5. Roll Angle Error Surface |Δφ| for μ = 0.1
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
15
A similar strategy can be used for the non-aerospace sequence Ryxz to stabilize Equation 29 to give the
following equations:
– G px
tan θ yxz = ----------------------------------------------------------2
2
sign ( G pz ) G pz + μ G py
Eqn. 42
G py
tan φ yxz = ----------------------------2
2
G px + G pz
Eqn. 43
The error surface for Equation 42 will have the same form as shown in Figures 4 and 5.
4
Calculating the Angle Between Two Accelerometer
Readings
Simple vector algebra provides a means to calculate the angle change α of the apparent gravity vector
between any two accelerometer readings.
The scalar product a.b between any two vectors a and b gives the angle α between the two vectors. This
result is easily proved by applying the triangle cosine theorem to the triangle with sides comprised of the
vectors a, b and a-b.
⎛ ax ⎞ ⎛ bx
⎜
⎟ ⎜
a.b = ⎜ a y ⎟ . ⎜ b y
⎜
⎟ ⎜
⎝ az ⎠ ⎝ bz
⎞
⎟
⎟ = a x b x + a y b y + a z b z = a b cos α
⎟
⎠
Eqn. 44
ax bx + ay by + az bz
⇒ cos α = ------------------------------------------------------------------------------2
2
2
2
2
2
ax + ay + az bx + by + bz
Eqn. 45
The unit vector nˆ which is normal to both a and b, such that a, b, and nˆ form a right-handed triplet, is
given by the vector product:
⎛ ax ⎞ ⎛ bx
⎜
⎟ ⎜
a × b = ⎜ ay ⎟ × ⎜ by
⎜
⎟ ⎜
⎝ az ⎠ ⎝ bz
⎞
⎟
⎟ =
⎟
⎠
ˆi ˆj kˆ
⎛ ay bz – az by
⎜
ax ay az = ⎜ az bx – ax bz
⎜
bx by bz
⎝ ax by – ay bx
⎞
⎟
⎟ = a b nˆ sin α
⎟
⎠
⎛ a b – az by
⎛
⎞⎛
⎞⎜ y z
1
1
⇒ nˆ sin α = ⎜ ---------------------------------------⎟ ⎜ ---------------------------------------⎟ ⎜ a z b x – a x b z
⎝ a 2 + a 2 + a 2⎠ ⎝ b 2 + b 2 + b 2⎠ ⎜
x
y
z
x
y
z ⎝ a b –a b
x y
y x
⎞
⎟
⎟
⎟
⎠
Eqn. 46
Eqn. 47
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
16
Freescale Semiconductor, Inc.
Figure 6 shows these vector relationships graphically.
a
a.b=abcosξα
^ (into the
n
page)
^ ξα
axb=abnsin
α
ξ
b
Figure 6. Definitions of the Scalar and Vector Products
Worked Example 2
Two accelerometer readings are recorded (in native units of g) as:
G p1
⎛ 0.745439 ⎞
⎜
⎟
= ⎜ 0.065434 ⎟ , G p1 = 1.007443
⎜
⎟
⎝ 0.674523 ⎠
Eqn. 48
G p2
⎛ 0.393567 ⎞
⎜
⎟
= ⎜ 0.348443 ⎟ , G p2 = 0.994750
⎜
⎟
⎝ 0.844523 ⎠
Eqn. 49
Note that as a result of noise or handshake, neither Gp1 nor Gp2 have modulus exactly equal to 1g.
Using Equation 45, the angle between the two vectors is:
0.745439 × 0.393567 + 0.065434 × 0.348443 + 0.674523 × 0.844523
cos α = ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- = 0.884606 ⇒ α = 27.8° Eqn. 50
1.007443 × 0.994750
Using Equation 47, the vector nˆ is the instantaneous rotation axis and has direction:
⎛ ay bz – az by
⎜
1
1
nˆ sin α = ⎛ ----------------------⎞ ⎛ ----------------------⎞ ⎜ a z b x – a x b z
⎝ 1.007443⎠ ⎝ 0.994750⎠ ⎜
⎝ ax by – ay bx
⎞
⎛ – 0.179772 ⎞
⎜
⎟
⎟ ⎛
1
1
⎟ = ⎝ ----------------------⎞⎠ ⎛⎝ ----------------------⎞⎠ ⎜ – 0.364070 ⎟
1.007443 0.994750 ⎜
⎟
⎟
⎠
⎝ 0.233990 ⎠
Eqn. 51
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
17
⎛ – 0.383610 ⎞
⎜
⎟
⇒ nˆ = ⎜ – 0.776878 ⎟ , sin α = 0.467626 ⇒ α = 27.8°
⎜
⎟
⎝ 0.499304 ⎠
Eqn. 52
The vector product calculation confirms the angle of 27.8° between the two gravitational vectors and also
provides the vector of the rotation axis nˆ normal to the two accelerometer measurements. Direct
evaluation proves that the vector nˆ is orthogonal to both Gp1 and Gp2.
⎛ 0.745439 ⎞ ⎛ – 0.383610
⎜
⎟ ⎜
G p1 .nˆ = ⎜ 0.065434 ⎟ . ⎜ – 0.776878
⎜
⎟ ⎜
⎝ 0.674523 ⎠ ⎝ 0.499304
5
⎞
⎟
⎟ =0
⎟
⎠
Eqn. 53
⎛ 0.393567 ⎞ ⎛ – 0.383610 ⎞
⎜
⎟ ⎜
⎟
G p2 .nˆ = ⎜ 0.348443 ⎟ . ⎜ – 0.776878 ⎟ = 0
⎜
⎟ ⎜
⎟
⎝ 0.844523 ⎠ ⎝ 0.499304 ⎠
Eqn. 54
Calculating the Tilt Angle
The techniques of the previous section can be used to calculate the angle ρ between the gravitational vector
measured by the accelerometer and the initial orientation with the gravitational field pointing downwards
along the z-axis.
Gp
⎠ρ
(0, 0, 1)
Figure 7. Calculation of the Tilt Angle ρ from Vertical
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
18
Freescale Semiconductor, Inc.
If the accelerometer reading is Gp, then in the absence of linear acceleration:
⎛ 0⎞
⎜ ⎟
G pz
G p . ⎜ 0 ⎟ = G pz = G p cos ρ ⇒ cos ρ = ------------------------------------------------2
2
2
⎜ ⎟
G px + G py + G pz
⎝ 1⎠
Eqn. 55
Worked Example 3
The measured accelerometer reading Gp is below. By what angle ρ is the smartphone tilted from its
original flat position?
⎛ 0.324322 ⎞
⎜
⎟
G p = ⎜ – 0.653423 ⎟
⎜
⎟
⎝ 0.684234 ⎠
Eqn. 56
The modulus of Gp is easily determined to be:
2
2
2
G px + G py + G pz = 1.000161
Eqn. 57
Using Equation 55, the tilt angle ρ is:
0.684234
cos ρ = ---------------------- = 0.684124 ⇒ ρ = 46.8°
1.000161
Eqn. 58
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
19
6
Selecting Portrait and Landscape Modes
The first high volume use of accelerometers in consumer products was to switch a display between
landscape and portrait orientations. A tablet PC, shown in Figure 8, has four possible display orientations
labeled here as Bottom, Top, Right and Left, according to the location of the lower edge of the displayed
text or imagery.
x
z
Bottom
y
Right
Left
.
Top
Figure 8. Portrait and Landscape Screen Orientations for a Tablet PC
This problem is rather more complex than it would appear at first sight since a direct mapping between the
display orientation and the accelerometer reading is not ergonomically suitable. Specifically, a user
viewing the screen in any of the four modes shown in Figure 8 expects that display mode to remain
unchanged as the tablet is rotated to a flat orientation. The accelerometer reading resulting from the tablet
being flat on the table can therefore be associated with any of the four display orientations.
The solution is a simple state machine in which the accelerometer reading leads to transitions between the
screen orientations rather than directly defining the screen orientation. Simple state transition rules are
listed below.
• (|Gpz| < 0.5g) AND (Gpx > 0.5g) AND (|Gpy| < 0.4g): Change orientation to Top
• (|Gpz| < 0.5g) AND (Gpx < -0.5g) AND (|Gpy| < 0.4g): Change orientation to Bottom
• (|Gpz| < 0.5g) AND (Gpy > 0.5g) AND (|Gpx| < 0.4g): Change orientation to Right
• (|Gpz| < 0.5g) AND (Gpy < -0.5g) AND (|Gpx| < 0.4g): Change orientation to Left.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
20
Freescale Semiconductor, Inc.
The regions in the accelerometer measurement space Gpx, Gpy, and Gpz corresponding to these rules are
shown in Figure 9 with the 1g sphere superimposed.
Figure 9. State Transition Zones for Display Orientation Changes
The restriction that |Gpz| < 0.5g for a display transition to occur prevents any change in orientation when
the tablet is within 0.5g of being complete flat or inverted (since |Gpz| = 1g when the tablet is flat). This is
known as z-axis lockout and ensures that the screen orientation remains unchanged as the user lowers the
tablet to a flat orientation.
The remaining constraints define four non-overlapping regions for the transition between the four screen
orientations to occur. The lack of overlap between the regions leads to a unique transition within each
region and prevents the display orientation oscillating at the boundary between regions.
Tilt Sensing Using a Three-Axis Accelerometer, Rev. 6
Freescale Semiconductor, Inc.
21
How to Reach Us:
Information in this document is provided solely to enable system and software
Home Page:
freescale.com
implementers to use Freescale products. There are no express or implied copyright
Web Support:
freescale.com/support
information in this document.
licenses granted hereunder to design or fabricate any integrated circuits based on the
Freescale reserves the right to make changes without further notice to any products
herein. Freescale makes no warranty, representation, or guarantee regarding the
suitability of its products for any particular purpose, nor does Freescale assume any
liability arising out of the application or use of any product or circuit, and specifically
disclaims any and all liability, including without limitation consequential or incidental
damages. “Typical” parameters that may be provided in Freescale data sheets and/or
specifications can and do vary in different applications, and actual performance may
vary over time. All operating parameters, including “typicals,” must be validated for each
customer application by customer’s technical experts. Freescale does not convey any
license under its patent rights nor the rights of others. Freescale sells products pursuant
to standard terms and conditions of sale, which can be found at the following address:
freescale.com/salestermsandconditions.
Freescale, the Freescale logo, AltiVec, C-5, CodeTest, CodeWarrior, ColdFire, C-Ware,
Energy Efficient Solutions logo, Kinetis, mobileGT, PowerQUICC, Processor Expert,
QorIQ, Qorivva, StarCore, Symphony, and VortiQa are trademarks of Freescale
Semiconductor, Inc., Reg. U.S. Pat. & Tm. Off. Airfast, BeeKit, BeeStack, ColdFire+,
CoreNet, Flexis, MagniV, MXC, Platform in a Package, QorIQ Qonverge, QUICC
Engine, Ready Play, SafeAssure, SMARTMOS, TurboLink, Vybrid, and Xtrinsic are
trademarks of Freescale Semiconductor, Inc. All other product or service names are
the property of their respective owners.
© 2013 Freescale Semiconductor, Inc.
Document Number: AN3461
Rev. 6
03/2013