简体   繁体   English

Android SensorEvent时间戳记常量

[英]Android SensorEvent Timestamp constant

I'm currently writing a simple android application to calculate the biases of the devices imu. 我目前正在编写一个简单的android应用程序,以计算设备imu的偏差。 While doing this I've come across a problem with the value of event.timestamp 在执行此操作时,我遇到了event.timestamp值的问题

Using the code: 使用代码:

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

from the example at the Android Reference Guide to calculate the rotation matrix from the quaternion. Android参考指南中的示例中,根据四元数计算旋转矩阵。

When I run the code using a Galaxy Nexus-S I get a dT of 0.06~0.07 sec between measurements but when I run the same code on a LG Nexus 4 or Nexus 7 the dT is always 0. I'm aware of the question, Android SensorEvent timestamp issue that the Nexus 7 timestamp is a unix timestamp but the difference between successive measurements shouldn't always be zero. 当我使用Galaxy Nexus-S运行代码时,两次测量之间的dT为0.06〜0.07秒,但是当我在LG Nexus 4或Nexus 7上运行相同的代码时,dT始终为0。我知道这个问题, Android SensorEvent时间戳问题是Nexus 7时间戳是unix时间戳,但连续测量之间的差值不应该始终为零。 The Nexus 4 and Nexus 7 both have the same IMU could this be a bug in how the timestamp is created from the IMU? Nexus 4和Nexus 7都具有相同的IMU,这可能是从IMU创建时间戳的过程中的错误吗?

Wow, ok this is surely a bug! 哇,好吧,这肯定是一个错误!

The timestamp of each SensorEvent is being overwritten as if it were a static variable... 每个SensorEvent的时间戳都被覆盖,就像它是一个静态变量一样。

When I record a string of the timestamp when the event occurs, all values are different. 当我记录事件发生时的时间戳字符串时,所有值都不同。

The events are stored in an array unchanged. 这些事件未更改地存储在数组中。

Every SensorEvent in the array now has the same timestamp, but the values arrays are still different (ie, they're not the same object and contain different information... EXCEPT for the timestamp). 现在,数组中的每个SensorEvent都具有相同的时间戳,但是value数组仍然不同(即,它们不是同一对象,并且包含不同的信息……时间戳除外)。

Google/HTC, please return 3 hours of my life! Google / HTC,请返回我生命中的3个小时!

I'll go file a bug report, unless anyone can explain this behaviour. 除非有人可以解释此行为,否则我将提交错误报告。 It's certainly not documented in the API . 它肯定没有在API中记录

In the meantime, try out 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));
}

The refactoring should be quite easy, since SensorEvent and UnbrokenSensorEvent have the same public fields. 重构应该非常容易,因为SensorEvent和UnbrokenSensorEvent具有相同的公共字段。 If you need to use other SensorEvent functionality, just go ahead and chuck it into the Unbroken version. 如果您需要使用其他SensorEvent功能,只需继续并将其插入完整版本即可。

It's hacky, but IMHO a quick hack is always better than waiting for an API to be updated! 它很hacky,但是恕我直言,快速hack总是比等待更新API更好!

Also do note the documentation on SensorEventListener's onSensorChanged -method: 还要注意有关SensorEventListener的onSensorChanged-方法的文档:

NOTE: The application doesn't own the event object passed as a parameter and therefore cannot hold on to it. 注意:该应用程序不拥有作为参数传递的事件对象,因此无法保留该对象。 The object may be part of an internal pool and may be reused by the framework. 该对象可以是内部池的一部分,并且可以由框架重用。

Found here: http://developer.android.com/reference/android/hardware/SensorEventListener.html#onSensorChanged%28android.hardware.SensorEvent%29 在这里找到: http : //developer.android.com/reference/android/hardware/SensorEventListener.html#onSensorChanged%28android.hardware.SensorEvent%29

...which suggests that one should not hold references to SensorEvent -objects. ...这表明不应保存对SensorEvent对象的引用。

If you copied snippet from here Notice it has a bug. 如果您从此处复制了代码段,请注意它有一个错误。 Need replace 需要更换

private float timestamp;

to

private long timestamp;

In other case your delta time will always contain weird value 在其他情况下,您的增量时间将始终包含奇怪的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM