Nexus 6P上Android硬件传感器的采样率变化 [英] Android sampling rates variation of hardware Sensors on Nexus 6P

查看:71
本文介绍了Nexus 6P上Android硬件传感器的采样率变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序以进行研究,并即时读取多个传感器数据,例如加速度计,陀螺仪,气压计等.因此,我有4台Nexus 6P设备,所有设备均具有最新的工厂映像,并且没有其他设置预先安装了比标准版本安装的应用程序.因此,现在出现的问题是其中一部电话一直处于落后状态,例如,我以105 Hz的频率记录了半小时的加速度计(因此,加速度计的最大可能速率为400Hz),只是为了确保我能至少大约是我希望100Hz采样的数量,结果如下:

I'm developing an Android app, for a research, and im reading several Sensor data like accelerometer, gyroscope, barometer etc. So I have 4 Nexus 6P devices all with the newest Factory Image and freshly set up with no other app installed than the standard once which are pre-installed. So the Problem that occurs now is that one of the phones is constantly lagging behind, so for example i record for half an hour the accelerometer at 105 Hz (so the max possible rate for the accelerometer is 400Hz), just to make sure i get at least about the amount of samples i would expect for 100Hz and the results are the following:

以100Hz采样半小时-> 180000个采样

Smapling for Half an hour at 100Hz -> 180000 Samples

以105Hz采样半小时-> 189000个采样

Smapling for Half an hour at 105Hz -> 189000 Samples

(这只是加速度计的一个示例,但对于每个设备上的每个其他传感器都是相同的.因此,设备1,3,4对于其他传感器可获得大致相同的好结果,而设备2在其上可获得相同的不良结果.所有其他传感器).

(This is now just an example for the accelerometer but is the same for every other sensor on each device. So device 1,3,4 get about the same good results for other senors while device 2 gets the same bad results on all other sensors).

  • 设备1:180000个样本
  • 设备2:177273采样<-落后的手机
  • 设备3:181800个样本
  • 设备4:179412个样本

所以问题出在2号设备上,我缺少了将近3000个样本(我知道这在高水平上是哭泣),我对此问题的猜测是可能与硬件有关.我可能会排除这可能是一个性能问题,因为无论读取多少传感器,也可以以400Hz的频率读取它们,都可以按预期工作(如果需要,我也可以提供示例).我还尝试将采样率设置为400Hz,以使其达到最快,然后根据时间戳进行记录,从而得到相同的结果.

So the problem is at device number 2 where I'm missing almost 3000 Samples (I know this is crying at a high level) and my guess for this problem is that it is probably Hardware related. That it might be a performance issue i can probably rule out since it does not matter how many Sensors im reading and also reading them at 400Hz works as expected (if wanted i can also offer the Samples for this too). I also tried to set the sampling rate to 400Hz so to the fastest and then take recordings according to the timestamp which led to the same result.

以防万一,我将提供注册传感器侦听器的方式:

So just in case I'll provide how I register the Sensor Listener:

protected void onCreate(Bundle savedInstanceState){
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    unaccDataSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER_UNCALIBRATED);
}
....
private void Start(){
    sensorManager.registerListener(unaccDataListener, unaccDataSensor, 10000);
}

所以我想要的是至少得到我应该期望的样本数量,因此上面没有问题,仅低于一点点也是可以接受的.因此,如果有人对我可以尝试的其他方法或导致问题的原因有所了解,我将非常感激.

So what i want is to get at least about the amount of samples that i should expect so above is no problem and just a bit below is also acceptable. So if anyone has an idea about what else I can try or what can cause the problem i would be really thankful.

这是我的第一篇文章,因此,如果缺少任何内容,或者如果我用不好的方式解释了某些内容,对不起,我会尽力解决.

This is my first Post so if anything is missing or if i explained something in a bad way im sorry and i try my best to fix it.

推荐答案

我经常使用Android传感器,而且我可以告诉您硬件的质量是可变的.如果需要在手机之间保持一致的结果,通常会使用过滤器:

I work with Android sensors a lot, and i can tell you the hardware is of variable quality. I usually use a filter if I need the results to be consistent across phones:

// Filter to remove readings that come too often
        if (TS < LAST_TS_ACC + 100) {
            //Log.d(TAG, "onSensorChanged: skipping");
            return;
        }

但是,这意味着您只能将手机设置为与最低公分母匹配.如果有帮助,我发现对于大多数应用,甚至是医疗应用,超过25hz都是过大的.

however this means you can only set the phones to match the lowest common denominator. If it helps I find that getting any more than 25hz is overkill for most applications, even medical ones.

由于写入文件是一项昂贵的操作,因此它还可以帮助确保正在执行的所有文件写操作都是在线程外进行的,并且是分批进行的.

It can also help to make sure any file writes you are doing are done off thread, and in batches, as writing to file is an expensive operation.

accelBuffer = new StringBuilder();
accelBuffer.append(LAST_TS_ACC + "," + event.values[0] + "," + event.values[1] + "," + event.values[2] + "\n");

if((accelBuffer.length() > 500000) && (writingAccelToFile == false) ){
                writingAccelToFile = true;

                AccelFile = new File(path2 +"/Acc/"  + LAST_TS_ACC +"_Service.txt");
                Log.d(TAG, "onSensorChanged: accelfile created at : " + AccelFile.getPath());

                File parent = AccelFile.getParentFile();
                if(!parent.exists() && !parent.mkdirs()){
                    throw new IllegalStateException("Couldn't create directory: " + parent);
                }

                //Try threading to take of UI thread

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //Log.d(TAG, "onSensorChanged: in accelbuffer");
                        // Log.d(TAG, "run: in runnable");
                        //writeToStream(accelBuffer);
                        writeStringBuilderToFile(AccelFile, accelBuffer);
                        accelBuffer.setLength(0);
                        writingAccelToFile = false;

                    }
                }).start();

            }

完成上述所有操作都为我带来了不错的成绩,但是由于硬件的差异,它永远不会是完美的.

Doing all of the above has got me reasonably good results, but it will never be perfect due to differences in the hardware.

祝你好运!

这篇关于Nexus 6P上Android硬件传感器的采样率变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