ios开发 cmdevicemotion 兼容性旋转矩阵怎么算幅度

一、CoreMotion框架介绍
我们知道有一些iOS的应用,会有一些特殊的要求,比如:
电子罗盘指南针之类的应用:让我们知道方向。
运动类型软件:让我们知道我们跑步多少公里。
社交软件中的摇一摇功能。
游戏中扮演角色类中根据设备的晃动等进行操作。
其实,他们多半是使用了iOS中的一个核心运动框架CoreMotion.framework
使用iOS提供给我们的CoreMotion框架,主要是为了访问加速度计和陀螺仪的相关数据。
它不仅仅提供给你获得实时的加速度值和旋转速度值,更重要的是,苹果在其中集成了很多算法,可以直接给你输出把重力加速度分量剥离的加速度,省去你的高通滤波操作,以及提供给你一个专门的设备的三维位置信息。
传感器介绍:
加速度计:
加速度计的原理很简单,现在手机里面基本配备的都是3维线传感器,也就是说,用来测量x,y,z三个轴上的加速力。加速力就是当物体在加速过程中作用在物体上的力,就好比地球引力,也就是重力。
陀螺仪的主要作用,是基于角动量守恒的理论,沿着某个特定的坐标轴测量旋转速率。在使用中,陀螺仪的转子在高速旋转时,始终指向一个固定的方向,当运动物体的运动方向偏离预定方向时,陀螺仪就可以感受出来。
二、CoreMotion使用
CoreMotion主要负责三种数据:
加速度值CMAccelerometerData
陀螺仪值CMGyroData
设备motion值CMDeviceMotion
实际上,这个设备motion值就是通过加速度和旋转速度进行变换算出来的
CMDeviceMotion属性介绍:
attitude:通俗来讲,就是告诉你手机在当前空间的位置和姿势
gravity:重力信息,其本质是重力加速度矢量在当前设备的参考坐标系中的表达
userAcceleration:加速度信息
rotationRate:即时的旋转速率,是陀螺仪的输出
使用CoreMotion的步骤:
初始化CMMotionManager管理对象
调用管理对象的对象方法获取数据,有2种方式
当你不需要使用的时候,停止获取数据
-(void)stopAccelerometerU//停止获取加速度计数据
-(void)stopGyroU//停止获取陀螺仪数据
-(void)stopDeviceMotionU//停止获取设备motion数据
在CoreMotion中有2种获取数据方式:
Push方式:
提供一个线程管理器NSOperationQueue和一个回调Block,CoreMotion自动在每一个采样数据到来的时候回调这个Block,进行处理。在这种情况下,Block中的操作会在你自己的主线程内执行。
Pull方式:
你必须主动去向CMMotionManager要数据,这个数据就是最近一次的采样数据。你不去要,CMMotionManager就不会给你。
1. 加速度计使用Pull方式获取:
- (void)useAccelerometerPull{
//初始化全局管理对象
CMMotionManager *manager = [[CMMotionManager alloc] init];
self.motionManager =
//判断加速度计可不可用,判断加速度计是否开启
if ([manager isAccelerometerAvailable] && [manager isAccelerometerActive]){
//告诉manager,更新频率是100Hz
manager.accelerometerUpdateInterval = 0.01;
//开始更新,后台线程开始运行。这是Pull方式。
[manager startAccelerometerUpdates];
//获取并处理加速度计数据
CMAccelerometerData *newestAccel = self.motionManager.accelerometerD
NSLog(@&X = %.04f&,newestAccel.acceleration.x);
NSLog(@&Y = %.04f&,newestAccel.acceleration.y);
NSLog(@&Z = %.04f&,newestAccel.acceleration.z);
2. 加速度计使用Push方式获取:
- (void)useAccelerometerPush{
//初始化全局管理对象
CMMotionManager *manager = [[CMMotionManager alloc] init];
self.motionManager =
//判断加速度计可不可用,判断加速度计是否开启
if ([manager isAccelerometerAvailable] && [manager isAccelerometerActive]){
//告诉manager,更新频率是100Hz
manager.accelerometerUpdateInterval = 0.01;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//Push方式获取和处理数据
[manager startAccelerometerUpdatesToQueue:queue
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error)
NSLog(@&X = %.04f&,accelerometerData.acceleration.x);
NSLog(@&Y = %.04f&,accelerometerData.acceleration.y);
NSLog(@&Z = %.04f&,accelerometerData.acceleration.z);
3. 陀螺仪使用Push方式获取,Pull方式就不列出来了,和加速度计使用相似:
- (void)useGyroPush{
//初始化全局管理对象
CMMotionManager *manager = [[CMMotionManager alloc] init];
self.motionManager =
//判断陀螺仪可不可以,判断陀螺仪是不是开启
if ([manager isGyroAvailable] && [manager isGyroActive]){
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//告诉manager,更新频率是100Hz
manager.gyroUpdateInterval = 0.01;
//Push方式获取和处理数据
[manager startGyroUpdatesToQueue:queue
withHandler:^(CMGyroData *gyroData, NSError *error)
NSLog(@&Gyro Rotation x = %.04f&, gyroData.rotationRate.x);
NSLog(@&Gyro Rotation y = %.04f&, gyroData.rotationRate.y);
NSLog(@&Gyro Rotation z = %.04f&, gyroData.rotationRate.z);
以上代码必须是真机上才能正常运行,利用上面的知识我们可以做类似于这样的效果:
有什么建议可以在下方评论区中提出!
阅读(...) 评论()How to use deviceMotion attitude values (roll,pitch and yaw) to detect a motion [如何使用devicemotion态度价值观(滚动,俯仰和偏航)来检测运动] - 问题-字节技术
How to use deviceMotion attitude values (roll,pitch and yaw) to detect a motion
如何使用devicemotion态度价值观(滚动,俯仰和偏航)来检测运动
问题 (Question)
I'm using coreMotion in my app to detect a motion of iOS device. I know how to get the different sensor's data from coreMotion. I am getting roll, pitch, and yaw data as follows from the deviceMotion:
float pitch =
(180/M_PI)*self.manager.deviceMotion.attitude.pitch];
float roll = (180/M_PI)*self.manager.deviceMotion.attitude.roll];
float yaw = (180/M_PI)*self.manager.deviceMotion.attitude.yaw];
How can I use these data to detect a motion?
What are the calculations that I need to do for this?
我用的coreMotion在我的应用程序检测到一个iOS设备的运动。我知道如何把不同传感器的数据coreMotion。我要滚动,俯仰,偏航数据如下从deviceMotion: float pitch =
(180/M_PI)*self.manager.deviceMotion.attitude.pitch];
float roll = (180/M_PI)*self.manager.deviceMotion.attitude.roll];
float yaw = (180/M_PI)*self.manager.deviceMotion.attitude.yaw];
我如何使用这些数据来检测运动?我需要做的计算是什么?
最佳答案 (Best Answer)
The attitude values need the device motion updates to be started as:
startDevicemotionUpdates
To monitor attitude values when device moves, use the attitude values to be displayed in labels:
[self.cmMotionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *devMotion, NSError *error) {
float pitch =
(180/M_PI)*self.manager.devMotion.attitude.pitch];
float roll = (180/M_PI)*self.manager.devMotion.attitude.roll];
float yaw = (180/M_PI)*self.manager.devMotion.attitude.yaw];
self.rollLabel.text = [NSString stringWithFormat:@"%f", roll];
self.pitchLabel.text = [NSString stringWithFormat:@"%f",pitch];
self.yawLabel.text = [NSString stringWithFormat:@"%f",yaw];
Also it is better not to use roll, pitch and yaw(aka Euler Angles). Use Quaternion or Rotation matrices for better accuracy. Euler angles have the tendency to be in a situation called gimbal lock which results in unreliable data.
Please check this link for how to use quaternion, and convert that value to the roll, pitch and yaw values:
态度价值观需要设备更新是开始运动:startDevicemotionUpdates
监控装置的动作姿态的值时,使用态度值被显示在标签:[self.cmMotionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *devMotion, NSError *error) {
float pitch =
(180/M_PI)*self.manager.devMotion.attitude.pitch];
float roll = (180/M_PI)*self.manager.devMotion.attitude.roll];
float yaw = (180/M_PI)*self.manager.devMotion.attitude.yaw];
self.rollLabel.text = [NSString stringWithFormat:@"%f", roll];
self.pitchLabel.text = [NSString stringWithFormat:@"%f",pitch];
self.yawLabel.text = [NSString stringWithFormat:@"%f",yaw];
也最好不要使用滚动,俯仰和偏航(即欧拉角)。使用四元数或旋转矩阵具有更高的精度。欧拉角必须在这种情况下所谓的趋势万向节锁结果在不可靠的数据。请检查此链接如何使用四元数,并将该值的滚动,俯仰和偏航值:
本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:magneticField property of the CMMagnetometer class, these values reflect the earth?EUR(TM)s magnetic field plus surrounding fields, minus device bias. If the device does not have a magnetometer, the accuracy field of the property?EUR(TM)s value (a CMCalibratedMagneticField structure) is CMMagneticFieldCalibrationAccuracyUncalibrated.
AvailabilityAvailable in iOS 5.0 and later.Declared InrotationRateThe rotation rate of the device. (read-only)@property(readonly, nonatomic) CMRotationRate rotationRateDiscussionA CMRotationRate structure contains data specifying the device?EUR(TM)s rate of rotation around three axes. The value of this property contains a measurement of gyroscope data whose bias has been removed by Core Motion algorithms. The identically name property of CMGyroData, on the other hand, gives the raw data from the gyroscope. The structure type is declared in CMGyroData.h.
AvailabilityAvailable in iOS 4.0 and later.See Also
  @property attitudeDeclared InuserAccelerationThe acceleration that the user is giving to the device. (read-only)@property(readonly, nonatomic) CMAcceleration userAccelerationDiscussionThe total acceleration of the device is equal to gravity plus the acceleration the user imparts to the device.
