Constant Timestamp Android SensorEvent

I am currently writing a simple Android application to calculate imu device offsets. While doing this, I ran into a problem with the value of event.timestamp

Using the code:

float dT = (event.timestamp-accel_timestamp)*NS2S; 

from the example in the Android Reference Guide for calculating the rotation matrix from a quaternion.

When I run the code using Galaxy Nexus-S, I get dT 0.06-0.07 seconds between measurements, but when I run the same code on LG Nexus 4 or Nexus 7, dT is always 0. I know the issue, the problem with an Android SensorEvent timestamp that the Nexus 7 timestamp is a unix timestamp, but the difference between consecutive measurements should not always be zero. On Nexus 4 and Nexus 7, both of the same IMU may be a mistake in how the timestamp is created from the IMU?

+4
source share
3 answers

Wow, this is definitely a mistake!

The timestamp of each SensorEvent is overwritten as if it were a static variable ...

When I record a timestamp string when an event occurs, all values ​​are different.

Events are stored in the array unchanged.

Each SensorEvent element in the array now has the same time stamp, but the arrays of values ​​are still different (i.e. they are not the same object and contain different information ... EXCEPT for the time stamp).

Google / HTC, please return 3 hours of my life!

I will write an error report if no one can explain this behavior. This is certainly not described in the API .

In the meantime, try this solution :

 import android.hardware.Sensor; import android.hardware.SensorEvent; public class UnbrokenSensorEvent { public long timestamp; public float[] values; public Sensor sensor; public UnbrokenSensorEvent(SensorEvent event){ this.timestamp = event.timestamp; this.values = event.values; this.sensor = event.sensor; } } 

Then do something like this in your listener:

 ArrayList<UnbrokenSensorEvent> results = new ArrayList<UnbrokenSensorEvent>(); public void onSensorChanged(SensorEvent event) { results.add(new UnbrokenSensorEvent(event)); } 

Refactoring should be fairly simple, as SensorEvent and UnbrokenSensorEvent have the same public fields. If you need to use other SensorEvent functions, just run it and paste it into the Unbroken version.

This is a hacked, but IMHO fast hack is always better than waiting for an API update!

+1
source

Also check out the documentation for SensorEventListener onSensorChanged -method:

NOTE. The application does not have an event object transmitted as and therefore cannot be held on it. An object can be part of an internal pool and can be reused by a wireframe.

Found here: http://developer.android.com/reference/android/hardware/SensorEventListener.html#onSensorChanged%28android.hardware.SensorEvent%29

... which suggests that you should not store references to SensorEvent objects.

+1
source

If you copied a fragment from here Please note that it has an error. Need to replace

 private float timestamp; 

to

 private long timestamp; 

Otherwise, your delta time will always contain a weird value.

0
source

All Articles