Counter/Timer

cancel
Showing results for 
Search instead for 
Did you mean: 

synchronize cpp code that run in 400Hz to hardware 1 PPS signal

Hi, I am new with my work on NI tools, and learn more and more every day. 

 

I have a mission to synchronize simulation that written in cpp -that communicate with other computers in lab.

To cut a long story short, my code need to send and receive UDP messages in 400 HZ, and we want that the simulation sending time will be synchronized to hardware 1PPS from the lab. 

 

I noticed that the 1 PPS trigger worked good. I also noticed in test-panel in NI-MAX that the 1 PPS is enter fine.   

 My problem is that I see that the 400 Hz is not work good. I added printings to screen, that supposed to be every second, but it seem to work much faster.

 

when I run in debug I don't find an error in the output of DAQmx functions. 

 

Any advice will help me, thanks.  

 

My steps was: 

 

1. I connected the hardware 1 PPS signal to "/dev1/PFI0"  

2. The code I run: 

 

TaskHandle counterOutputTask; 

TaskHandle counterInputTask; 

static long lStatus; 

 

const char ppsSource[] = "/Dev1/PFI0";

const char outputCounter[] = "Dev1/ctr0";

const char inputCounter[] = "Dev1/ctr1";

const char routedPFI[] = "/Dev1/PFI12";

 

(more irrelevant code)...

.... 

...,

... 


DAQmxCreateTask("", &counterOutputTask);

 

lStatus = DAQmxCreateCOPulseChanFreq(counterOutputTask, outputCounter, "", DAQmx_Val_Hz, DAQmx_Val_Low, 0.0, 400.0, 0.5);

 

lStatus = DAQmxCfgDigEdgeStartTrig(counterOutputTask, ppsSource, "", DAQmx_Val_Rising);

 

lStatus = DAQmxSetCOPulseTerm(counterOutputTask, outputCounter, routedPFI);

 

 

DAQmxCreateTask("", &counterInputTask);

 

lStatus = DAQmxCreateCICountEdgesChan(counterInputTask, inputCounter, "", DAQmx_Val_Rising, 0, DAQmx_Val_CountUp);

 

lStatus = DAQmxSetCICountEdgesTerm(counterInputTask, inputCounter, routedPFI); 

 

DAQmxStartTask(counterOutputTask);

DAQmxStartTask(counterInputTask);

 

 // 4. Synchronize UDP message sending to the 400 Hz clock

uInt32 prevCount = 0;

 

while (running)

{

uInt32 currentCount = 0;

 

DAQmxReadCounterScalarU32(counterInputTask, 10.0, &currentCount, NULL);

 

if (currentCount > prevCount) {

// Receive and send a UDP message at every tick

receive_send_udp_message(...);

 

prevCount = currentCount;

 

}

 

// Stop and clear tasks

DAQmxStopTask(counterInputTask);

DAQmxClearTask(counterInputTask);

DAQmxStopTask(counterOutputTask);

DAQmxClearTask(counterOutputTask);

 

// Stop UDP receive thread

running = false;

 

}

...

running = false;

}

 

 

Thanks, 

Ariel

 

 

 

 

 

 

 

 

0 Kudos
Message 1 of 2
(242 Views)

The Read API is not synchronized to any signal. It merely reads from the buffer.

You should consider using Digital Change Detection instead. See example at C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Digital\Read Values\Read Dig Chan-Change Detection

-------------------------------------------------------
Control Lead | Intelline Inc
0 Kudos
Message 2 of 2
(223 Views)