运动传感器,读取旋转

owe*_*rig 5 objective-c ios

我在android和ios上都试过这个项目但收效甚微.这个东西很有可能就在我头上.但是我想我会在这里发布我的问题作为最后的努力.
我想弄清楚设备何时旋转或翻转.我的应用程序应该知道它何时执行180,360或者设备是否垂直翻转.
为了理解它的工作方式,我尝试下载两个示例项目:AccelerometerGraph和CoreMotionTeapot.有了这些和其他东西的混合,我发现我正在尝试这个:

motionManager = [[CMMotionManager alloc] init]; 
motionManager.accelerometerUpdateInterval = 0.01;
motionManager.deviceMotionUpdateInterval = 0.01;
[motionManager startDeviceMotionUpdates];

if (motionManager.gyroAvailable) {
    motionManager.gyroUpdateInterval = 1.0/60.0;
        motionManager.deviceMotionUpdateInterval = 0.01;
    [motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue]
                               withHandler: ^(CMGyroData *gyroData, NSError *error)
     {
         CMRotationRate rotate = gyroData.rotationRate;
         NSLog(@"rotation rate = [%f, %f, %f]", rotate.x, rotate.y, rotate.z);
     }];
} else {
    NSLog(@"No gyroscope on device.");
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何从这三个值(x,y,z)收集所要求的信息(水平和垂直旋转).

mox*_*oxy 0

来自苹果的文档:CMMotionManager类参考(抱歉阅读太多,我将一些句子加粗以便快速阅读)

\n\n
\n

创建 CMMotionManager 实例后,应用程序可以使用它来接收四种类型的运动:原始加速度计数据、原始陀螺仪数据、原始磁力计数据和处理后的设备运动数据(包括加速度计、旋转速率和姿态测量) 。Core Motion\xe2\x80\x99s 传感器融合算法提供的经过处理的设备运动数据给出了设备\xe2\x80\x99s 的姿态、旋转速率、校准磁场、重力方向以及用户施加的加速度装置。

\n\n

重要应用程序应该只创建 CMMotionManager 类的单个实例。此类的多个实例可能会影响从加速计和陀螺仪接收数据的速率。\n 应用程序在接收运动数据时可以采用两种方法之一:按指定的更新间隔处理数据或定期采样运动数据。使用这两种方法,应用程序应在完成处理加速度计、旋转速率、磁力计或设备运动数据时调用适当的停止方法(stopAccelerometerUpdates、stopGyroUpdates、stopMagnetometerUpdates 和 stopDeviceMotionUpdates)。

\n\n

以指定的时间间隔处理运动更新\n 要以特定的时间间隔接收运动数据,应用程序会调用 \xe2\x80\x9cstart\xe2\x80\x9d 方法,该方法采用操作队列(NSOperationQueue 的实例)和特定的块处理程序用于处理这些更新的类型。运动数据被传递到块处理程序中。更新频率由 \xe2\x80\x9cinterval\xe2\x80\x9d 属性的值确定。

\n\n

加速度计。设置 AccelerometerUpdateInterval 属性以指定更新间隔。调用 startAccelerometerUpdatesToQueue:withHandler: 方法,传入 CMAccelerometerHandler 类型的块。加速度计数据作为 CMAccelerometerData 对象传递到块中。\n 陀螺仪。设置 gyroUpdateInterval 属性以指定更新间隔。调用 startGyroUpdatesToQueue:withHandler: 方法,传入 CMGyroHandler 类型的块。旋转速率数据作为 CMGyroData 对象传递到块中。\n 磁力计。设置 MagnetometerUpdateInterval 属性以指定更新间隔。调用 startMagnetometerUpdatesToQueue:withHandler: 方法,传递一个 CMMagnetometerHandler 类型的块。磁场数据作为 CMMagnetometerData 对象传递到块中。\n 设备运动。设置 deviceMotionUpdateInterval 属性以指定更新间隔。调用 startDeviceMotionUpdatesUsingReferenceFrame:toQueue:withHandler: 或 startDeviceMotionUpdatesToQueue:withHandler: 方法,传入 CMDeviceMotionHandler 类型的块。使用前一种方法(iOS 5.0 中的新方法),您可以指定用于姿态估计的参考系。旋转速率数据作为 CMDeviceMotion 对象传递到块中。\n 运动数据的定期采样\n 要通过定期采样处理运动数据,应用程序调用不带参数的 \xe2\x80\x9cstart\xe2\x80\x9d 方法并周期性地访问由给定类型的运动数据的属性所保存的运动数据。此方法是游戏等应用程序的推荐方法。处理块中的加速度计数据会带来额外的开销,并且大多数游戏应用程序在渲染帧时只对最新的运动数据样本感兴趣。

\n\n

加速度计。调用 startAccelerometerUpdates 开始更新并通过读取 accelerometerData 属性定期访问 CMAccelerometerData 对象。\n 陀螺仪。调用 startGyroUpdates 开始更新并通过读取 gyroData 属性定期访问 CMGyroData 对象。\n 磁力计。调用 startMagnetometerUpdates 开始更新并通过读取MagnetometerData 属性定期访问CMMagnetometerData 对象。\n 设备运动。调用 startDeviceMotionUpdatesUsingReferenceFrame: 或 startDeviceMotionUpdates 方法开始更新并通过读取 deviceMotion 属性定期访问 CMDeviceMotion 对象。startDeviceMotionUpdatesUsingReferenceFrame: 方法(iOS 5.0 中的新增功能)可让您指定用于姿态估计的参考系。

\n
\n\n

关于收集数据:

\n\n
@property(readonly) CMGyroData *gyroData\n
Run Code Online (Sandbox Code Playgroud)\n\n

讨论\n如果没有可用的陀螺仪数据,则此属性的值为 nil。调用 startGyroUpdates 后接收陀螺仪数据的应用程序会定期检查此属性的值并处理陀螺仪数据。

\n\n

所以你应该有类似的东西

\n\n
gyroData.rotationRate.x\ngyroData.rotationRate.y\ngyroData.rotationRate.z\n
Run Code Online (Sandbox Code Playgroud)\n\n

通过存储它们并定期比较它们,您应该能够看到设备是否绕轴翻转等。

\n