Mercurial > hg > beaglert
comparison 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 |
comparison
equal
deleted
inserted
replaced
148:6cd38e261027 | 149:134bff10e561 |
---|---|
68 * immediately after the clockSync has been sent or -1 if there was an error or timeout expired. | 68 * immediately after the clockSync has been sent or -1 if there was an error or timeout expired. |
69 */ | 69 */ |
70 myClock_t ClockSync::send(){ | 70 myClock_t ClockSync::send(){ |
71 // print(); | 71 // print(); |
72 int ret; | 72 int ret; |
73 ret=client.waitUntilReady(false, 0); | 73 ret=client.waitUntilReady(false, isSlave() ? 110 : 5); |
74 if(ret<=0){ //don't retry | 74 if(ret<=0){ //don't retry |
75 return -1; | 75 return -1; |
76 } | 76 } |
77 ret=client.send(buffer, bufferLength); | 77 ret=client.send(buffer, bufferLength); |
78 myClock_t timestamp=(myClock_t)virtualClock->getNow(); | 78 myClock_t timestamp=(myClock_t)virtualClock->getNow(); |
268 if(calls<10) { //get an initial guess | 268 if(calls<10) { //get an initial guess |
269 movingAverage.add(offset); | 269 movingAverage.add(offset); |
270 // printf("-----------OFFSET IS : %04.4f samples, average: %04.4f samples\n", | 270 // printf("-----------OFFSET IS : %04.4f samples, average: %04.4f samples\n", |
271 // offset, movingAverage.getAverage()); | 271 // offset, movingAverage.getAverage()); |
272 } else if (calls==10){ //then compensate for initial offset | 272 } else if (calls==10){ //then compensate for initial offset |
273 // printf("compensating for offset: %f\n", offset); | 273 printf("compensating for offset: %f\n", offset); |
274 virtualClock->addOffset(movingAverage.getAverage()); | 274 virtualClock->addOffset(movingAverage.getAverage()); |
275 movingAverage.reset(); | 275 movingAverage.reset(); |
276 } else if (calls>=10){ //use IIR filter from now on | 276 } else if (calls>=10){ //use IIR filter from now on |
277 //filter coefficients obtained from Matlab : [B,A]=butter(2,0.005); | 277 //filter coefficients obtained from Matlab : [B,A]=butter(2,0.005); |
278 // static float B[3]={6.10061787580662e-05, 0.000122012357516132, 6.10061787580662e-05}; | 278 // static float B[3]={6.10061787580662e-05, 0.000122012357516132, 6.10061787580662e-05}; |
287 pastOut[1]=out; | 287 pastOut[1]=out; |
288 pastIn[2]=pastIn[1]; | 288 pastIn[2]=pastIn[1]; |
289 pastIn[1]=in; | 289 pastIn[1]=in; |
290 offset=out; | 290 offset=out; |
291 static float maxOffset=0; | 291 static float maxOffset=0; |
292 maxOffset=fabsf(offset) > fabsf(maxOffset) ? offset : maxOffset; | 292 if(calls > 0 ) { |
293 printf("%10.3f, %10.3f, %10.3f, %10.3f\n", in, offset, offset-pastOut[2], maxOffset); //unfiltered, filtered | 293 maxOffset=fabsf(offset) > fabsf(maxOffset) ? offset : maxOffset; |
294 if(fabsf(offset)>10 && calls>30){ | 294 pid.setError(offset); |
295 calls=11; | 295 float correction=pid.getOutput(); |
296 //TODO: correct for offset | 296 static float oldSamplingRate=44100; |
297 float targetSamplingRate=offset>0 ? 44097 : 44103; | 297 printf("%10.3f, %10.3f, %10.3f, %10.3f, %10.3f, %10.3f\n", in, offset, offset-pastOut[2], maxOffset, correction, oldSamplingRate); //unfiltered, filtered |
298 #ifndef USE_JUCE | 298 // if(fabsf(offset)>3 && calls>30){ |
299 gAudioCodec->setAudioSamplingRate(targetSamplingRate); | 299 //TODO: correct for offset |
300 #endif | 300 // float targetSamplingRate=offset>0 ? 44095 : 44105; |
301 // pastOut[1]=pastOut[2]=pastIn[1]=pastIn[2]=offset; | 301 float targetSamplingRate = oldSamplingRate - correction; |
302 printf("------setAudioSmplingRate to %f\n", targetSamplingRate); | 302 #ifndef USE_JUCE |
303 // if(oldSamplingRate != targetSamplingRate) | |
304 // gAudioCodec->setAudioSamplingRate(targetSamplingRate); | |
305 #endif | |
306 oldSamplingRate=targetSamplingRate; | |
307 // pastOut[1]=pastOut[2]=pastIn[1]=pastIn[2]=offset; | |
308 // printf("------setAudioSmplingRate to %f\n", targetSamplingRate); | |
309 // } | |
303 } | 310 } |
304 } | 311 } |
305 calls++; | 312 calls++; |
306 } | 313 } |
307 int ClockSync::masterHandleMessage(){ | 314 int ClockSync::masterHandleMessage(){ |