Android Accelerometer App

This entry describes a quick and dirty way of displaying accelerometer data on an android device. The application outputs the applied force as a circle, where the radius of the circle represents the magnitude of the force vector, with the gravity subtracted. It also demonstrates the difference between a shifting buffer and a ring buffer.

Reading the accelerometer

The accelerometer returns the forces applied on the phone for x, y and z directions.

A sensor manager keeps track of which sensors will send data to which listener methods, and how often data should be sent. Sensor managers in Android takes care of all sensors, not only accelerometers.

This initializes the sensor manager, which only needs to be done once. Then a sensor listener object must be made to handle incoming acceleration data. Inside it is also necessary to override the onSensorChanged method to receive the data somehow:

Here, the data is taken from the event.values array. Each axis is given separately. The force of gravity gives a value of 9.81 divided on all three axis, depending on how the phone is oriented. If the phone lies on a table with the display pointing upwards, the force in z-axis is 9.81 while y and z is 0. In this application, the orientation of the phone is not important, and will be removed. The magnitude of the force vector is found by the square root of the sum of the squares, or sqrt(x*x+y*y+z*z). Then, 9.81 can be subtracted directly from the scalar, giving 0 when no other forces are applied on the android device.

When you let your phone fall towards the ground, you get a negative force value. This is because the gravity force is subtracted, but in free fall the phone is weightless (like your belly in a roller coaster).

Buffering force magnitudes

As the view class receives the accelerometer values, they are placed in a shifting or circular buffer. All samples are multiplied by the FORCE_TO_PIXEL value, determining how many pixels of change the force should cause on the circle display. Since accelerometers give a lot of jitter, the result is also rounded to the nearest integer.

Circular buffer
An array can store a set number of samples, in this case 32. When the array is full, the next sample starts again from 0, and the old data is overwritten.

Shifting buffer
For a shifting buffer, the array doesn’t start over when it’s full. Instead, old samples are shifted backwards by one step, and the new sample is placed at the end of the array.

Drawing the circle

A separate class was made to contain the graph view. The most important method is generateCircle:

The method uses trigonometry to draw a line, starting and ending at 0 degrees forming a circle. The circle is divided into 32 points, where each points distance from the center is determined by the corresponding magnitude sample.

The Activity Class

In order to stop the screen from turning off when viewing the graph, this line was added in the onCreate method for the activity:

Future work

This application is meant as an example only, and is not optimized. A SurfaceView would give faster 2D graphics for a lower CPU cost, and would draw the graphics in a separate thread. These are more complicated to set up however. It would also be faster to use a lookup table to determine the angles of each sample in the buffer, so that they don’t have to be calculated every time a new sample is added.


Google Play Store – Force

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">