changeset 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 53e3c0a3295d
children 5edc6d0713ef
files .cproject core/ClockSync.cpp core/ClockSyncThread.cpp core/VirtualClock.cpp
diffstat 4 files changed, 20 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/.cproject	Sun Sep 13 21:33:01 2015 +0100
+++ b/.cproject	Sun Sep 13 21:34:47 2015 +0100
@@ -108,11 +108,11 @@
 				</extensions>
 			</storageModule>
 			<storageModule moduleId="cdtBuildSystem" version="4.0.0">
-				<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release,org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.exe.release.1521194538" name="Release" parent="cdt.managedbuild.config.gnu.exe.release" postannouncebuildStep="Stopping process on BBB and copying new binary" postbuildStep="scp ${PWD}/${BuildArtifactFileName} root@192.168.7.2:~/beaglert/ &amp;&amp; echo 'done copying\n' | wall || echo 'error'|wall">
+				<configuration artifactName="${ProjName}" buildArtefactType="org.eclipse.cdt.build.core.buildArtefactType.exe" buildProperties="org.eclipse.cdt.build.core.buildType=org.eclipse.cdt.build.core.buildType.release,org.eclipse.cdt.build.core.buildArtefactType=org.eclipse.cdt.build.core.buildArtefactType.exe" cleanCommand="rm -rf" description="" id="cdt.managedbuild.config.gnu.exe.release.1521194538" name="Release" parent="cdt.managedbuild.config.gnu.exe.release" postannouncebuildStep="Stopping process on BBB and copying new binary" postbuildStep="ssh root@192.168.7.2 &quot;kill -s 9 \`pidof ${BuildArtifactFileName}\` 2&gt;/dev/null; sleep 0.5;&quot;; scp ${PWD}/${BuildArtifactFileName} root@192.168.7.2:beaglert/ &amp;&amp; echo 'done copying\n' | wall || echo 'error'|wall">
 					<folderInfo id="cdt.managedbuild.config.gnu.exe.release.1521194538." name="/" resourcePath="">
 						<toolChain id="cdt.managedbuild.toolchain.gnu.exe.release.1612059942" name="Linux GCC" superClass="cdt.managedbuild.toolchain.gnu.exe.release">
 							<targetPlatform id="cdt.managedbuild.target.gnu.platform.exe.release.908983575" name="Debug Platform" superClass="cdt.managedbuild.target.gnu.platform.exe.release"/>
-							<builder buildPath="${workspace_loc:/BBB_audio+input/Release}" id="cdt.managedbuild.target.gnu.builder.exe.release.511190290" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.exe.release"/>
+							<builder arguments="-j6" buildPath="${workspace_loc:/BBB_audio+input/Release}" command="make" id="cdt.managedbuild.target.gnu.builder.exe.release.511190290" keepEnvironmentInBuildfile="false" managedBuildOn="true" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.exe.release"/>
 							<tool command="arm-linux-gnueabihf-g++&#10;" id="cdt.managedbuild.tool.gnu.archiver.base.810674388" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
 							<tool command="arm-linux-gnueabihf-g++" commandLinePattern="${COMMAND} ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}" id="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release.163790048" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.exe.release">
 								<option id="gnu.cpp.compiler.exe.release.option.optimization.level.2031085570" name="Optimization Level" superClass="gnu.cpp.compiler.exe.release.option.optimization.level" value="gnu.cpp.compiler.optimization.level.more" valueType="enumerated"/>
--- a/core/ClockSync.cpp	Sun Sep 13 21:33:01 2015 +0100
+++ b/core/ClockSync.cpp	Sun Sep 13 21:34:47 2015 +0100
@@ -14,9 +14,9 @@
 	T1p=-1;
 	T2=-1;
 	T2p=-1;
-	receiveLoopSleepUs=10;
+	receiveLoopSleepUs=100;
 	receiveLoopTimeout=1e5;
