1

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

4

1 回答 1

3

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

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

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

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

[motionManager startDeviceMotionUpdates];
于 2013-04-14T16:21:24.370 回答