Android的指南针似乎不可靠, [英] android compass seems unreliable

查看:436
本文介绍了Android的指南针似乎不可靠,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在一个小指南针的应用程序在过去的几天,并且已经得到了code运行起来,但似乎罗盘读数不准确。校准这两款手机之后,我,我发现这是什么,我只是把手机反对,平坦的表面上的第一个测试看了看读数,然后翻转它水平,并把它针对同一平面上(180 *转),和值的确不改变180 *这是接近240 *。

I have been working on a small compass app the past couple of days, and have gotten the code up and running, but it seems that the compass reading is not accurate. After calibrating both phones, my first test that i found this in what that i simply held the phone against and flat surface looked at the reading then flipped it horizontally and put it against the same flat surface (a 180* turn) and the value did not change 180* it was closer to 240*.

然后我测试了雷丁VS指南针,有时读数似乎接近,但在其他方面也已超过50 *关闭。我甚至试图把我的手机和指南针在地板上,保持罗盘远离具有相同的结果(注意:我也排队了一本书边保持罗盘和手机除了让他们在同一方向)的任何磁场干扰

I then tested the reading vs a compass, at times the reading seemed to be close but at other points it was more than 50* off. I even tried to put my phone and compass on the floor to keep the compasses away from any magnetic interference with the same results (note i also keep the compass and phone apart keeping them in the same direction by lining up with edges of a book).

我然后把另一部电话的示例应用程序(第一次是Nexus S的,其次是摩托罗拉droid 1)。这两款手机被等同于一些点,但顶多点之间的50度和15度关是区别范围之间。

I then put the sample application on another phone (first was nexus S, second was Motorola droid 1). Between the two phones the difference ranges from being equal at some points but at most points being between 50 and 15 degrees off.

我已经通过文件看,也期待通过许多不同的论坛帖子,我没有看到任何人都具有相同的结果。有可能是在我的code一些小错误导致我的阅读错误地走出来,或者说我没有看到一些记录错误。

I have looked through the documentation and also looked through many different forum posts and I do not see anyone with the same results. There may be some small error in my code that is causing my reading to come out incorrectly, or some documented bug that I am not seeing.

任何有识之士或者建议,将不胜AP preciated !!

Any insight or suggestions would be greatly appreciated!!

这是我的传感器改变了code在我SensorEventListener类

Heres my on sensor changed code in my SensorEventListener class

public void onSensorChanged(SensorEvent event)
    {
        // If the sensor data is unreliable return
        if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
        {
            Toast.makeText(main.this, "Sensor Status Unreliable",Toast.LENGTH_SHORT).show();
        }





        // Gets the value of the sensor that has been changed
        switch (event.sensor.getType())
        {
        case Sensor.TYPE_ACCELEROMETER:
            m_vfgravity = event.values.clone();
            break;
        case Sensor.TYPE_MAGNETIC_FIELD:
            m_vfgeomag = event.values.clone();
            break;
        }

        if (m_vfgravity != null && m_vfgeomag != null)
        {
            if(SensorManager.getRotationMatrix(m_vfinR, m_vfI, m_vfgravity, m_vfgeomag))
            {
                SensorManager.getOrientation(m_vfinR, m_vforientVals);

                m_fCompBearing = (float) Math.round((Math.toDegrees(m_vforientVals[0])) *2)/2;

                //convert to 0-360 from -180-180
                if(m_fCompBearing < 0.0)
                {
                    m_fCompBearing = 360 + m_fCompBearing;
                }


                mCompHead.setText("" + (int)m_fCompBearing);
            }

            calcOffset();   
            rotateCmp();
        }
    }

和code在创建我的活动

And code in on create of my activity

    mSMngr = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSListener = new cSensorListener();

    mSMngr.registerListener(mSListener,
            mSMngr.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
            SensorManager.SENSOR_DELAY_UI);
    mSMngr.registerListener(mSListener,
            mSMngr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_UI);

在此先感谢!

Thanks in advance!

编辑:也试过跟在Droid X与最坏的结果却......当手机45度左右推出(围绕Z轴旋转的计算机坐标系)的罗盘航向返回能改变超过180度,其实标题值进去的其他手机相反的方向时,在相同的方向上纺丝。这是一个即使在校准设置后,产生这一结果的唯一手机。同时他们是一个指南针动态壁纸我测试的,这并不有同样的问题。所以我会假设会有什么事情我可以在软件中做,以避免这一点。

also tried it with the droid X with the worst results yet... When the phone is rolled around 45 degrees (rotated around z axis a computer coordinate system) the compass heading returned can change over 180 degrees, in fact the heading value goes in the opposite direction of the other phones when spun in the same direction. This is the only phone that produces this result even after calibrating in the settings. Also their is a compass live wallpaper i test against that doesnt have the same issue. So I would assume there would be something i can do in software to avoid this.

推荐答案

在那么多的测试和调试。我得出的结论是肯定的,因为一些你所提到的差异我的机器人1和Nexus S的纯粹是硬件差和磁场干扰。

Well after much testing, and debugging. I came to the conclusion that yes as some of you mentioned the differences my droid 1 and Nexus S were purely hardware difference and magnetic interference.

不过在Droid X是一个不同的问题,无论我尝试我不能得到正确的读数超出了推荐的方式与getRotationMatrix和getOrientation,甚至当添加重地图坐标功能。所以在没有成功,我想通了一些修修补补ID提供了方向感应器方式了一枪。

However the Droid X was a different issue, whatever i tried i could not get the correct readings out of the recommended way with getRotationMatrix and getOrientation, even when added re-map coordinates function. So after some tinkering with no success i figured id give the Orientation sensor way a shot.

谷歌表示,这种方式去precated,他们建议做它的方式,我开始了与,但是我尝试了所有类型的组合与方式,但没有成功。于是我继续忽略thier警告,使用方向传感器...和它的工作。为什么 ?我不知道,在Droid X是比我的机器人1新的操作系统,所以它不应该有做使用传统code。然而,它有一定道理,为什么指南针的应用程序写信给目标1.6会的工作,而我的应用程序做了推荐方法是行不通的。

Google says that this way is deprecated and they recommend doing it the way i started off with, however i tried all types of combinations with that way with no success. So I went ahead and ignored thier warning and used the orientation sensor ... and it worked. Why ? i have no clue, the droid x is newer os than my droid 1 so it shouldn't have to do with using legacy code. However it does make sense why compass apps wrote to target 1.6 would work while my app doing the "recommended way" was not working.

如果任何人有任何更好的方法来做到这一点让我知道,或者如果你知道一种方法,使其与getRotationMatrix和getOrientation工作也做出来。

If anyone has any better way to do this let me know, or if you know a way to make it work with getRotationMatrix and getOrientation also do tell.

否则,对于其他人谁打这个砖墙很难,因为我没有继承人的code,结束了为我工作。

Otherwise for anyone else who hits this brick wall as hard as I did heres the code that ended up working for me.

我的传感器改变了

        switch (event.sensor.getType())
        {
        case Sensor.TYPE_ORIENTATION:
            m_vforientVals = event.values.clone();
            break;
        }

        if(m_vforientVals != null)
        {
            m_fCompBearing = m_vforientVals[0];             


            mCompHead.setText("" + (int)m_fCompBearing);

            calcOffset();   
            rotateCmp();
        }

和初始化传感器监听器

    mSMngr.registerListener(mSListener,mSMngr.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                            SensorManager.SENSOR_DELAY_NORMAL);

这篇关于Android的指南针似乎不可靠,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