由于公司要求,了解了一下IOS上传感器数据的处理方式,在此作为记录。
Core Motion
Core Motion是IOS提供的一个封装好的框架,核心运动框架,可以很方便地获取基本九轴传感器的数据(加速度计,陀螺仪和磁力计),还提供了高通滤波的算法,直接获取剥离了重力加速度的加速度分量。
Core Motion的数据结构
- CMAccelerometerData
实际包含一个结构体acceleration(类型:CMAcceleration),提供三轴加速度值,如下所示:
typedef struct {
double x;
double y;
double z;
} CMAcceleration;
// A structure containing 3-axis acceleration data.
- CMGyroData
实际包含一个结构体rotationRate(类型:CMRotationRate),提供三轴角速度值,如下所示:
typedef struct {
double x;
double y;
double z;
} CMRotationRate;
// A structure containing 3-axis rotation rate data.
- CMMagnetometerData
实际包含一个结构体magneticField(类型:CMMagneticField),提供三轴磁力值,如下所示:
typedef struct {
double x;
double y;
double z;
} CMMagneticField;
// A structure containing 3-axis magnetometer data.
CMDeviceMotion
包含四种数据attitude(类型:CMAttitude) 姿态
// Returns the attitude of the device.
其中包含欧拉角度(roll,pitch,yaw),四元组 (CMQuaternion),还有一个旋转矩阵(CMRotationMatrix)
multiplyByInverseOfAttitude方法rotationRate(类型:CMRotationRate) 角速度
// Returns the rotation rate of the device for devices with a gyro.
和上述的陀螺仪角速度一致gravity(类型:CMAcceleration) 重力加速度
// Returns the gravity vector expressed in the device’s reference
frame. Note that the total acceleration of the device is equal to
gravity plus userAcceleration.userAcceleration(类型:CMAcceleration) 用户的加速度,即高通滤波后的加速度
// Returns the acceleration that the user is giving to the device.
Note that the total acceleration of the device is equal to gravity
plus userAcceleration.magneticField (类型:CMCalibratedMagneticField) 磁场数值
//Returns the magnetic field vector with respect to the device.
还有一些磁场校准的精度数据CMMagneticFieldCalibrationAccuracy等等
数据获取方式
push方式
1.创建运动管理对象
CMMotionManager *mgr = [[CMMotionManageralloc]init];
2.判断加速器是否可用
if(mgr.isAccelerometerAvailable){
}
3.设置采样间隔
mgr.accelerometerUpdateInterval= 1.0/60.0;// 1秒钟采样60次
4.开始采样
-(void)startAccelerometerUpdatesToQueue:(NSOperationQueue*)queue withHandler:(CMAccelerometerHandler)handler;
pull方式
1.创建运动管理对象
CMMotionManager *mgr = [[CMMotionManageralloc]init];
2.判断加速器是否可用
if(mgr.isAccelerometerAvailable){
}
3.开始采样
-(void)startAccelerometerUpdates;
4.在需要时获取数据
CMAcceleration acc = mgr.accelerometerData.acceleration;
Device Motion信息的提供
CMDeviceMotion中的所有数据都是可读数据,用户可以直接从中获取需求的数据,比如说重力加速度等等,而不只是传感器的原始数据,IOS提供的库已经对原始数据进行了加工处理
以上信息总结至多个博文
IOS传感器的基本使用
Core Motion框架使用方法
CoreMotion可以测到的各种值