Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to measure the tilt of the phone in XY plane using accelerometer in Android

I tried to use the Z axis data from SensorEvent.values, but it doesn't detect rotation of my phone in the XY plane, ie. around the Z-axis.

I am using this as a reference for the co-ordinate axes. Is it correct?

axes

How do I measure that motion using accelerometer values?

These games do something similar: Extreme Skater, Doodle Jump.

PS: my phone orientation will be landscape.

like image 473
udiboy1209 Avatar asked Jun 24 '12 07:06

udiboy1209


People also ask

How do you measure tilt on an accelerometer?

If you want to measure tilt in both x and y axis with a 2-axis accelerometer then you can simply use sin-1(a) where a is the output from one axis of the accelerometer. The component of gravity acting on the x axis is a sine function whilst that acting on the y axis is a cosine.

Can accelerometer detect tilt?

This application note describes how accelerometers are used to measure the tilt of an object. Accelerometers can be used for measuring both dynamic and static measurements of acceleration. Tilt is a static measurement where gravity is the acceleration being measured.

How does a phone measure tilt?

The accelerometer is an in-built comment of a smartphone to measure its acceleration. It tracks the different motion like shaking, tilting, swinging, and rotating and accordingly change the orientation of your app. To calculate and detect the motion, the accelerometer uses the value of XYZ.

How do I know if my phone has an accelerometer?

Open the Phone app and tap Keypad, then type *#0*#. A diagnostic screen pops up with buttons for a variety of tests. Tap Red, Green, or Blue to test those pixel colors. Tap Receiver to check the audio, Vibration to try the vibrating feature, or Sensor to test the accelerometer and other sensors.


1 Answers

Essentially, there is 2 cases here: the device is laying flat and not flat. Flat here means the angle between the surface of the device screen and the world xy plane (I call it the inclination) is less than 25 degree or larger than 155 degree. Think of the phone lying flat or tilt up just a little bit from a table.

First you need to normalize the accelerometer vector.
That is if g is the vector returns by the accelerometer sensor event values. In code

float[] g = new float[3];  g = event.values.clone();  double norm_Of_g = Math.sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2]);  // Normalize the accelerometer vector g[0] = g[0] / norm_Of_g g[1] = g[1] / norm_Of_g g[2] = g[2] / norm_Of_g 

Then the inclination can be calculated as

int inclination = (int) Math.round(Math.toDegrees(Math.acos(g[2]))); 

Thus

if (inclination < 25 || inclination > 155) {     // device is flat } else {     // device is not flat } 

For the case of laying flat, you have to use a compass to see how much the device is rotating from the starting position.

For the case of not flat, the rotation (tilt) is calculated as follow

int rotation = (int) Math.round(Math.toDegrees(Math.atan2(g[0], g[1]))); 

Now rotation = 0 means the device is in normal position. That is portrait without any tilt for most phone and probably landscape for tablet. So if you hold a phone as in your picture above and start rotating, the rotation will change and when the phone is in landscape the rotation will be 90 or -90 depends on the direction of rotation.

like image 159
Hoan Nguyen Avatar answered Sep 19 '22 17:09

Hoan Nguyen