annotate core/ClockSyncThread.cpp @ 141:44d07fa9bd03 ClockSync

Ultra-basic feedback for clock sync works^CIssues: response time of the IIR filter is too slow, requires PID and better filtering algorithm.
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 14 Sep 2015 15:42:11 +0100
parents 4e2dd3eb1d28
children 134bff10e561
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@135 55
giuliomoro@135 56 void ClockSyncThread::run(){
giuliomoro@141 57 printf("var=[");
giuliomoro@135 58 while(!threadShouldExit()){
giuliomoro@135 59 clockSync.sendReceiveLoop();
giuliomoro@135 60 // double now=virtualClock->getNow();
giuliomoro@139 61 // printf("th(end+1)=%f;\n", now);
giuliomoro@135 62 // printf("act(end+1)=%lld;\n", Clock::getTimeUs());
giuliomoro@135 63 }
giuliomoro@141 64 printf("];\n");
giuliomoro@141 65 // printf("Thread is not running \n");
giuliomoro@135 66 }