检测 iphone 何时被碰撞 [英] Detect when an iphone has been bumped
问题描述
我希望能够检测到 iphone 是否被撞到了什么东西……最好听一下陀螺仪/加速度计并判断它移动的速度有多快,或者它是否突然移动和停止.
I want to be able to detect if an iphone has been bumped into something...would it be best to listen to the gyroscope / accelerometer and judge how fast its moved or if its moved and stopped suddenly.
实际上如何判断设备是否移动然后突然停止?
In fact how would I judge if the device has moved then stopped suddenly?
这个答案很好,但反过来iOS:根据加速度计输出准确确定碰撞能量 - 如果我想检测到剧烈运动,它可以平滑运动.
This answer is great but in reverse iOS: Accurately determining energy of a bump from accelerometer output - it smooths out the movement, if anything I want to detect a sharp movement.
陀螺仪和加速度计也可用于 3GS 吗?
Also are both the Gyroscope and Accelerometer available for 3GS ?
更新代码
From the Apple doc http://developer.apple.com/library/iOS/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/MotionEvents/MotionEvents.html#//apple_ref/doc/uid/TP40009541-CH4-SW26
从加速度数据中分离瞬时运动如果您使用加速度计数据仅检测设备的即时运动,您需要能够将运动的突然变化与重力的持续影响隔离开来.你可以用高通滤波器做到这一点.
Isolating Instantaneous Motion from Acceleration Data If you are using accelerometer data to detect just the instant motion of a device, you need to be able to isolate sudden changes in movement from the constant effect of gravity. You can do that with a high-pass filter.
清单 4-6 显示了简化的高通滤波器计算.来自前一个事件的加速度值存储在类的 accelX、accelY 和 accelZ 成员变量中.此示例计算低通滤波器值,然后将其从当前值中减去以获得运动的瞬时分量.
Listing 4-6 shows a simplified high-pass filter computation. The acceleration values from the previous event are stored in the accelX, accelY, and accelZ member variables of the class. This example computes the low-pass filter value and then subtracts it from the current value to obtain just the instantaneous component of motion.
清单 4-6 从加速度计数据中获取运动的瞬时部分
Listing 4-6 Getting the instantaneous portion of movement from accelerometer data
define kFilteringFactor 0.1
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
// Subtract the low-pass value from the current value to get a simplified high-pass filter
accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) + (accelX * (1.0 - kFilteringFactor)) );
accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) + (accelY * (1.0 - kFilteringFactor)) );
accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor)) );
// Use the acceleration data.
}
如果检测到瞬时运动,我将如何使用加速度数据"来计算?
How would I then 'Use the acceleration data' to work out if an instantaneous motion is detected?
推荐答案
我使用了这个代码:
#define kUpdateFrequency 100.0
#define kFilteringFactor 0.1
float accelZ;
int spikeZCount = 0;
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / kUpdateFrequency];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
- (void) accelerometer: (UIAccelerometer *) accelerometer didAccelerate: (UIAcceleration *) acceleration
{
accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) + (accelZ * (1.0 - kFilteringFactor)) );
if (accelZ > 0.0f)
{
if (spikeZCount > 9)
{
// NSLog(@"SPIKE!");
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[self isBumped];
}
else
{
spikeZCount++;
// NSLog(@"spikeZCount %i",spikeZCount);
}
}
else
{
// NSLog(@"spikeZCount Reset");
spikeZCount = 0;
}
}
这篇关于检测 iphone 何时被碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!