指南针 - 跟踪完整360度旋转的数量

pez*_*pez 5 android android-sensors compass-geolocation

假设一个人正在使用这个指南针,从90度开始他们开始顺时针或逆时针旋转.什么是计算完成360度旋转的最佳方法?假设它们只是顺时针旋转,或者只是从头到尾逆时针旋转.

我一直在提出解决方案,如果开始轴承是例如90度,我会在传感器数据发生变化时继续检查下一个轴承,如果它始终朝一个方向移动,我知道它们正在旋转.如果它们继续沿那个方向旋转并使其回到90度,那就算是一个旋转.我的方式似乎非常复杂和低效,我很难想出一个更好的方法.

在这种情况下,我期待多次完整旋转.

我很感激任何帮助.谢谢!

我发现了这个相关的答案,我正在尝试为此编写一个代码示例.如果某人已经做过类似的事情,请发布!

@Override
public void onSensorChanged(SensorEvent event)
{
    switch(event.sensor.getType())
    {
        case Sensor.TYPE_GRAVITY:
        {
            mValuesAccelerometer = lowPass(event.values.clone(), mValuesAccelerometer);
            break;
        }
        case Sensor.TYPE_MAGNETIC_FIELD:
        {
            mValuesMagneticField = lowPass(event.values.clone(), mValuesMagneticField);
            break;
        }
    }

    boolean success = SensorManager.getRotationMatrix(
            mMatrixR,
            mMatrixI,
            mValuesAccelerometer,
            mValuesMagneticField);

    if (success)
    {
        SensorManager.getOrientation(mMatrixR, mMatrixValues);

        float azimuth = toDegrees(mMatrixValues[0]);
        float pitch = toDegrees(mMatrixValues[1]);
        float roll = toDegrees(mMatrixValues[2]);

        if (azimuth < 0.0d)
        {
            //The bearing in degrees
            azimuth += 360.0d;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 1

如果您确定它们只会朝 1 个方向移动,为了优化您的代码,您可以设置度数检查点,而不是持续监控它们是否仍在朝正确方向移动。

这是一个粗略的算法来做到这一点

//You noted 90 degree as starting point
// checkpoint 1 will be 180 keep it as a boolean 
// now you've reached 180 if the meter gets to 180 before going to next checkpoint 
// which is 270 then make 180 false. it means they turned back. 
// if they make it to 270 then wait for 0 degrees and do the same. 
// if they make it back to 90 like that. You got a rotation and hopefully 
// a bit of complexity is reduced as you're just checking for 4 checkpoints 
Run Code Online (Sandbox Code Playgroud)

我目前手边没有任何代码。