-	movingAverage.setLength(201);
+	movingAverage.setLength(31);
 	expectedClockSyncType=isSlave() ? kSync : kNone;
 }
 ClockSync::ClockSync(bool thisIsSlave, int aPort, VirtualClock &aVirtualClock){
@@ -41,6 +41,7 @@
 	((int*)buffer)[0]=clockSyncType;
 }
 void ClockSync::setTimestamp(myClock_t timestamp){
+//	printf("setting timestamp: %lld\n", timestamp);
 	((myClock_t*)((char*)buffer+sizeof(int)))[0]=timestamp;
 }
 void ClockSync::print(){
@@ -146,13 +147,13 @@
 		localTimestamp=receive();
 		if(localTimestamp<=0){
 			if(localTimestamp==0){
-				//  //printf("Socket not ready to be read: %lld\n", localTimestamp);
+//				  printf("Socket not ready to be read: %lld\n", localTimestamp);
 			}
 			else if(localTimestamp==-1){
-				//printf("Error while receiving: %lld\n", localTimestamp);
+				printf("Error while receiving: %lld\n", localTimestamp);
 			}
 			else if(localTimestamp==-2){
-				//  //printf("Wrong size of the received clockSync: %lld\n", localTimestamp);
+				  printf("Wrong size of the received clockSync: %lld\n", localTimestamp);
 			}
 			continue ; //keep waiting
 		}
@@ -162,7 +163,7 @@
 			//printf("Wrong clockSync type: %d, expected: %d\n",clockSyncType, expectedClockSyncType);
 			return -2; //start over
 		}
-		//printf("Received clockSync type: %d, clockSyncTimestamp: %lld\n", clockSyncType, clockSyncTimestamp);
+//		printf("Received clockSync type: %d, clockSyncTimestamp: %lld\n", clockSyncType, clockSyncTimestamp);
 		if(isSlave()==true){
 			int ret=slaveHandleMessage();
 			if(ret==1 && clockSyncType==kDelayResp){ //we are done, end of a cycle!
@@ -208,7 +209,8 @@
 			T2p=clockSyncTimestamp;
 			//TODO: evaluate things
 			double offset=(T1p-T1-T2p+T2)/2.0d;
-			printf("-----------OFFSET IS : %04.1f, average: %04.2f\n", offset, movingAverage.add(offset));
+			printf("-----------OFFSET IS : %04.4f seconds, average: %04.4f seconds\n",
+					offset/44100.f, movingAverage.add(offset)/44100.f);
 			expectedClockSyncType=kSync; //end of the cycle, wait for next sync.
 			break;
 		}
--- a/core/ClockSyncThread.cpp	Sun Sep 13 21:33:01 2015 +0100
+++ b/core/ClockSyncThread.cpp	Sun Sep 13 21:34:47 2015 +0100
@@ -31,7 +31,7 @@
 	startThread(5);
 #else
 	threadIsExiting=false;
-	clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,98, "clockSyncTask");
+	clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,60, "clockSyncTask");
 	//TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
 #endif /* USE_JUCE */
 }
@@ -58,9 +58,8 @@
 void ClockSyncThread::run(){
 	while(!threadShouldExit()){
 		clockSync.sendReceiveLoop();
-		usleep(5000);
 //		double now=virtualClock->getNow();
-//		printf("th(end+1)=%f;\n", now/44100.0f);
+//		printf("th(end+1)=%f;\n", now);
 //		printf("act(end+1)=%lld;\n", Clock::getTimeUs());
 	}
 	printf("Thread is not running \n");
--- a/core/VirtualClock.cpp	Sun Sep 13 21:33:01 2015 +0100
+++ b/core/VirtualClock.cpp	Sun Sep 13 21:34:47 2015 +0100
@@ -20,19 +20,20 @@
 		period=movingAverage.add((currentTime-lastSync)/count); //TODO: replace with Kalman filter
 	}
 	lastSync=currentTime;
+//	printf("lastSync: %lld\n",lastSync-startTime);
 }
 
 double VirtualClock::getNow(){
-	myClock_t now=Clock::getTimeUs();
+	myClock_t currentSystemTime=Clock::getTimeUs();
 	if(period<=0){
-		return now;
+		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=(now-startTime);
-	double frac=elapsed/(double)period;
-	//  printf("now=%lld; beginningOfPeriod=%f; lastSync=%lld; period=%lld; frac=%f\n", now, beginningOfPeriod, lastSync, period, frac);
-	return frac;
+	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(){