CMMotionManager从缓存的参考框架更改无法按预期工作

时间:2016-02-05 15:25:48

标签: quaternions euler-angles cmmotionmanager

当我调用startDeviceMotionUpdatesUsingReferenceFrame时,然后缓存对我的第一个参考帧的引用,并在之后的所有运动更新上调用multiplyByInverseOfAttitude,我不会从我期望的参考帧中获得更改。这是一个我不理解的简单演示。

self.motionQueue = [[NSOperationQueue alloc] init];
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.deviceMotionUpdateInterval = 1.0/20.0;
[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame: CMAttitudeReferenceFrameXArbitraryZVertical toQueue:self.motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error){
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        CMAttitude *att = motion.attitude;
        if(self.motionManagerAttitudeRef == nil){
            self.motionManagerAttitudeRef = att;
            return;
        }
        [att multiplyByInverseOfAttitude:self.motionManagerAttitudeRef];
        NSLog(@"yaw:%+0.1f, pitch:%+0.1f, roll:%+0.1f, att.yaw, att.pitch, att.roll);
    }];
}];

首先,在我的应用程序中,我只关心俯仰和翻滚。但是偏航也在那里表现出我的困惑。

如果我将手机放在平板桌上,启动应用程序并查看日志,那么一切都按预期工作。所有的偏航,俯仰滚动值都是0.0,那么如果我将手机旋转90度而不将其从表面抬起只有偏航变化。那里一切都很好。

为了证明我认为是什么问题...现在把手机放在(例如)一个空的咖啡杯里面,这样所有的角度都会略微倾斜,重力方向会有一些小部分值轴。现在启动应用程序并使用上面的代码,您会认为一切正常,因为偏航,俯仰和滚转的值再次为0.0。但现在将咖啡杯旋转90度而不将其从桌面上抬起。 为什么我看到对所有偏航,俯仰和滚动的态度发生了重大变化?因为我缓存了我最初的态度(现在是我的新参考态度),并且称为muptiplyByInverseOfAttitude不应该我只是在改变偏航?

1 个答案:

答案 0 :(得分:0)

我真的不明白为什么使用缓存参考态度的态度不起作用......而且我认为这不是万向节锁定问题。但这就是我所需要的东西。如果您尝试使用我上面描述的咖啡杯进行实验,这可以提供完全预期的结果(即在平坦表面上旋转咖啡杯不会影响俯仰和滚动值,并且现在只在所有其他方向上倾斜咖啡杯一次影响一个轴)。加上而不是保存参考框架,我只保存参考俯仰和滚动,然后当应用程序启动时,一切都被清零,直到有一些移动。

现在好了。但我仍然希望我理解为什么其他方法没有按预期工作。

    self.motionQueue = [[NSOperationQueue alloc] init];
    self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.deviceMotionUpdateInterval = 1.0/20.0;
    [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame: CMAttitudeReferenceFrameXArbitraryZVertical toQueue:self.motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error)
    {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            if(self.motionManagerAttitude == nil){
                                CGFloat x = motion.gravity.x;
                                CGFloat y = motion.gravity.y;
                                CGFloat z = motion.gravity.z;
                                refRollF = atan2(y, x) + M_PI_2;

                                CGFloat r = sqrtf(x*x + y*y + z*z);
                                refPitchF = acosf(z/r);

                                self.motionManagerAttitude = motion.attitude;
                                return;
            }

                            CGFloat x = motion.gravity.x;
                            CGFloat y = motion.gravity.y;
                            CGFloat z = motion.gravity.z;
                            CGFloat rollF = refRollF - (atan2(y, x) + M_PI_2);

                            CGFloat r = sqrtf(x*x + y*y + z*z);
                            CGFloat pitchF = refPitchF - acosf(z/r);

                            //I don't care about yaw, so just printing out whatever the value is in the attitude
                            NSLog(@"yaw: %+0.1f, pitch: %+0.1f, roll: %+0.1f", (180.0f/M_PI)*motion.attitude.yaw, (180.0f/M_PI)*pitchF, (180.0f/M_PI)*rollF);
                }];
        }];