指南针和加速度计精度 [英] Compass and Accelerometer precision
问题描述
我在 Android 中制作了自己的应用程序,该应用程序使用指南针和加速计传感器来显示我的设备的旋转度数和倾斜度.我初始化了我需要的所有侦听器和对象(我遵循了一些教程),现在我可以按照自己的意愿获得学位.问题是传感器返回的测量值不准确.我的意思是,即使我试图四舍五入我从传感器捕获的度数值,它们也会每几分之一秒在 -/+ 7(或 8)度之间振荡,即使我留在远离任何干扰源的草地上.我想要的是对度数的准确测量,例如一种对我从传感器接收到的值进行四舍五入的方法.
i made my own application in Android that use compass and accelerometer sensors to display the degrees of rotation and inclination of my device. I initialized all the listener and objects i needed (i followed some tutorials), and now i can catch the degrees as i wished. The problem is that the measures that sensors return aren't accurate. I mean even if i try to round the values of degrees i catch from the sensor, they oscillate between -/+ 7 (or 8) degrees every fraction of a second, even if i stay in a grass field away from any disturbing source. What i want to have is a accurate measure of the degrees, something like a method to round the values i recieve from the sensors.
float[] mags = null;
float[] accels = null;
float[] R = new float[matrix_size];
float[] outR = new float[matrix_size];
float[] I = new float[matrix_size];
float[] values = null;
private void startSensor() {
sensorMan.registerListener(this, sensorMan.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI);
sensorMan.registerListener(this, sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
}
@Override
public void onSensorChanged(SensorEvent event) {
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE) {
return;
}
switch (event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
mags = event.values.clone();
break;
case Sensor.TYPE_ACCELEROMETER:
accels = event.values.clone();
break;
}
if (mags != null && accels != null) {
SensorManager.getRotationMatrix(R, I, accels, mags);
// Correct if screen is in Landscape
SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X,
SensorManager.AXIS_Z, outR);
SensorManager.getOrientation(outR, values);
azimuth = (float) Math.round((Math.toDegrees(values[0]))*7)/7;
azimuth = ( azimuth + 360)%360;
//here is inclination. The problem is just the same with compass
//inclination=-Math.round((float) (values[1]*(360/(2*Math.PI))));
//other code to update my view
//in azimuth i have the degree value. It changes continuously
//even if i aim still the same direction
}
}
推荐答案
在此处查看我的答案:平滑来自传感器的数据
See my answer here: Smoothing data from a sensor
在将加速度计和磁力计事件值传递给 SensorManager.getRotationMatrix()
之前,我在它们上运行此过滤器.我认为这个算法的优点是不必保留大量的历史值,只需保留先前的低通输出数组.
I run this filter on both the accelerometer and magetometer event values before passing them to SensorManager.getRotationMatrix()
. I think this algorithm has the advantage of not having to keep a large array of historic values, just the prior low-pass output array.
该算法源自以下维基百科条目:http://en.wikipedia.org/wiki/Low-pass_filter#Algorithmic_implementation
The algorithm was derived from this Wikipedia entry: http://en.wikipedia.org/wiki/Low-pass_filter#Algorithmic_implementation
这篇关于指南针和加速度计精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!