ios中陀螺仪CoreMotion的使用

以前在iphone中要得到加速度时,只能使用Accelerometer模块得到重力加速度分量,然后通过滤波得到加速度值。其实在ios中有一个陀螺仪模块,CoreMotion,使用更方便。

CoreMotion中主要有以下几个模块


初始化CoreMotion

[html]  view plain copy
  1. #import <CoreMotion/CoreMotion.h>  
  2.   
  3. CMMotionManager *motionManager = [[CMMotionManager alloc]init];  

1. Accelerometer 获取手机加速度数据

[html]  view plain copy
  1. CMAccelerometerData *newestAccel = motionManager.accelerometerData;  
  2. double accelerationX = newestAccel.acceleration.x;  
  3. double accelerationY = newestAccel.acceleration.y;  
  4. double accelerationZ = newestAccel.acceleration.z;  

2. Gravity 获取手机的重力值在各个方向上的分量,根据这个就可以获得手机的空间位置,倾斜角度等

[html]  view plain copy
  1. double gravityX = motionManager.deviceMotion.gravity.x;  
  2. double gravityY = motionManager.deviceMotion.gravity.y;  
  3. double gravityZ = motionManager.deviceMotion.gravity.z;  

获取手机的倾斜角度:

[html]  view plain copy
  1. double zTheta = atan2(gravityZ,sqrtf(gravityX*gravityX+gravityY*gravityY))/M_PI*180.0;  
  2.   
  3. double xyTheta = atan2(gravityX,gravityY)/M_PI*180.0;  

zTheta是手机与水平面的夹角, xyTheta是手机绕自身旋转的角度


3. DeviceMotion 获取陀螺仪的数据 包括角速度,空间位置等

旋转角速度:

[html]  view plain copy
  1. CMRotationRate rotationRate = motionManager.deviceMotion.rotationRate;  
  2. double rotationX = rotationRate.x;  
  3. double rotationY = rotationRate.y;  
  4. double rotationZ = rotationRate.z;  

空间位置的欧拉角(通过欧拉角可以算得手机两个时刻之间的夹角,比用角速度计算精确地多)

[html]  view plain copy
  1. double roll    = motionManager.deviceMotion.attitude.roll;  
  2. double pitch   = motionManager.deviceMotion.attitude.pitch;  
  3. double yaw     = motionManager.deviceMotion.attitude.yaw;  

空间位置的四元数(与欧拉角类似,但解决了万向结死锁问题)

[html]  view plain copy
  1. double w = motionManager.deviceMotion.attitude.quaternion.w;  
  2. double wx = motionManager.deviceMotion.attitude.quaternion.x;  
  3. double wy = motionManager.deviceMotion.attitude.quaternion.y;  
  4. double wz = motionManager.deviceMotion.attitude.quaternion.z;  


通过陀螺仪模块可以实现模拟赛车,模拟射击等。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值