Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Issue synchronizing analog output and counter output

Hello,

 

The device I am using is a NI USB-6229 BNC. I am trying to synchronize the execution of 2 tasks such that they start and stop at the same time as one another. The first of the tasks contains 2 channels for analog output and the second of the tasks contains 2 channels for counter output. Each analog output channel produces the same amount of finite samples so  _waveforms[0].SampleCount will always be equal to _waveforms[1].SampleCount in the context below.

 

_task = new Task();
_task.AOChannels.CreateVoltageChannel("Dev4/ao0", "ao0", -5, 5, AOVoltageUnits.Volts);
_task.AOChannels.CreateVoltageChannel("Dev4/ao1", "ao1", -5, 5, AOVoltageUnits.Volts); _task.Timing.ConfigureSampleClock("", 5000, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, _waveforms[0].SampleCount); _task.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger("/Dev4/PFI1", DigitalEdgeStartTriggerEdge.Rising); _task.Control(TaskAction.Verify); _task.Done += delegate { _task.Stop(); _countTask.Stop(); _task.Control(TaskAction.Unreserve); _countTask.Control(TaskAction.Unreserve); };
_analogMultiChannelWriter = new AnalogMultiChannelWriter(_task.Stream);

 

_countTask = newTask();
_countTask.COChannels.CreatePulseChannelTicks("Dev4/ctr0", "ctr0", "/Dev4/ao/SampleClock", COPulseIdleState.Low, 0, counterZeroLowTicks, counterZeroHighTicks);
_countTask.COChannels.CreatePulseChannelTicks("Dev4/ctr1", "ctr1", "/Dev4/ao/SampleClock", COPulseIdleState.Low, counterOneInitialDelay, 2, counterOneHighTicks);
_countTask.COChannels.All.CounterTimebaseActiveEdge = COCounterTimebaseActiveEdge.Rising;
_countTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger("/Dev4/PFI1", DigitalEdgeStartTriggerEdge.Rising);
_counterTask.Control(TaskAction.Verify);
_counterMultiChannelWriter = new CounterMultiChannelWriter(_countTask.Stream)

You can see that I have configured the counter task to use the same start trigger as the analog output task. You can also see that I have configured the counter output channels to use the analog output sample clock. It is my understanding that these are the 2 things that must be done to synchronize my tasks as desired.

 

You may be wondering what the counterZeroLowTicks, counterZeroHighTicks, counterOneInitialDelay, & counterOneHighticks variables are or how they are calculated.  They are calculated such that the low ticks + high ticks + initial delay for a given counter is equal to  _waveforms[0].SampleCount (+/- 2). This means that each analog output channel should produce the (nearly) same number of samples as my counter output channels.

 

When a button is clicked in my application the following code is executed...

var deliveryData = new AnalogWaveform<double>[2];
... 
/* Populate deliveryData here*/
...
_analogMultiChannelWriter.WriteWaveform(false, deliveryData);
_task.Start();
_countTask.Start();

I expect my counter task to stop at the same time as my analog output task. Once my analog output task has finished delivering its finite number of samples, I expect both of my counters to be turned off. But, that is not always the case.

 

 Consider the following use case that produces the mentioned issue on my system...

  • My analog output channels are configured to each produce finite waveforms, each with a sample length of 1276.
  • My counter0 initial delay is 0, counter0 low ticks are 2, & counter0 high ticks are 1275 equaling 1277 ticks.
  • My counter1 initial delay is 225, counter1 low ticks are 2, & counter1 high ticks are 1050...totaling to 1277 ticks.

Below is a screenshot from an oscilloscope used to monitor the analog and counter outputs. Channel 1 is hooked up to analog output 0. Channel 2 is hooked up to analog output 1. D0 is hooked up to counter 0 output. D1 is hooked up to counter 1 output. And D2 is hooked up to the digital start trigger used to start the tasks.scope_0.png

 

My analog outputs appear to be behaving precisely as expected. Also, my counter 1 output appears to be behaving exactly as expected. Counter 1 waits its initial delay of 225 samples then goes high precisely at the time that the pulse on channel 1 returns to 0V. Counter 1 then stays high for 1050 samples and returns to 0V at precisely the time that the pulses on channel 2 complete (this is expected behavior).

 

Counter 0 goes high at the correct time (in line with the digital start trigger). The problem depicted in the screenshot above is that Counter 0 (D0) should have returned to low (0V) at the exact same time as Counter 1 did. But as you can see, it didn't return low until roughly 20-25ms after counter 0 did. I mean after all, they are using the same sample clock and producing the same number of samples, they should have shut off at the same time.

 

With that being said I have some ideas regarding what is causing the issue but honestly they are just ideas and nothing more...

  • I originally thought the number of ticks my counters were being created with did not match the number of samples being produced on the analog output, but after a significant amount of debugging I really don't suspect this is the case
  • It seems like my counter may be missing sample clock ticks
  • It seems like my counter task is not getting stopped with my analog output task or it is taking a long time to stop it
  • I am not using async writing, maybe I need to?
  • Somehow the sample clock for the analog output is different than the sample clock for my counter output

There is a lot of information in this post so if I missed anything that would be useful for helping out just lmk and I will add it. Otherwise, any idea what is wrong here?

0 Kudos
Message 1 of 3
(2,596 Views)

Update: I have posted a refined version of this question at the forum link below, I figured the counter thread may be a better place to ask this question. It includes an example to run that demonstrates the issue mentioned in this post.

 

Counter pulse generation issues when synchronizing with analog sample clock

0 Kudos
Message 2 of 3
(2,527 Views)

Hey all,

 

Check out the link above for the solution to this issue.

0 Kudos
Message 3 of 3
(2,518 Views)