美文网首页
iOS磁力传感器极简教程

iOS磁力传感器极简教程

作者: lydsnm木鱼凡心 | 来源:发表于2019-10-26 10:20 被阅读0次

    iOS磁力传感器.png

    有三种获取磁力计数据的方法,如上图所示

    1. Core Motion框架 CMMotionManagersCMMagnetometer

    2. Core Motion框架 CMDeviceMotionCMCalibratedMagneticField属性

    3. Core Location框架 CLLocationManagerCLHeading

    1.CMMagnetometer

    来自磁力计的原始读数

     *  magneticField
     *  
     *  Discussion:
     *    Returns the magnetic field measured by the magnetometer. Note
     *        that this is the total magnetic field observed by the device which
     *        is equal to the Earth's geomagnetic field plus bias introduced
     *        from the device itself and its surroundings.
     */
    @property(readonly, nonatomic) CMMagneticField magneticField; 
    

    2.CMDeviceMotion(CMCalibratedMagneticField *)

    磁场磁场 计读数针对器件偏差(板载磁场)进行了校正,没有器件偏差。

    /*
     *  magneticField
     *  
     *  Discussion:
     *          Returns the magnetic field vector with respect to the device for devices with a magnetometer.
     *          Note that this is the total magnetic field in the device's vicinity without device
     *          bias (Earth's magnetic field plus surrounding fields, without device bias),
     *          unlike CMMagnetometerData magneticField.
     */
    @property(readonly, nonatomic) CMCalibratedMagneticField magneticField NS_AVAILABLE(NA,5_0);
    

    CMCalibratedMagneticField包括两个字段:field(XYZ轴上的磁场强度)和accuracy(磁场长度的精度)。

    3.CLHeading [x | y | z]

    针对器件偏置校正磁性计读数并过滤以消除局部外部磁场(通过器件移动检测 - 如果器件随器件移动,忽略它;否则测量)

    /*
     *  CLHeading
     *  
     *  Discussion:
     *    Represents a vector pointing to magnetic North constructed from 
     *    axis component values x, y, and z. An accuracy of the heading 
     *    calculation is also provided along with timestamp information.
     *  
     *  x|y|z
     *  Discussion:
     *    Returns a raw value for the geomagnetism measured in the [x|y|z]-axis.
    

    CLHeading是“ 偏离设备跟踪的磁场线”,而magnetometerData.magneticField是设备观察到的总磁场,等于地球的地磁场加上设备本身及其周围的偏置“ 。

    因此,CLHeading会提供经过滤的值,其中“参照系”是现有的地球磁场。而磁力计数据提供未经过滤的值,参考帧是设备。

    例子

    #import <CoreMotion/CoreMotion.h>
    @property (nonatomic, strong) CMMotionManager * motionManager;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        _motionManager = [[CMMotionManager alloc] init];
        
        // 判断设备是否支持磁强计
        if (_motionManager.magnetometerAvailable){
            [_motionManager startMagnetometerUpdates];
            NSLog(@"该设备支持磁强计");
        }else{
            NSLog(@"该设备不支持磁强计");
        }
    
        // 磁强计 开启
        [_motionManager startMagnetometerUpdates];
        [self useMagnetometerPush];
    }
    
    // 使用第二种CMDeviceMotion的CMCalibratedMagneticField属性
    - (void)useMagnetometerPush{
        // 设定获取间隔时间
        _motionManager.magnetometerUpdateInterval = 0.5;//0.1f;
        // 添加线程
        NSOperationQueue * queue = [[NSOperationQueue alloc]init];
            
        [_motionManager startDeviceMotionUpdatesUsingReferenceFrame: CMAttitudeReferenceFrameXArbitraryCorrectedZVertical toQueue:queue withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
            NSLog(@"磁强计X = %.04f 磁强计Y = %.04f 磁强计Z = %.04f   精度:%d",motion.magneticField.field.x,motion.magneticField.field.y,motion.magneticField.field.z, motion.magneticField.accuracy);
        }];
    }
    
    // 通过第一种CMMotionManagers的CMMagnetometer类获取原始数据
    - (void)useMagnetometerPushOriginal{
        _motionManager.magnetometerUpdateInterval = 0.5;//0.1f;
        NSOperationQueue * queue = [[NSOperationQueue alloc]init];
            
        [_motionManager startMagnetometerUpdatesToQueue:queue withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
            NSLog(@"磁强计X = %.04f 磁强计Y = %.04f 磁强计Z = %.04f",magnetometerData.magneticField.x,magnetometerData.magneticField.y,magnetometerData.magneticField.z);
        }];
    }
    
    
    

    最后

    如果你想知道哪里是磁性或真正北方的位置,推荐使用CLHeading,可以做指南针的一类应用。如果想要创建一个响应紧邻设备磁场的应用,或者想要执行某种特定的传感器融合,可以尝试创建一个AHRS,然后使用CMMagneticField即第二种方式。

    相关文章

      网友评论

          本文标题:iOS磁力传感器极简教程

          本文链接:https://www.haomeiwen.com/subject/kmeyvctx.html