我有一个非常简单的应用程序,我试图通过核心运动使用陀螺仪。
此时,出于测试目的,我只是获取滚动、俯仰和偏航的值并在屏幕上打印。根据下图,据我所知,roll、pitch、yaw分别对应红绿蓝轴,对吧?
好的。当我将 iphone 放在左侧的桌子上时(右侧的主页按钮),与桌面平面成 90 度角,这些是我读取的滚动、俯仰和偏航值:-90、0、 0.然后我开始根据桌子的垂直轴以逆时针方式旋转iPhone,即根据桌子垂直轴的正方向。在 iPhone 上,这意味着俯仰旋转,但当我旋转时,俯仰保持不变,而 YAW 是变化的!!!!!!
如果 iPhone 靠在桌子的左侧,绿色轴(间距)是垂直的。如果我在 X 上逆时针(正)旋转设备,我应该会看到俯仰角增加,而不是偏航。
我对此的唯一解释是,旋转设备时陀螺仪轴不旋转。所以,如果我使用默认的姿态参考,iPhone 认为面朝上的静止位置是默认的,蓝轴(偏航)将始终是垂直的。它是否正确?
这是我正在使用的代码...
在主代码上
motionManager = [[CMMotionManager alloc] init];
motionManager.deviceMotionUpdateInterval = 1.0/60.0; //60 Hz
[motionManager startDeviceMotionUpdates];
timer = [[NSTimer scheduledTimerWithTimeInterval:(1.0/60.0)
target:self
selector:@selector( readIt )
userInfo:nil
repeats:YES]
retain];
剩下的代码
#define degrees(x) (180.0 * x / M_PI)
- (void) readIt {
// CMAttitude *referenceAttitude;
CMAttitude *attitude;
CMDeviceMotion *motion = motionManager.deviceMotion;
if (!motion) {
return;
}
attitude = motion.attitude;
NSLog(@"roll = %f... pitch = %f ... yaw = %f", degrees(attitude.roll), degrees(attitude.pitch), degrees(attitude.yaw));
}