11-19-2019 10:48 PM
Hi guys,
Apologize in advance if what I'm asking is too silly. I have very limited experience with NI DAQ.
I have a NI USB-6356 and I want to use its analog output channel to control one component of my experimental setup while using its analog input channels to acquire some voltage signals. I'm using NI-DAQmx Python API for the programming.
After reading a few articles here, so far I'm able to sync the analog output channel write task and the analog input channel read task using the following approach:
import nidaqmx
import nidaqmx.constants as niconst
import numpy as np
system = nidaqmx.system.System.local()
devicename =system.devices[0].name
intask = nidaqmx.Task(new_task_name="in") # read task
outtask = nidaqmx.Task(new_task_name="out") # write task
InputSampleRate = 10
OutputSampleRate = 10
# Set up the read task
intask.ai_channels.add_ai_voltage_chan(
physical_channel=devicename + "/" + 'ai0',
terminal_config=niconst.TerminalConfiguration.DEFAULT,
min_val=-10, max_val=10,
units=niconst.VoltageUnits.VOLTS)
intask.timing.cfg_samp_clk_timing(
rate=InputSampleRate,
active_edge=niconst.Edge.FALLING,
sample_mode=niconst.AcquisitionType.FINITE, samps_per_chan=10)
# Set up the write task, use the sample clock of the Analog input
outtask.ao_channels.add_ao_voltage_chan(
physical_channel=devicename + "/" + 'ao1',
min_val=-10, max_val=10,
units=niconst.VoltageUnits.VOLTS)
outtask.timing.cfg_samp_clk_timing(
rate=OutputSampleRate,
source="/" + devicename + "/ai/SampleClock",
active_edge=niconst.Edge.FALLING,
sample_mode=niconst.AcquisitionType.FINITE, samps_per_chan=10)
writedata = np.linspace(0, 5, 10)
outtask.write(writedata, auto_start=False)
outtask.start() # Start the write task first, waiting for the analog input sample clock
data = intask.read(10) # Start the read task
intask.close()
outtask.close()
However, in the above code, it seems that changing the sample rate of the cfg_samp_clk_timing function for the outtask does not do anything. My understanding is that as I set the source of sample clock of the outtask to be the sample clock of the analog input channels, the sample rate of the outtask is also fixed to be the sample as the analog input channels. Therefore I'm not able to have different sample rate for the outtask and intask. Is this correct?
For my measurement purpose, I do need to have different sample rate for the analog input channel and analog output channel. However I also want to sync the read and write. Is there a way to accomplish that? I'm thinking of using a 10 MHz Reference Clock for both the intask and outtask. Is this a viable approach or is there a better approach?
Thanks a lot in advance!
Yan
Solved! Go to Solution.
11-20-2019 07:35 AM
I'm generally a big advocate of syncing tasks by sharing sample clocks *instead* of using triggers. But for your situation where both tasks are on the same device, the simplest thing for you to do is sync via trigger.
Let both derive their own sample clock from the internal timebase (this is the default if you don't go out of your way to specify otherwise). Then you could configure AO to be triggered by "/Dev1/ai/StartTrigger". This is an internal signal that gets asserted when the AI task starts. (And continue to make the API call to start the AO task before starting AI.)
-Kevin P
11-21-2019 05:05 PM
Hi Kelvin,
Thanks a lot for your help!
I tried it out today and it works perfect well.
Regards,
Yan
09-30-2020 07:03 PM
Hi Yan,
Can you please show me the code that how you link the start trigger of AO to the Ai?