2013-04-05 76 views
1

從CMAttitude類別獲取俯仰,滾轉和偏航角度有問題。陀螺儀CMAttitude俯仰,滾轉和偏航角度問題

首先,我使用'CMMotionManager'類和屬性x,y,z做了一個正常的陀螺儀,並且工作正常。 然後,我試圖使用CMAttitude作爲「絕對角度」,但我沒有工作,因爲它似乎沒有更新數據。角度始終爲0(但不是錯誤或警告)

我已經在stackoverflow中搜索了很多,並使用了一些我找到的解決方案,但我遇到了同樣的問題。 這裏是我的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
// Do any additional setup after loading the view, typically from a nib. 

    motionManager = [[CMMotionManager alloc] init]; 

    CMDeviceMotion *deviceMotion = motionManager.deviceMotion; 
    CMAttitude *attitude = deviceMotion.attitude; 
    referenceAttitude = attitude; 

    [motionManager startGyroUpdates]; 

    timer = [NSTimer scheduledTimerWithTimeInterval:1/30.0 
             target:self 
             selector:@selector(doGyroUpdate) 
             userInfo:nil 
             repeats:YES]; 
} 

-(void)doGyroUpdate { 


//cambia el frame de referencia 
    [motionManager.deviceMotion.attitude multiplyByInverseOfAttitude: referenceAttitude]; 

    double rRotation = motionManager.deviceMotion.attitude.roll*180/M_PI; 
    double pRotation = motionManager.deviceMotion.attitude.pitch*180/M_PI; 
    double yRotation = motionManager.deviceMotion.attitude.yaw*180/M_PI; 

NSString *myString = [NSString stringWithFormat:@"%f",rRotation]; 
self.angYaw.text = myString; 

myString = [NSString stringWithFormat:@"%f",pRotation]; 
self.angPitch.text = myString; 

myString = [NSString stringWithFormat:@"%f",yRotation]; 
self.angRoll.text = myString; 

} 

非常感謝! :D

回答

3

motionManager有4種模式:加速度計,陀螺儀,磁力計和設備運動。

取決於您需要哪一個,您需要啓動適當的模式:startAccelerometerUpdates,startGyroUpdates,startMagnetometerUpdates或startDeviceMotionUpdates。

你開始startGyroUpdates,但讀deviceMotion財產。在你的情況下,只有gyroData可用。

做到這一點,而不是和你會得到的deviceMotion數據:

[motionManager startDeviceMotionUpdates]; 
+0

高興它幫助:) – 2013-04-23 15:27:48

相關問題