我需要一些关于在 iPhone 上使用陀螺仪的帮助。在特定情况下,我无法理解 CMAttitude 关于俯仰、滚动和偏航的读数。
这是我的代码
- (void)handleDeviceMotion:(CMDeviceMotion*)motion {
NSLog(@"Yaw %f ",attitude.yaw * 180 / M_PI);
NSLog(@"Pitch %f ",attitude.pitch * 180 / M_PI);
NSLog(@"Roll %f ",attitude.roll * 180 / M_PI);
}
让我们假设 iPhone 躺在飞机上,如下图所示:
俯仰、滚动和偏航(几乎)为 0 度,任何绕轴转动都会返回可理解的读数。例如,向右转动设备,Yaw 减小,Pitch and Roll 保持为 0。
现在 iPhone 处于以下位置:
并重新开始测量。
读数为:Yaw = 0,Pitch = 90,Roll = 0
由于设备围绕该轴旋转,因此间距增加。
将 iPhone 移动到这个位置:
读数为:Yaw = 30,Pitch = 90,Roll = 0
再一次,由于设备围绕 Yaw 轴旋转,因此该值会发生变化,而其他值不会发生变化。
围绕滚动轴移动设备:
读数为:Yaw = 0,Pitch = 90,Roll = -20。
现在我无法理解。围绕半径为 R (R > 0) 的圆移动 iPhone,如下图所示:
Yaw 改变,而 Pitch 和 Roll 没有改变。
我本来预计 Yaw 保持不变,而 Roll 改变了。
由于我只对用户绕偏航轴的旋转感兴趣,如何弥补这一点?
我遇到的另一个问题是漂移。iPhone 处于第二个图中描述的位置,长时间(1 分钟或更长时间)握在我手中。Yaw 不断增加。知道如何补偿漂移吗?
先感谢您
更新 我遵循了 Kay 的建议,但没有任何变化。关于我的代码的更多细节。我只想在用户围绕 Yaw 轴旋转设备时使用 Yaw 来旋转 UIImage。这是可行的,但是当用户围绕自己的垂直轴旋转时,Yaw 会发生变化。我认为这是不正确的,因为当用户围绕其垂直轴移动时,设备不会围绕其自己的 Yaw 轴旋转。可能是我错了。这是我的原始代码:
- (void)handleDeviceMotion:(CMDeviceMotion*)motion {
CMAttitude *attitude = motion.attitude;
NSLog(@"Yaw %f ",attitude.yaw * 180 / M_PI);
NSLog(@"Pitch %f ",attitude.pitch * 180 / M_PI);
NSLog(@"Roll %f ",attitude.roll * 180 / M_PI);
image.transform = CGAffineTransformMakeRotation(-attitude.yaw);
}
这是凯建议后的代码:
- (void)handleDeviceMotion:(CMDeviceMotion*)motion {
CMAttitude *attitude = motion.attitude;
if (startAttitude == 0) {
startAttitude = attitude;
}
[attitude multiplyByInverseOfAttitude:startAttitude];
NSLog(@"Yaw %f ",attitude.yaw * 180 / M_PI);
NSLog(@"Pitch %f ",attitude.pitch * 180 / M_PI);
NSLog(@"Roll %f ",attitude.roll * 180 / M_PI);
image.transform = CGAffineTransformMakeRotation(-attitude.yaw);
}
我开始设备运动监控
[motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical toQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMDeviceMotion *motion, NSError *error){
[self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
}];