0

I'm working with a Samsung Galaxy sii phone which has a gyroscope, magnometer, and accelerometer. Everything is using NDK.

I am using the toadlet library to abstract out a lot of the phone interface, but within that library there is function to set the sampling rate which calls the expected NDK function:

void AndroidSensorDevice::setSampleTime(int dt){
    if(mSensor!=NULL){
        ASensorEventQueue_setEventRate(mEventQueue,mSensor,dt*1000);
    }
} 

When I change the sampling rates of the accelerometer and magnometer I see, roughly, the expected change in actual sampling rates. For the gyroscope, though, it always updates at only the fastest rate, i.e. no matter what I set the sample rate to I get time differences similar to this:

I/toadlet (10266): dt= 0.619000
I/toadlet (10266): dt= 4.423000
I/toadlet (10266): dt= 0.511000
I/toadlet (10266): dt= 4.335000
I/toadlet (10266): dt= 0.586000
I/toadlet (10266): dt= 4.371000
I/toadlet (10266): dt= 0.586000
I/toadlet (10266): dt= 4.559000
I/toadlet (10266): dt= 0.340000
I/toadlet (10266): dt= 3.827000
I/toadlet (10266): dt= 0.617000
I/toadlet (10266): dt= 5.337000
I/toadlet (10266): dt= 0.154000
I/toadlet (10266): dt= 3.538000
I/toadlet (10266): dt= 0.556000
I/toadlet (10266): dt= 4.547000

Even trying ridiculously large sample rates has no effect. Has anyone else seen this?

The phone is running Android 2.3.5 and I've tried using both android-9 and android-14 targets with no noticeable difference.

ryan0270
  • 1,135
  • 11
  • 33

1 Answers1

2

Based on the documentation for the setEventRate() function, the sample rate you set is really only a hint, not a definite specification. While the behavior you're seeing is unwanted, it's not wrong. Since most applications need to integrate the gyro output anyway, this behavior is not entirely unreasonable. If you really need a lower rate, I would suggest averaging samples together and reading out the results based on the timestamps.

/*
 * Sets the delivery rate of events in microseconds for the given sensor.
 * Note that this is a hint only, generally event will arrive at a higher
 * rate. It is an error to set a rate inferior to the value returned by
 * ASensor_getMinDelay().
 * Returns a negative error code on failure.
 */
int ASensorEventQueue_setEventRate(ASensorEventQueue* queue, ASensor const* sensor, int32_t usec);
Steven Bell
  • 622
  • 5
  • 11
  • I know the value passed in is only a hint, but in this case it has absolutely zero effect. For other sensors I can see the event rate change for values above a certain threshold but that never happens with the gyroscope. – ryan0270 Jun 13 '12 at 05:42
  • Understood, but remember that the underlying hardware may be completely different, and there's no intrinsic reason that the sensors should all behave the same way. In the typical scenario of deriving orientation from the sensors, the gyro has to be sampled continuously, while the other two sensors can be read only occasionally. Because of this fundamental difference, it doesn't surprise me that the driver/hardware would behave this way. – Steven Bell Jun 16 '12 at 18:14
  • It's not the underlying hardware; when using Android 8, before the NDK has this function and just used RATE_GAME, RATE_FASTEST, etc, the sampling rate could be changed. – ryan0270 Jun 16 '12 at 21:43
  • Have you tried disabling the sensor, re-enabling it, and setting a new event rate on the queue? – stuyguy Sep 15 '16 at 16:50