12-15-2017 03:51 PM - edited 12-15-2017 03:56 PM
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...
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.
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...
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?
12-18-2017 11:15 AM
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
12-18-2017 02:52 PM
Hey all,
Check out the link above for the solution to this issue.