Android sampling rates variation of hardware Sensors on Nexus 6P

不羁岁月 提交于 2021-01-27 07:56:24

问题


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:

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

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

(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).

  • Device 1: 180000 Samples
  • Device 2: 177273 Samples <- the phone that is lagging behind
  • Device 3: 181800 Samples
  • Device 4: 179412 Samples

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.


回答1:


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;
        }

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.

Good luck!



来源:https://stackoverflow.com/questions/52589631/android-sampling-rates-variation-of-hardware-sensors-on-nexus-6p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!