SensorEvent.timestamp 和 Location.getElapsedRealtimeNanos() 时间戳延迟偏移量

SensorEvent.timestamp and Location.getElapsedRealtimeNanos() Timestamp Delay Offset

我目前正在从加速度计、磁力计和陀螺仪获取时间戳,并在 android 设备上执行传感器与 GPS 定位的融合。我正在使用 SensorEvent.timestamp and Location.getElapsedRealtimeNanos().

获取传感器时间戳

我的代码如下:

传感器时间戳

public void onSensorChanged( SensorEvent event ) {
    if( event.sensor.getType() == Sensor.TYPE_ACCELEROMETER )
        System.out.println( "Acc:" + event.timestamp );
}

GPS 时间戳

public void onLocationChanged( Location loc ) {
    System.out.println( loc.getElapsedRealtimeNanos() );
}

我的问题 是时间戳被任意量偏移。我知道这一点,因为所有 GPS 值都通过与其余传感器的一些偏移量聚集在一起。有时此偏移量以分钟为单位,有时以小时为单位,有时超前有时滞后。为什么我的实施中存在这种延迟,我该如何解决?

这是我所说的聚类的快速图表。我对时间戳进行了排序,锐利脱节后的时间戳都是与 GPS 测量相关的时间戳。

在日志中,数据是顺序输出的。消息类型 1 和 4 与传感器读数有关,而 -1 与 GPS 相关。如您所见,时间戳不是单调的。其余 GPS 时间戳与传感器的偏移量相似。请注意,此数据点来自另一个数据集。

我用下面的代码输出时间。

System.out.println( SystemClock.elapsedRealtimeNanos() );

查看hooks中的系统时钟,GPS时间戳是一致的。然而,传感器显然偏离了系统时钟。第一列是系统时钟,第二列是来自各个事件对象的时间戳,第三个时间戳是消息类型(GPS 为 -1,其他为 IMU 传感器)。

我调查过的事情

我还发现 GPS 时钟同步延迟了大约 10-15 秒,但由于我使用的是开机后的时间,所以这应该不是问题。

我已经研究过这个 SO question 但我认为它不适用,因为该问题的延迟似乎是一致的(100 毫秒),并且相对于我所经历的而言,幅度较小。

尽管我很想在这些事件挂钩中使用 SystemClock.elapsedRealtime(),但我知道在测量传感器和调用事件之间存在延迟。我不想在我的模型中引入任何 delay/uncertainty。

经过数小时的挖掘,我还发现了许多 android 几年前的错误,并且大多数都被标记为已过时。我真的很难过。任何关于这个问题的线索都将不胜感激。

答案很简单,SensorEvent.timestamp有一个任意零参考:

It turns out after a bit of Googling (tip o' the hat to Whosebug, as usual) that the timestamp one receives isn't based off of any particular 0-point defined in the Android OS or the API; it's an arbitrary per-sensor value intended to allow for different measurements from the same sensor to be compared, not for the measurements to be compared to other timestamped events. It's a known issue (bug concerning the documentation; bug concerning the behavior), but as of right now it's the way of the world for Android developers.

来源: http://fixermark.blogspot.ca/2014/06/quirkiness-of-android-sensor-library.html

我的解决方案是通过将 SystemClock.elapsedRealtimeNanos() 添加到日志中并估计每个传感器的 delay/offset 来估计偏移量。