Not able to fetch correct degree from getOrientati

2019-08-27 19:15发布

I am trying to get the tilt angel from Android orientation.

The values which i am getting from

SensorManager.getOrientation(mRotationMatrix, mValuesOrientation)

is not degrees.... On putting the phone on a horizontal plane it returns improper values

I have tries using few methods found on net without any luck..

So far tried

Math.sin(Math.toRadians(mValuesOrientation[0]));

Math.toDegrees(mValuesOrientation[0]);

and others

Code is below

public class MainActivity extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        

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

    final float[] mValuesMagnet      = new float[3];
    final float[] mValuesAccel       = new float[3];
    final float[] mValuesOrientation = new float[3];
    final float[] mRotationMatrix    = new float[9];

    final Button btn_valider = (Button) findViewById(R.id.button1);
    final TextView txt1 = (TextView) findViewById(R.id.editText1);
    final SensorEventListener mEventListener = new SensorEventListener() {
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        public void onSensorChanged(SensorEvent event) {
            // Handle the events for which we registered
            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;
            }
        };
    };

    // You have set the event lisetner up, now just need to register this with the
    // sensor manager along with the sensor wanted.
    setListners(sensorManager, mEventListener);

    btn_valider.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
            SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
            String test;
           /* double accX = -mValuesOrientation[0]/SensorManager.GRAVITY_EARTH;
            double accY = -mValuesOrientation[1]/SensorManager.GRAVITY_EARTH;
            double accZ = -mValuesOrientation[2]/SensorManager.GRAVITY_EARTH;
            double totAcc = Math.sqrt((accX*accX)+(accY*accY)+(accZ*accZ));
            double tiltX = Math.asin(accX/totAcc);
            double tiltY = Math.asin(accY/totAcc);
            double tiltZ = Math.asin(accZ/totAcc);*/
            //float tiltX = mValuesOrientation[0] * 57.2957795f;
            //float tiltY = mValuesOrientation[1] * 57.2957795f;
            //float tiltZ = mValuesOrientation[2] * 57.2957795f;
            //double tiltX =Math.sin(Math.toRadians(mValuesOrientation[0]));
            //double tiltY =Math.sin(Math.toRadians(mValuesOrientation[1]));
            //double tiltZ =Math.sin(Math.toRadians(mValuesOrientation[2]));
            //String.format("Azimuth: %.2f\n\nPitch:%.2f\nRoll", azimuth,
            //        pitch, roll);
            double tiltX = Math.toDegrees(mValuesOrientation[0]);
            double tiltY = Math.toDegrees(mValuesOrientation[1]);
            double tiltZ = Math.toDegrees(mValuesOrientation[2]);
            test = "results New: " +tiltX +" "+tiltY+ " "+ tiltZ;
            Log.d("test", test);
            txt1.setText(test);
        }
    });


}
public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
{
    sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
            SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
            SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

1条回答
beautiful°
2楼-- · 2019-08-27 19:50

The getOrientation return the azimuth, pitch and roll in Radian.

mValuesOrientation[0] is the azimuth, rotation about the z-axis.
mValuesOrientation[1] is the pitch, rotation about the x-axis.
mValuesOrientation[2] is the roll, rotation about the y-axis.

If your phone is lying flat on a table, the pitch and roll should be almost 0, but not the azimuth. It is only 0 if your phone longer size, the y axis, is pointing exactly toward magnetic North.

You can use pitch or roll (depending on the device orientation) to calculate the inclination of the phone, ie the angle between the surface of the screen and the world xy plane or you can use the inclination of my answer at How to measure the tilt of the phone in XY plane using accelerometer in Android

You should also read my answer at Convert magnetic field X, Y, Z values from device into global reference frame to get a better understanding of the rotation matrix.

查看更多
登录 后发表回答