Core Motion

  

  demo:TP

  1. CoreMotion 框架介绍

  随着iPhone 4的推出和M7或者M8处理器的使用,苹果增加一个一个专门负责该方面处理的框架,就是Core Motion Framework。它不仅仅提供给你获得实时的加速度值和旋转速度值,更重要的是,苹果在其中集成了很多算法,可以直接给你输出把重力 加速度分量剥离的加速度,省去你的高通滤波操作,以及提供给你一个专门的设备的三维attitude信息!Core Motion可以让开发者从各个内置传感器那里获取未经修改的传感数据,并观测或响应设备各种运动和角度变化。这些传感器包括陀螺仪、加速器和磁力仪(罗盘)。

  2.  常用的类或者结构介绍

  CoreMotion负责处理四种数据,一种是加速度数据,一种是螺旋仪数据,一种是磁感应数据,还有一种是前三种数据通过复杂运算得到的设备的运动数据。从Core Motion中获取数据主要是两种方式,一种是Push,就是你提供一个线程管理器NSOperationQueue,再提供一个Block(有点像C中 的回调函数),这样,Core Motion自动在每一个采样数据到来的时候回调这个Block,进行处理。在这中情况下,block中的操作会在你自己的主线程内执行。另一种方式叫做 Pull,在这个方式里,你必须主动去像Core Motion Manager要数据,这个数据就是最近一次的采样数据。你不去要,Core Motion Manager就不会给你。当然,在这种情况下,Core Motion所有的操作都在自己的后台线程中进行,不会有任何干扰你当前线程的行为。我们可以进入到CoreMotion.h看到如下:

#import <CoreMotion/CMAccelerometer.h>
#import <CoreMotion/CMAltimeter.h>
#import <CoreMotion/CMAltitude.h>
#import <CoreMotion/CMAttitude.h>
#import <CoreMotion/CMAvailability.h>
#import <CoreMotion/CMDeviceMotion.h>
#import <CoreMotion/CMError.h>
#import <CoreMotion/CMErrorDomain.h>
#import <CoreMotion/CMGyro.h>
#import <CoreMotion/CMLogItem.h>
#import <CoreMotion/CMMagnetometer.h>
#import <CoreMotion/CMMotionActivity.h>
#import <CoreMotion/CMMotionActivityManager.h>
#import <CoreMotion/CMMotionManager.h>
#import <CoreMotion/CMPedometer.h>
#import <CoreMotion/CMStepCounter.h>
#import <CoreMotion/CMSensorRecorder.h>
  • CMMotionManager 设备管理对象

  Core Motion框架包含有一个专门的Manager类,CMMotionManager,然后由这个manager去管理三种和运动相关的数据封装类,而 且,这些类都是CMLogItem类的子类,所以相关的motion数据都可以和发生的时间信息一起保存到对应文件中,有了时间戳,两个相邻数据之间的实 际更新时间就很容易得到了。

  • CMAccelerometer设备的加速度数据
typedef struct {
    double x;
    double y;
    double z;
} CMAcceleration;
// 加速度数据
@interface CMAccelerometerData : CMLogItem { @private id _internal; } /* * acceleration * * Discussion: * 加速度结构体数据 * */ @property(readonly, nonatomic) CMAcceleration acceleration; @end
  • CMAltitudeData 设备的海拔数据

 

@interface CMAltitudeData : CMLogItem

/*
 *  relativeAltitude
 *
 *  Discussion:
 *    海拔
 *
 */
@property(readonly, nonatomic) NSNumber *relativeAltitude;

/*
 *  pressure
 *
 *  Discussion:
 *    气压
 *
 */
@property(readonly, nonatomic) NSNumber *pressure;

@end
  •    CMDeviceMotion设备的运动状态数据,这个类稍微复杂,包括的数据比较多

@interface CMDeviceMotion : CMLogItem
{
@private
    id _internal;
}

/*
 *  attitude
 *  
 *  Discussion:
 *    设备状态
 *
 */
@property(readonly, nonatomic) CMAttitude *attitude;

