Centigrade GmbH
Thinking Out of the Box

Philip Schäfer

From windows to tiles: WinRT sensors – Overview and C# sample

November 12th, 2012 by Philip Schäfer

Sensors are one of the most defining features that distinguish mobile devices such as tablets or smartphones from common PCs. This being the case, we have been enormously enthusiastic to play around with these components when our brand-new Surface RT tablet arrived recently. And by the way, this device feels like a charm.

Eventually, we decided to share our experiences in this regard in terms of a blog post giving an overview on the entire sensor landscape being available on the current Surface RT tablet running WinRT – the ARM chips targeting brother of Windows 8 – as an operating system.

Overview

Basically, the currently available version of Microsoft´s Surface tablet ships with four sensor devices:

  1. An ambient light sensor
  2. An accelerometer
  3. A gyroscope
  4. A magnetometer

Given the existence of these four sensors on a hardware layer, we may exploit them in various ways on an application layer; therefore, WinRT abstracts 7 sensor interfaces in its respective API:

  1. An accelerometer
  2. A gyroscope
  3. A compass
  4. A light sensor
  5. An orientation sensor
  6. A simplified orientation sensor
  7. An inclinometer

For detailed information regarding the interplay of hardware-level sensor devices and application-layered sensor interfaces, please see here.

Positioning in a 3D Cartesian coordinate system

Whenever we intend to sense values related to a device´s actual positioning in our physical, real world, we need to take a certain geometrical model as a basis we may refer to. For instance, we may want to differentiate, if a device is being moved from left to right or up and down. This implies that we need to align the device with a set of axes in our three-dimensional space.

Regardless of which 3D sensor interface we might think of, we can always take three axes as a basis:

  • X: the long side (left/ right)
  • Y: the short side (up/ down)
  • Z: “into” the screen

Sensor interfaces & functionality

As MSDN already provides some sample C# code illustrating how to use WinRT´s sensor API that appears to be fairly straight-forward, in this section we are going to focus exclusively on the particular sensor´s actual functionality. We are aware that this code sample only considers the usage of the accelerometer interface, but as all sensors as listed below may be applied analogously we trust that our audience may apply further sensors as well without any considerable overhead.

Accelerometer

Returned property Measurement Unit Type
X Acceleration in relation to free-fall G-Force Double
Y Acceleration in relation to free-fall G-Force Double
Z Acceleration in relation to free-fall G-Force Double

The accelerometer senses the device´s acceleration in relation to free-fall; this means, that we cannot (at least not on earth or without any zero-gravity-simulation) achieve a state where the results are zero for all three axes at the same point of time. For instance, assuming that our Surface tablet is lying on its back, the returned z-value will be -1 as 1g is the standard g-force drawing our device towards the center of the earth (similarly, the z-value will be +1 if the device is lying on its front).

Gyrometer

Returned property Measurement Unit Type
X Angular velocity Degrees per second Double
Y Angular velocity Degrees per second Double
Z Angular velocity Degrees per second Double

As the gyrometer (also referred to as gyroscope) senses our device´s angular velocity while being moved, all three axes´ values will be zero when we hold it motionlessly. Simultaneously, only the x value will change when we flip our Surface´s screen open and shut again without moving it into any other direction (as we are rotating the device about its x-axis).

Compass

Returned property Measurement Unit Type
Magnetic north Angle relative to direction of magnetic north Degrees Double
True north Angle relative to direction of true north Degrees Double?

While the magnetic north (and consequently the device´s current angle to the direction to this point) may be determined by the built-in magnetometer, this single sensor device is not able to sense the geographical (or true) north as well, since magnetic and true north usually differ. As the Surface RT tablet features no GPS sensor, we initially assumed that the compass interface might exploit the tablet´s internet connection to approximate the true north – unfortunately, we were not able to receive a value, though.

Light sensor

Returned property Measurement Unit Type
Illuminance Ambient illuminance Lux Float

Please note, that the Surface´s light sensor is positioned left of the front camera. Hold your hands in front of the sensor and you can see that the sensor delivers different results.

Orientation sensor

Returned property Measurement Unit Type
Quaternion Orientation 4×1 Matrix SensorQuaternion
Rotation matrix Orientation 3×3 Matrix SensorRotationMatrix

The orientation sensor interface exposes a quaternion and a rotation matrix indicating the device´s current orientation, both of which can i.e. be utilized when (similarly) rotating view elements. As diving deeper into computer graphics would go beyond the scope of this article, we will refrain from a detailed explanation of the underlying mathematical concepts (nevertheless, please feel free to look up those concepts yourself, e.g. in this paper).

Simple orientation sensor

Returned property Type Members
Simple orientation Enum NotRotated
Rotated90DegreesCounterclockwise
Rotated180DegreesCounterclockwise
Rotated270DegreesCounterclockwise
Faceup
Facedown

This sensor simplifies sensing the device´s current orientation by auto-converting complex numeric sensor values (as provided by the actual orientation sensor) to a set of discrete, human-readable states.

Inclinometer

Returned property Measurement Unit Type
Pitch Angle relative to x-axis Degrees Double
Roll Angle relative to y-axis Degrees Double
Yaw Angle relative to z-axis Degrees Double

As the inclinometer sensor provides information about our device´s angle in relation to its respective axis, the question about the device´s assumed initial position arises: to achieve a distinct initial position, the inclinometer utilizes the magnetometer. Thus, acknowledging a certain variance, we may observe that Pitch=Roll=Yaw=0 if our device is perfectly aligned with the magnetic north (similarly, that Yaw=0 if MagneticNorth=0).

C# Sample

To enable a smooth get-in-touch with the sensors provided by the Surface tablet, please feel free to download our sample project .
All trademarks or registered trademarks are properties of their respective owners.