Problem with SensorManager.getOrientation()
n reference pages of android.view.onSensorChanged() the axes of the device are described as
"The X axis refers to the screen's horizontal axis (the small edge in portrait mode, the long edge in landscape mode) and points to the right. The Y axis refers to the screen's vertical axis and points towards the top of the screen (the origin is in the lower-left corner). The Z axis points toward the sky when the device is lying on its back on a table."
and, in android.hardware.SensorManager.getOrientation(), it is mentioned that, the method will return device's azimuth, pitch and roll, which are positive in counter-clockwise direction. But, when i called the function from my code and printing the values, the azimuth and pitch are positive in clock-wise direction, and negative in counter-clockwise direction. The Rotation Matrix which is obtained by android.hardware.SensorManager.getRotationMatrix() is meeting the requirements, if what i claim is correct, i.e the azimuth and pitch are +ve in clock-wise direction and roll is +ve in counter-clockwise direction. Please correct me if i am wrong. I'm using HTC Wildfire S, which runs on Android 2.3.3.
I'm adding the code segment, which i'm using for obtaining the orientation valu开发者_如何转开发es.
sensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor gsensor = sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
Sensor msensor = sensormanager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
sensormanager.registerListener(this, gsensor, SensorManager.SENSOR_DELAY_GAME);
sensormanager.registerListener(this, msensor, SensorManager.SENSOR_DELAY_GAME);
-------------------------------------------------------------------------------
final float rad2deg = (float)(180.0f/Math.PI);
int type = event.sensor.getType();
float[] data;
if (type == Sensor.TYPE_ACCELEROMETER) {
data = gravityValues;
} else if (type == Sensor.TYPE_MAGNETIC_FIELD) {
data = geomagneticValues;
} else {
return;
}
for (int i=0 ; i<3 ; i++)
data[i] = event.values[i];
SensorManager.getRotationMatrix(rValues, null, gravityValues, geomagneticValues);
SensorManager.getOrientation(rValues, mOrientation);
result.setText("Compass yaw: " + (int)(mOrientation[0]*rad2deg) + " pitch: " + (int)(mOrientation[1]*rad2deg) +" roll: " + (int)(mOrientation[2]*rad2deg) );
Please let me konw, if i missed mentioning any required information. Thanks in advance.
The documentation is correct, the Z-Axis in the getOrientation call is pointing down toward the earth. Thus counterclockwise is clockwise in normal sense.
精彩评论