/*
 *  rotationRate
 *  
 *  Discussion:
 *    设备的角速度
 *
 */
@property(readonly, nonatomic) CMRotationRate rotationRate;

/*
 *  gravity
 *  
 *  Discussion:
 *    设备的重力加速度
 *
 */
@property(readonly, nonatomic) CMAcceleration gravity;

/*
 *  userAcceleration
 *  
 *  Discussion:
 *    用户施加的加速度
 */
@property(readonly, nonatomic) CMAcceleration userAcceleration;

/*
 *  magneticField
 *  
 *  Discussion:
 *    磁场矢量对象
 */
@property(readonly, nonatomic) CMCalibratedMagneticField magneticField NS_AVAILABLE(NA,5_0);

@end
  •    CMGyroData设备的陀螺仪数据
typedef struct {
    double x;
    double y;
    double z;    
} CMRotationRate;

/*
 *  CMGyroData
 *
 *  Discussion:
 *    陀螺仪数据
 *
 */@interface CMGyroData : CMLogItem
{
@private
    id _internal;
}

/*
 *  rotationRate
 *  
 *  Discussion:
 *    数据结构体
 *
 */
@property(readonly, nonatomic) CMRotationRate rotationRate;

@end
  • CMMagnetometerData 设备的磁力数据

typedef struct {
    double x;
    double y;
    double z;
} CMMagneticField;

/*
 *  CMMagnetometerData
 *  
 *  Discussion:
 *    Contains a single magnetometer measurement.
 */
@interface CMMagnetometerData : CMLogItem
{
@private
    id _internal;
}

/*
 *  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;

@end
  • CMPedometerData 用户的行走活动数据,包括:步数,距离,楼层等。

@interface CMPedometerData : NSObject <NSSecureCoding, NSCopying>

/*
 *  startDate
 *
 *  Discussion:
 *      The start time of the period for which the pedometer data is valid.
 *
 *      This is the start time requested for the session or historical query.
 */
@property(readonly, nonatomic) NSDate *startDate;

/*
 *  endDate
 *
 *  Discussion:
 *      The end time of the period for which the pedometer data is valid.
 *
 *      For updates this is the time for the most recent update. For historical
 *      queries this is the end time requested.
 */
@property(readonly, nonatomic) NSDate *endDate;

/*
 *  numberOfSteps
 *
 *  Discussion:
 *      Number of steps taken by the user.
 */
@property(readonly, nonatomic) NSNumber *numberOfSteps;

/*
 *  distance
 *
 *  Discussion:
 *      Estimated distance in meters traveled by the user while walking and
 *      running. Value is nil unsupported platforms.
 */
@property(readonly, nonatomic, nullable) NSNumber *distance;

/*
 *  floorsAscended
 *
 *  Discussion:
 *      Approximate number of floors ascended by way of stairs. Value is nil
 *      on unsupported platforms.
 *
 */
@property(readonly, nonatomic, nullable) NSNumber *floorsAscended;

/*
 *  floorsDescended
 *
 *  Discussion:
 *      Approximate number of floors descended by way of stairs. Value is nil
 *      on unsupported platforms.
 */
@property(readonly, nonatomic, nullable) NSNumber *floorsDescended;

/*
 * currentPace
 *
 *
 * Discussion:
 *      For updates this returns the current pace, in s/m (seconds per meter).
 *      Value is nil if any of the following are true:
 *
 *         (1) Information not yet available;
 *         (2) Historical query;
 *         (3) Unsupported platform.
 *
 */
@property(readonly, nonatomic, nullable) NSNumber *currentPace NS_AVAILABLE(NA,9_0);

/*
 * currentCadence
 *
 *
 * Discussion:
 *      For updates this returns the rate at which steps are taken, in steps per second.
 *      Value is nil if any of the following are true:
 *
 *         (1) Information not yet available;
 *         (2) Historical query;
 *         (3) Unsupported platform.
 *
 */
@property(readonly, nonatomic, nullable) NSNumber *currentCadence NS_AVAILABLE(NA,9_0);

@end
原文地址:https://www.cnblogs.com/yzvictory/p/5242526.html