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.
- When the phone lies flat top up on the table, it's 0.
- When the head of the phone pointing to the ground, it's PI/2.
- When the phone lies flat bottom up on the table, it's 0 again.
- 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
精彩评论