使用iOS设备核心动作计算精益角度

时间:2015-10-21 08:24:42

标签: c# ios iphone xamarin core-motion

我正在尝试计算iOS设备的倾斜角度/倾斜度。 我做了很多研究,并找到了一种如何给我最精确的倾斜角度/倾斜度的方法。我使用四元数来计算它。

这是我用来计算它的代码。

public void CalculateLeanAngle ()
    {           
        motionManager = new CMMotionManager ();
        motionManager.DeviceMotionUpdateInterval = 0.02;  // 50 Hz

        if (motionManager.DeviceMotionAvailable) {
            motionManager.StartDeviceMotionUpdates (CMAttitudeReferenceFrame.XArbitraryZVertical, NSOperationQueue.CurrentQueue, (data, error) => {

                CMQuaternion quat = motionManager.DeviceMotion.Attitude.Quaternion;
                double x = quat.x;
                double y = quat.y;
                double w = quat.w;
                double z = quat.z;
                double degrees = 0.0;

                //Roll
                double roll = Math.Atan2 (2 * y * w - 2 * x * z, 1 - 2 * y * y - 2 * z * z);                        
                Console.WriteLine("Roll: " + Math.Round(-roll * 180.0/Constants.M_PI));
                degrees = Math.Round (-applyKalmanFiltering (roll) * 180.0 / Constants.M_PI);

                string degreeStr = string.Concat (degrees.ToString (), "°");
                this.LeanAngleLbl.Text = degreeStr;
        });
    }

public double applyKalmanFiltering (double yaw)
    {
        // kalman filtering
        if (motionLastYaw == 0) {
            motionLastYaw = yaw;
        }

        float q = 0.1f;   // process noise
        float r = 0.1f;   // sensor noise
        float p = 0.1f;   // estimated error
        float k = 0.5f;   // kalman filter gain

        double x = motionLastYaw;
        p = p + q;
        k = p / (p + r);
        x = x + k * (yaw - x);
        p = (1 - k) * p;
        motionLastYaw = x;

        return motionLastYaw;
    }

当您走路和倾斜设备时,此功能非常完美。但是当我驾驶我的汽车时,发生的事情是四元数没有给我正确的倾斜角度/倾斜度。它只给我180°..或突然它显示我一个完全错误的倾斜角度/倾斜..就像当我静止不动(在交通灯处)它显示我23°...然后在开车后它再次工作或它再次显示180°。

四元数是否受到我车加速的影响?那么,因为我的车以一定的速度行驶,所以没有给我正确的值?

有没有人对此有任何解决方案? 我想开自行车/车的时候想计算我的倾斜角度/倾斜度。因此,如果我驾驶汽车/自行车,我真的想知道如何调整正确的倾斜角度/倾斜度。

提前致谢!

0 个答案:

没有答案