# Accelerometer

As implied by its name, an accelerometer measures acceleration; in our case, it measures your device's linear acceleration in three-dimensional space. So how do you define acceleration exactly? You might recall from high-school physics that acceleration is a vector giving the rate of change of velocity per unit of time (a vector is a quantity having direction and magnitude). Velocity in turn is the rate at which an object changes position per unit of time. Expressing this mathematically, we can write the following:

An accelerometer can therefore be used in order to measure

n Velocity and displacement by integrating the accelerometer readings.

n A vibration or impact indicator (for example, when you shake or jolt the device).

So how should you proceed to integrate accelerometer values to obtain the device's velocity and position in practice? You will first need to capture accelerometer readings at regular time intervals, as previously illustrated using the QTimer technique. You will then need to integrate twice. The first integration step is acceleration with respect to time in order to obtain the device's velocity. You will then integrate velocity with respect to time in order to obtain the device's displacement. To illustrate this, let us consider the acceleration readings given in Figure 9-5.

Figure 9-5. Acceleration readings with linear interpolation

You will notice that I am using linear interpolation for acceleration, which also makes the integration

trivial. The velocity's value at time t is therefore given by:

Repeating the same procedure at time t , we get (I am going to consider here that the time samples

are equally spaced and t = 2t ) the following:

In the general case, the following recursion stands:

In other words, you can calculate your device's velocity at any time by sampling the acceleration and applying this recursive relation.

You can measure displacement applying the same technique, but this time by integrating velocity, as follows:

You will then also get a recursive relation of the following form:

# Gyroscope

A gyroscope measures angular velocity. By integrating the gyroscope readings with respect to time, you will get the device's angular position (note that you will need to integrate along all three axes of the coordinate system to get a complete view of the device's rotations). The gyroscope's angular velocity is given by:

And the angular position is given by:

If you want to use the relation in recursive form, it is given by:

In practice, you will combine the gyroscope and accelerometer readings to measure your device's displacement using six degrees of freedom (i.e., three translations measured by the accelerometer and three rotations measured by the gyroscope).

The first application that comes to mind is gaming. For example, let us consider the infamous first person shooter: you could use the gyroscope in order to “aim” with your weapon at various targets. A tap on the screen would fire that weapon, and then jolting the device would reload the weapon.

Summary

This chapter introduced you to the rich world of sensors and their applications in mobile computing. I showed you how to write sensor-aware applications by using the QtMobility module, which is part of the BlackBerry 10 platform. You also saw how easily you could obtain sensor readings in C++ and QML by using the sensor types supported by BlackBerry 10. I emphasized the fact that obtaining those readings is extremely simple and that the real difficulty lies in the data post-processing.

The obvious application of sensors is in game programming by combining the accelerometer and gyroscope. However, as the BlackBerry 10 platform evolves and new sensor types are introduced in the future, the potential applications will grow exponentially. Applications in domains such as personal health management have huge potential. For example, imagine an application using sensors capable of monitoring your heart and stress levels and capable of playing a specific playlist on your device in order to lower your stress.

Sensor-aware applications are a largely untapped market at the moment and this is something you should definitely consider when designing your next BlackBerry 10 killer app.