diff projects/scope/render.cpp @ 152:8f98b32d0e23 ClockSync

Last commit on this branch for a while. Overall not very succesful
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 05 Oct 2015 13:06:14 +0100
parents e9c9404e3d1f
children
line wrap: on
line diff
--- a/projects/scope/render.cpp	Tue Sep 22 04:10:07 2015 +0100
+++ b/projects/scope/render.cpp	Mon Oct 05 13:06:14 2015 +0100
@@ -4,6 +4,7 @@
 #include <ClockSynchronizer.h>
 #include <cmath>
 #include <ClockSyncThread.h>
+#include <Pid.h>
 
 float gPhase1, gPhase2;
 float gFrequency1, gFrequency2;
@@ -56,7 +57,7 @@
 	gPhase1 = 0.0;
 	gPhase2 = 0.0;
 	
-	gFrequency1 = 200.0;
+	gFrequency1 = 441.0;
 	gFrequency2 = 201.0;
 
 //	testTime=BeagleRT_createAuxiliaryTask(testTimeFunction, 80, "testTimeTask");
@@ -67,7 +68,8 @@
 // Input and output are given from the audio hardware and the other
 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
 // will be 0.
-
+extern Pid* gClockSyncPid;
+extern ClockSync* gClockSync;
 void render(BeagleRTContext *context, void *userData)
 {
 	virtualClock.sync(context->audioFrames);
@@ -101,12 +103,32 @@
 	float phaseInc=gFrequency1/44100.0*2*M_PI;
 //	rt_printf("phaseInc: %f, phase: %f\n",phaseInc,phase);
 	for(unsigned int n=0; n<context->audioFrames; n++){
-		context->audioOut[n*2]=sinf(phase);//context->audioIn[n*2];
-		phase+=200.0/44100.0*2*M_PI;
+		context->audioOut[n*2]=sinf(phase) > 0 ? 1 : -1;
+		context->audioOut[n*2+1]=sinf(phase) > 0 ? 1 : -1;
+//		context->audioOut[n*2]= context->audioIn[n*2];
+//		context->audioOut[n*2+1]= context->audioIn[n*2+1];
+		phase+=phaseInc;
 		if(phase>=2*M_PI)
 			phase-=2*M_PI;
-		context->audioOut[n*2+1]=rand()/(float)RAND_MAX;//context->audioIn[n*2];
+//		context->audioOut[n*2+1]=rand()/(float)RAND_MAX;//context->audioIn[n*2];
 	}
+	gClockSyncPid->setProportionalGain(context->analogIn[0]*10);
+//	gClockSyncPid->setIntegralGain(context->analogIn[1]);
+	gClockSyncPid->setIntegralGain(0);
+//	gClockSyncPid->setDerivativeGain(context->analogIn[2]);
+	gClockSyncPid->setDerivativeGain(0);
+	gClockSyncPid->setGlobalGain(context->analogIn[3]*1/0.828);
+	for (int n = 0; n < context->analogFrames; n++){
+		context->analogOut[n*context->analogChannels + 0] = (gClockSync->getOffset() )/ 100 + 0.5;
+		context->analogOut[n*context->analogChannels + 1] = (gAudioCodec->getAudioSamplingRate()-44100)/20 + 0.5;
+	}
+	static bool pastSwitch = false;
+	bool thisSwitch = context->analogIn[4]>0.5;
+	if(thisSwitch && pastSwitch == false){
+		rt_printf("--------------reset offset\n");
+		virtualClock.addOffset(gClockSync->getIirOffset());
+	}
+	pastSwitch = thisSwitch;
 	count++;
 	/*
 //	if((count&262143)==0){