Search code examples
androidaccelerometerandroid-sensorsgyroscopemagnetometer

Difference between Android sensors's sampling rates


I am trying to write accelerometer, gyroscope and magnetometer sensor values into txt file. I wrote this sensor values into different files. But I realized that when I give sensors's delay to fastest, accelerometer and gyroscope sensors had same number of samplings but, unlike them, magnetometer sensor has almost half of number samplings. Then I tried all of sensors delay number to 200000 ms and their sampling number was almost same. I want to know why this difference is caused. I am new to Android and sensors so a little help will be really appreciated.

Thanks.

     if(sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

            x_axis = event.values[0];
            y_axis = event.values[1];
            z_axis = event.values[2];

            try {
                writeToFile("accelerometer.txt", letter + " " + subject + " " + timestamp + " " + x_axis + " " + y_axis + " " + z_axis + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(sensor.getType() == Sensor.TYPE_GYROSCOPE) {

            gyro_x_axis = event.values[0];
            gyro_y_axis = event.values[1];
            gyro_z_axis = event.values[2];

            try {
                writeToFile("gyroscope.txt", letter + " " + subject + " " + timestamp + " " + gyro_x_axis + " " + gyro_y_axis + " " + gyro_z_axis + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if(sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
            mag_x_axis = event.values[0];
            mag_y_axis = event.values[1];
            mag_z_axis = event.values[2];

            try {
                writeToFile("magnetometer.txt", letter + " " + subject + " " + timestamp + " " + mag_x_axis + " " + mag_y_axis + " " + mag_z_axis + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Solution

  • Sensors are physical devices. That means they all respond to inputs at different rates. For example, the accelerometer sensor may be able to detect a change in 2ms, and the light sensor may take 100ms. So you have two choices- either sample at the rate of the slowest sensor on the device (in which case you lose 98% of the data from the accelerometer in this example) or have each sensor have a different sampling rate for FASTEST. Android chose the second, because otherwise you'd lose a lot of valuable data.

    What FASTEST is for each device and sensor will vary- different devices will have different implementations of sensors that work in different ways.