ios 如何使用加速度计计算步数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8310250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count steps using an Accelerometer?
提问by Tariq
I have to develop the same functionality as of this Pedometer App
我必须开发与此计步器应用程序相同的功能
I have observed this Pedometer appin very high detail.
我非常详细地观察了这个计步器应用程序。
It's not a perfect pedometer app. For example, if you stay/sit at one place and shake your hand, it also detects steps counts and distance.
这不是一个完美的计步器应用程序。例如,如果您停留/坐在一个地方并握手,它还会检测步数和距离。
Ignore this ideal and gravity behavior, because in the instructions of this app it's already been mentioned that you should tie up your iPhone or you should place it in your pocket to count steps. This way, I have found this app working very well, it detects almost all steps.
忽略这种理想和重力行为,因为在这个应用程序的说明中已经提到你应该把你的 iPhone 绑起来或者你应该把它放在你的口袋里来计算步数。通过这种方式,我发现这个应用程序运行良好,它几乎可以检测所有步骤。
My problem is: I have developed one sample according to the above logic, but it's not working up to that level. For example, sometimes it detects 2-3 steps at the same time. And sometimes it works fine.
我的问题是:我根据上述逻辑开发了一个样本,但它没有达到那个水平。例如,有时它会同时检测 2-3 个步骤。有时它工作正常。
My code:
我的代码:
In viewDidLoad:
在 viewDidLoad 中:
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:0.2]
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
const float violence = 1.2;
static BOOL beenhere;
BOOL shake = FALSE;
if (beenhere) return;
beenhere = TRUE;
if (acceleration.x > violence || acceleration.x < (-1* violence))
shake = TRUE;
if (acceleration.y > violence || acceleration.y < (-1* violence))
shake = TRUE;
if (acceleration.z > violence || acceleration.z < (-1* violence))
shake = TRUE;
if (shake) {
steps=steps+1;
}
beenhere = false;
}
What am I doing wrong? I am not able to determine the threshold. If I make it high, it won't detect minor steps. If I make it small, it registers 3-4 steps simultaneously.
我究竟做错了什么?我无法确定阈值。如果我把它调高,它就不会检测到小步骤。如果我把它变小,它会同时注册 3-4 个步骤。
Is there any other implementation required to do this, or some tweaks in this code?
是否需要其他任何实现来执行此操作,或者此代码中是否有一些调整?
I have seen all other similar Stack Overflow links. Nothing I have found performs up to this level.
我已经看到了所有其他类似的 Stack Overflow 链接。我发现没有任何东西能达到这个水平。
Please help.
请帮忙。
回答by Hot Licks
Been counting snores, not steps, but have some of the same issues. No actual answers, but some suggestions:
一直在数打鼾,而不是步数,但有一些相同的问题。没有实际答案,但有一些建议:
- Require a time interval between steps. Yes, someone can be walking slowly or jogging, but even at the fastest there is a time interval of maybe 1/5 second between steps. If "impacts" appear more rapid than that they're likely just from rebound/rattling.
- Rather than your fixed threshold (
violence
) employ a variable threshold, based on a moving averageof previous events. - Consider keeping separate x, y, and z thresholds, based on the assumption that the phone will not, over a short period of time, change orientation.
- Rather than just ignoring events stronger than a certain level, consider ignoring those outside a range, with limits specified by two thresholds (one perhaps a fraction of the other).
- Consider what happens when you walk -- there is a forward/backward acceleration of the body that is quite rhythmic, along with a "shock" as the foot strikes the ground. It may be best to attempt to ignore the shock (a fairly short-term signal) and instead look for the rhythmic forward/backward motion.
- 步骤之间需要时间间隔。是的,有人可以慢走或慢跑,但即使在最快的情况下,步骤之间也可能有 1/5 秒的时间间隔。如果“影响”看起来比这更快,那么它们很可能只是反弹/嘎嘎作响。
- 而不是您的固定阈值 (
violence
) 使用可变阈值,基于先前事件的移动平均值。 - 考虑保持单独的 x、y 和 z 阈值,假设手机不会在短时间内改变方向。
- 与其仅仅忽略强于某个级别的事件,不如考虑忽略范围之外的事件,其限制由两个阈值指定(一个阈值可能是另一个阈值的一小部分)。
- 想想当你走路时会发生什么——身体有一个很有节奏的向前/向后加速,伴随着脚着地时的“震动”。最好尝试忽略冲击(一个相当短期的信号),而是寻找有节奏的向前/向后运动。
Another suggestion
另一个建议
Testing this beast "live" would be impossible. (I can imagine you trying to jog along while holding the laptop in front of you, trying to get the debugger console to focus.) What you should do is first rig your app to make some recordings (ie, write files) containing the raw measurements, then re-rig your app (#ifdefs would be handy here) to be able to "play back" those measurements so that you can step through the app with the debugger and observe its behavior.
“活”测试这头野兽是不可能的。(我可以想象你试图在将笔记本电脑放在你面前的同时慢跑,试图让调试器控制台聚焦。)你应该做的是首先装备你的应用程序来制作一些包含原始数据的录音(即,写入文件)测量,然后重新装配您的应用程序(#ifdefs 在这里很方便)以便能够“回放”这些测量值,以便您可以使用调试器逐步浏览应用程序并观察其行为。
回答by kalpesh
var motionManager = CMMotionManager()
motionManager.deviceMotionUpdateInterval = 0.1
motionManager.startDeviceMotionUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler:{
deviceManager, error in
var accelerationThreshold:Double = 1;
var userAcceleration:CMAcceleration = deviceManager.userAcceleration;
if(fabs(userAcceleration.x) > accelerationThreshold) || (fabs(userAcceleration.y) > accelerationThreshold) || (fabs(userAcceleration.z) > accelerationThreshold)
{
println("LowPassFilterSignal")
}
})