0

我正在尝试从 Android 方向获取倾斜天使。

我从中得到的价值观

SensorManager.getOrientation(mRotationMatrix, mValuesOrientation)

不是度数....将手机放在水平面上会返回不正确的值

我尝试使用网上找到的几种方法,但没有任何运气..

到目前为止尝试过

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

Math.toDegrees(mValuesOrientation[0]);

和别的

代码如下

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;
}

}

4

1 回答 1

2

getOrientation 以弧度返回方位角、俯仰角和滚动角。

mValuesOrientation[0] 是方位角,围绕 z 轴旋转。
mValuesOrientation[1] 是俯仰,围绕 x 轴的旋转。
mValuesOrientation[2] 是滚动,围绕 y 轴旋转。

如果您的手机平放在桌子上,俯仰和横滚应该几乎为 0,但方位角不是。如果您的手机尺寸较长,即 y 轴正好指向磁北,则它仅为 0。

您可以使用俯仰或滚动(取决于设备方向)来计算手机的倾斜度,即屏幕表面与世界 xy 平面之间的角度,或者您可以使用我在如何测量倾斜度中的答案的倾斜度手机在 XY 平面上使用 Android 中的加速度计

您还应该阅读我在将磁场 X、Y、Z 值从设备转换为全局参考系中的答案,以更好地理解旋转矩阵。

于 2013-03-11T18:01:04.440 回答