【发布时间】:2015-04-23 14:56:53
【问题描述】:
我正在尝试仅使用加速度计和磁力计传感器在 Android 手机上获取用户航向的值。我一直在寻找许多其他解决方案,但是在尝试实现自己的代码时遇到了一些问题。
代码如下:
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD){
Log.d("MAG_FIELD", "ITS A MAG FIELD");
//aX, aY, aZ are raw accelerometer values
float aTheta = (float) Math.atan2(-aX, aZ);
float aPhi = (float) Math.atan2(-aY, aZ);
//Obtaining the current magnetometer readings
float mDegX = event.values[0];
float mDegY = event.values[1];
float mDegZ = event.values[2];
float rotationVectorX = (float) ((mDegX * Math.cos(aPhi)) + (mDegY *Math.sin(aTheta) * Math.sin(aPhi)) - (mDegZ * Math.cos(aTheta) * Math.sin(aPhi)));
float rotationVectorY = (float) ((mDegY * Math.cos(aTheta)) + (mDegZ * Math.sin(aTheta)));
//HEADING calculation
float heading = (float) Math.abs(Math.toDegrees(Math.atan(rotationVectorY /rotationVectorX)));
if(rotationVectorX >= 0.0f && rotationVectorY >=0.0f) {}
else if(rotationVectorX < 0.0f && rotationVectorY >= 0.0f) {heading = 180 - heading ;}
else if(rotationVectorX < 0.0f && rotationVectorY < 0.0f) {heading = 180 + heading;}
else if(rotationVectorX >= 0.0f && rotationVectorY < 0.0f){heading = 360 - heading;}
我的问题是:
- 当设备时,航向总是偏离大约 90 度 平坦
- 当屏幕从平面上改变时,标题会改变 - 我需要它是相同的
【问题讨论】: