Awesome Accelerometers


This and the following pages include some recipes for specific sensors. These just look at things you might commonly like to do with each sensor and combine the filtering methods we’ve seen in the previous pages. Hopefully this will give you an idea of ways that you might apply these filtering methods in practice.

So, lets talk about accelerometers:

The accelerometer is a really neat sensor which outputs the acceleration that the device is experiencing. We can use this to detect gestures, estimate orientation of the device, estimate how fast the device is moving, detect bumps, all sorts of things. However processing sensor data from it is relatively difficult.

Any idea why accelerometer data is hard to process?

The accelerometer reports all acceleration on the device in terms of 3 axes X, Y and Z. This acceleration is a combination of two signals, firstly the acceleration due to motion of the device in space, and secondly, the constant downwards acceleration due to gravity which all objects are subject to.

Further to that, the accelerometer axes are aligned to the axes of the device; this means that when a device is rotated as well as moved in space, the axes rotate along with the device. This means that filtering of each individual axis alone may be problematic if the device is rotated.

So, what are some ways to process the accelerometer that are useful:

Method 1 - Ensure fixed alignment of the device and remove constant gravitational acceleration

Stick your phone on the floor. As long as it is flat, then the Z axis should be pointing up. Then we can just treat that z axis as a single sensor value as before. Try playing with this knock sensor code on your phone. It should say knock when you hit the floor or jump next to it hard enough.

We use a high pass filter in this example to remove the gravitational acceleration, which we know will be constant as long as the device is still.

Method 2 - Use the magnitude

The magnitude of the acceleration is found by taking the magnitude of the vector (x,y,z), i.e.

\(magnitude=\sqrt{x^2+y^2+z^2}\).

This value is useful because it is not dependent on rotation of the device, so we can use it just to detect e.g. how much a device is being shaken.

Pick up your phone, and run this sample, and give the phone a good shake. If you have your speakers on, it should complain if you shake it too much.

Method 3 - Use the gravitational acceleration for orientation sensing

If you assume the accelerometer is still, then you can use the gravitational acceleration to detect orientation. For example, if a phone is flat on it’s back, the z acceleration is \(9.8m/s^2\) (or 1G).

The code below detects whether a phone is tipped to it’s left or right (in portrait orientation) by looking at the angle defined by Z and X.

` roll = math.atan2(z,x ) `

where math.atan2 is the full circle arc-tangent function, which gives an angle from a vector.

What can’t you do with an accelerometer?

A warning… One thing that is commonly attempted with an accelerometer is to identify device motion and changes in position. The reasoning behind this is that in theory if we know the initial position and velocity of our device, by integrating the acceleration we can estimate speed, and then by integrating the speed, we get relative position.

This process of estimating changes in position from a known starting point is known as dead reckoning. It is sort of possible in some situations when the accelerometer orientation is known to a high degree of accuracy. However it is extremely hard to do successfully for one reason, which is that as well as integrating the actual value of the acceleration, we also integrate any error in the accelerometer signal. Any error in accelerometer means that the velocity is wrong, which means that the position quickly drifts away from the real position.

Try the code below on your phone to see - put the phone on a flat surface, and hit start, then move it 20cm to the right. See what the distance moved is reported as, and whether it stops when you stop.