Accelerometer logger: experiencing occasional long delays between frames

╄→尐↘猪︶ㄣ 提交于 2019-12-04 18:04:48

You absolutely should be using event.timestamp. If you want local time, calculate the adjustment factor between event.timestamp and System.currentTimeMills() on the first event, and apply the same adjustment to subsequent samples.

The whole point of a hardware-provided timestamp attached to the sample is that it isn't messed up by thread scheduling delays.

As Ben Voigt said, it's necessary to use the event.timestamp in order to get accurate timestamps for the sensor measurements. Here's a code sample I've used myself and worked for me:

@Override
public void onSensorChanged(SensorEvent event) {
    if (sampleCounter == 0) {
        long miliTime = System.currentTimeMillis();

        long nanoTime = event.timestamp;

        timeDiff = miliTime - nanoTime / 1000000;
        log.info("Synchornizing sensor clock. Current time= " + miliTime
                + ", difference between clocks = " + timeDiff);
    }

    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    long ts = event.timestamp / 1000000 + timeDiff;

    //Do your stuff

    sampleCounter++;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!