[英]Synchronizing “SensorEvent.timestamp” with “System.nanoTime()” or “SystemClock.elapsedRealtimeNanos()”
The problem: 问题:
I need to synchronize some data set time-stamped using System.nanoTime()
with another one time-stamped using SensorEvent.timestamp
. 我需要将使用
System.nanoTime()
加上时间戳的某些数据集与使用SensorEvent.timestamp
加上时间戳的其他数据集进行同步。
Description: 描述:
I am working on System Identification of a quadcopter. 我正在研究四轴飞行器的系统识别。 Data acquisition is done using Android API.
数据采集是使用Android API完成的。 The input to my system is the pulse width modulation (PWM) which are time-stamped using
System.nanoTime()
with a frequency of 200 Hz. 我系统的输入是脉宽调制(PWM),它使用
System.nanoTime()
以200 Hz的频率进行时间戳记。
The output of the system is Androids's sensors measurements which are time-stamped using SensorEvent.timestamp
. 系统的输出是Android的传感器测量值,这些测量值使用
SensorEvent.timestamp
进行了时间戳记。
I've checked many online resources but unfortunately didn't help. 我检查了许多在线资源,但不幸的是没有帮助。 Can I access
SensorEvent.timestamp
whenever I want so I time-stamp the inputs using this clock? 是否可以随时访问
SensorEvent.timestamp
以便使用此时钟为输入加上时间戳记?
I'm afraid this problem is a bit complicated. 恐怕这个问题有点复杂。
SensorEvent.timestamp
switched from being System.nanoTime()
to SystemClock.elapsedRealtimeNanos()
at some point, but it is unclear when that happened in terms of devices and API levels. SensorEvent.timestamp
在某个时候从System.nanoTime()
切换为SystemClock.elapsedRealtimeNanos()
,但就设备和API级别而言,何时发生尚不清楚。 In fact, it appears to be completely manufacturer-dependent: https://code.google.com/p/android/issues/detail?id=56561 实际上,它似乎完全取决于制造商: https : //code.google.com/p/android/issues/detail?id=56561
Furthermore, these clocks count relative to different events or points in time, and nanoTime()
does not necessarily continue counting in deep sleep mode; 此外,这些时钟相对于不同事件或时间点进行计数,并且
nanoTime()
不必在深度睡眠模式下继续计数; Android: time intervals with deep sleep (System.nanoTime(), System.currentTimeMillis(), SystemClock.elapsedRealtimeNanos()) Android:具有深度睡眠的时间间隔(System.nanoTime(),System.currentTimeMillis(),SystemClock.elapsedRealtimeNanos())
The only way we found to solve the problem is to periodically record System.currentTimeMillis()
, System.nanoTime()
and SystemClock.elapsedRealtimeNanos()
as atomically as possible, and use the offset between SensorEvent.timestamp
and both System.nanoTime()
and SystemClock.elapsedRealtimeNanos()
to figure out which one the sensor is using. 我们发现解决问题的唯一方法是尽可能原子地定期记录
System.currentTimeMillis()
, System.nanoTime()
和SystemClock.elapsedRealtimeNanos()
,并使用SensorEvent.timestamp
与System.nanoTime()
之间的偏移量和SystemClock.elapsedRealtimeNanos()
确定传感器正在使用哪个传感器。 Once you have that you can get an absolute timestamp for each sensor event adding the delta between your SensorEvent.timestamp
and System.nanoTime()
or SystemClock.elapsedRealtimeNanos()
(respectively) to your System.currentTimeMillis()
. 一旦有了,就可以为每个传感器事件获取绝对时间戳,分别将
SensorEvent.timestamp
和System.nanoTime()
或SystemClock.elapsedRealtimeNanos()
之间的增量分别添加到System.currentTimeMillis()
。 It's not perfect, and could be off by as much as a few millisecond, but it's as good as we could get it. 它不是完美的,可能会延迟几毫秒,但它和我们能得到的一样好。
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.