diff core/ClockSync.cpp @ 149:134bff10e561 ClockSync

Added simple one-variable one-measurement Kalman filter, Pid controller(which output is not used). Virtual clock is now much more precise and reactive for period. Still it is lagging behind a bit on the overall offset.
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 21 Sep 2015 03:12:21 +0100
parents 55c1e591cb2e
children e9c9404e3d1f
line wrap: on
line diff
--- a/core/ClockSync.cpp	Mon Sep 21 03:11:32 2015 +0100
+++ b/core/ClockSync.cpp	Mon Sep 21 03:12:21 2015 +0100
@@ -70,7 +70,7 @@
 myClock_t ClockSync::send(){
 	//  print();
 	int ret;
-	ret=client.waitUntilReady(false, 0);
+	ret=client.waitUntilReady(false, isSlave() ? 110 : 5);
 	if(ret<=0){ //don't retry
 		return -1;
 	}
@@ -270,7 +270,7 @@
 //		printf("-----------OFFSET IS : %04.4f samples, average: %04.4f samples\n",
 //				offset, movingAverage.getAverage());
 	} else if (calls==10){ //then compensate for initial offset
-//		printf("compensating for offset: %f\n", offset);
+		printf("compensating for offset: %f\n", offset);
 		virtualClock->addOffset(movingAverage.getAverage());
 		movingAverage.reset();
 	} else if (calls>=10){ //use IIR filter from now on
@@ -289,17 +289,24 @@
 		pastIn[1]=in;
 		offset=out;
 		static float maxOffset=0;
-		maxOffset=fabsf(offset) > fabsf(maxOffset) ? offset : maxOffset;
-		printf("%10.3f, %10.3f, %10.3f, %10.3f\n", in, offset, offset-pastOut[2], maxOffset); //unfiltered, filtered
-		if(fabsf(offset)>10 && calls>30){
-			calls=11;
-			//TODO: correct for offset
-			float targetSamplingRate=offset>0 ? 44097 : 44103;
-#ifndef USE_JUCE
-			gAudioCodec->setAudioSamplingRate(targetSamplingRate);
-#endif
-//			pastOut[1]=pastOut[2]=pastIn[1]=pastIn[2]=offset;
-			printf("------setAudioSmplingRate to %f\n", targetSamplingRate);
+		if(calls > 0 ) {
+			maxOffset=fabsf(offset) > fabsf(maxOffset) ? offset : maxOffset;
+			pid.setError(offset);
+			float correction=pid.getOutput();
+			static float oldSamplingRate=44100;
+			printf("%10.3f, %10.3f, %10.3f, %10.3f, %10.3f, %10.3f\n", in, offset, offset-pastOut[2], maxOffset, correction, oldSamplingRate); //unfiltered, filtered
+	//		if(fabsf(offset)>3 && calls>30){
+				//TODO: correct for offset
+	//			float targetSamplingRate=offset>0 ? 44095 : 44105;
+			float targetSamplingRate = oldSamplingRate - correction;
+	#ifndef USE_JUCE
+//			if(oldSamplingRate != targetSamplingRate)
+//				gAudioCodec->setAudioSamplingRate(targetSamplingRate);
+	#endif
+			oldSamplingRate=targetSamplingRate;
+	//			pastOut[1]=pastOut[2]=pastIn[1]=pastIn[2]=offset;
+	//			printf("------setAudioSmplingRate to %f\n", targetSamplingRate);
+	//		}
 		}
 	}
 	calls++;