我希望能够检测出iphone是否被撞到某个东西......最好是听陀螺仪/加速度计并判断其移动的速度或突然移动和停止的速度。
实际上我如何判断设备是否已移动然后突然停止?
这个答案很棒但反过来iOS: Accurately determining energy of a bump from accelerometer output - 如果有什么我想检测到一个急剧的动作,它会平滑运动。
陀螺仪和加速度计也可用于3GS吗?
更新代码
将瞬时运动与加速度数据隔离 如果您使用加速度计数据来仅检测设备的瞬时运动,则需要能够将突然的运动变化与恒定的重力影响隔离开来。你可以用高通滤波器来做到这一点。
清单4-6显示了简化的高通滤波器计算。上一个事件的加速度值存储在类的accelX,accelY和accelZ成员变量中。此示例计算低通滤波器值,然后从当前值中减去它,以获得运动的瞬时分量。
清单4-6从加速度计数据中获取移动的瞬时部分
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.
}
如果检测到瞬时运动,我将如何“使用加速度数据”计算出来?
答案 0 :(得分:4)
答案 1 :(得分:4)
从之前的答案中,我进行了一些概括,以获得以下效果非常好的结果:
// Isolate Instantaneous Motion from Acceleration Data
// (using a simplified high-pass filter)
CMAcceleration acceleration = accelerometerData.acceleration;
float prevAccelX = w_self.accelX;
float prevAccelY = w_self.accelY;
float prevAccelZ = w_self.accelZ;
w_self.accelX = acceleration.x - ( (acceleration.x * kFilteringFactor) +
(w_self.accelX * (1.0 - kFilteringFactor)) );
w_self.accelY = acceleration.y - ( (acceleration.y * kFilteringFactor) +
(w_self.accelY * (1.0 - kFilteringFactor)) );
w_self.accelZ = acceleration.z - ( (acceleration.z * kFilteringFactor) +
(w_self.accelZ * (1.0 - kFilteringFactor)) );
// Compute the derivative (which represents change in acceleration).
float deltaX = ABS((w_self.accelX - prevAccelX));
float deltaY = ABS((w_self.accelY - prevAccelY));
float deltaZ = ABS((w_self.accelZ - prevAccelZ));
// Check if the derivative exceeds some sensitivity threshold
// (Bigger value indicates stronger bump)
float sensitivityThreshold = 1.0;
float bumpVectorLength = sqrtf(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ);
if ( bumpVectorLength > sensitivityThreshold ) {
NSLog( @"BUMP: |%.3f, %.3f, %.3f| = %.3f", deltaX, deltaY, deltaZ, bumpVectorLength);
}
答案 2 :(得分:3)
我使用了这段代码:
#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;
}
}
答案 3 :(得分:2)
iPhone 3GS有加速度计,但没有陀螺仪。但是,http://techcrunch.com/2010/06/18/iphone-4-gyroscope-perspectiverse/表示支持陀螺仪。
答案 4 :(得分:0)
取决于不同的情况:
我同意阿里关于加速的答案,但不同意投掷时的答案。但无论如何请记住,一个让用户投入iPhone的应用程序将没有很好的机会获得应用商店批准, - )