Android:如何控制onSensorChange采样率
我对 Android 中的传感器读取率有点困惑。下面的代码报告约 53 毫秒的延迟(ZTE Blade,速率传感器事件设置为 SENSOR_DELAY_FASTEST)。
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
switch (event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
TimeNew = event.timestamp;
delay = (long)((TimeNew - TimeOld)/1000000);
TimeOld = TimeNew;
Log.d("Test", delay + " ms");
break;
}
}
}
日志:
DEBUG/Test(23024): 52 ms
DEBUG/Test(23024): 53 ms
DEBUG/Test(23024): 54 ms
DEBUG/Test(23024): 56 ms
DEBUG/Test(23024): 52 ms
DEBUG/Test(23024): 52 ms
DEBUG/Test(23024): 55 ms
DEBUG/Test(23024): 52 ms
如果我们要平均 100 个样本,然后保存数据,则每个第 100 个样本之间的时间会有很大差异。这可能是因为传感器值在固定时间段内没有变化。
但我错过了什么吗?有没有办法获得(更多)定期测量(例如 100 毫秒)?或者也许我应该在特定时间段内取平均值而不是样本数量?
而且50ms似乎有点长。这可能是设备的硬件限制吗?这个数字在不同平台上会有所不同吗?
任何建议表示赞赏。
I am a bit puzzled by the sensor reading rates in Android. The code below reports delays of ~53 ms (ZTE Blade, rate sensor events is set to SENSOR_DELAY_FASTEST).
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
switch (event.sensor.getType()) {
case Sensor.TYPE_MAGNETIC_FIELD:
TimeNew = event.timestamp;
delay = (long)((TimeNew - TimeOld)/1000000);
TimeOld = TimeNew;
Log.d("Test", delay + " ms");
break;
}
}
}
The log:
DEBUG/Test(23024): 52 ms
DEBUG/Test(23024): 53 ms
DEBUG/Test(23024): 54 ms
DEBUG/Test(23024): 56 ms
DEBUG/Test(23024): 52 ms
DEBUG/Test(23024): 52 ms
DEBUG/Test(23024): 55 ms
DEBUG/Test(23024): 52 ms
If we want to average say 100 samples, and then save the data, the time between each 100th sample will vary significantly. That is presumably because the sensor value is not changing in regular time periods.
But am I missing something ? Is there a way to get (more) regular measurements (e.g. 100 ms) ? Or perhaps I should go for averaging over a specific period of time instead of no.of samples ?
Also 50ms seems to be a bit long time. Could that be hardware limitation of the device ? Will this number vary on different platforms ?
Any advice is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会在一段时间内而不是多个样本上进行平均。如果您采用基于数字的方法,我预计具有不同设备功能的不同设备会产生截然不同的结果。如果您想要对采样进行更定期的管理,我可能会断开测试与事件的连接,并以您希望的任何频率轮询设备。
I would average over a period of time rather than a number of samples. I would expect different devices with different device capabilities would generate substantially different results if you were to go with the number-based approach. If you want more regular management of the sampling I would probably disconnect the test from the event and just poll the device at whatever frequency you wish.