Reading Sensor Values with high frequency

412 views
Skip to first unread message

steckl

unread,
May 7, 2012, 5:07:55 PM5/7/12
to android-ndk
Hi,
I'm writing an app, that should read out sensor values as fast as
possible. For this I use a native activity. But it's not faster than
doing the same from SDK. For the magnetic sensor I get a sample rate
of about 100Hz, that's good enough, but for the Acc-Sensor the sample
rate is only about 50Hz. Can this sample rate be improved somehow?

Here is the Code I used:
void Java_de_tum_ndktest_TestJNIActivity_sensorValue( JNIEnv* env,
jobject thiz ) {
ASensorEvent event;
int events, ident;
ASensorManager* sensorManager;
const ASensor* accSensor;
void* sensor_data = malloc(1000);

ALooper* looper = ALooper_forThread();

if(looper == NULL)
{
looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
}

sensorManager = ASensorManager_getInstance();

accSensor = ASensorManager_getDefaultSensor(sensorManager,
ASENSOR_TYPE_ACCELEROMETER);

sensorEventQueue = ASensorManager_createEventQueue(sensorManager,
looper, 3, get_sensor_events, sensor_data);

ASensorEventQueue_enableSensor(sensorEventQueue, accSensor);

int a = ASensor_getMinDelay(accSensor);

ASensorEventQueue_setEventRate(sensorEventQueue, accSensor, a);

LOGI("sensorValue() - START");
}




static int get_sensor_events(int fd, int events, void* data) {
ASensorEvent event;
//ASensorEventQueue* sensorEventQueue;
while (ASensorEventQueue_getEvents(sensorEventQueue, &event, 1) > 0)
{
if(event.type == ASENSOR_TYPE_ACCELEROMETER) {
if(SensorSamplingRate == 0)
{
LOGI("Time1 of SensorValue %lld", event.timestamp);
}
if(SensorSamplingRate == 1000)
{

LOGI("Time2 of SensorValue %lld", event.timestamp);
}

SensorSamplingRate++;
}

}
//should return 1 to continue receiving callbacks, or 0 to
unregister
return 1;
}

steckl

unread,
May 9, 2012, 1:51:07 PM5/9/12
to andro...@googlegroups.com
I have a weird behaviour. I modified the code in a way, that it uses Acc-, Gyro- and magnetic sensor at the same time. Now the sample rate for all three sensors is about 100Hz. This means that the Acc-Sensor doubles its frequency when combining it with the other sensors. Can someone explain this behaviour?
The same thing happens if I use SDK instead of NDK.
Reply all
Reply to author
Forward
0 new messages