AvailabilityAvailable in iOS 4.0 and later.Declared InConstantsCMCalibratedMagneticFieldCalibrated magnetic field data and an estimate of the accuracy of the calibration.typedef struct {CMMagneticFCMMagneticFieldCalibrationA} CMCalibratedMagneticFFieldsfieldA structure containing 3-axis calibrated magnetic field data. See the description of the CMMagneticField structure. accuracyAn enum-constant value that indicates the accuracy of the magnetic field estimate. See ?EURoeCMMagneticFieldCalibrationAccuracy.?EUR?AvailabilityAvailable in iOS 5.0 and later.Declared InCMMagneticFieldCalibrationAccuracyIndicates the calibration accuracy of a magnetic field estimatetypedef enum {
CMMagneticFieldCalibrationAccuracyUncalibrated = -1,CMMagneticFieldCalibrationAccuracyLow,CMMagneticFieldCalibrationAccuracyMedium,CMMagneticFieldCalibrationAccuracyHigh } CMMagneticFieldCalibrationAConstantsCMMagneticFieldCalibrationAccuracyUncalibratedThe magnetic field estimate is not calibrated.Available in iOS 5.0 and later.Declared in CMDeviceMotion.h.CMMagneticFieldCalibrationAccuracyLowThe accuracy of the magnetic field calibration is low. Available in iOS 5.0 and later.Declared in CMDeviceMotion.h.CMMagneticFieldCalibrationAccuracyMediumThe accuracy of the magnetic field calibration is medium.Available in iOS 5.0 and later.Declared in CMDeviceMotion.h.CMMagneticFieldCalibrationAccuracyHighThe accuracy of the magnetic field calibration is high.Available in iOS 5.0 and later.Declared in CMDeviceMotion.h.DiscussionOne of the enum constants of the CMMagneticFieldCalibrationAccuracy type is the value of the accuracy field of the CMCalibratedMagneticField structure returned from the magneticField property.
Declared In
上一篇:下一篇:
CMDeviceMotion Class Reference相关文章请点击
责任申明:本站内容均整理自互联网,若有侵权,请联系我们。使用本站提供的任务技术内容造成不良后果,本站不负任何责任。
欢迎投稿,电子邮件:(#号换成@)&& QQ群1: &&

我要回帖

更多关于 devicemotion事件 的文章

 

随机推荐