diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml index 3b31283..105ce2d 100644 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -1,7 +1,6 @@ - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 5e11765..cc75a76 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,14 +1,4 @@ - - - - - - - - - - - + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml index 71b2f86..fb8818c 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 325af3f..071d1a1 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,13 +1,15 @@ - + - + + + @@ -18,7 +20,6 @@ - @@ -28,26 +29,43 @@ - + - - + + - - - - + + + + + + + + + + + - + + + + + + + + + + + @@ -72,26 +90,16 @@ - - - - - - - - - - + @@ -110,12 +118,6 @@ - - - - - + - - + - + - - + + + - + + - - @@ -485,15 +498,30 @@ + + + + + + + + + + + + + + + + + + - + - - - @@ -501,7 +529,7 @@ - + @@ -509,12 +537,9 @@ - + - - - @@ -522,12 +547,9 @@ - + - - - @@ -535,14 +557,14 @@ - + - + @@ -550,12 +572,9 @@ - + - - - @@ -563,14 +582,14 @@ - + - + @@ -578,12 +597,9 @@ - + - - - @@ -591,7 +607,7 @@ - + @@ -599,12 +615,9 @@ - + - - - @@ -612,12 +625,9 @@ - + - - - @@ -625,19 +635,16 @@ - + - + - - - @@ -645,12 +652,9 @@ - + - - - @@ -658,7 +662,7 @@ - + @@ -666,37 +670,43 @@ - + - + - - + + + - + - - - - - - - - + + + - + - - + + + + + + + + + + + + \ No newline at end of file diff --git a/DAQ.py b/DAQ.py index 3bf3809..2a3ed76 100644 --- a/DAQ.py +++ b/DAQ.py @@ -82,7 +82,7 @@ def __init__(self, device, samprate, secs, write, clock=''): self.AutoRegisterDoneEvent(0) - self.write = Util.binaryToDigitalMap(write) + self.write = Util.binary_to_digital_map(write) def DoTask(self): print ('Starting digital output') @@ -239,7 +239,102 @@ def DoneCallback(self, status): # region [MultiTasks] +class DoTriggeredCoTask: + def __init__(self, do_device, co_device, samp_rate, secs, write, trigger_source, controlled_carrier=False): + self.do_handle = TaskHandle(0) + self.co_handle = TaskHandle(1) + self.do_device = do_device + + + DAQmxCreateTask('', byref(self.do_handle)) + DAQmxCreateTask('', byref(self.co_handle)) + + DAQmxCreateCOPulseChanFreq(self.co_handle, '/cDAQ1/Ctr0', '', DAQmx_Val_Hz, DAQmx_Val_Low, 0.0, samp_rate, 0.5) + DAQmxCreateDOChan(self.do_handle, do_device, '', DAQmx_Val_ChanForAllLines) + + + DAQmxCfgDigEdgeStartTrig(self.co_handle, trigger_source, DAQmx_Val_Rising) + self.totalLength = numpy.uint64(samp_rate * secs) + self.secs = secs + self.sampsPerChanWritten = int32() + self.write = Util.binary_to_digital_map(write) + self.sampsPerChan = self.write.shape[1] + self.chans = self.write.shape[0] + self.write = numpy.sum(self.write, axis=0) + if controlled_carrier: + self.write += 2**(self.chans+1) + DAQmxCfgImplicitTiming(self.co_handle, DAQmx_Val_FiniteSamps, self.totalLength) + DAQmxCfgSampClkTiming(self.do_handle, '/cDAQ1/Ctr0InternalOutput', samp_rate, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, + numpy.uint64(self.totalLength)) + + def DoTask(self): + + DAQmxWriteDigitalU32(self.do_handle, self.sampsPerChan, 0, -1, DAQmx_Val_GroupByChannel, self.write, + byref(self.sampsPerChanWritten), None) + + + DAQmxStartTask(self.do_handle) + DAQmxStartTask(self.co_handle) + + DAQmxWaitUntilTaskDone(self.co_handle, 100) + DAQmxWaitUntilTaskDone(self.do_handle, 100) + + self.ClearTasks() + + def ClearTasks(self): + time.sleep(0.05) + + DAQmxStopTask(self.do_handle) + DAQmxStopTask(self.co_handle) + + DAQmxClearTask(self.do_handle) + DAQmxClearTask(self.co_handle) + #closeValves(self.do_device) + +class DoCoTask: + def __init__(self, do_device, co_device, samp_rate, secs, write, controlled_carrier=False): + self.do_handle = TaskHandle(0) + self.co_handle = TaskHandle(1) + self.do_device = do_device + + DAQmxCreateTask('', byref(self.do_handle)) + DAQmxCreateTask('', byref(self.co_handle)) + + DAQmxCreateCOPulseChanFreq(self.co_handle, 'cDAQ1/Ctr0', '', DAQmx_Val_Hz, DAQmx_Val_Low, 0.0, samp_rate, 0.5) ## Creates a channel to generate digital pulses + DAQmxCreateDOChan(self.do_handle, do_device, '', DAQmx_Val_ChanForAllLines) + + self.totalLength = numpy.uint64(samp_rate * secs) + self.secs = secs + self.sampsPerChanWritten = int32() + self.write = Util.binary_to_digital_map(write) + self.sampsPerChan = self.write.shape[1] + self.chans = self.write.shape[0] + self.write = numpy.sum(self.write, axis=0) + # if controlled_carrier: + # self.write += 2**(self.chans+1) + + DAQmxCfgImplicitTiming(self.co_handle, DAQmx_Val_FiniteSamps, self.totalLength) + DAQmxCfgSampClkTiming(self.do_handle, '/cDAQ1/Ctr0InternalOutput', samp_rate, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, + numpy.uint64(self.totalLength)) + def DoTask(self): + DAQmxWriteDigitalU32(self.do_handle, self.sampsPerChan, 0, -1, DAQmx_Val_GroupByChannel, self.write, + byref(self.sampsPerChanWritten), None) + + DAQmxStartTask(self.do_handle) + DAQmxStartTask(self.co_handle) + DAQmxWaitUntilTaskDone(self.co_handle, 100) + DAQmxWaitUntilTaskDone(self.do_handle, 100) + self.ClearTasks() + + def ClearTasks(self): + time.sleep(0.05) + DAQmxStopTask(self.do_handle) + DAQmxStopTask(self.co_handle) + + DAQmxClearTask(self.do_handle) + DAQmxClearTask(self.co_handle) + #closeValves(self.do_device) class DoAiMultiTask: def __init__(self, ai_device, ai_channels, do_device, samp_rate, secs, write, sync_clock): @@ -320,6 +415,7 @@ def __init__(self, ai_device, ai_channels, do_device, samp_rate, secs, write, sy numpy.uint64(self.totalLength)) def DoTask(self): + DAQmxWriteDigitalU32(self.do_handle, self.sampsPerChan, 0, -1, DAQmx_Val_GroupByChannel, self.write, byref(self.sampsPerChanWritten), None) @@ -427,7 +523,80 @@ def DoTask(self): self.totalLength * self.di_channels, byref(self.di_read), None) - +def closeValves(do_device): + do_handle = TaskHandle(0) + co_handle = TaskHandle(1) + DAQmxCreateTask('', byref(do_handle)) + DAQmxCreateTask('', byref(co_handle)) + + DAQmxCreateCOPulseChanFreq(co_handle, 'cDAQ1/Ctr0', '', DAQmx_Val_Hz, DAQmx_Val_Low, 0.0, 20000, 0.5) + DAQmxCreateDOChan(do_handle, do_device, '', DAQmx_Val_ChanForAllLines) + + DAQmxCfgImplicitTiming(co_handle, DAQmx_Val_FiniteSamps, 100) + DAQmxCfgSampClkTiming(do_handle, '/cDAQ1/Ctr0InternalOutput', 20000, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, + numpy.uint64(100)) + DAQmxWriteDigitalU32(do_handle, 100, 0, -1, DAQmx_Val_GroupByChannel, numpy.zeros(100, dtype=numpy.uint32), + byref(int32()), None) + + DAQmxStartTask(do_handle) + DAQmxStartTask(co_handle) + # DAQmxWaitUntilTaskDone(co_handle, 100) + # DAQmxWaitUntilTaskDone(do_handle, 100) + + time.sleep(0.05) + DAQmxStopTask(co_handle) + DAQmxClearTask(co_handle) + + DAQmxStopTask(do_handle) + DAQmxClearTask(do_handle) + + +## TODO generalise for other devices, currently needs to be the same one as the valves +# def carrierControlTask(do_device, samp_rate, secs): +# do_handle = TaskHandle(2) +# co_handle = TaskHandle(3) +# DAQmxCreateTask('', byref(do_handle)) +# DAQmxCreateTask('', byref(co_handle)) +# DAQmxCreateCOPulseChanFreq(co_handle, 'cDAQ1/Ctr0', '', DAQmx_Val_Hz, DAQmx_Val_Low, 0.0, samp_rate, 0.5) ## Creates a channel to generate digital pulses +# DAQmxCreateDOChan(do_handle, do_device, '', DAQmx_Val_ChanForAllLines) +# totallength = numpy.uint64(samp_rate * secs) +# secs = secs +# sampsPerChanWritten = int32() +# chan_index = int(do_device[-1]) +# write = np.ones(totallength)*(2**chan_index) +# write = write.astype(np.uint32) +# print(write) +# DAQmxCfgImplicitTiming(co_handle, DAQmx_Val_FiniteSamps, totallength) +# DAQmxCfgSampClkTiming(do_handle, '/cDAQ1/Ctr0InternalOutput', samp_rate, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, +# numpy.uint64(totallength)) +# DAQmxWriteDigitalU32(do_handle, len(write), 0, -1, DAQmx_Val_GroupByChannel, write, +# byref(sampsPerChanWritten), None) +# DAQmxStartTask(do_handle) +# DAQmxStartTask(co_handle) +# DAQmxWaitUntilTaskDone(co_handle, 100) +# DAQmxWaitUntilTaskDone(do_handle, 100) +# # do_handle = TaskHandle(2) +# # DAQmxCreateTask('', byref(do_handle)) +# # DAQmxCreateDOChan(do_handle, do_device, '', DAQmx_Val_ChanForAllLines) +# # totallength = numpy.uint64(samp_rate * secs) +# # secs = secs +# # sampsPerChanWritten = int32() +# # write = np.ones(totallength)*512 +# # write = write.astype(np.uint32) +# # print(write) +# # DAQmxCfgSampClkTiming(do_handle, '/cDAQ1/Ctr0InternalOutput', samp_rate, DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, +# # numpy.uint64(totallength)) +# # DAQmxWriteDigitalU32(do_handle, totallength, 0, -1, DAQmx_Val_GroupByChannel, write, +# # byref(sampsPerChanWritten), None) +# # DAQmxStartTask(do_handle) +# # DAQmxWaitUntilTaskDone(do_handle, 10) +# DAQmxStopTask(co_handle) +# DAQmxClearTask(co_handle) +# DAQmxStopTask(do_handle) +# DAQmxClearTask(do_handle) + + +# carrierControlTask(do_device, 20000, 2) # TODO TESTING # # region DoAiMultiTaskTest # a = DoAiMultiTask('cDAQ1Mod3/ai0', 1, 'cDAQ1Mod1/port0/line0', 1000.0, 1.0, numpy.zeros((2, 1000)), diff --git a/README.md b/README.md deleted file mode 100644 index 6234804..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# daqface -A high-level python interface for a number of common NI DAQmx hardware tasks.