CMCalibratedMagneticField doesn't update

2019-07-08 14:01发布

问题:

CMCalibratedMagneticField returns 0 all the time.

CMAcceleration returns values, CMMagnetometerData returns values too. Only CMCalibratedMagneticField doesn't, I need it because it is the magneticfield without bias.

I'm testing on iPhone 4S.

[MotionManager startDeviceMotionUpdatesToQueue:deviceQueue
                                   withHandler:^(CMDeviceMotion *motion, NSError *error) {
        CMCalibratedMagneticField f = motion.magneticField;

        //CMMagneticField t = motion.;
        //CMAcceleration s = motion.gravity; Works!
        //_xlabelnew.text = [NSString stringWithFormat:@"%f", s.x];

        _xlabelnew.text = [NSString stringWithFormat:@"%f", f.field.x];
        _ylabelnew.text = [NSString stringWithFormat:@"%f", f.field.y];
        _zlabelnew.text = [NSString stringWithFormat:@"%f", f.field.z];

        //_totallabelnew.text = [NSString stringWithFormat:@"%f", sqrt(f.field.x*f.field.x + f.field.y*f.field.y + f.field.z*f.field.z)];

    }];

回答1:

Have you checked the magnetometer accuracy? My guess is your value for motion.magneticField.accuracy will be -1 (uncalibrated). Unfortunately I have no idea why this is. I'm getting the same thing with my iPhone 4, even though the magnetometer is working just fine.

The only thing I can figure is that the phone was never calibrated before leaving the factory, but that makes more sense for my 4 than for your 4S. The calibrated field feature was new for iOS 5.0 which was publicly released in October 2011, where as the iPhone 4 was released over a year before. But your phone came out right along with 5.0, so one would think that the hardware being released would match the software capability it was supporting. Again, just speculation. Sorry I can't be more help. Hopefully you'll find a real answer. I just plan on calibrating the magnetometer myself.

Freescale provides a free tool for doing this. The software (and some useful documentation) is available here. You might have to create a free account to download it, but I can't remember.

http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=eCompass

UPDATE: I got a hold of an iPhone 5 and the CMCalibratedMagneticField worked just fine.



回答2:

[MotionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryCorrectedZVertical toQueue:deviceQueue
                               withHandler:^(CMDeviceMotion *motion, NSError *error) {
    CMCalibratedMagneticField f = motion.magneticField;

    //CMMagneticField t = motion.;
    //CMAcceleration s = motion.gravity; Works!
    //_xlabelnew.text = [NSString stringWithFormat:@"%f", s.x];

    _xlabelnew.text = [NSString stringWithFormat:@"%f", f.field.x];
    _ylabelnew.text = [NSString stringWithFormat:@"%f", f.field.y];
    _zlabelnew.text = [NSString stringWithFormat:@"%f", f.field.z];

    //_totallabelnew.text = [NSString stringWithFormat:@"%f", sqrt(f.field.x*f.field.x + f.field.y*f.field.y + f.field.z*f.field.z)];

}];