0

当我调用 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 我不应该只是改变偏航吗?

4

1 回答 1

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);
                }];
        }];
于 2016-02-16T18:32:12.607 回答