Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get tilt angle from the android accelerometer

I have a class that implements SensorEventListener and I would like to get the tilt Angle of my device using the Accelerometer.

I looked for examples on the internet but they use Sensor.TYPE_MAGNETIC_FIELD.

I believe my device doesn't have this sensor because when I do the following check
manager.getSensorList(Sensor.TYPE_ACCELEROMETER).size(), I get zero.

Is there a way to get the tilt Angle by just using Sensor.TYPE_ACCELEROMETER values?

like image 287
user1615888 Avatar asked Aug 22 '12 04:08

user1615888


People also ask

How do you find the tilt angle of an accelerometer?

Measuring Tilt Angle using One Axis 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.

Can an accelerometer measure tilt?

In some applications, where the net acceleration or force on a system over time is gravity, an accelerometer can be used to measure the static angle of tilt or inclination.

How do I access the accelerometer on my phone?

To use the accelerometer (or any sensor in general) your class should implement the SensorEventListener interface, or you could do anonymous inner classes for them. To access the accelerometer you will need to get the SystemManager from the system and get a sensors list from that. sensors = myManager.

What is Android accelerometer?

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.


3 Answers

As people suggested you can use the accelerometer and magnetic sensor to do this.

Here is a blog post with a complete solution.

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

like image 146
Gurinder Singh Avatar answered Sep 18 '22 09:09

Gurinder Singh


Try this,

SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);        

        final SensorEventListener mEventListener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }


            public void onSensorChanged(SensorEvent event) {
                // TODO Auto-generated method stub
                switch (event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                    break;

                case Sensor.TYPE_MAGNETIC_FIELD:
                    System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                    break;
                }
            };
        };

        setListners(sensorManager, mEventListener);

SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
                SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
                final CharSequence test;
                test = ","+mValuesOrientation[0] +","+mValuesOrientation[1]+ ","+ mValuesOrientation[2];
like image 36
AndroidCrazy Avatar answered Sep 22 '22 09:09

AndroidCrazy


You can use the accelerometer to get a tilt reading. If you set up an accelerometer you will notice it includes the force of gravity. So if you phone is face-up on a table the z-axis will register somewhere close to 9.81 (the force of gravity) and the x and y axes will be at 0. As you tilt the phone the force of gravity will be projected onto the x and/or y axis. Thus you the x and y values will tell you the tilt of the phone.

like image 35
sam Avatar answered Sep 21 '22 09:09

sam