Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Analog In- and Output with NIDAQmx in Python

Solved!
Go to solution

Hi guys,

 

I am using the USB-6002 and want to read an analog input and write an analog output. I am using therefore the Python Package NIDAQmx and the package asyncio to make asynchronous working possible.

My problem is now, that the write-methods always needs unread samples in the buffer and it is really hard to guess, how many samples are available and how many samples need to be written. For the output I am currently using the following configuration: 

# Create tasks for the analog outputs
with nidaqmx.Task() as output_task:

# Configure analog output channels
for channel in output_channels:
output_task.ao_channels.add_ao_voltage_chan(channel)

# Configure the sample rate and the number of samples per channel
output_task.timing.cfg_samp_clk_timing(sample_rate,
samps_per_chan=samples_per_channel,
sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS
)

output_task.out_stream.regen_mode = nidaqmx.constants.RegenerationMode.DONT_ALLOW_REGENERATION

 

And I am using the standard output_task.write() method to write samples into the output buffer. 
Now I am wondering if there is a possibility to change from continuous acquisition to something like "changing output state". Means, if there is a possibility to just change the output state for example from 0V to 1V if a defined condition is fulfilled. This would make it possible to just change the state and to stay in this state for example 3 seconds, without writing samples continuously and without providing unread samples in the output buffer the whole time.

 

Is there a function, which is able to do something like that?

I would be really thankful if somebody knows an answer to my problem.

 

BR, Kathi

0 Kudos
Message 1 of 5
(742 Views)
Solution
Accepted by topic author KathiSt

@KathiSt wrote:

And I am using the standard output_task.write() method to write samples into the output buffer. 
Now I am wondering if there is a possibility to change from continuous acquisition to something like "changing output state". Means, if there is a possibility to just change the output state for example from 0V to 1V if a defined condition is fulfilled. This would make it possible to just change the state and to stay in this state for example 3 seconds, without writing samples continuously and without providing unread samples in the output buffer the whole time.

 

Is there a function, which is able to do something like that?


In that case, you don't need to use the sample clock timing. You can just update the AO data on demand, when the condition is met.

nidaqmx-python/examples/ao_voltage_sw_timed.py at master · ni/nidaqmx-python · GitHub

0 Kudos
Message 2 of 5
(727 Views)

Thank you for the answer, it was the correct solution!!

 

Now I have a second question. It seems like the NI USB-6002 stops to acquire new data at the analog input after a while. The timepoint when this occurs varies strong (mostly between 5 and 10 minutes after starting my program). I have absolutely no clue why it is stopping, cause it is running correct for quite a long time.

 

Here is the initialization of my input:

 

# Create tasks for the analog inputs
with nidaqmx.Task() as input_task:

# Configure analog input channel
for channel in input_channels:
input_task.ai_channels.add_ai_voltage_chan(channel,
terminal_config=nidaqmx.constants.TerminalConfiguration.RSE)

# Configure the sample rate and the number of samples per channel
input_task.timing.cfg_samp_clk_timing(sample_rate,
sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS)

starttask_time = time.monotonic()
input_task.start()

 

Does somebody has an idea??

 

I am using the following property to check the available (not read) samples in the input buffer: available_samples = input_task.in_stream.avail_samp_per_chan
But it is constant after a while, cause the NI USB-6002 stops acquiring data. But this is just what I think.

0 Kudos
Message 3 of 5
(686 Views)

I guess a buffer overflown has occurred.

What are the values of sample rate, number of sample for sample_clock, and number of sample for daqmx_read?

Specifying Number of Samples When Continuously Acquiring with NI-DAQmx for LabVIEW

0 Kudos
Message 4 of 5
(672 Views)

But the value for "available_samples = input_task.in_stream.avail_samp_per_chan" is always below 150, therefore I didn't expect an overlow. 

 

I have the following configuration:

sample_rate = 2000

number_of_samples for daqmx_read = 66

 

But I didn't specify the number of samples for samples_clock.

Additionally, during logging I get the following warning:

2023-12-15 14:53:18,017 Executing <Task pending name='Task-2' coro=<record_emg() running at ....link......\closed_loop_asyncio_v10.py:272> wait_for=<Future pending cb=[Task.task_wakeup()] created at ...link....\base_events.py:429> cb=[_wait.<locals>._on_completion() at ...link...\asyncio\tasks.py:475] created at ...link...\asyncio\tasks.py:337> took 5.734 seconds

0 Kudos
Message 5 of 5
(668 Views)