0

for my Master thesis i am developing an android application able to collect data from the 3 axes accelerometer of a Samsung Galaxy S5. Cause I want to track human activity I need a sampling rate of approximately 50Hz. I achieve this by setting the sampling rate to SENSOR_DELAY_GAME which works fine. To check the sampling rate, I display the length of the List which contains the acceleromter data on the phones display. Everything works fine when the screen of the phone is on, even if I switch during data collection between different applications. Doing so, I receive about 1500 values in 30 seconds, which means that the accuracy of the sampling rate is suitable for my purposes.

My Problem now is, that when the screen is in timeout, the accelerometer sampling rate decreases to 5Hz and changes up to 50 Hz again when the screen is on. Is there a way to fix the accelerometer sampling rate to 50 Hz?

PS: I would prefer a solution without setting the screen to ON permanently to save battery life.

thanks, Ziegi

Edit: I added the necessary methods which are needed for the accelerometer. The already existing methods onPause(), onStop() and onResume() are not used cause the measurement should only stop if the stop button click event happens.

 public void initListeners(){
    mSensorManager.registerListener(this,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
            SensorManager.SENSOR_DELAY_GAME);

}

public void onSensorChanged(SensorEvent event) {
    if (startFlag == true) {

        switch(event.sensor.getType()) {
            case Sensor.TYPE_ACCELEROMETER:
                // copy new accelerometer data into accel array
                // then calculate new orientation
                System.arraycopy(event.values, 0, accel, 0, 3);
                calculateAccMagOrientation();

                xText.setText("X: " + decimal.format(event.values[0]));
                yText.setText("Y: " + decimal.format(event.values[1]));
                zText.setText("Z: " + decimal.format(event.values[2]));


                xValue.add(event.values[0]);
                yValue.add(event.values[1]);
                zValue.add(event.values[2]);


                try {
                    xSize.setText(String.valueOf(xValue.size()));} catch(Exception e) {
                    Toast.makeText(getBaseContext(), "Fehler bei Add to List", Toast.LENGTH_SHORT).show();
                }
                break;

        }

}
}
Ziegi
  • 1
  • 2

3 Answers3

0

My Problem now is, that when the screen is in timeout, the accelerometer sampling rate decreases to 5Hz and changes up to 50 Hz again when the screen is on. Is there a way to fix the accelerometer sampling rate to 50 Hz?

PS: I would prefer a solution without setting the screen to ON permanently to save battery life.

This is the whole point. When the screen is off, less power should be used. A user also expects the power usage to be lower. Sampling at a lower rate is one of many ways that any decent operating system deals with power constraint situations.

As such I would be very surprised if there was any supported way of increasing the sample rate while the phone has it's screen off.

user9993
  • 5,833
  • 11
  • 56
  • 117
  • But i think you can't compare energy consumption of sampling rate at 50Hz and permanent screen on. If there is the opportunity the set the screen permanently on, why should it be impossible to set the sampling rate to a constant value... – Ziegi Mar 13 '16 at 12:47
  • I found this approach in another thread. Maybe someone can help me with that? "Why not do it manually. Set a Thread to wake every 33 ms (30 Hz) and get the Accelerometer reading at these times." – Ziegi Mar 13 '16 at 13:03
0

You probably want to run the code which reads the accelerometer as a service instead of in an App. This is described well by AleAdam at this similar answer here: Keep UI Thread running when screen is off

Several tutorials are mentioned on that other thread answer.

Community
  • 1
  • 1
Sevron
  • 64
  • 3
0

What you probably want is to keep your CPU running at the same rate, try to use Wakelock:

PowerManager powerManager = null;
PowerManager.WakeLock wakeLock = null;    

powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyWakelockTag");
wakeLock.acquire();