Question

Lets say you have the acceleration readings in all the 3 dimensions i.e X, Y and Z. How do you infer using the readings the phone was tilted left or right? The readings get generated every 20ms.

I actually want the logic of inferring the tilt from the readings. The tilt needs to be smooth.

Was it helpful?

Solution

A tilt can be detected in a sort of diferent ways. You can take into account 1 axis, 2 axis, or the 3 axis. Depending on how accurate you want it, and how much you feel like fighting with maths.

If you use only one axis, it is quite simple. Think the mobile is completely horizontal, and you move it like this:

enter image description here

using just one axis, lets say, axis x, will be enough, since you can detect accurately a change in that axis position, since even any small movement will do a change in the axis. But, if your application is only reading that axis, and the user has the phone almost vertical, the difference in x axis will be really small even rotating the phone a big angle. Anyways,for applications that only need coarse resolution, a single-axis can be used.

Referring to basic trigonometry, the projection of the gravity vector on the x-axis produces an output acceleration equal to the sine of the angle between the accelerometer x-axis and the horizon. This means that having the values of an axis (those are acceleration values) you can calculate the angle in which the device is.

enter image description here

this means that the value given to you by the sensor, is = to 9,8 * sine of the angle, so doing the maths you can get the actual angle.

But don't worry, you don't even have to do this. Since the values are more or less proportional, as you can see in the table below, you can work directly with the value of the sensor, without taking much care of what angle represents, if you don't need it to be much accurate, since a change in that value means a proportional change in the angle, so with a few test, you will find out how big should be the change in order to be relevant to you.

enter image description here

So, if you take the value over the time, and compare to each other, you can figure out how big the rotation was. For this,

  1. you consider just one axis. this will be axis X.
  2. write a function to get the difference in the sensor value for that axis between one function call, and the next
  3. Decide a maximum time and a minimum sensor difference, that you will consider a valid movement (e.g. a big rotation is good but only if it is fast enough, and a fast movement is good only if the difference in the angle is big enough)
  4. if you detect two measurements that accomplish those conditions, you take note of half tilt done (in a boolean for instance), and start measuring again, but now, the new reference value is the value that was considered half tilt.
  5. if the last difference was positive, now you need a negative difference, and if the last difference was negative, now you need a positive difference; this is, coming back. so start taking values comparing the new reference value with the new values coming from the sensor, and see if one accomplish what you decided in point 3.
  6. if you find a valid value (accomplishing value difference and time conditions ), you have a tilt. But if you dont get a good value and the time is consumed, you reset everything: let your reference value be the last one, reset the timers, reset the half-tilt-done boolean to false, and keep measuring.

I hope this is good enough for you. For sure you can find some libraries or code snippets to help you out with this, but i think is good, as you say, to know the logic of inferring the tilt from the readings

The pictures was taken from this article, wich i recomend to read if you want to improve the accuracy and consider 2 o 3 axis for the tilt

OTHER TIPS

The commonsware Sensor Monitor app does a pretty good job with this. It converts the sensor readouts to X, Y, Z values on each sensor reading, so it's pretty easy from there to determine which way the device is moving.

https://github.com/commonsguy/cw-omnibus/tree/master/Sensor/Monitor

Another item worth noting (from the Commonsware book):

There are four standard delay periods, defined as constants on the SensorManager class:

  1. SENSOR_DELAY_NORMAL, which is what most apps would use for broad changes, such as detecting a screen rotating from portrait to landscape
  2. SENSOR_DELAY_UI, for non-game cases where you want to update the UI continuously based upon sensor readings
  3. SENSOR_DELAY_GAME, which is faster (less delay) than SENSOR_DELAY_UI, to try to drive a higher frame rate
  4. SENSOR_DELAY_FASTEST, which is the “firehose” of sensor readings, without delay

You can use the accelerometer and magnetic field sensor to accomplish this. You can call this method in your OnSensorChanged method to detect if the phone was tilt upwards. This currently only works if the phone is held horizontally. Check the actual blog post for a more complete solution.

