Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

Python NIDAQmx Analog output with USB-6001: cannot se

Solved!
Go to solution

If I run the code below on USB-6001, I get no errors, and the printout tells me that the sample rate has been set to 2000 as requested.  However, I also get no output (occasionally I'll get a little glitch on the output, looks like maybe one or two samples get written, but not the full 1 second of 440 Hz sine wave) .  If I comment out the line "task.timing.cfg_samp_clk_timing(Fs)", then the printout tells me that the sample rate is 1000 (which I guess is the default) and I get the output that I would expect for a 1000 S/s sample rate.  If I try to set Fs to 10000, I get an error saying the max sample rate is 5000.  So, given that the hardware is telling me that it will support up to 5000 S/s, how come it does not work when I try to change it to anything other than the default?  Any suggestions?

 

 

import nidaqmx
import numpy
from scipy import signal

Fs = 2000
freq = 440
tf = 1
t = numpy.arange(0, tf, 1/Fs);
data = 0.02*numpy.sin( 2 * 3.14 * freq * t)

with nidaqmx.Task() as task:
    ch =task.ao_channels.add_ao_voltage_chan("Dev1/ao0")
    task.timing.cfg_samp_clk_timing(Fs)
    print( task.timing.samp_clk_rate)
    task.write(data, auto_start=True)   

 

0 Kudos
Message 1 of 4
(5,495 Views)

Hey Daniel:

 

      I think that the issue is related to the fact that the 6001 doesn't support hardware timing

 

       The line of code that is commented is the one that attempts to do hardware timing. In this case what you can do is use software timing, which should run as fast as the computer allows to. You can check the examples here, particularly the software timed analog output. 

0 Kudos
Message 2 of 4
(5,464 Views)

Thanks for your reply.  I read the article you linked to... but that seems to directly contradict the 6001 specifications (http://www.ni.com/pdf/manuals/374369a.pdf) which say "Maximum update rate: 5 kS/s simultaneous per channel, hardware timed" (emphasis mine).  I guess it's possible that when they said "hardware timed", they meant "NOT hardware timed", but I think it's more likely that I'm doing something wrong in the code (to be clear, I don't need "hardware timed single point", just the normal buffered output)

 

To check, I was able to generate 5000 S/s output in C by modifying the NI DAQmx examples that came with the library and compiling with visual studio.  Starting with C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DAQmx ANSI C\Analog Out\Generate Voltage\Cont Gen Volt Wfm-Int Clk

and just modifying the line

DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",1000,DAQmx_Val_Rising,DAQmx_Val_ContSamps,N));

 

to be 

DAQmxErrChk (DAQmxCfgSampClkTiming(taskHandle,"",5000,DAQmx_Val_Rising,DAQmx_Val_ContSamps,N));

 

appears to give me what I'm asking for.  At the very least, it generates output, whereas my python code does not generate any output.  So I think that I'm just doing something wrong in Python.  I could switch to C++ for my application if I had to, but python would be more convenient.  Any suggestions?

0 Kudos
Message 3 of 4
(5,457 Views)
Solution
Accepted by topic author Daniel_Kiracofe

nevermind... figured it out myself.  I just needed to add timer.sleep(5) to the end of my code.  The task.start(5) was the very last line in my code. Looks when you set the sample clock to a specific value, python will abort any transfer in progress when the program ends.  But when you don't specify the sample clock, it will wait for the transfer to finish before ending the program.  I guess that behind the scenes the sample clock function is also changing some other settings that change this behavior.  Anyway, works fine now as long as I pause at the end to let everything finish.

Message 4 of 4
(5,455 Views)