annotate core/ClockSyncThread.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 44d07fa9bd03
children 8f98b32d0e23
rev   line source
giuliomoro@135 1 #include "ClockSyncThread.h"
giuliomoro@135 2 #ifdef USE_JUCE
giuliomoro@135 3 #else //declare static members TODO: rather refactor this similar to other threads so that only run and clockSyncTask are static
giuliomoro@135 4 myClock_t ClockSyncThread::lastTime; // Used for clock synchronization
giuliomoro@135 5 bool ClockSyncThread::listening;
giuliomoro@135 6 ClockSync ClockSyncThread::clockSync;
giuliomoro@135 7 VirtualClock* ClockSyncThread::virtualClock;
giuliomoro@135 8 bool ClockSyncThread::threadIsExiting;
giuliomoro@135 9 AuxiliaryTask ClockSyncThread::clockSyncTask;
giuliomoro@135 10 #endif
giuliomoro@135 11 #ifdef USE_JUCE
giuliomoro@135 12 ClockSyncThread::ClockSyncThread(const String &threadName) :
giuliomoro@135 13 Thread(threadName)
giuliomoro@135 14 #else
giuliomoro@135 15 ClockSyncThread::ClockSyncThread()
giuliomoro@135 16 #endif /* USE_JUCE */
giuliomoro@135 17 {
giuliomoro@135 18 };
giuliomoro@135 19 ClockSyncThread::~ClockSyncThread(){
giuliomoro@135 20 #ifdef USE_JUCE
giuliomoro@135 21 stopThread(1000);
giuliomoro@135 22 #else
giuliomoro@135 23 stopThread();
giuliomoro@135 24 #endif /* USE_JUCE */
giuliomoro@135 25 }
giuliomoro@135 26 void ClockSyncThread::init(bool isSlave, int aPort, VirtualClock &aVirtualClock){
giuliomoro@135 27 setVirtualClock(aVirtualClock);
giuliomoro@135 28 listening=false;
giuliomoro@135 29 clockSync.init(isSlave, aPort, *virtualClock);
giuliomoro@135 30 #ifdef USE_JUCE
giuliomoro@135 31 startThread(5);
giuliomoro@135 32 #else
giuliomoro@135 33 threadIsExiting=false;
giuliomoro@139 34 clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,60, "clockSyncTask");
giuliomoro@135 35 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
giuliomoro@135 36 #endif /* USE_JUCE */
giuliomoro@135 37 }
giuliomoro@135 38
giuliomoro@135 39 #ifdef USE_JUCE
giuliomoro@135 40 #else
giuliomoro@135 41 void ClockSyncThread::startThread(){
giuliomoro@135 42 BeagleRT_scheduleAuxiliaryTask(clockSyncTask);
giuliomoro@135 43 }
giuliomoro@135 44 void ClockSyncThread::stopThread(){
giuliomoro@135 45 threadIsExiting=true;
giuliomoro@135 46 }
giuliomoro@135 47 bool ClockSyncThread::threadShouldExit(){
giuliomoro@135 48 return(gShouldStop || threadIsExiting );
giuliomoro@135 49 }
giuliomoro@135 50 #endif /* USE_JUCE */
giuliomoro@135 51
giuliomoro@135 52 void ClockSyncThread::setVirtualClock(VirtualClock &aVirtualClock){
giuliomoro@135 53 virtualClock=&aVirtualClock;
giuliomoro@135 54 };
giuliomoro@149 55 #ifndef USE_JUCE
giuliomoro@149 56 extern I2c_Codec* gAudioCodec;
giuliomoro@149 57 #endif
giuliomoro@135 58 void ClockSyncThread::run(){
giuliomoro@149 59 printf("variable=[");
giuliomoro@135 60 while(!threadShouldExit()){
giuliomoro@149 61 static int count = 0;
giuliomoro@135 62 clockSync.sendReceiveLoop();
giuliomoro@149 63 #ifndef USE_JUCE
giuliomoro@149 64 // if (count == 300){
giuliomoro@149 65 // printf("0 0 0\n");
giuliomoro@149 66 // gAudioCodec->setAudioSamplingRate(44101);
giuliomoro@149 67 // }
giuliomoro@149 68 #endif
giuliomoro@149 69 count++;
giuliomoro@135 70 // double now=virtualClock->getNow();
giuliomoro@139 71 // printf("th(end+1)=%f;\n", now);
giuliomoro@135 72 // printf("act(end+1)=%lld;\n", Clock::getTimeUs());
giuliomoro@135 73 }
giuliomoro@141 74 printf("];\n");
giuliomoro@141 75 // printf("Thread is not running \n");
giuliomoro@135 76 }