我正在编写一个应用程序,每隔40毫秒(25Hz)记录手机的加速度。这个帧速率可以保持平均,但有时我会遇到5'000ms的延迟 - 在时间帧之间50'000ms。我想知道为什么会这样。
这里有一个延迟图表,您可以看到它们经常发生:
这就是我在做什么(可能不好):
以下是我认为可能存在的问题:
event.timestamp
代替System.currentTimeMills()
。 (我不想这样做,因为有些传感器有不同的时区,这就是为什么我使用System.currentTimeMillis()
,但如果有必要我会切换。)您是否对此有任何经验或建议问题可能存在?
这是我的代码:
@SuppressLint("NewApi")
public class AccelerometerLogger implements SensorEventListener {
private static AccelerometerLogger singleton = new AccelerometerLogger();
private LoggerDao loggerDao;
private SensorManager sensorManager;
private Sensor accelerometer;
private double acceleorometerRate = 25; // Hz
int accelerometerDelayMicroseconds = (int) (Math.round(((1/this.acceleorometerRate)*1000000.0)));
private AccelerometerLogger()
{
this.loggerDao = LoggerDao.getInstance();
}
public static AccelerometerLogger getInstance()
{
return singleton;
}
public void start(Context context)
{
this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
this.accelerometer = this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
int accelerometerMinDelay = this.accelerometer.getMinDelay();
//Log.d("lggr-r", "desired delay: "+this.accelerometerDelayMicroseconds+" microseconds");
//Log.d("lggr-r", "provided min delay: "+accelerometerMinDelay+" microseconds");
if(accelerometerMinDelay < this.accelerometerDelayMicroseconds)
{
this.sensorManager.registerListener(this, this.accelerometer, this.accelerometerDelayMicroseconds);
//Log.d("lggr-r", "listener registered for desired rate: "+this.acceleorometerRate+"Hz (delay of "+this.accelerometerDelayMicroseconds+" microseconds).");
}
else if(accelerometerMinDelay==0)
{
this.sensorManager.registerListener(this, this.accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
// Log.d("lggr-r", "listener registered for streaming api. only changes will be notified (interrupt).");
}
else
{
int providedRate = (int) Math.round(1 / (accelerometerMinDelay / 1000000.0));
this.sensorManager.registerListener(this, this.accelerometer, SensorManager.SENSOR_DELAY_FASTEST);
// Log.d("lggr-r", "can't read at the desired rate ("+this.acceleorometerRate+"Hz), app will read at "+providedRate+"Hz instead (delay of "+accelerometerMinDelay+" microseconds).");
}
}
public void stop()
{
this.sensorManager.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// String name = sensor.getName();
// Log.d("lggr", "the accurracy of "+name+" changed to "+accuracy+".");
}
@Override
public void onSensorChanged(SensorEvent event)
{
// lazy load loggerDao (TODO: fix all of those)
if(this.loggerDao == null)
{
this.loggerDao = LoggerDao.getInstance();
}
String values = "";
for(float value : event.values) values += value+",";
values = values.substring(0,values.length()-2);
// long timestamp = System.currentTimeMillis();
// Log.d("lggr", "acc = {time:"+timestamp+", data: ["+values+"]}");
AccelerometerSample accelerometerSample = new AccelerometerSample();
accelerometerSample.setTimestamp(System.currentTimeMillis());
accelerometerSample.setValues(event.values);
this.loggerDao.save(accelerometerSample);
}
}
显然这个问题只发生在三星Galaxy SIII mini上。我用三星Galaxy SII(自定义ROM)测试了它,延迟总是大约0.04s(介于0.005和0.12s之间 - 好多了)。
您对Samsung Galaxy SIII mini发生这种情况有什么建议吗?
更新
Ben Voigts的回答旨在使用event.timestamp
,这显着改善了延迟。不过,我有时会遇到更长的延迟。你知道如何进一步改善它们吗?
答案 0 :(得分:4)
你绝对应该使用event.timestamp
。如果您想要当地时间,请计算第一个事件的event.timestamp
和System.currentTimeMills()
之间的调整系数,并对后续样本应用相同的调整。
附加到示例的硬件提供的时间戳的重点是线程调度延迟不会搞砸。
答案 1 :(得分:0)
正如Ben Voigt所说,使用event.timestamp是必要的,以便获得传感器测量的准确时间戳。这是我自己使用并为我工作的代码示例:
@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++;
}