我无法正确读取 iphone 的内部陀螺仪。
我开发了一个基于磁力计的增强现实引擎。因为陀螺仪比磁力计提供更可靠的值,所以我想改进引擎。
现在,问题是,如果 iphone 处于横向模式,陀螺仪值,参考北方,工作良好。但是,如果我在横向和纵向之间移动 iphone,则值是错误的。我试图根据加速度计计算偏移量,但它仍然无法正常工作。第二个问题是,如果 iphone 处于纵向模式并且我将其向后滚动,则指南针数据会跳跃 180 度。
_mmanager.deviceMotionUpdateInterval = 1/60.0;
_mmanager.accelerometerUpdateInterval = 1.0/90.0;
[_mmanager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXTrueNorthZVertical];
// queue to get accelation data
NSOperationQueue *motionQueue = [[NSOperationQueue alloc] init];
[_mmanager startAccelerometerUpdatesToQueue: motionQueue withHandler:^(CMAccelerometerData *data, NSError *error) {
_xacc = (data.acceleration.x * 0.1) + (_xacc * (1.0 - 0.1));
_yacc = (data.acceleration.y * 0.1) + (_yacc * (1.0 - 0.1));
_zacc = (data.acceleration.z * 0.1) + (_zacc * (1.0 - 0.1));
}];
// in my update method
double gyx = _mmanager.deviceMotion.attitude.yaw;
double gyy = _mmanager.deviceMotion.attitude.roll;
double gyz = _mmanager.deviceMotion.attitude.pitch;
// sidewise rotation of the iphone to calculate a offset
double rotation = fmod(((atan2(-_yacc, _xacc)+M_PI/2)*180/M_PI)+180, 360);
// heading with compass data and offset just work perfectly
double compassheding = fmod(_lmanager.heading.trueHeading+rotaion, 360);
double gyroheading = -gyx*180/M_PI-90;
if(gyroheading < 0){
gyroheading = gyroheading+360;
}
// same offset as obove. but wont work correctly.
double ro2 = fmod(((atan2(-_yacc, _xacc)+M_PI/2)*180/M_PI)+180, 360);
head = fmod(head+ro2, 360);
我现在的问题是,我怎样才能正确读出带有北参考的陀螺仪并计算偏移量,所以当我将 iphone 放在我眼前时,这些值仍然是正确的,就像拍照一样。我只想用陀螺仪获得可靠的航向数据并摆脱滞后的指南针。