Android:指南针太不精确?

发布于 2024-10-06 05:29:10 字数 930 浏览 4 评论 0原文

我有一个应用程序,我想在其中访问磁力计。我从我的应用程序中得到结果,但与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光无声 2024-10-13 05:29:10

您必须使用Math.toDegrees。因为 value[0] 返回弧度。

编辑
我在其他帖子上看到,有人说 value[0] 返回弧度。
但是,在我尝试对其进行编码后,我不知道 value[0] 返回弧度或等级/gon。因为当我转换为学位时..该值大于360。所以我尝试将其转换为等级。而且看起来不错,没有问题。代码是这样的,

Double azimuth = value[0];
azimuth = azimuth * 360 / 400;

抱歉英语不好:)

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

Double azimuth = value[0];
azimuth = azimuth * 360 / 400;

sorry for bad english :)

暖伴 2024-10-13 05:29:10

我不确定卡尔曼滤波器对于方向传感器值是否有效,但这正是您可能需要的。 此处提到了有关 Android 卡尔曼滤波器的更多信息

如果您不想经历麻烦, 学习和实现卡尔曼滤波器,请使用我下面所做的。它对我来说效果相当好。 :

private boolean isMagneticFieldFine(float[] fieldValues){
    double value = Math.sqrt(fieldValues[0]*fieldValues[0] + fieldValues[1]*fieldValues[1] + fieldValues[2]*fieldValues[2]);        
    return value < 65.0 && value > 25.0;
}

另一件事,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. :

private boolean isMagneticFieldFine(float[] fieldValues){
    double value = Math.sqrt(fieldValues[0]*fieldValues[0] + fieldValues[1]*fieldValues[1] + fieldValues[2]*fieldValues[2]);        
    return value < 65.0 && value > 25.0;
}

Another thing, SensorListener is deprecated, try using SensorEventListener instead !

つ可否回来 2024-10-13 05:29:10

校准方向的方法有很多,比如使用其他传感器数据来校准(例如陀螺仪和加速度计),或者使用统计模型(例如粒子滤波器和卡尔曼滤波器)。本演示介绍了行业中用于校准运动传感器的一些最新技术: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文