Question about the rotation of X axis on Android

℡╲_俬逩灬. 提交于 2019-12-11 06:07:19

问题


I want to detect the correct rotations around X axis with Android sensors. After googling, I find this code:

   public void onSensorChanged(SensorEvent event) {
   Sensor sensor = event.sensor;
   switch(sensor.getType()) {
   case Sensor.TYPE_ACCELEROMETER:
    mAcc = event.values.clone();
    break;
   case Sensor.TYPE_MAGNETIC_FIELD:
    mMag = event.values.clone();
    break;
   }
   if (mAcc == null || mMag == null) return;

   float R[] = new float[9];
   if (SensorManager.getRotationMatrix(R, null, mAcc, mMag)) {
    SensorManager.getOrientation(R, mOrientation);
   }
  }

mOrientation[1] represents the radians around the X axis. However, the value is very odd.

  1. When the phone lies flat top up on the table, it's 0.
  2. When the head of the phone pointing to the ground, it's PI/2.
  3. When the phone lies flat bottom up on the table, it's 0 again.
  4. When the head of the phone pointing to the sky, it -PI/2.

The states between 1,2 have the same rotation values of those between 2,3. How could I tell which state my phone is in?


回答1:


Please verify your readings.

The signs & range you report are completely out of sync with
what is expected on an android device.

developer.android.com/reference/android/hardware/Sensor.html#TYPE_ORIENTATION

Note: This sensor type exists for legacy reasons, please use getRotationMatrix() in conjunction with remapCoordinateSystem() and getOrientation() to compute these values instead.

Regards
CVS@2600Hertz



来源:https://stackoverflow.com/questions/3282401/question-about-the-rotation-of-x-axis-on-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!