All iOS devices, to date, can
sense motion through the use of the accelerometer hardware. This
capability was supplemented with a gyroscope in the iPhone 4 and
presumably will be expanded to other hardware in the future. To get a
better sense for what this means to your applications, let’s review what
information each of these pieces of hardware can provide.
Accelerometer
An accelerometer uses a unit of
measure called a g, which is short for gravity. 1g is the force pulling
down on something resting at sea level on earth (9.8 meters/second2).
You don’t normally notice the feeling of 1g (that is until you trip and
fall, and then 1g hurts pretty bad). You are familiar with g-forces
higher and lower than 1g if you’ve ever ridden on a roller coaster. The
pull that pins you to your seat at the bottom of the roller coaster hill
is a g-force greater than 1, and the feeling of floating up out of your
seat at the top of a hill is negative g-force at work.
By the Way
An accelerometer measures
acceleration relative to a free fall—meaning that if you dropped your
iPhone into a sustained free fall, say off the Empire State Building,
its accelerometer would measure 0g on the way down. (Just trust me;
don’t try this out.) The accelerometer of an iPhone sitting in your lap,
on the other hand, measures 1g along the axis it is resting on.
The measurement of the 1g
pull of earth’s gravity on the device while it’s at rest is how the
iPhone accelerometer can be used to measure the orientation of the
phone. The accelerometer provides a measurement along three axes, called
x, y, and z (see Figure 1).
Depending
on how your iPhone is resting, the 1g of gravity will be pulling
differently on the three possible axes. If your device is standing
straight up on one of its edges or is flat on its back or on its screen,
the entire 1g will be measured on one axis. If the device is tilted at
an angle, the 1g will be spread across multiple axes (see Figure 2).
Gyroscope
Think about what we’ve just
learned about the accelerometer hardware. Is there anything it can’t
do? It might seem, at first, that using the measurements from the
accelerometer we can make a good guess as to what the user is doing, no
matter what. Unfortunately, that’s not quite the case.
The accelerometer measures
the force of gravity distributed across your iPhone. Imagine, however,
that your iPhone is lying face up on a table. We can detect this with
the accelerometer, but what we can’t
detect is if you start spinning the iPhone around in a rousing game of
“spin the bottle... err... iPhone.” The accelerometer will still
register the same value regardless of how the phone is spinning.
The same goes for if the iPhone
is standing on one of its edges and rotates. The accelerometer can be
used only if the iPhone is changing orientation with respect to gravity,
but the gyroscope can determine if, in any given orientation, the
iPhone is also rotating.
When
querying the iPhone’s gyroscope, the hardware will report back with a
rotation value along x, y, and z axes. The value is a measurement, in
radians, of the speed of rotation along that axis. If you don’t remember
your geometry, rotating 2 radians is a complete circle, so a reading of
2 on any of the gyroscope’s 3 axes would indicate that the device is
spinning once per second, along that axis, as shown in Figure 3.