如何使用设备运动姿态值(横滚、俯仰和偏航)来检测运动。

7

我正在我的应用程序中使用coreMotion来检测iOS设备的运动。我知道如何从coreMotion获取不同传感器的数据。我正从deviceMotion获取如下的横滚、俯仰和偏航数据:

 float pitch =  (180/M_PI)*self.manager.deviceMotion.attitude.pitch];
 float roll = (180/M_PI)*self.manager.deviceMotion.attitude.roll];
 float yaw = (180/M_PI)*self.manager.deviceMotion.attitude.yaw];

我该如何利用这些数据来检测运动?我需要进行哪些计算?


先生,您想用这些值做什么?如果您想在设备移动时监视这些值,那么您可以将它们显示为标签。 - iSeeker
1个回答

10

为了开始设备运动更新,需要设置姿态值:

startDevicemotionUpdates

为了在设备移动时监测姿态值,请使用要在标签中显示的姿态值:

[self.cmMotionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *devMotion, NSError *error) {

float pitch =  (180/M_PI)*self.manager.devMotion.attitude.pitch];
float roll = (180/M_PI)*self.manager.devMotion.attitude.roll];
float yaw = (180/M_PI)*self.manager.devMotion.attitude.yaw];

self.rollLabel.text = [NSString stringWithFormat:@"%f", roll];
self.pitchLabel.text = [NSString stringWithFormat:@"%f",pitch];
self.yawLabel.text = [NSString stringWithFormat:@"%f",yaw];
}

更好的选择是不使用滚转、俯仰和偏航(也称欧拉角)。使用四元数或旋转矩阵可以获得更准确的数据。欧拉角存在一种称为万向锁的情况,导致数据不可靠。

请查看此链接了解如何使用四元数,并将其转换为滚转、俯仰和偏航值:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接