Sensors Overview for Windows Phone
March 22, 2012
Windows Phone supports multiple sensors that allow applications to determine the orientation and motion of the device. These sensors enable the development of applications where the physical device itself is a means of user input. Typical uses for this functionality include motion-controlled games and augmented reality applications. For many of these purposes, the combined motion API, which combines and processes input from all sensors, is the simplest way to obtain motion and orientation information. For applications that require it, Windows Phone also provides APIs to retrieve data from the individual sensors. This topic provides a brief overview of each sensor API.
Each of the sensor APIs is built on the base class SensorBase<TSensorReading> and follow a similar pattern of starting the sensor and acquiring data. For each sensor, there is a how-to topic that quickly walks you through using sensors in your application.
Caution: |
|---|
The Sensor APIs cannot be used while the application is running under the lock screen. For more information, see Idle Detection for Windows Phone. The Sensor APIs also cannot be used in background agents. For more information, see Background Agents Overview for Windows Phone. |
The accelerometer measures the forces applied to the device at a moment in time. These forces can be used to determine in which direction the user is moving the device. The acceleration value is expressed as a 3-dimensional vector representing the acceleration components in the X, Y, and Z axes in gravitational units. The orientation of the acceleration is relative to the device such that -1g is applied in the Z-axis when the device is face up on a level table and -1g is applied to the Y-axis when the device is placed perpendicular to the table top.
The Accelerometer sensor detects the force of gravity along with any forces resulting from the movement of the phone. The combined motion API, accessed using the Motion class, uses multiple device sensors to separate the gravity vector from the device acceleration and allows you to easily determine the current attitude (yaw, pitch, and roll) of the device.
The Compass, or magnetometer, sensor can be used to determine the angle by which the device is rotated relative to the Earth’s magnetic north pole. An application can also use raw magnetometer readings to detect magnetic forces around the device.
The compass sensor is not required for all Windows Phone devices. It is important that you consider this when designing and implementing your application. Your application should always check to see whether the sensor is available and either provide an alternative input mechanism or fail gracefully if it is not.
The Gyroscope sensor is used to determine the rotational velocity of the device in each axis. You can use the values obtained from the gyroscope sensor to determine the orientation of the device in space. Because a gyroscope measures rotational velocity and not angle, it is susceptible to issues related to drift. If you are interested in determining the attitude (yaw, pitch, and roll) of the device, you should use Motion instead of the gyroscope API.
The gyroscope sensor is not required for all Windows Phone devices. It is important that you consider this when designing and implementing your application. Your application should always check to see whether the sensor is available and either provide an alternative input mechanism or fail gracefully if it is not.
There are physical limitations that can make it difficult to determine the actual orientation and motion of a device from the raw data from the sensors by Windows Phone. For example, readings from the accelerometer include the force of gravity applied to the device in addition to the force resulting from the motion of the device. The gyroscope sensor measures rotational velocity, not position, and so it is subject to drift. Also, there are complex geometrical calculations that are needed to translate the raw data from these sensors into the true orientation of the device. The Motion class handles the low-level sensor calculation and allows applications to easily obtain the device’s attitude (yaw, pitch, and roll), rotational acceleration, and linear acceleration both due to gravity and user movement. For typical applications that use this type of data, such as augmented reality applications, using the Motion class is recommended. If you want to use sensor data in an atypical manner or want to implement your own motion and orientation calculations, you can use the individual sensor classes to obtain raw data from each sensor.
The Motion API can use two different sensor configurations. Normal Motion uses the compass and the accelerometer sensor and is less accurate than Enhanced Motion, which uses the compass, the accelerometer, and the gyroscope. If your application requires the accuracy of Enhanced motion, you should check to verify that the device on which the application is running supports the gyroscope sensor.
When a user views an application on Windows Phone Marketplace, a warning is issued if the user’s device does not support a sensor that is used by the application. For the accelerometer, compass, and gyroscope this is straightforward – if an application uses the sensor and the user’s device does not support it, the user will receive a warning.
As stated above, the Motion API will function on a device that has at least a compass sensor. It will, however, yield better data if a gyroscope is present. Currently, Windows Phone Marketplace uses the compass sensor to determine if the Motion API is supported on a device. If a user views an application that uses the Motion API and their device lacks a compass, they will receive a warning that the application requires a compass sensor.
Caution: