CoreMotion的实际使用,主要获得加速度或者手机朝向
2016-03-20 11:46
411 查看
以前在iphone中要得到加速度时,只能使用Accelerometer模块得到重力加速度分量,然后通过滤波得到加速度值。其实在ios中有一个陀螺仪模块,CoreMotion,使用更方便。
deviceMotion.m 外部引用 原始文档
//初始化CoreMotion
#import
CMMotionManager *motionManager = [[CMMotionManager alloc]init];
//1. Accelerometer 获取手机加速度数据
CMAccelerometerData *newestAccel = motionManager.accelerometerData;
double accelerationX = newestAccel.acceleration.x;
double accelerationY = newestAccel.acceleration.y;
double accelerationZ = newestAccel.acceleration.z;
//2. Gravity 获取手机的重力值在各个方向上的分量,根据这个就可以获得手机的空间位置,倾斜角度等 doublegravityX = motionManager.deviceMotion.gravity.x;
double gravityY = motionManager.deviceMotion.gravity.y;
double gravityZ = motionManager.deviceMotion.gravity.z;
//获取手机的倾斜角度:
double zTheta = atan2(gravityZ,sqrtf(gravityX*gravityX+gravityY*gravityY))/M_PI*180.0;
double xyTheta = atan2(gravityX,gravityY)/M_PI*180.0;
//zTheta是手机与水平面的夹角, xyTheta是手机绕自身旋转的角度
//3. DeviceMotion 获取陀螺仪的数据 包括角速度,空间位置等
//旋转角速度:
CMRotationRate rotationRate = motionManager.deviceMotion.rotationRate;
double rotationX = rotationRate.x;
double rotationY = rotationRate.y;
double rotationZ = rotationRate.z;
//空间位置的欧拉角(通过欧拉角可以算得手机两个时刻之间的夹角,比用角速度计算精确地多)
double roll = motionManager.deviceMotion.attitude.roll;
double pitch = motionManager.deviceMotion.attitude.pitch;
double yaw = motionManager.deviceMotion.attitude.yaw;
//空间位置的四元数(与欧拉角类似,但解决了万向结死锁问题)
double w = motionManager.deviceMotion.attitude.quaternion.w;
double wx = motionManager.deviceMotion.attitude.quaternion.x;
double wy = motionManager.deviceMotion.attitude.quaternion.y;
double wz = motionManager.deviceMotion.attitude.quaternion.z;
//通过陀螺仪模块可以实现模拟赛车,模拟射击等。
DEMO
#import "GravityInduction.h"
@interface GravityInduction ()
{
NSTimeInterval updateInterval;
}
@property (nonatomic,strong) CMMotionManager *mManager;
@end
@implementation GravityInduction
- (CMMotionManager *)mManager
{
if (!_mManager) {
updateInterval = 1.0;
_mManager = [[CMMotionManager alloc] init];
}
return _mManager;
}
- (void)startDeviceMotionUpdatesResult:(void (^)(CMDeviceMotion *))result {
__weak typeof(self) weakSelf = self;
if ([weakSelf.mManager isDeviceMotionAvailable] == YES) {
[weakSelf.mManager setDeviceMotionUpdateInterval:updateInterval];
[weakSelf.mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
result(motion);
}];
}
}
- (void)stopUpdate
{
if ([self.mManager isDeviceMotionAvailable] == YES)
{
[self.mManager stopDeviceMotionUpdates];
}
}
- (void)dealloc
{
_mManager = nil;
}
@end
deviceMotion.m 外部引用 原始文档
//初始化CoreMotion
#import
CMMotionManager *motionManager = [[CMMotionManager alloc]init];
//1. Accelerometer 获取手机加速度数据
CMAccelerometerData *newestAccel = motionManager.accelerometerData;
double accelerationX = newestAccel.acceleration.x;
double accelerationY = newestAccel.acceleration.y;
double accelerationZ = newestAccel.acceleration.z;
//2. Gravity 获取手机的重力值在各个方向上的分量,根据这个就可以获得手机的空间位置,倾斜角度等 doublegravityX = motionManager.deviceMotion.gravity.x;
double gravityY = motionManager.deviceMotion.gravity.y;
double gravityZ = motionManager.deviceMotion.gravity.z;
//获取手机的倾斜角度:
double zTheta = atan2(gravityZ,sqrtf(gravityX*gravityX+gravityY*gravityY))/M_PI*180.0;
double xyTheta = atan2(gravityX,gravityY)/M_PI*180.0;
//zTheta是手机与水平面的夹角, xyTheta是手机绕自身旋转的角度
//3. DeviceMotion 获取陀螺仪的数据 包括角速度,空间位置等
//旋转角速度:
CMRotationRate rotationRate = motionManager.deviceMotion.rotationRate;
double rotationX = rotationRate.x;
double rotationY = rotationRate.y;
double rotationZ = rotationRate.z;
//空间位置的欧拉角(通过欧拉角可以算得手机两个时刻之间的夹角,比用角速度计算精确地多)
double roll = motionManager.deviceMotion.attitude.roll;
double pitch = motionManager.deviceMotion.attitude.pitch;
double yaw = motionManager.deviceMotion.attitude.yaw;
//空间位置的四元数(与欧拉角类似,但解决了万向结死锁问题)
double w = motionManager.deviceMotion.attitude.quaternion.w;
double wx = motionManager.deviceMotion.attitude.quaternion.x;
double wy = motionManager.deviceMotion.attitude.quaternion.y;
double wz = motionManager.deviceMotion.attitude.quaternion.z;
//通过陀螺仪模块可以实现模拟赛车,模拟射击等。
DEMO
#import "GravityInduction.h"
@interface GravityInduction ()
{
NSTimeInterval updateInterval;
}
@property (nonatomic,strong) CMMotionManager *mManager;
@end
@implementation GravityInduction
- (CMMotionManager *)mManager
{
if (!_mManager) {
updateInterval = 1.0;
_mManager = [[CMMotionManager alloc] init];
}
return _mManager;
}
- (void)startDeviceMotionUpdatesResult:(void (^)(CMDeviceMotion *))result {
__weak typeof(self) weakSelf = self;
if ([weakSelf.mManager isDeviceMotionAvailable] == YES) {
[weakSelf.mManager setDeviceMotionUpdateInterval:updateInterval];
[weakSelf.mManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
result(motion);
}];
}
}
- (void)stopUpdate
{
if ([self.mManager isDeviceMotionAvailable] == YES)
{
[self.mManager stopDeviceMotionUpdates];
}
}
- (void)dealloc
{
_mManager = nil;
}
@end
相关文章推荐
- 19.CoreMotion(陀螺仪)
- CoreMotion
- Swift用CoreMotion得到屏幕当前方向的方法
- Core Motion
- CoreMotion可以测到的各种值
- iOS 监测用户将屏幕朝下翻转
- IOS_加速计_碰撞检测_小球位置修复_dispatch_once单例
- iOS传感器开发——加速度传感器,螺旋仪传感器,磁力传感器的应用
- iOS加速器简单介绍
- Commotion框架 —— CMPedometer 统计步数的使用
- iOS_传感器
- CoreMotion iOS陀螺仪使用
- 教你 Debug 的正确姿势——记一次 CoreMotion 的 Crash
- iOS计步器功能实现之CoreMotion(一)
- iOS 探讨之 传感器库(CoreMotion.framework)
- iOS使用CoreMotion实现摇一摇功能
- Java用wait() notify()实现线程间通信
- 辩数字
- 【VB.NET】常用函数概览
- 24.NSURLConnection与RunLoop