Search code examples
iosaccelerometercore-motion

Core Motion accelerometer shows strange data


Short question: acceleration from CoreMotion values get rather large drift even device laying on table. Is it ok or what am I doing wrong?

Long question: I am using CoreMotion accelerometer like that, in -init method:

motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 1.0 / 60.0;

then start updating it:

if ([motionManager isDeviceMotionAvailable])
{
    [motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical];
}

and then read values in some update method (invoked by Cocos3d by timer). Also, I've got a static variable accum - accumulator, which add current values of acceleration to it. So accum holds a sum of all acceleration values:

if (motionManager.deviceMotionActive)
{
    CMDeviceMotion *deviceMotion = motionManager.deviceMotion;
    CMAcceleration accel = deviceMotion.userAcceleration;
    static CC3Vector accum = {0,0,0};
    accum.x += accel.x;
    accum.y += accel.y;
    accum.z += accel.z;
    NSLog(@"%f, %f, %f", accum.x, accum.y, accum.z);
}

I lay my iPad 3 on table and acceleration values seemed to be ok by first glance, but it becomes visible that value in accum in one axis start to increase pretty quickly. I know, that removing gravity force from raw data is not 100% accurate, but I did not expected that this is so bad even with iPad laying on table and not moving. So the question is am I doing something wrong or this is how it should be?


Solution

  • The accelerometer is far too noisy to accurately integrate velocity, even if you know the direction and strength of gravity precisely.

    You might be able to neutralise bias somewhat by accumulating an average acceleration vector and subtracting that, but obviously that will cease to work the instant you tilt or accelerate the device.