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