Android:指南针太不精确?
我有一个应用程序,我想在其中访问磁力计。我从我的应用程序中得到结果,但与 g1 指南针应用程序的方向结果相比,我的应用程序得到的结果与指南针应用程序完全不同。例如:我的应用程序显示 250°,而指南针应用程序显示 90°! 那是不可能的,这是我的代码。为了获得更好的结果,有什么区别吗?
public class MySensorListener implements SensorListener{
int orientation;
public MySensorListener(){
orientation = 0;
}
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(int sensor, float[] values) {
orientation = (int)values[0];
}
}
我像这样访问 Sensormanager:
private MySensorListener doCompass(){
MySensorListener cl;
SensorManager cm;
compassListener = new MySensorListener();
cm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
cm.registerListener(
cl,
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_UI); //updateRate
return cl;
}
任何人都可以帮忙吗?! :)
谢谢
I have an app where I want to access the magnotometer. I get results out of my application, but compared with orientation results from the compass app of g1 I get completely different results with my app than the compass app. e.g.: my app sais 250° and the compass app 90°!
That just can't be, here is my code. Is there any difference to become better results?
public class MySensorListener implements SensorListener{
int orientation;
public MySensorListener(){
orientation = 0;
}
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(int sensor, float[] values) {
orientation = (int)values[0];
}
}
I acess the Sensormanager like this:
private MySensorListener doCompass(){
MySensorListener cl;
SensorManager cm;
compassListener = new MySensorListener();
cm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
cm.registerListener(
cl,
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_UI); //updateRate
return cl;
}
Can anyone help, pleeease?! :)
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须使用Math.toDegrees。因为 value[0] 返回弧度。
编辑
我在其他帖子上看到,有人说 value[0] 返回弧度。
但是,在我尝试对其进行编码后,我不知道 value[0] 返回弧度或等级/gon。因为当我转换为学位时..该值大于360。所以我尝试将其转换为等级。而且看起来不错,没有问题。代码是这样的,
抱歉英语不好:)
you must use
Math.toDegrees
. because value[0] return radian.edit
i see on other post, some people says that value[0] return radian.
but, after i try to code it, i don't know value[0] return radian or grade/gon. because when i convert into degree.. the value is bigger than 360. so i try to convert it into grade. and it look good and don't have problem. the code is like this
sorry for bad english :)
我不确定卡尔曼滤波器对于方向传感器值是否有效,但这正是您可能需要的。 此处提到了有关 Android 卡尔曼滤波器的更多信息
如果您不想经历麻烦, 学习和实现卡尔曼滤波器,请使用我下面所做的。它对我来说效果相当好。 :
另一件事,SensorListener 已被弃用,请尝试使用 SensorEventListener 代替!
I am not sure whether Kalman filters is valid for Orientation Sensor values, but that is what you might need. More about Kalman filters for android is mentioned here
If you don't want to go through the headache of studying and implementing Kalman filter, use what I did below.It worked reasonably well for me. :
Another thing, SensorListener is deprecated, try using SensorEventListener instead !
校准方向的方法有很多,比如使用其他传感器数据来校准(例如陀螺仪和加速度计),或者使用统计模型(例如粒子滤波器和卡尔曼滤波器)。本演示介绍了行业中用于校准运动传感器的一些最新技术:https://www.youtube。 com/watch?v=C7JQ7Rpwn2k
There are many ways to calibrate directions, such as using other sensors data to calibrate (e.g. gyroscope and accelerometer), or use statistical models (e.g. particle filter and Kalman Filter). This presentation talks about some latest technologies used in industry to calibrate movement sensors: https://www.youtube.com/watch?v=C7JQ7Rpwn2k