http://www.ahotbrew.com/how-to-detect-forward-and-backward-tilt/

public boolean isTiltUpward()
{
    if (mGravity != null && mGeomagnetic != null) 
    {
          float R[] = new float[9];
          float I[] = new float[9];

          boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);

          if (success) 
          {
            float orientation[] = new float[3];
            SensorManager.getOrientation(R, orientation);               

            /*
             * If the roll is positive, you're in reverse landscape (landscape right), and if the roll is negative you're in landscape (landscape left)
             * 
             * Similarly, you can use the pitch to differentiate between portrait and reverse portrait. 
             * If the pitch is positive, you're in reverse portrait, and if the pitch is negative you're in portrait.
             * 
             * orientation -> azimut, pitch and roll
             * 
             *
             */

            pitch = orientation[1];
            roll = orientation[2];              

            inclineGravity = mGravity.clone();

            double norm_Of_g = Math.sqrt(inclineGravity[0] * inclineGravity[0] + inclineGravity[1] * inclineGravity[1] + inclineGravity[2] * inclineGravity[2]);

            // Normalize the accelerometer vector
            inclineGravity[0] = (float) (inclineGravity[0] / norm_Of_g);
            inclineGravity[1] = (float) (inclineGravity[1] / norm_Of_g);
            inclineGravity[2] = (float) (inclineGravity[2] / norm_Of_g);

            //Checks if device is flat on ground or not
            int inclination = (int) Math.round(Math.toDegrees(Math.acos(inclineGravity[2])));                     

            /*
             *   Float obj1 = new Float("10.2");
             *   Float obj2 = new Float("10.20");
             *   int retval =  obj1.compareTo(obj2);
             *   
             *   if(retval > 0) {
             *      System.out.println("obj1 is greater than obj2");
             *   }
             *   else if(retval < 0) {
             *      System.out.println("obj1 is less than obj2");
             *   }
             *   else {
             *      System.out.println("obj1 is equal to obj2");
             *   }
             */
            Float objPitch = new Float(pitch);
            Float objZero = new Float(0.0);
            Float objZeroPointTwo = new Float(0.2);
            Float objZeroPointTwoNegative = new Float(-0.2);

            int objPitchZeroResult = objPitch.compareTo(objZero);
            int objPitchZeroPointTwoResult = objZeroPointTwo.compareTo(objPitch);
            int objPitchZeroPointTwoNegativeResult = objPitch.compareTo(objZeroPointTwoNegative);

            if (roll < 0 && ((objPitchZeroResult > 0 && objPitchZeroPointTwoResult > 0) || (objPitchZeroResult < 0 &&  objPitchZeroPointTwoNegativeResult > 0)) && (inclination > 30 && inclination < 40))
            {
                return true;
            }
            else
            {
                return false;
            }               
        }
    }

    return false;
}

Is this what you're looking for?

public class AccelerometerHandler implements SensorEventListener
{
    float accelX;
    float accelY;
    float accelZ;

    public AccelerometerHandler(Context paramContext)
    {
        SensorManager localSensorManager = (SensorManager)paramContext.getSystemService("sensor");

        if (localSensorManager.getSensorList(1).size() != 0)
            localSensorManager.registerListener(this, (Sensor)localSensorManager.getSensorList(1).get(0), 1);
    }

    public float getAccelX()
    {
        return this.accelX;
    }

    public float getAccelY()
    {
        return this.accelY;
    }

    public float getAccelZ()
    {
        return this.accelZ;
    }

    public void onAccuracyChanged(Sensor paramSensor, int paramInt)
    {
    }

    public void onSensorChanged(SensorEvent paramSensorEvent)
    {
        this.accelX = paramSensorEvent.values[0];
        this.accelY = paramSensorEvent.values[1];
        this.accelZ = paramSensorEvent.values[2];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top