Reputation: 63
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.
Upvotes: 5
Views: 9790
Reputation: 2179
From API 19 (2013+) onwards, there is a new variant of the registration API, in which you could mention at what interval you would like to receive the sensor readings. From the docs:
boolean registerListener (SensorEventListener listener, Sensor sensor, int samplingPeriodUs, int maxReportLatencyUs) Registers a SensorEventListener for the given sensor at the given sampling frequency and the given maximum reporting latency.
This function is similar to registerListener(SensorEventListener, Sensor, int) but it allows events to stay temporarily in the hardware FIFO (queue) before being delivered. The events can be stored in the hardware FIFO up to maxReportLatencyUs microseconds. Once one of the events in the FIFO needs to be reported, all of the events in the FIFO are reported sequentially. This means that some events will be reported before the maximum reporting latency has elapsed.
Upvotes: 0
Reputation: 13240
Events may be received faster or slower than the specified rate
From https://developer.android.com/reference/android/hardware/SensorManager.html
Events may be received faster or slower than the specified rate. Usually events are received faster. Can be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, SENSOR_DELAY_FASTEST or the delay in microseconds.
Upvotes: 0
Reputation: 9870
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.
Upvotes: 4