如何使 Android 中的方位角和度数不那么不稳定
How to make azimuth and degree less shaky in Android
首先我使用了 TYPE_ORIENTATION
,但这个已经贬值了。因此,我将代码更改为更新的版本,并按照 this post 中的建议使用了 TYPE_ACCELEROMETER
和 TYPE_MAGNETIC_FIELD
。
// Initialize android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
然后在onResume()中,我使用了这个:
// For the system's orientation sensor registered listeners
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
及以下我使用的:
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimuthInRadians = orientation[0];
float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadians);
if (azimuthInDegrees < 0.0f) {
azimuthInDegrees += 360f;
}
float degree = Math.round(azimuthInDegrees);
directionHeading.setText("Heading: " + degree + " degrees");
// Create rotation animation
RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// How long the animation will take place
ra.setDuration(210);
// Set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
}
}
老方法很精确,一点也不抖。但是现在,以不贬值的方式,它震动很大。为什么会这样,我该如何解决抖动问题?
解决方案是应用 (LPF) 低通滤波器。它很顺利,现在工作得很好。
我在这里找到了解决方案:Low Pass Filter
首先我使用了 TYPE_ORIENTATION
,但这个已经贬值了。因此,我将代码更改为更新的版本,并按照 this post 中的建议使用了 TYPE_ACCELEROMETER
和 TYPE_MAGNETIC_FIELD
。
// Initialize android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
然后在onResume()中,我使用了这个:
// For the system's orientation sensor registered listeners
mSensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_UI);
mSensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_UI);
及以下我使用的:
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}
float[] mGravity;
float[] mGeomagnetic;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
mGravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
mGeomagnetic = event.values;
if (mGravity != null && mGeomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic);
if (success) {
float orientation[] = new float[3];
SensorManager.getOrientation(R, orientation);
float azimuthInRadians = orientation[0];
float azimuthInDegrees = (float) Math.toDegrees(azimuthInRadians);
if (azimuthInDegrees < 0.0f) {
azimuthInDegrees += 360f;
}
float degree = Math.round(azimuthInDegrees);
directionHeading.setText("Heading: " + degree + " degrees");
// Create rotation animation
RotateAnimation ra = new RotateAnimation(currentDegree, -degree,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// How long the animation will take place
ra.setDuration(210);
// Set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
}
}
老方法很精确,一点也不抖。但是现在,以不贬值的方式,它震动很大。为什么会这样,我该如何解决抖动问题?
解决方案是应用 (LPF) 低通滤波器。它很顺利,现在工作得很好。 我在这里找到了解决方案:Low Pass Filter