Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to read android sensors with fixed sampling rate

I am new to android programming. I need to develop an application which reads data from gyroscope with a sampling time of 0.05 seconds. I need to get a datum every 0.05 seconds.

I looked into android's sensor manager which gives four different types of sampling rates but they are not uniform.

like image 737
Manasa Reddy Avatar asked Oct 03 '22 17:10

Manasa Reddy


1 Answers

for which API are You developing? Since API 11 You can also specify the delay for getting results from the sensor. Usualy, the Sensor will be registered with four fixed delays:

SensorManager.SENSOR_DELAY_NORMAL (delay of 200000 microseceonds) (default value) SensorManager.SENSOR_DELAY_GAME (delay of 20000 microseconds) SensorManager.SENSOR_DELAY_UI (delay of 60000 microseconds) SensorManager.SENSOR_DELAY_FASTEST (delay of 0 microseconds)

The Sensor will be registered as follows:

   mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);

So if You are developing from up to API 11, You can register

   mSensorManager.registerListener(this, mSensor, 50000);

If not, than it´s gonna be hard, You can´t get the result every 0.05 Seconds, but You can get the result for every 0,06 seconds by setting SENSOR_DELAY_UI. Another possibility is to set SENSOR_DELAY_FASTEST and count up to 50000 with something like

    private int value = 0:

and every onSensorChanged() event

    value++;

until You reached 50000. But this is no good practise, as the Sensor is only firing event if He could. If the system is high busy there is no guaranty that the sensor is firing everytime.

like image 155
Opiatefuchs Avatar answered Oct 07 '22 18:10

Opiatefuchs