我目前正在编写Android AR应用程序,我的方位角计算存在问题,具体取决于初始设备倾斜。
我使用ROTATION_VECTOR传感器获取方位角,海拔高度和倾斜度。
当我启动我的应用程序并且手机与地面垂直时,我有来自北方的良好方位角。当我启动应用程序并且手机与地面平行时,我的值很差。当我改变设备倾斜时,我也有奇怪的值。
这是我的代码:
public void onSensorChanged(SensorEvent event)
{
if (event.sensor.getType() == sensor.TYPE_ROTATION_VECTOR)
{
SensorManager.getRotationMatrixFromVector(rotationVectorMatrix, event.values);
SensorManager.remapCoordinateSystem(rotationVectorMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotationMatrix);
SensorManager.getOrientation(rotationMatrix, orientation);
...
}
}
答案 0 :(得分:1)
要获得方位角,您可以使用加速度计和磁场传感器。您可以使用以下代码记录方位角值。
public class MainActivity extends AppCompatActivity {
private int mAzimuth = 0; // degree
private SensorManager mSensorManager = null;
private Sensor mAccelerometer;
private Sensor mMagnetometer;
boolean haveAccelerometer = false;
boolean haveMagnetometer = false;
@Override
protected void onCreate( Bundle savedInstanceState ) {
super.onCreate(savedInstanceState);
mSensorManager = (SensorManager) getSystemService(Service.SENSOR_SERVICE);
this.mAccelerometer = this.mSensorManager.getDefaultSensor( Sensor.TYPE_ACCELEROMETER );
this.haveAccelerometer = this.mSensorManager.registerListener( mSensorEventListener, this.mAccelerometer, SensorManager.SENSOR_DELAY_GAME );
this.mMagnetometer = this.mSensorManager.getDefaultSensor( Sensor.TYPE_MAGNETIC_FIELD );
this.haveMagnetometer = this.mSensorManager.registerListener( mSensorEventListener, this.mMagnetometer, SensorManager.SENSOR_DELAY_GAME );
if ( haveAccelerometer && haveMagnetometer ) {
// ready to go
} else {
// unregister and stop
}
}
private SensorEventListener mSensorEventListener = new SensorEventListener() {
float[] gData = new float[3]; // accelerometer
float[] mData = new float[3]; // magnetometer
float[] rMat = new float[9];
float[] iMat = new float[9];
float[] orientation = new float[3];
public void onAccuracyChanged(Sensor sensor, int accuracy ) {}
@Override
public void onSensorChanged( SensorEvent event ) {
float[] data;
switch ( event.sensor.getType() ) {
case Sensor.TYPE_ACCELEROMETER:
gData = event.values.clone();
break;
case Sensor.TYPE_MAGNETIC_FIELD:
mData = event.values.clone();
break;
default: return;
}
if ( SensorManager.getRotationMatrix( rMat, iMat, gData, mData ) ) {
mAzimuth= (int) ( Math.toDegrees( SensorManager.getOrientation( rMat, orientation )[0] ) + 360 ) % 360;
Log.d("AzimuthTag", "Azimuth:"+mAzimuth);
}
}
};
}