Quantcast
Channel: Counter/Timer topics
Viewing all articles
Browse latest Browse all 1271

using asynchronous timer for data flow control

$
0
0

Hi all,

  I am using system sleep to control the data flow (some digital lines and analog output). The pseudo code is something like this

 

Sleep(150);
// the following sections are exectuted in parallel
  #pragma omp parallel sections
  {
    #pragma omp section
    {
      DAQmxWriteDigitalLines(...); // output TTL to one digitla line
    }
    #pragma omp section
    {
      DAQmxWriteDigitalLines(...); // output TTL to another digitla line     
    }
    #pragma omp section
    {
      Sleep(2); // sleep 2ms
    }
  }

// the following sections are exectuted in parallel
  #pragma omp parallel sections
  {
    #pragma omp section
    {
      DAQmxWriteDigitalLines(...); // output TTL to one digitla line
    }
    #pragma omp section
    {
      DAQmxWriteAnalogScalarF64(...); // analog output to one channel
    }
    #pragma omp section
    {
      Sleep(1); // delay 1ms
    }
  }

// the following sections are exectuted in parallel
  #pragma omp parallel sections
  {
    #pragma omp section
    {
      DAQmxWriteDigitalLines(...); // output TTL to one digitla line
    }
    #pragma omp section
    {
      DAQmxWriteAnalogScalarF64(...); // analog output to one channel
    }   
#pragma omp section
    {
      DAQmxWriteAnalogScalarF64(...); // analog output to another channel
    }
    #pragma omp section
    {
      Sleep(11); // delay 11ms
    }
  }

// ... other stuffs

 

 

I am running windows XP and I know it is not possible to get realtime control but  I want a as precise timing as possible. Above code is not perfect but it works 95% of times. I just read an article about using the asynchronous timer to control the time delay. I try that idea with the following code frame

 


int CVICALLBACK ATCallback(int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2) { if (event==EVENT_TIMER_TICK) { int *nextdelay = (int *)callbackData; SuspendAsyncTimerCallbacks(); if (timerId>=0) { double time; if (*nextdelay==0) time=2.0; else if (*nextdelay==1) time=1.0; else time=12.0; SetAsyncTimerAttribute(timerId, ASYNC_ATTR_INTERVAL, time); if (*nextdelay==0) { #pragma omp parallel sections { #pragma omp section { DAQmxWriteDigitalLines(...); // output TTL to one digitla line } #pragma omp section { DAQmxWriteDigitalLines(...); // output TTL to another digitla line } } *nextdelay++; } else if (*nextdelay==2) { #pragma omp parallel sections { #pragma omp section { DAQmxWriteDigitalLines(...); // output TTL to one digitla line } #pragma omp section { DAQmxWriteAnalogScalarF64(...); // analog output to one channel } } *nextdelay++; } else if (*nextdelay==3) { #pragma omp parallel sections { #pragma omp section { DAQmxWriteDigitalLines(...); // output TTL to one digitla line } #pragma omp section { DAQmxWriteAnalogScalarF64(...); // analog output to one channel } #pragma omp section { DAQmxWriteAnalogScalarF64(...); // analog output to another channel } } *nextdelay++; } } ResumeAsyncTimerCallbacks(); } return 0; } void main(void) { int n = 0; int timeid; timeid = NewAsyncTimer(120.0/1000.0, 3, 1, ATCallback, &n); }

But it doesn't work. There is no compilation and runtime error but the timing just not right. I wonder do I have to suspend the timer in the callback function when I reset the delay for next call? If I do so, I am worry if it will apply too much delay (since I suspend and resume the timer in the delay) so it will cause even worse timing. But if I don't suspend the timer before I reset the time, what happen if the code running in the callback function not finished before the next callback arrive. It is quite confusing how to use asynchronous timer in this case. Thanks.

 

 


Viewing all articles
Browse latest Browse all 1271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>