2
我查了各地關於如何使用設備的硬件(羅盤,加速度計)使用加速度計和獲得穩定的方位
下面的指南是驚人的旋轉3D對象: http://ibuzzlog.blogspot.co.uk/2012/08/how-to-use-android-sensors.html?showComment=1409868331799#c665801926070707020
然而方位在多種設備上是令人難以置信的跳躍。 (想象一下這是綁在你的臉,看着左和右)
@Override
public void onSensorChanged(SensorEvent event) {
int type = event.sensor.getType();
//Log.i("TAG", "Sensor " + type);
if(event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
{
MagneticFieldValues_last[0] = event.values[0];
MagneticFieldValues_last[1] = event.values[1];
MagneticFieldValues_last[2] = event.values[2];
bHaveMagneticField = true;
}
if(event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
{
AccelerometerValues_last[0] = event.values[0];
AccelerometerValues_last[1] = event.values[1];
AccelerometerValues_last[2] = event.values[2];
bHaveAccelerometer = true;
}
if(bHaveMagneticField && bHaveAccelerometer)
{
if(SensorManager.getRotationMatrix(R, null, AccelerometerValues_last, MagneticFieldValues_last))
{
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X, remapR);
SensorManager.getOrientation(remapR, orientationValues);
Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0);
pitch2 = (float) (-Math.atan2(orientationVector[1], orientationVector[2]) * RADIANS_TO_DEGREES);
Matrix.multiplyMV(orientationVector, 0, remapR, 0, sZVector, 0);
orientation = (float) (-Math.atan2(orientationVector[0], orientationVector[1]) * RADIANS_TO_DEGREES);
Matrix.invertM(remapR_inv, 0, remapR, 0);
Matrix.multiplyMV(azimuthVector, 0, remapR_inv, 0, sZVector, 0);
azimuth = (float) (180 + Math.atan2(azimuthVector[0], azimuthVector[1]) * RADIANS_TO_DEGREES);
}
}
}
UPDATE
發現這一點,似乎這樣的伎倆 http://blog.thomnichols.org/2011/08/smoothing-sensor-data-with-a-low-pass-filter
更新2
可悲的是,它並沒有做到這一點,仍然需要一種平滑價值的方法。其他人如何做到這一點?