使用CMDeviceMotion跟踪iPhone随时间的倾斜
我尝试使用CMDeviceMotion跟踪iPhone何时向后倾斜,然后做一些事情。我已经成功地创建了CMMotionManager,我从系统中获取运动数据,并且我正在过滤超过某个加速度的结果 但我想做的是,检测设备何时向后或向前倾斜超过某个速度。我该怎么做 以下是我目前掌握的代码: 更新:我想我解决了。我在寻找rotationRate属性,CMRotationRate。我已经把它们配对在一起了,我真的只需要x值,所以我会继续努力。如果有人对下面的代码有一些建议,我们将不胜感激使用CMDeviceMotion跟踪iPhone随时间的倾斜,iphone,ios4,motion-detection,gyroscope,Iphone,Ios4,Motion Detection,Gyroscope,我尝试使用CMDeviceMotion跟踪iPhone何时向后倾斜,然后做一些事情。我已经成功地创建了CMMotionManager,我从系统中获取运动数据,并且我正在过滤超过某个加速度的结果 但我想做的是,检测设备何时向后或向前倾斜超过某个速度。我该怎么做 以下是我目前掌握的代码: 更新:我想我解决了。我在寻找rotationRate属性,CMRotationRate。我已经把它们配对在一起了,我真的只需要x值,所以我会继续努力。如果有人对下面的代码有一些建议,我们将不胜感激 - (v
- (void)startMotionManager{
if (motionManager == nil) {
motionManager = [[CMMotionManager alloc] init];
}
motionManager.deviceMotionUpdateInterval = 1/15.0;
if (motionManager.deviceMotionAvailable) {
NSLog(@"Device Motion Available");
[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler: ^(CMDeviceMotion *motion, NSError *error){
//CMAttitude *attitude = motion.attitude;
//NSLog(@"rotation rate = [%f, %f, %f]", attitude.pitch, attitude.roll, attitude.yaw);
[self performSelectorOnMainThread:@selector(handleDeviceMotion:) withObject:motion waitUntilDone:YES];
}];
//[motionManager startDeviceMotionUpdates];
} else {
NSLog(@"No device motion on device.");
[self setMotionManager:nil];
}
}
- (void)handleDeviceMotion:(CMDeviceMotion*)motion{
CMAttitude *attitude = motion.attitude;
float accelerationThreshold = 0.2; // or whatever is appropriate - play around with different values
CMAcceleration userAcceleration = motion.userAcceleration;
float rotationRateThreshold = 7.0;
CMRotationRate rotationRate = motion.rotationRate;
if ((rotationRate.x) > rotationRateThreshold) {
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {
NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
NSLog(@"motion.rotationRate = %f", rotationRate.x);
[self showMenuAnimated:YES];
}
}
else if ((-rotationRate.x) > rotationRateThreshold) {
if (fabs(userAcceleration.x) > accelerationThreshold || fabs(userAcceleration.y) > accelerationThreshold || fabs(userAcceleration.z) > accelerationThreshold) {
NSLog(@"rotation rate = [Pitch: %f, Roll: %f, Yaw: %f]", attitude.pitch, attitude.roll, attitude.yaw);
NSLog(@"motion.rotationRate = %f", rotationRate.x);
[self dismissMenuAnimated:YES];
}
}
}
你看过吗?听起来像你需要的,再加上一些数学
编辑:好的 引用“获取当前设备方向”一章: 如果你只需要知道将军 设备的方向,而不是 准确的方向向量,你知道吗 应使用UID设备的方法 类来检索该信息。 使用UIDevice接口很简单 而且不要求你 计算方向向量 你自己 太好了,他们帮我们做数学题。然后,我想像您一样跟踪加速+跟踪方向,正如上面的文档所解释的,应该使用
UIDeviceOrientationFaceUp
和UIDeviceOrientationFaceDown来完成这项工作
如果没有一个UIDeviceOrientation
适合您的需要,您需要的是从两个Cmatitude
参考计算一些空间角度,这提供了一个旋转矩阵和一个四元数
。。。这些学校数学对我来说太遥远了。。。然后我会建议用这些搜索/问一个数学问题。我更感兴趣的是加速度和旋转速度,以检测设备“向后翻转”。想想一个警察和他的徽章,当他们翻动它的时候(如果他们仍然这样做的话)。我想我已经解决了大部分问题。我会编辑我的作业。这是我有问题的数学。我能掌握基本知识,我能阅读,但做我自己的事比我高得多。我想我会努力坚持我所知道的,并问其他数学问题…数学计算。。。brrr。。。我不想在这件事上“回头”。使用mathematica/Matrical/quaternion标记询问仅数学问题。。。我相信有人会帮上忙的。祝你好运