view core/VirtualClock.cpp @ 139:4e2dd3eb1d28 ClockSync

The reported offset is now meaningful. The whole thing is waaay too jittery.
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 13 Sep 2015 21:34:47 +0100
parents e77e2e712fbc
children 44d07fa9bd03
line wrap: on
line source
#include "VirtualClock.h"
void VirtualClock::init(){
	firstRun=true;
	movingAverage.setLength(31); //TODO: a better filtering algorithm ( Did you say Kalman?)
	period=-1;
}
	
VirtualClock::VirtualClock(){
	init();
}
void VirtualClock::sync(){
	sync(1);
}
void VirtualClock::sync(double count){
	myClock_t currentTime=Clock::getTimeUs();
	if(firstRun==true){
		firstRun=false;
		startTime=currentTime;
	} else {
		period=movingAverage.add((currentTime-lastSync)/count); //TODO: replace with Kalman filter
	}
	lastSync=currentTime;
//	printf("lastSync: %lld\n",lastSync-startTime);
}

double VirtualClock::getNow(){
	myClock_t currentSystemTime=Clock::getTimeUs();
	if(period<=0){
		return currentSystemTime;
	}
	//  double beginningOfPeriod=lastSync; // TODO: if sync() does not get called every time (but e.g. only every so often),
													 // then this line (and the class) needs editing
	myClock_t elapsed=(currentSystemTime-startTime);
	double now=elapsed/(double)period;
//	printf("elapsed=%lld;  sincelastSync=%lld; period=%f; now=%f\n", elapsed, currentSystemTime-lastSync, period, now);
	return now;
}

double VirtualClock::getPeriod(){
	return period;
}