【问题标题】:Using CMDeviceMotion to track iPhone tilt over time使用 CMDeviceMotion 跟踪 iPhone 随时间的倾斜
【发布时间】:2011-04-17 14:22:44
【问题描述】:

我正在尝试使用 CMDeviceMotion 来跟踪 iPhone 何时向后倾斜,然后做一些事情。我已经成功创建了 CMMotionManager,我正在从系统中获取运动数据,并且我正在过滤超过某个加速度的结果。

不过,我想做的是检测设备何时向后或向前倾斜超过一定速度。我该怎么做?

这是我目前的代码:

更新:我想我解决了。我正在寻找rotationRate 属性CMRotationRate。我已经将它们配对在一起,我真的只需要 x 值,所以我会继续努力。如果有人对以下代码有一些提示,我们将不胜感激。

    - (void)startMotionManager{
        if (motionManager == nil) {
            motionManager = [[CMMotionManager alloc] init];
        }

        motionManager.deviceMotionUpdateInterval = 1/15.0;
        if (motionManager.deviceMotionAvailable) {

            NSLog(@"Device Motion Available");
            [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
                                               withHandler: ^(CMDeviceMotion *motion, NSError *error){
                                                       //CMAttitude *attitude = motion.attitude;
                                                       //NSLog(@"rotation rate = [%f, %f, %f]", attitude.pitch, attitude.roll, attitude.yaw);
                                                   [self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];

                                               }];
                //[motionManager startDeviceMotionUpdates];


        } else {
            NSLog(@"No device motion on device.");
            [self setMotionManager:nil];
        }
    }

   - (void)handleDeviceMotion:(CMDeviceMotion*)motion{
    CMAttitude *attitude = motion.attitude;

    float accelerationThreshold = 0.2; // or whatever is appropriate - play around with different values
    CMAcceleration userAcceleration = motion.userAcceleration;

    float rotationRateThreshold = 7.0;
    CMRotationRate rotationRate = motion.rotationRate;

    if ((rotationRate.x) > rotationRateThreshold) {
        if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {

            NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
            NSLog(@"motion.rotationRate = %f", rotationRate.x);

            [self showMenuAnimated:YES];
        }
    }

    else if ((-rotationRate.x) > rotationRateThreshold) {
        if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {

            NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
            NSLog(@"motion.rotationRate = %f", rotationRate.x);

            [self dismissMenuAnimated:YES];
        }
    }
}

【问题讨论】:

    标签: iphone ios4 motion-detection gyroscope


    【解决方案1】:

    你看过CMAttitude吗?听起来像你需要的,加上我猜的一些数学。


    编辑:好的,你做到了。

    引用Apple documentation,在“获取当前设备方向”一章

    如果您只需要了解一般情况 设备的方向,而不是 方向的精确向量,你 应该使用 UIDevice 的方法 类来检索该信息。 使用 UIDevice 接口很简单 并且不要求您 计算方向向量 自己。

    太好了,他们为我们计算。然后我想像你这样的跟踪加速+跟踪方向,如上面的文档解释,应该使用UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown来完成这项工作?

    如果没有一个UIDeviceOrientation 符合您的需求,您需要从两个CMAttitude 参考计算一些空间角度,这提供了一个rotationMatrix 和一个quaternion...这些学校数学很远对我来说……然后我建议也许用这些搜索/问一个数学问题。

    【讨论】:

    • 我对加速度和旋转速率更感兴趣,以检测设备“向后翻转”。当他们翻转它时想想警察和他的徽章(如果他们仍然这样做的话)。我想我已经解决了大部分问题。我将编辑我的 OP。
    • 这是我遇到的数学问题。我可以掌握基础知识,我可以阅读它,但是做我自己的事情远远超过我。我想我会尝试坚持我所知道的并提出其他数学问题....
    • 矩阵计算... brrr... 我不想在这个中挖掘“回”。用mathematica / matricial / quaternion标签问一个数学问题......我相信有人可能会在那里提供帮助。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    相关资源
    最近更新 更多