罗盘和陀螺仪的传感器融合:0到360度之间

时间:2012-01-29 03:07:32

标签: ios sensor gyroscope fusion magnetometer

我正在开发一个小型室内导航应用程序,其中我使用陀螺仪和指南针进行设备定位。我使用陀螺仪来平滑罗盘数据。我的传感器融合如下所示。这是我的motionHandler,一切都在发生。

// Listen to events from the motionManager
motionHandler = ^ (CMDeviceMotion *motion, NSError *error) {

        __block float heading;
        heading = mHeading;

        CMAttitude *currentAttitude = motion.attitude;

        //Initial heading setting
        if (lastHeading == 0 && heading != 0) {
            updatedHeading = heading;
        }
        lastHeading = heading;

        if (oldQuaternion.w != 0 || oldQuaternion.x != 0 || oldQuaternion.y != 0 || oldQuaternion.z != 0){
            diffQuaternion = [self multiplyQuaternions:[self inverseQuaternion:oldQuaternion] :currentAttitude.quaternion];
            diffQuaternion = [self normalizeQuaternion:diffQuaternion];
        }            
        oldQuaternion = currentAttitude.quaternion;

        diffYaw = RADIANS_TO_DEGREES([self yawFromQuaternion:diffQuaternion]);         

        quaternion = currentAttitude.quaternion;

        //Get Pitch
        rpy.pitch = -[self pitchFromQuaternion:quaternion];
        rpy.pitch += M_PI/2;                        

        //Use Yaw-Difference for Heading
        updatedHeading = updatedHeading - diffYaw;

        //Heading has to be between 0 and 360 degrees
        if (updatedHeading < 0) {
            updatedHeading = 360 + updatedHeading;
        }
        else if (updatedHeading > 360) {
            updatedHeading -= 360;
        }

        //fusionate gyro estimated heading with new magneticHeading
        updatedHeading = (19.0*updatedHeading + 1.0*heading)/20.0;

        //generate queternion
        rotation = [self createFromAxisAngle:0 :rpy.pitch :DEGREES_TO_RADIANS(updatedHeading)];
    };

实际的传感器融合公式是这一行:updatedHeading = (19.0*updatedHeading + 1.0*heading)/20.0;。 这是我的didUpdateHeading-function,它接收最新的标题信息:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
    // Get new heading
    mHeading = newHeading.magneticHeading;    

    mHeading += 90;

    if (mHeading > 360) {
        mHeading -= 360;
    }
}

diffYaw是陀螺仪计算的航向的变化。 rotation是最终的四元数。 除了一个特殊情况外,这种方法很完美:在0到360度之间的过渡时间。

如果updatedHeading接近但小于360且mHeading刚好超过0,则结果会以圆圈形式移动。例如,如果updatedHeading = 355且mHeading = 5,则正确的结果应介于360和5之间。但我的公式计算337,5度,这显然是完全错误的!

对于这个问题,必须有任何常见的解决方法,我认为......

1 个答案:

答案 0 :(得分:1)

我通常会在这些类型的角度计算中执行以下操作:

updatedHeading -= angleDiff(updatedHeading, mHeading) * 0.05;

其中angleDiff()是:

double angleDiff( double angle1, double angle2 )
{
    double angle = angle1 - angle2;
    if( angle > 180 ) {
        angle -= 360;
    } else if( angle <= -180 ) {
        angle += 360;
    }
    return angle;
}

您可能希望在此之后通过以下方式在0-360范围内恢复更新:

updatedHeading = fmod( updatedHeading + 360, 360 );

你的例子给出355.5这个计算。