0

CMMotionManager で奇妙な動作をしました。アプリが複数のデバイスの向きをサポートできるように、デバイスの位置を調整しようとしています。

(シミュレーターではなく) 実際のデバイスでアプリをデバッグすると、すべて正常に動作します。デバッグせずに同じアプリを実行すると、キャリブレーションが機能しません。

これが私のコードです:

static CMMotionManager* _motionManager;
static CMAttitude* _referenceAttitude;

// Returns a vector with the current orientation values
// At the first call a reference orientation is saved to ensure the motion detection works for multiple device positions
+(GLKVector3)getMotionVectorWithLowPass{
    // Motion
    CMAttitude *attitude = self.getMotionManager.deviceMotion.attitude;
    if (_referenceAttitude==nil) {
        // Cache Start Orientation
        _referenceAttitude = [_motionManager.deviceMotion.attitude copy];
    } else {
        // Use start orientation to calibrate
        [attitude multiplyByInverseOfAttitude:_referenceAttitude];
        NSLog(@"roll: %f", attitude.roll);
    }
    return [self lowPassWithVector: GLKVector3Make(attitude.pitch,attitude.roll,attitude.yaw)];
}

+(CMMotionManager*)getMotionManager {
    if (_motionManager==nil) {
        _motionManager=[[CMMotionManager alloc]init];
        _motionManager.deviceMotionUpdateInterval=0.25;
        [_motionManager startDeviceMotionUpdates];
    }
    return _motionManager;
}
4

1 に答える 1

0

私は解決策を見つけました。この問題は、デバッグ モードと非デバッグ モードの間でタイミング動作が異なるために発生しました。CMMotionManager は、正しい値を返す前に、初期化に少し時間がかかります。解決策は、キャリブレーションを 0.25 秒間延期することでした。

このコードは機能します:

+(GLKVector3)getMotionVectorWithLowPass{
    // Motion
    CMAttitude *attitude = self.getMotionManager.deviceMotion.attitude;
    if (_referenceAttitude==nil) {
        // Cache Start Orientation
        // NEW:
        [self performSelector:@selector(calibrate) withObject:nil afterDelay:0.25];

    } else {
        // Use start orientation to calibrate
        [attitude multiplyByInverseOfAttitude:_referenceAttitude];
        NSLog(@"roll: %f", attitude.roll);
    }
    return [self lowPassWithVector: GLKVector3Make(attitude.pitch,attitude.roll,attitude.yaw)];
}

// NEW:
+(void)calibrate  
      _referenceAttitude = [self.getMotionManager.deviceMotion.attitude copy]
}
于 2014-04-27T12:52:13.193 に答える