Quantcast
Channel: Measurement Studio for .NET Languages topics
Viewing all articles
Browse latest Browse all 2011

Unexpected offset in first analog input channel's read samples

$
0
0

Hello friends,

 

I am writing an application that interfaces with a NI USB-6229 device. I have wired my hardware such that ao0 is connected to ai0 and ao1 is connected to ai1. The application writes samples to the mentioned analog output channels when a button is pressed (after which a digital trigger must be received as well), the output is then fed back into the system via the mentioned analog inputs for validation.

 

The problem is, is that there is consistent, unexpected offset in the samples being read by ai0 and I'm not sure what is the cause of this and/or if this behavior is deterministic. Before describing the issue in detail, I think it will be useful to provide some code. Here is the configuration code for the analog output.

_task = new Task("AnalogOutputTask");
_task.AOChannels.CreateVoltageChannel(_deviceLocator.DeviceName + "/ao0", "ao0", -10, 10, AOVoltageUnits.Volts);
_task.AOChannels.CreateVoltageChannel(_deviceLocator.DeviceName + "/ao1", "ao1", -10, 10, AOVoltageUnits.Volts);
_task.Timing.ConfigureSampleClock("", SamplingInfo.OutputSampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, _numberOfSamplesToOutput);
_task.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger("/" + _deviceLocator.DeviceName + "/PFI1", DigitalEdgeStartTriggerEdge.Rising);
_task.Control(TaskAction.Verify);
_writer = new AnalogMultiChannelWriter(_task.Stream);

Here is the configuration code for the analog input.

_task = new Task("AnalogInputTask");
_task.AIChannels.CreateVoltageChannel(_deviceLocator.DeviceName + "/ai0", "ai0", (AITerminalConfiguration)(-1), -10.0, 10.0, AIVoltageUnits.Volts);
_task.AIChannels.CreateVoltageChannel(_deviceLocator.DeviceName + "/ai1", "ai1", (AITerminalConfiguration)(-1), -10.0, 10.0, AIVoltageUnits.Volts);
_task.Timing.ConfigureSampleClock("", 20000, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, _numberOfSamplesToRead);
_task.ConfigureLogging("C:\\tmp\\input.tdms", TdmsLoggingOperation.OpenOrCreate, LoggingMode.LogAndRead, "Group Name");
_task.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger("/" + _deviceLocator.DeviceName + "/PFI1", DigitalEdgeStartTriggerEdge.Rising)
_task.Control(TaskAction.Verify);
_reader = new AnalogMultiChannelReader(_task.Stream);

The relevant code that is executed when the mentioned button is pressed by the user is shown below.

_writer.WriteMultiSample(false, _deliverySamples);
_outputTask.Start();
_inputTask.Start();
_reader.BeginReadMultiSample(_numberOfSamplesToRead, asyncResult =>
{
    _readSamples = _reader.EndReadMultiSample(asyncResult);
}, null);

Let's take note of a few things in the code above...

  • _numberOfSamplesToRead is 4x greater than _numberOfSamplesToWrite because the input sample rate is 4x higher than the output sample rate to prevent undersampling
  • _deliverySamples is a 2D array that contains the samples that will be sent to the analog output channels
  • _readSamples is a 2D array that will contain the samples that were read from the analog input channels
  • The _outputTask and the _inputTask start on the same trigger

Illustrated below are the waveforms being sent out of the analog outputs. The black one is being sent out ao0 and read in ai0. The black one is representative of what will be contained in the first rows of the _deliverySamples and _readSamples arrays. The blue one is being sent out ao1 and read in ai1. Also, the blue one is representative of what will be contained in the second rows of the _deliverySamples and _readSamples arrays.

2018-05-08 19_33_50-Untitled - Paint.png

Let's say the rising edge of Pulse 1 output (the first high sample in the _deliverySamples[0] array) has an index of 175. And the falling edge of Pulse 1 output (the first low sample after Pulse 1 high samples in the _deliverySamples[0] array) has an index of 185.

 

Keeping in mind that the waveforms above will be sampled at a rate 4x as fast as the output, one would expect to find the rising edge of Pulse 1 on the input side (the first high sample in the _readSamples[0] array) to have an index of 700. One would also expect to find the falling of Pulse 1 on the input side (the first low sample after Pulse 1 high samples in the readSamples[0] array) to have an index of 740.

 

But, this is not what I am finding. In reference to the input side, I am finding that the first high sample in the _readSamples[0] array is actually at index 701 and the first low sample after Pulse 1 high samples in the _readSamples[0] array is actually at index 741. This offset I am seeing is unexpected and is issue at hand.

 

Everything is where I expect it to be in the _readSamples[1] array. This is interesting and suggests that the offset that I am seeing only occurs in the first analog input channel being read from in. To test that it is the first channel being read that is effected and not the others, I modified my application use ai0, ai1, & ai2 by adding the following line of code to the analog input configuration block.

_task.AIChannels.CreateVoltageChannel(_deviceLocator.DeviceName + "/ai2", "ai2", (AITerminalConfiguration) (-1), -10, 10, AIVoltageUnits.Volts);

Also, I modified my hardware such that ao0 got connected to ai1 and ao1 got connected to ai2. I left ai0 unconnected. After doing so, I output the same set of black and blue sample sequences used before and I examined the contents of the _readSamples array after input was received. I now find that the first high sample in the _readSamples[1] array is at index 700 (as expected) and the first low sample after Pulse 1 high samples in the _readSamples[1] array is actually at index 740 (as expected. The offset I was seeing before is no longer present. Also, everything is where I expect it to be in the _readSamples[2] array.

 

Any ideas on what might be happening here?

 

Edit: Added bolding


Viewing all articles
Browse latest Browse all 2011


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