陀螺CMAttitude俯仰,滚转和偏航角度问题

时间:2013-04-05 07:24:03

标签: iphone xcode gyroscope motion angle

我在从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

1 个答案:

答案 0 :(得分:3)

motionManager有4种模式:加速度计,陀螺仪,磁力计和设备运动。

根据您需要的模式,您需要启动适当的模式:startAccelerometerUpdates,startGyroUpdates,startMagnetometerUpdates或startDeviceMotionUpdates。

您正在开始startGyroUpdates,但正在阅读deviceMotion属性。在您的情况下,只有gyroData可用。

改为执行此操作,您将获得deviceMotion数据:

[motionManager startDeviceMotionUpdates];