开发者

Question about the rotation of X axis on Android

开发者 https://www.devze.com 2023-01-08 10:37 出处:网络
I want to detect the correct rotations around X axis with Android sensors. After googling, I find this code:

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消