Search code examples
ioscore-motion

Core Motion with yaw constantly changing


I've got Core Motion manager in my iOS app:

motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 1.0 / 60.0;
if ([motionManager isDeviceMotionAvailable]) {
    [motionManager startDeviceMotionUpdates];
}

and in update method (I'm using cocos3d, but it does not matter) I've got this:

-(void) updateBeforeTransform:(CC3NodeUpdatingVisitor *)visitor
{
    if (motionManager.deviceMotionActive)
    {
        CMDeviceMotion *deviceMotion = motionManager.deviceMotion;
        CMAttitude *attitude = deviceMotion.attitude;

        NSLog(@"%f, %f, %f", attitude.yaw, attitude.pitch, attitude.roll);

    }
}

I put device on table and start to watch yaw, pitch and roll values, and the yaw is constantly changing! it is changing for about 10 degrees in couple of minutes, and it is absolutely inadmissibly in my app. What is the reason of that changing and how can I avoid it? I started to think that it happens because of Earth rotation, but the speed is too much :)

Thanks in advance!


Solution

  • Let me try a shot in the dark. With iOS 5 magnetometer data is part of the Core Motion sensor fusion algorithm. For many applications like games there is no need or better is's dangerous because of increased energy consumption and the possible need to calibrate the compass forcing the user to do this 8 like motion.

    So I speculate that compass data is considered within sensor fusion only if explicitly stated by using CMMotionManager's startDeviceMotionUpdatesUsingReferenceFrame instead of startDeviceMotionUpdates. Try out CMAttitudeReferenceFrameXMagneticNorthZVertical and check if the drifting effect decreases.