def test_manual(): with nidaqmx.Task() as write_task, nidaqmx.Task( ) as read_task, nidaqmx.Task() as sample_clk_task: # pd_voltage_channel = write_task.ai_channels.add_ai_voltage_chan("Dev2/ai0") # wavelength_channel = write_task.ai_channels.add_ai_voltage_chan("Dev2/ai1") sample_rate = 1001 read_task.ai_channels.add_ai_voltage_chan("Dev2/ai0") read_task.ai_channels.add_ai_voltage_chan("Dev2/ai1") # read_task.timing.cfg_samp_clk_timing( # sample_rate, source=samp_clk_terminal, # active_edge=Edge.FALLING, samps_per_chan=number_of_samples) reader = AnalogMultiChannelReader(read_task.in_stream) read_task.start() number_of_channels = 3 number_of_samples = 100 values_read = numpy.zeros((number_of_channels, number_of_samples), dtype=numpy.float64) reader.read_many_sample( values_read, number_of_samples_per_channel=number_of_samples, timeout=2) print(values_read) plt.plot(values_read[0, :], values_read[1, :]) plt.show()
def NIDAQ_Trigger(): import numpy import nidaqmx from nidaqmx.stream_readers import (AnalogSingleChannelReader, AnalogMultiChannelReader) from nidaqmx.constants import (Edge, Slope) from nidaqmx._task_modules.triggering.start_trigger import StartTrigger #from nidaqmx.tests.fixtures import x_series_device # import nidaqmx.task as task data = numpy.zeros((3, 1000), dtype=numpy.float64) with nidaqmx.Task() as read_task: read_task.ai_channels.add_ai_voltage_chan( "Dev1/ai0:2", terminal_config=nidaqmx.constants.TerminalConfiguration.RSE) read_task.timing.cfg_samp_clk_timing(1e4, active_edge=Edge.RISING, samps_per_chan=1000) read_task.triggers.start_trigger.cfg_dig_edge_start_trig( trigger_source="/Dev2/PFI0") #read_task.timing.delay_from_samp_clk_delay=0 #s=nidaqmx.stream_readers.AnalogMultiChannelReader() reader = AnalogMultiChannelReader(read_task.in_stream) reader.read_many_sample(data, number_of_samples_per_channel=1000, timeout=1) # print(data) return (data)
def run(self): """ Starts writing a waveform continuously to the patchclamp. While reading the buffer periodically """ self.patchVoltOutChan = self.configs["VpPatch"] self.patchCurOutChan = self.configs["Ip"] self.patchVoltInChan = self.configs["patchAO"] # DAQ with nidaqmx.Task() as writeTask, nidaqmx.Task() as readTask: writeTask.ao_channels.add_ao_voltage_chan(self.patchVoltInChan) readTask.ai_channels.add_ai_voltage_chan(self.patchVoltOutChan) readTask.ai_channels.add_ai_voltage_chan(self.patchCurOutChan) self.setTiming(writeTask, readTask) reader = AnalogMultiChannelReader(readTask.in_stream) writer = AnalogSingleChannelWriter(writeTask.out_stream) writer.write_many_sample(self.wave) """Reading data from the buffer in a loop. The idea is to let the task read more than could be loaded in the buffer for each iteration. This way the task will have to wait slightly longer for incoming samples. And leaves the buffer entirely clean. This way we always know the correct numpy size and are always left with an empty buffer (and the buffer will not slowly fill up).""" # output = np.zeros([2, self.readNumber]) writeTask.start() # Will wait for the readtask to start so it can use its clock readTask.start()
def rec_only(device, t_array, inputs): dt = t_array[1] - t_array[0] sampling_rate = 1. / dt # if outputs.shape[0]>0: input_channels = get_analog_input_channels(device)[:inputs.shape[0]] with nidaqmx.Task() as read_task, nidaqmx.Task() as sample_clk_task: # Use a counter output pulse train task as the sample clock source # for both the AI and AO tasks. sample_clk_task.co_channels.add_co_pulse_chan_freq('{0}/ctr0'.format( device.name), freq=sampling_rate) sample_clk_task.timing.cfg_implicit_timing(samps_per_chan=len(t_array)) samp_clk_terminal = '/{0}/Ctr0InternalOutput'.format(device.name) read_task.ai_channels.add_ai_voltage_chan( flatten_channel_string(input_channels), max_val=10, min_val=-10) read_task.timing.cfg_samp_clk_timing(sampling_rate, source=samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=len(t_array)) reader = AnalogMultiChannelReader(read_task.in_stream) # Start the read task before starting the sample clock source task. read_task.start() sample_clk_task.start() reader.read_many_sample(inputs, number_of_samples_per_channel=len(t_array), timeout=t_array[-1] + 2 * dt)
def daq_acquisition(): global running global reader global daq_data running = True reader = AnalogMultiChannelReader(daq_read_task.in_stream) daq_read_task.register_every_n_samples_acquired_into_buffer_event( bufsize_callback, reading_task_callback)
def run(self): """Runs when the start method is called on the thread. First, a nidaqmx is started using the Python with structure, which automatically stops the task when it is exited. Next, the thread tries to open the channels, but if it fails, emits an error signal. Next, the timing is set including the DAQ rate and sample mode. If not, again, it will emit an error. A multi-channel reader is defined and attached to the in_stream according to the nidaqmx structure. The task is start which then enables control over the reader. While the running bool is true, the thread waits for there to be *readchunksize* values, then emits them as an array. """ self.running = True with nidaqmx.Task() as readTask: # Add input channels for index, i in enumerate(self.readchannel_list): channel_string = self.daq_name + '/' + i try: # RSE = Referenced Single Ended readTask.ai_channels.add_ai_voltage_chan( channel_string, terminal_config=nidaqmx.constants. TerminalConfiguration.RSE) except Exception as e: if index == 0: self.errorMessage.emit( "Couldn't initialize the read channels - is the read device name correct? " f'Devices connected: {find_ni_devices()}') return # Set timing and acquisition mode try: readTask.timing.cfg_samp_clk_timing( rate=self.daq_rate, sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS) except Exception as e: self.errorMessage.emit( "Couldn't start the read task - is the read device name correct? " f'Devices connected: {find_ni_devices()}') return # Define the reader and start the *task* reader = AnalogMultiChannelReader(readTask.in_stream) readTask.start() # Waits until there are *readchunksize* values in the buffer and emits the output array while self.running: try: reader.read_many_sample( data=self.output, number_of_samples_per_channel=self.readchunksize) self.output = np.around(self.output, 4) self.newData.emit(self.output) except Exception as e: print(str(e)) pass
def __init__(self, mouseID, name, version, session, sniffing, notes): self.mouseID = mouseID self.name = name self.version = version self.session = session self.sniffing = sniffing self.notes = notes self.session_start = None self.reader = AnalogMultiChannelReader(ni_data.in_stream) self.data = np.zeros((channel_num, buffersize), dtype=np.float64) #buffersize.value
def __enter__(self): self._device_name = nidaqmx.system.System.local().devices[0].name self.channels = [ self._device_name + '/ai' + str(n) for n in [1, 2, 3, 4, 5, 6] ] # x is 7 - 8 (ai3 - ai11) # y may be 3 - 4 (ai1 - ai9) self._device = nidaqmx.Task() self._device.ai_channels.add_ai_voltage_chan( ','.join(self.channels), terminal_config=TerminalConfiguration.DIFFERENTIAL) self._reader = AnalogMultiChannelReader(self._device.in_stream) self._device.start() return self
def __enter__(self): self._device_name = nidaqmx.system.System.local().devices[0].name self.channels = [self._device_name + '/ai' + str(n) for n in [1, 2, 3, 4, 5]] # todo: check self._device = nidaqmx.Task() self._device.ai_channels.add_ai_voltage_chan(','.join(self.channels), terminal_config=TerminalConfiguration.DIFFERENTIAL, min_val=-10.0, max_val=10.0) self._device.timing.cfg_samp_clk_timing(self.sampling_frequency, sample_mode=AcquisitionType.CONTINUOUS) self._reader = AnalogMultiChannelReader(self._device.in_stream) self._device.register_every_n_samples_acquired_into_buffer_event(self._data_buffer.shape[1], self.callback) self._device.start() return self
def read_multiple_data_from_an_ai_channel(name_device='Dev1', name_chans='Dev1/ai0'): import nidaqmx.task as task from nidaqmx.stream_readers import AnalogMultiChannelReader from nidaqmx._task_modules.in_stream import InStream import numpy as np task_read_data = task.Task('task_in_stream') task_read_data.ai_channels.add_ai_voltage_chan('Dev1/ai0:1') task_read_data.read(number_of_samples_per_channel=512) task_in_stream = InStream(task_read_data) stored_data = np.empty((2, 512)) analog_multi_chan_reader = AnalogMultiChannelReader(task_in_stream) analog_multi_chan_reader.read_many_sample(data=stored_data, number_of_samples_per_channel=512) task_read_data.close()
def niRead(self,dev,samples,indexs): #buf = np.empty((len(indexs), 0), dtype=np.float64) data = np.empty((len(indexs), samples), dtype=np.float64) try: with nidaqmx.Task() as task: task.ai_channels.add_ai_voltage_chan( ','.join([dev + '/ai' + str(c) for c in indexs])) task.timing.cfg_samp_clk_timing(samples, samps_per_chan=samples) stream_task = AnalogMultiChannelReader(task.in_stream) stream_task.read_many_sample( data, number_of_samples_per_channel=samples) s1 = data.mean(axis=1) return s1.copy() except: return
def enter(self): # assume first NI DAQ is the one we want self._device_name = nidaqmx.system.System.local().devices[0].name chans = [2, 9, 1, 8, 0, 10, 3, 11, 4, 12] sub_chans = [chans[i] for i in self._indices] channels = [self._device_name + ('/ai%i' % n) for n in sub_chans] channels = ','.join(channels) dev = nidaqmx.Task() dev.ai_channels.add_ai_voltage_chan( channels, terminal_config=TerminalConfiguration.RSE) dev.timing.cfg_samp_clk_timing(self.sampling_frequency, sample_mode=AcquisitionType.CONTINUOUS) self._reader = AnalogMultiChannelReader(dev.in_stream) dev.start() self._device = dev
def read_data(self, arr=None): if not self.reader: from nidaqmx.stream_readers import AnalogMultiChannelReader self.reader = AnalogMultiChannelReader(self._task.in_stream) #chan_names = stream._task.ai_channels.channel_names number_of_channels = len(self.channels) nsamp = self.samp_per_chan data = np.empty((number_of_channels, nsamp), dtype=np.float64) nread = self.reader.read_many_sample( data, number_of_samples_per_channel=nsamp, timeout=self.timeout) data_dict = {} for k, chan in enumerate(self.channels): #print(chan.phys_name) #print(data[k,:]) data_dict[chan.phys_name] = data[k, :] return data_dict
def measure(self): """ Starts writing a waveform continuously to the patchclamp. While reading the buffer periodically. """ self.patchVoltOutChan = self.configs["Vp"] self.patchCurOutChan = self.configs["Ip"] self.patchVoltInChan = self.configs["patchAO"] # DAQ with nidaqmx.Task() as writeTask, nidaqmx.Task() as readTask: writeTask.ao_channels.add_ao_voltage_chan(self.patchVoltInChan) readTask.ai_channels.add_ai_voltage_chan(self.patchVoltOutChan) readTask.ai_channels.add_ai_voltage_chan(self.patchCurOutChan) self.setTiming(writeTask, readTask) reader = AnalogMultiChannelReader(readTask.in_stream) writer = AnalogSingleChannelWriter(writeTask.out_stream) writer.write_many_sample(self.wave) """Reading data from the buffer in a loop. The idea is to let the task read more than could be loaded in the buffer for each iteration. This way the task will have to wait slightly longer for incoming samples. And leaves the buffer entirely clean. This way we always know the correct numpy size and are always left with an empty buffer (and the buffer will not slowly fill up).""" if self.mode == 'voltageclamp': output = np.zeros([2, self.readNumber]) writeTask.start() # Will wait for the readtask to start so it can use its clock readTask.start() self.isRunning = True while self.isRunning: reader.read_many_sample(data=output, number_of_samples_per_channel=self.readNumber) # Emiting the data just received as a signal self.measurement.emit(output[0,:], output[1,:]) elif self.mode == 'zap': writeTask.start() # Will wait for the readtask to start so it can use its clock readTask.start() else: pass print('sealtest thread stopped')
def scan_loop(self, read_task, write_task): writer = AnalogMultiChannelWriter(write_task.out_stream) reader = AnalogMultiChannelReader(read_task.in_stream) first_write = True i_acquired = 0 while not self.stop_event.is_set() and ( not self.scanning_parameters.scanning_state == ScanningState.EXPERIMENT_RUNNING or i_acquired < self.scanning_parameters.n_frames): # The first write has to be defined before the task starts try: writer.write_many_sample(self.write_signals) if i_acquired == 0: self.check_start_plane() if first_write: read_task.start() write_task.start() first_write = False reader.read_many_sample( self.read_buffer, number_of_samples_per_channel=self.n_samples_in, timeout=1, ) i_acquired += 1 except nidaqmx.DaqError as e: print(e) break self.data_queue.put(self.read_buffer[0, :]) # if new parameters have been received and changed, update # them, breaking out of the loop if the experiment is not running try: self.new_parameters = self.parameter_queue.get(timeout=0.0001) if self.new_parameters != self.scanning_parameters and ( self.scanning_parameters.scanning_state != ScanningState.EXPERIMENT_RUNNING or self.new_parameters.scanning_state == ScanningState.PREVIEW): break except Empty: pass # calculate duration self.calculate_duration()
def __init__(self, number_of_samples, sample_rate): read_task = nidaqmx.Task() read_task.ai_channels.add_ai_voltage_chan("Dev2/ai0") read_task.ai_channels.add_ai_voltage_chan("Dev2/ai1") read_task.ai_channels.add_ai_voltage_chan("Dev2/ai2") #read_task.in_stream.over_write = nidaqmx.constants.OverwriteMode.OVERWRITE_UNREAD_SAMPLES # read_task.co_channels.add_co_pulse_chan_freq( # '{0}/ctr0'.format(x_series_device.name), freq=sample_rate) # read_task.timing.cfg_implicit_timing( # samps_per_chan=number_of_samples) # # samp_clk_terminal = '/{0}/Ctr0InternalOutput'.format( # x_series_device.name) # read_task.timing.cfg_implicit_timing() # # read_task.timing.cfg_samp_clk_timing( # sample_rate, source=samp_clk_terminal, active_edge=Edge.RISING, # samps_per_chan=number_of_samples) read_task.timing.cfg_samp_clk_timing(rate=sample_rate, samps_per_chan=number_of_samples) # read_task.timing.cfg_samp_clk_timing( # sample_rate, source=samp_clk_terminal, # active_edge=Edge.FALLING, samps_per_chan=number_of_samples) self._reader = AnalogMultiChannelReader(read_task.in_stream) read_task.start() self._task = read_task number_of_channels = 3 self._values_read = numpy.zeros( (number_of_channels, number_of_samples), dtype=numpy.float64) self._number_of_samples = number_of_samples self._timeout = number_of_samples / sample_rate + 2
def perform_coarse_scan(self): read_task = nidaqmx.Task() self.add_ai_channels_to_task(read_task) self.values_read = np.zeros( (self.number_of_channels, self.samples_per_channel), dtype=np.float64 ) #create buffer to store samples in (this is required if samples>1000) reader = AnalogMultiChannelReader(read_task.in_stream) print('Start sweeping now.') self.TL6800_sweep_start(printbool=False, readbool=False) # start sweeping read_task.start() read_task.wait_until_done(timeout=4000) reader.read_many_sample( self.values_read, number_of_samples_per_channel=self.samples_per_channel, timeout=4000) read_task.close() self.TL6800_read_ascii( ) #readbool is flagged down in the sweep, now laser buffer has to be emptied
def setup_DAQmx(self): self.read_task = nidaqmx.Task() self.write_task = nidaqmx.Task() self.sample_clk_task = nidaqmx.Task() # Use a counter output pulse train task as the sample clock source # for both the AI and AO tasks. self.sample_clk_task.co_channels.add_co_pulse_chan_freq('Dev1/ctr0', freq=self.sample_rate, idle_state=Level.LOW) self.sample_clk_task.timing.cfg_implicit_timing(sample_mode=AcquisitionType.CONTINUOUS,samps_per_chan=self.number_of_samples) self.sample_clk_task.control(TaskMode.TASK_COMMIT) samp_clk_terminal = '/Dev1/Ctr0InternalOutput' self.read_task.ai_channels.add_ai_voltage_chan(self.input_channel, max_val=10, min_val=-10) self.read_task.timing.cfg_samp_clk_timing(self.sample_rate, source=samp_clk_terminal, active_edge=Edge.FALLING,sample_mode=AcquisitionType.CONTINUOUS, samps_per_chan=self.number_of_samples) self.write_task.ao_channels.add_ao_voltage_chan(self.output_channel, max_val=10, min_val=-10) self.write_task.timing.cfg_samp_clk_timing(self.sample_rate, source=samp_clk_terminal, active_edge=Edge.FALLING,sample_mode=AcquisitionType.CONTINUOUS, samps_per_chan=self.number_of_samples) self.write_task.out_stream.regen_mode = RegenerationMode.DONT_ALLOW_REGENERATION self.write_task.out_stream.auto_start = False self.writer = AnalogSingleChannelWriter(self.write_task.out_stream) self.reader = AnalogMultiChannelReader(self.read_task.in_stream)
def openall(aoi=0, aof=3, aii=0, aif=31, read=True, write=False, clock=False, sample_rate=500000, initial_delay=0, duty_cycle=0.5, samps_per_chan=1): ''' 4 output channels, 32 input channels duty cycle = pulse width / pulse period clock must involve read and write ''' ad = address() rs = ad.lookup(mdlname) # Instrument's Address print(type(rs)) read_task, write_task, clock_task, max_samp_rate, writer, reader = None, None, None, None, None, None if read: read_task = nidaqmx.Task() read_task.ai_channels.add_ai_voltage_chan("%s/ai%s:%s" %(rs,aii,aif), terminal_config=TerminalConfiguration.RSE, min_val=-10, max_val=10) if write: write_task = nidaqmx.Task() write_task.ao_channels.add_ao_voltage_chan("%s/ao%s:%s" %(rs,aoi,aof)) # write_task.ao_channels.add_ao_func_gen_chan if clock and write and read: # one-shot data streaming # Use a counter output pulse train task as the sample clock source for both the AI and AO tasks. max_samp_rate = read_task.timing.samp_clk_max_rate print("Max reading rate: %s" %max_samp_rate) sample_rate = floor(min(sample_rate, max_samp_rate)) clock_task = nidaqmx.Task() clock_task.co_channels.add_co_pulse_chan_freq('{0}/ctr0'.format(rs), freq=sample_rate, initial_delay=initial_delay, duty_cycle=duty_cycle) clock_task.timing.cfg_implicit_timing(sample_mode=AcquisitionType.FINITE, samps_per_chan=samps_per_chan) samp_clk_terminal = '/{0}/Ctr0InternalOutput'.format(rs) write_task.timing.cfg_samp_clk_timing(sample_rate, source=samp_clk_terminal, active_edge=Edge.RISING, samps_per_chan=samps_per_chan) read_task.timing.cfg_samp_clk_timing(sample_rate, source=samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=samps_per_chan) # Single Channel: writer = AnalogSingleChannelWriter(write_task.out_stream) # reader = AnalogSingleChannelReader(read_task.in_stream) # Multi Channel: # writer = AnalogMultiChannelWriter(write_task.out_stream) reader = AnalogMultiChannelReader(read_task.in_stream) pack = dict(write_task=write_task, read_task=read_task, clock_task=clock_task, max_samp_rate=max_samp_rate, writer=writer, reader=reader) return pack
def run(self): """ Starts writing a waveform continuously to the patchclamp. While reading the buffer periodically """ self.patchVoltOutChan = self.configs.patchVoltOutChannel self.patchCurOutChan = self.configs.patchCurOutChannel self.patchVoltInChan = self.configs.patchVoltInChannel #DAQ with nidaqmx.Task() as writeTask, nidaqmx.Task() as readTask: writeTask.ao_channels.add_ao_voltage_chan(self.patchVoltInChan) readTask.ai_channels.add_ai_voltage_chan(self.patchVoltOutChan) readTask.ai_channels.add_ai_voltage_chan(self.patchCurOutChan) self.setTiming(writeTask, readTask) reader = AnalogMultiChannelReader(readTask.in_stream) writer = AnalogSingleChannelWriter(writeTask.out_stream) writer.write_many_sample(self.wave) """Reading data from the buffer in a loop. The idea is to let the task read more than could be loaded in the buffer for each iteration. This way the task will have to wait slightly longer for incoming samples. And leaves the buffer entirely clean. This way we always know the correct numpy size and are always left with an empty buffer (and the buffer will not slowly fill up).""" output = np.zeros([2, self.readNumber]) writeTask.start( ) #Will wait for the readtask to start so it can use its clock readTask.start() while not self.isInterruptionRequested(): reader.read_many_sample( data=output, number_of_samples_per_channel=self.readNumber) #Emiting the data just received as a signal #output = np.around(output, 7) #Round all values self.measurement.emit(output[0, :], output[1, :])
def test_one_sample(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) # Select a random loopback channel pair on the device. loopback_channel_pairs = self._get_analog_loopback_channels( x_series_device) number_of_channels = random.randint(2, len(loopback_channel_pairs)) channels_to_test = random.sample( loopback_channel_pairs, number_of_channels) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task: write_task.ao_channels.add_ao_voltage_chan( flatten_channel_string( [c.output_channel for c in channels_to_test]), max_val=10, min_val=-10) read_task.ai_channels.add_ai_voltage_chan( flatten_channel_string( [c.input_channel for c in channels_to_test]), max_val=10, min_val=-10) writer = AnalogMultiChannelWriter(write_task.out_stream) reader = AnalogMultiChannelReader(read_task.in_stream) values_to_test = numpy.array( [random.uniform(-10, 10) for _ in range(number_of_channels)], dtype=numpy.float64) writer.write_one_sample(values_to_test) time.sleep(0.001) values_read = numpy.zeros(number_of_channels, dtype=numpy.float64) reader.read_one_sample(values_read) numpy.testing.assert_allclose( values_read, values_to_test, rtol=0.05, atol=0.005)
def runWaveforms(self, clock_source, sampling_rate, analog_signals, digital_signals, readin_channels): """ Input: - samplingrate: Sampling rate of the waveforms. - analogsignals: Signals for the analog channels. It's a structured array with two fields: 1) 'Waveform': Raw 1-D np.array of actual float voltage signals. 2) 'Sepcification': string telling which device to control that help to specify the NI-daq port, check self.channel_LUT. Multiple waveforms should be stack on top of each other using np.stack. if empty, input can be a blank {}. -digitalsignals: Signals for the digital channels. It's a structured array with two fields: 1) 'Waveform': Raw 1-D np.array of type bool. 2) 'Sepcification': string that specifies the NI-daq port. for example: dtype = np.dtype([('Waveform', float, (self.reference_length,)), ('Sepcification', 'U20')]) -readinchannels: A list that contains the readin channels wanted. """ # ============================================================================= # Setting up waveforms # ============================================================================= Analog_channel_number = len(analog_signals) Digital_channel_number = len(digital_signals) self.readin_channels = readin_channels self.sampling_rate = sampling_rate #---------------------------------------------------------------------- # galvosx and galvosy as specification key words are already enough. # Get the average number and y pixel number information from data self.galvosx_originalkey = 'galvosx' self.galvosy_originalkey = 'galvosy' # If there are kyes with information like 'galvosxavgnum', extract the # information and then convert the key to 'galvosx'. if Analog_channel_number != 0: for i in range(len(analog_signals['Sepcification'])): if 'galvosxavgnum' in analog_signals['Sepcification'][i]: self.averagenumber = int(analog_signals['Sepcification'][i][analog_signals['Sepcification'][i].index('_')+1:len(analog_signals['Sepcification'][i])]) self.galvosx_originalkey = analog_signals['Sepcification'][i] analog_signals['Sepcification'][i] = 'galvosx' elif 'galvosyypixels' in analog_signals['Sepcification'][i]: self.ypixelnumber = int(analog_signals['Sepcification'][i][analog_signals['Sepcification'][i].index('_')+1:len(analog_signals['Sepcification'][i])]) self.galvosy_originalkey = analog_signals['Sepcification'][i] analog_signals['Sepcification'][i] = 'galvosy' elif 'galvos_X_contour' in analog_signals['Sepcification'][i]: self.galvosx_originalkey = analog_signals['Sepcification'][i] analog_signals['Sepcification'][i] = 'galvosx' elif 'galvos_Y_contour' in analog_signals['Sepcification'][i]: self.galvosy_originalkey = analog_signals['Sepcification'][i] analog_signals['Sepcification'][i] = 'galvosy' #---------------------------------------------------------------------- #-------------------Devide samples from Dev1 or 2---------------------- self.Dev1_analog_channel_list = [] self.Dev2_analog_channel_list = [] Dev1_analog_waveforms_list = [] Dev2_analog_waveforms_list = [] if Analog_channel_number != 0: if len(analog_signals['Waveform']) != 0: num_rows, num_cols = analog_signals['Waveform'].shape for i in range(int(num_rows)): if 'Dev1' in self.channel_LUT[analog_signals['Sepcification'][i]]: self.Dev1_analog_channel_list.append(self.channel_LUT[analog_signals['Sepcification'][i]]) Dev1_analog_waveforms_list.append(analog_signals['Waveform'][i]) else: self.Dev2_analog_channel_list.append(self.channel_LUT[analog_signals['Sepcification'][i]]) Dev2_analog_waveforms_list.append(analog_signals['Waveform'][i]) Dev1_analog_channel_number = len(self.Dev1_analog_channel_list) Dev2_analog_channel_number = len(self.Dev2_analog_channel_list) #---------------------------------------------------------------------- # See if only digital signal is involved. if Analog_channel_number == 0 and Digital_channel_number != 0: self.Only_Digital_signals = True else: self.Only_Digital_signals = False # See if Dev1 is involved. If only Dev2 is involved in sending analog # signals then the timing configs are different. #------------------Number of samples in each waveform------------------ if self.Only_Digital_signals == False: self.Waveforms_length = len(analog_signals['Waveform'][0]) num_rows, num_cols = analog_signals['Waveform'].shape print("row number of analog signals: "+str(num_rows)) elif self.Only_Digital_signals == True: self.Waveforms_length = len(digital_signals['Waveform'][0]) #---------------------------------------------------------------------- #-------Stack the Analog samples of dev1 and dev2 individually--------- # IN CASE OF ONLY ONE ARRAY, WE NEED TO CONVERT THE SHAPE TO (1,N) BY USING np.array([]) OUTSIDE THE ARRAY!! if Dev1_analog_channel_number == 1: Dev1_analog_samples_to_write = np.array([Dev1_analog_waveforms_list[0]]) elif Dev1_analog_channel_number == 0: Dev1_analog_samples_to_write = [] else: Dev1_analog_samples_to_write = Dev1_analog_waveforms_list[0] for i in range(1, Dev1_analog_channel_number): Dev1_analog_samples_to_write = np.vstack((Dev1_analog_samples_to_write, Dev1_analog_waveforms_list[i])) if Dev2_analog_channel_number == 1: Dev2_analog_samples_to_write = np.array([Dev2_analog_waveforms_list[0]]) elif Dev2_analog_channel_number == 0: Dev2_analog_samples_to_write = [] else: Dev2_analog_samples_to_write = Dev2_analog_waveforms_list[0] for i in range(1, Dev2_analog_channel_number): Dev2_analog_samples_to_write = np.vstack((Dev2_analog_samples_to_write, Dev2_analog_waveforms_list[i])) # Stack the digital samples if Digital_channel_number == 1: Digital_samples_to_write = np.array([digital_signals['Waveform'][0]]) elif Digital_channel_number == 0: Digital_samples_to_write = [] else: Digital_samples_to_write = digital_signals['Waveform'][0] for i in range(1, Digital_channel_number): Digital_samples_to_write = np.vstack((Digital_samples_to_write, digital_signals['Waveform'][i])) #---------------------------------------------------------------------- #------------------Set the dtype of digital signals-------------------- # For each digital waveform sample, it 0 or 1. To write to NI-daq, you # need to send int number corresponding to the channel binary value, # like write 8(2^3, 0001) to channel 4. # The same as (0b1 << n) Digital_samples_to_write = np.array(Digital_samples_to_write, dtype = 'uint32') for i in range(Digital_channel_number): convernum = int(self.channel_LUT[digital_signals['Sepcification'][i]] [self.channel_LUT[digital_signals['Sepcification'][i]].index('line')+ 4:len(self.channel_LUT[digital_signals['Sepcification'][i]])]) Digital_samples_to_write[i] = Digital_samples_to_write[i]*(2**(convernum)) # For example, to send commands to line 0 and line 3, you hava to write 1001 to digital port, convert to uint32 that is 9. if Digital_channel_number > 1: Digital_samples_to_write = np.sum(Digital_samples_to_write, axis = 0) # sum along the columns, for multiple lines Digital_samples_to_write = np.array([Digital_samples_to_write]) # here convert the shape from (n,) to (1,n) #------------Set up data holder for recording data--------------------- if len(self.readin_channels) != 0: self.has_recording_channel = True else: self.has_recording_channel = False if self.has_recording_channel == True: self.Dataholder = np.zeros((len(self.readin_channels), self.Waveforms_length)) else: self.Dataholder = np.zeros((1, self.Waveforms_length)) #---------------------------------------------------------------------- # ============================================================================= # Configure DAQ channels and execute waveforms # ============================================================================= """ # ============================================================================= # Analog signal in Dev 1 is involved # ============================================================================= """ if Dev1_analog_channel_number != 0: with nidaqmx.Task() as slave_Task_1_analog_dev1, nidaqmx.Task() as slave_Task_1_analog_dev2, nidaqmx.Task() as master_Task_readin, nidaqmx.Task() as slave_Task_2_digitallines: #------------------adding channels------------------------- # Set tasks from different devices apart for i in range(Dev1_analog_channel_number): slave_Task_1_analog_dev1.ao_channels.add_ao_voltage_chan(self.Dev1_analog_channel_list[i]) slave_Task_2_digitallines.do_channels.add_do_chan("/Dev1/port0", line_grouping=LineGrouping.CHAN_FOR_ALL_LINES) if self.has_recording_channel == True: self.Dataholder = np.zeros((len(self.readin_channels), self.Waveforms_length)) else: self.Dataholder = np.zeros((1, self.Waveforms_length)) master_Task_readin.ai_channels.add_ai_voltage_chan(self.channel_LUT['Vp']) # If no read-in channel is added, vp channel is added to keep code alive. # print(self.Dataholder.shape) if 'PMT' in self.readin_channels: master_Task_readin.ai_channels.add_ai_voltage_chan(self.channel_LUT['PMT']) if 'Vp' in self.readin_channels: master_Task_readin.ai_channels.add_ai_voltage_chan(self.channel_LUT['Vp']) if 'Ip' in self.readin_channels: master_Task_readin.ai_channels.add_ai_current_chan(self.channel_LUT['Ip']) #---------------------------------------------------------- #---------------get scaling coefficients------------------- self.aichannelnames=master_Task_readin.ai_channels.channel_names self.ai_dev_scaling_coeff_vp = [] self.ai_dev_scaling_coeff_ip = [] if "Vp" in self.readin_channels: self.ai_dev_scaling_coeff_vp = nidaqmx._task_modules.channels.ai_channel.AIChannel(master_Task_readin._handle, self.channel_LUT['Vp']) #https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019TuoSAE&l=nl-NL #self.ai_dev_scaling_coeff.ai_dev_scaling_coeff self.ai_dev_scaling_coeff_vp = np.array(self.ai_dev_scaling_coeff_vp.ai_dev_scaling_coeff) if "Ip" in self.readin_channels: self.ai_dev_scaling_coeff_ip = nidaqmx._task_modules.channels.ai_channel.AIChannel(master_Task_readin._handle, self.channel_LUT['Ip']) #https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019TuoSAE&l=nl-NL #self.ai_dev_scaling_coeff.ai_dev_scaling_coeff self.ai_dev_scaling_coeff_ip = np.array(self.ai_dev_scaling_coeff_ip.ai_dev_scaling_coeff) self.ai_dev_scaling_coeff_list = np.append(self.ai_dev_scaling_coeff_vp, self.ai_dev_scaling_coeff_ip) #---------------------------------------------------------- #----------------------setting clock----------------------- if clock_source == "DAQ": # If NI-DAQ is set as master clock source # Analog clock USE clock on Dev1 as center clock slave_Task_1_analog_dev1.timing.cfg_samp_clk_timing(self.sampling_rate, source='ai/SampleClock', sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) master_Task_readin.timing.cfg_samp_clk_timing(self.sampling_rate, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) # Export the clock timing of Dev1 to specific port, use BNC cable to bridge this port # and clock receiving port on Dev2. master_Task_readin.export_signals.samp_clk_output_term = self.channel_LUT['clock1Channel']#'/Dev1/PFI1'# master_Task_readin.export_signals.start_trig_output_term = self.channel_LUT["trigger1Channel"]#'/Dev1/PFI2' # If dev2 is involved, set the timing for dev2. if Dev2_analog_channel_number != 0: # By default assume that read master task is in dev1 for i in range(Dev2_analog_channel_number): slave_Task_1_analog_dev2.ao_channels.add_ao_voltage_chan(self.Dev2_analog_channel_list[i]) # Set the clock of Dev2 to the receiving port from Dev1. dev2Clock = self.channel_LUT['clock2Channel']#/Dev2/PFI1 slave_Task_1_analog_dev2.timing.cfg_samp_clk_timing(self.sampling_rate, source=dev2Clock, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) AnalogWriter = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev1.out_stream, auto_start= False) AnalogWriter.auto_start = False AnalogWriter_dev2 = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev2.out_stream, auto_start= False) AnalogWriter_dev2.auto_start = False #----------------------Digital clock----------------------- if Digital_channel_number != 0: slave_Task_2_digitallines.timing.cfg_samp_clk_timing(self.sampling_rate, source='ai/SampleClock', sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) #slave_Task_2_digitallines.triggers.sync_type.SLAVE = True #---------------------------------------------------------- elif clock_source == "Camera":# All the clock should refer to camera output trigger # Set all clock source to camera trigger receiving port. self.cam_trigger_receiving_port = '/Dev1/PFI0' slave_Task_1_analog_dev1.timing.cfg_samp_clk_timing(self.sampling_rate, source=self.cam_trigger_receiving_port, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) slave_Task_1_analog_dev1.triggers.start_trigger.cfg_dig_edge_start_trig(self.cam_trigger_receiving_port) master_Task_readin.timing.cfg_samp_clk_timing(self.sampling_rate, source=self.cam_trigger_receiving_port, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) master_Task_readin.export_signals.samp_clk_output_term = self.channel_LUT['clock1Channel']#'/Dev1/PFI1'# master_Task_readin.export_signals.start_trig_output_term = self.channel_LUT['trigger1Channel']#'/Dev1/PFI2' master_Task_readin.triggers.start_trigger.cfg_dig_edge_start_trig(self.cam_trigger_receiving_port) if Dev2_analog_channel_number != 0: # By default assume that read master task is in dev1 for i in range(Dev2_analog_channel_number): slave_Task_1_analog_dev2.ao_channels.add_ao_voltage_chan(self.Dev2_analog_channel_list[i]) # Set clock of dev2 to the input from dev1 timing output. dev2Clock = self.channel_LUT['clock2Channel']#/Dev2/PFI1 slave_Task_1_analog_dev2.timing.cfg_samp_clk_timing(self.sampling_rate, source=dev2Clock, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) #slave_Task_1_analog_dev2.triggers.sync_type.SLAVE = True #slave_Task_1_analog_dev2.triggers.start_trigger.cfg_dig_edge_start_trig(self.channel_LUT["trigger2Channel"])#'/Dev2/PFI7' AnalogWriter = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev1.out_stream, auto_start= False) AnalogWriter.auto_start = False AnalogWriter_dev2 = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev2.out_stream, auto_start= False) AnalogWriter_dev2.auto_start = False #----------------------Digital clock----------------------- if Digital_channel_number != 0: slave_Task_2_digitallines.timing.cfg_samp_clk_timing(self.sampling_rate, source=self.cam_trigger_receiving_port, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) slave_Task_2_digitallines.triggers.start_trigger.cfg_dig_edge_start_trig(self.cam_trigger_receiving_port) #slave_Task_2_digitallines.triggers.sync_type.SLAVE = True #---------------------------------------------------------- #------------Configure the writer and reader--------------- AnalogWriter = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev1.out_stream, auto_start= False) AnalogWriter.auto_start = False if Digital_channel_number != 0: DigitalWriter = nidaqmx.stream_writers.DigitalMultiChannelWriter(slave_Task_2_digitallines.out_stream, auto_start= False) DigitalWriter.auto_start = False reader = AnalogMultiChannelReader(master_Task_readin.in_stream) reader.auto_start = False # --------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------Begin to execute in DAQ------------------------------------------ AnalogWriter.write_many_sample(Dev1_analog_samples_to_write, timeout = 605.0) if Dev2_analog_channel_number != 0: AnalogWriter_dev2.write_many_sample(Dev2_analog_samples_to_write, timeout = 605.0) if Digital_channel_number != 0: DigitalWriter.write_many_sample_port_uint32(Digital_samples_to_write, timeout = 605.0) print('^^^^^^^^^^^^^^^^^^Daq tasks start^^^^^^^^^^^^^^^^^^') if Dev2_analog_channel_number != 0: slave_Task_1_analog_dev2.start() slave_Task_1_analog_dev1.start() if Digital_channel_number != 0: slave_Task_2_digitallines.start() master_Task_readin.start() #!!!!!!!!!!!!!!!!!!!! READIN TASK HAS TO START AHEAD OF READ MANY SAMPLES, OTHERWISE ITS NOT SYN!!! reader.read_many_sample(data = self.Dataholder, number_of_samples_per_channel = self.Waveforms_length, timeout=605.0) #self.data_PMT = [] slave_Task_1_analog_dev1.wait_until_done() if Dev2_analog_channel_number != 0: slave_Task_1_analog_dev2.wait_until_done() if Digital_channel_number != 0: slave_Task_2_digitallines.wait_until_done() master_Task_readin.wait_until_done() slave_Task_1_analog_dev1.stop() if Dev2_analog_channel_number != 0: slave_Task_1_analog_dev2.stop() if Digital_channel_number != 0: slave_Task_2_digitallines.stop() master_Task_readin.stop() if self.has_recording_channel == True: self.collected_data.emit(self.Dataholder) self.finishSignal.emit() print('^^^^^^^^^^^^^^^^^^Daq tasks finish^^^^^^^^^^^^^^^^^^') # set the keys of galvos back for next round if Analog_channel_number != 0: for i in range(len(analog_signals['Sepcification'])): if 'galvosx' in analog_signals['Sepcification'][i]: analog_signals['Sepcification'][i] = self.galvosx_originalkey elif 'galvosy' in analog_signals['Sepcification'][i]: analog_signals['Sepcification'][i] = self.galvosy_originalkey """ # ============================================================================= # Only Dev 2 is involved in sending analog signals # ============================================================================= """ elif Dev2_analog_channel_number != 0: with nidaqmx.Task() as slave_Task_1_analog_dev2, nidaqmx.Task() as master_Task_readin, nidaqmx.Task() as slave_Task_2_digitallines: # adding channels # Set tasks from different devices apart slave_Task_2_digitallines.do_channels.add_do_chan("/Dev1/port0", line_grouping=LineGrouping.CHAN_FOR_ALL_LINES) if self.has_recording_channel == True: self.Dataholder = np.zeros((len(self.readin_channels), self.Waveforms_length)) else: self.Dataholder = np.zeros((1, self.Waveforms_length)) master_Task_readin.ai_channels.add_ai_voltage_chan(self.channel_LUT['Vp']) # If no read-in channel is added, vp channel is added to keep code alive. # print(self.Dataholder.shape) if 'PMT' in self.readin_channels: master_Task_readin.ai_channels.add_ai_voltage_chan(self.channel_LUT['PMT']) if 'Vp' in self.readin_channels: master_Task_readin.ai_channels.add_ai_voltage_chan(self.channel_LUT['Vp']) if 'Ip' in self.readin_channels: master_Task_readin.ai_channels.add_ai_current_chan(self.channel_LUT['Ip']) #get scaling coefficients self.aichannelnames=master_Task_readin.ai_channels.channel_names self.ai_dev_scaling_coeff_vp = [] self.ai_dev_scaling_coeff_ip = [] if "Vp" in self.readin_channels: self.ai_dev_scaling_coeff_vp = nidaqmx._task_modules.channels.ai_channel.AIChannel(master_Task_readin._handle, self.channel_LUT['Vp']) #self.ai_dev_scaling_coeff.ai_dev_scaling_coeff self.ai_dev_scaling_coeff_vp = np.array(self.ai_dev_scaling_coeff_vp.ai_dev_scaling_coeff) if "Ip" in self.readin_channels: self.ai_dev_scaling_coeff_ip = nidaqmx._task_modules.channels.ai_channel.AIChannel(master_Task_readin._handle, self.channel_LUT['Ip']) #self.ai_dev_scaling_coeff.ai_dev_scaling_coeff self.ai_dev_scaling_coeff_ip = np.array(self.ai_dev_scaling_coeff_ip.ai_dev_scaling_coeff) self.ai_dev_scaling_coeff_list = np.append(self.ai_dev_scaling_coeff_vp, self.ai_dev_scaling_coeff_ip) #---------------------------------------------------------- #----------------------setting clock----------------------- if clock_source == "DAQ": # If NI-DAQ is set as master clock source # setting clock master_Task_readin.timing.cfg_samp_clk_timing(self.sampling_rate, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) master_Task_readin.export_signals.samp_clk_output_term = self.channel_LUT['clock1Channel']#'/Dev1/PFI1'# master_Task_readin.export_signals.start_trig_output_term = self.channel_LUT['trigger1Channel']#'/Dev1/PFI2' if Dev2_analog_channel_number != 0: # By default assume that read master task is in dev1 for i in range(Dev2_analog_channel_number): slave_Task_1_analog_dev2.ao_channels.add_ao_voltage_chan(self.Dev2_analog_channel_list[i]) dev2Clock = self.channel_LUT['clock2Channel']#/Dev2/PFI1 slave_Task_1_analog_dev2.timing.cfg_samp_clk_timing(self.sampling_rate, source=dev2Clock, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) AnalogWriter_dev2 = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev2.out_stream, auto_start= False) AnalogWriter_dev2.auto_start = False # Digital clock if Digital_channel_number != 0: slave_Task_2_digitallines.timing.cfg_samp_clk_timing(self.sampling_rate, source='ai/SampleClock', sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) elif clock_source == "Camera": # All the clock should refer to camera output trigger self.cam_trigger_receiving_port = '/Dev1/PFI0' master_Task_readin.timing.cfg_samp_clk_timing(self.sampling_rate, source=self.cam_trigger_receiving_port, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) master_Task_readin.triggers.start_trigger.cfg_dig_edge_start_trig(self.cam_trigger_receiving_port) master_Task_readin.export_signals.samp_clk_output_term = self.channel_LUT['clock1Channel']#'/Dev1/PFI1'# master_Task_readin.export_signals.start_trig_output_term = self.channel_LUT['trigger1Channel']#'/Dev1/PFI2' if Dev2_analog_channel_number != 0: # By default assume that read master task is in dev1 for i in range(Dev2_analog_channel_number): slave_Task_1_analog_dev2.ao_channels.add_ao_voltage_chan(self.Dev2_analog_channel_list[i]) dev2Clock = self.channel_LUT['clock2Channel']#/Dev2/PFI1 slave_Task_1_analog_dev2.timing.cfg_samp_clk_timing(self.sampling_rate, source=dev2Clock, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) slave_Task_1_analog_dev2.triggers.start_trigger.cfg_dig_edge_start_trig(self.cam_trigger_receiving_port) AnalogWriter_dev2 = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev2.out_stream, auto_start= False) AnalogWriter_dev2.auto_start = False #--------------------Digital clock--------------------- if Digital_channel_number != 0: slave_Task_2_digitallines.timing.cfg_samp_clk_timing(self.sampling_rate, source=self.cam_trigger_receiving_port, sample_mode= AcquisitionType.FINITE, samps_per_chan=self.Waveforms_length) slave_Task_2_digitallines.triggers.start_trigger.cfg_dig_edge_start_trig(self.cam_trigger_receiving_port) #---------------------------------------------------------- # Configure the writer and reader if Digital_channel_number != 0: DigitalWriter = nidaqmx.stream_writers.DigitalMultiChannelWriter(slave_Task_2_digitallines.out_stream, auto_start= False) DigitalWriter.auto_start = False reader = AnalogMultiChannelReader(master_Task_readin.in_stream) reader.auto_start = False # --------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------Begin to execute in DAQ------------------------------------------ if Dev2_analog_channel_number != 0: AnalogWriter_dev2.write_many_sample(Dev2_analog_samples_to_write, timeout = 605.0) if Digital_channel_number != 0: DigitalWriter.write_many_sample_port_uint32(Digital_samples_to_write, timeout = 605.0) print('^^^^^^^^^^^^^^^^^^Daq tasks start^^^^^^^^^^^^^^^^^^') if Dev2_analog_channel_number != 0: slave_Task_1_analog_dev2.start() if Digital_channel_number != 0: slave_Task_2_digitallines.start() master_Task_readin.start() #!!!!!!!!!!!!!!!!!!!! READIN TASK HAS TO START AHEAD OF READ MANY SAMPLES, OTHERWISE ITS NOT SYN!!! reader.read_many_sample(data = self.Dataholder, number_of_samples_per_channel = self.Waveforms_length, timeout=605.0) #self.data_PMT = [] if Dev2_analog_channel_number != 0: slave_Task_1_analog_dev2.wait_until_done() if Digital_channel_number != 0: slave_Task_2_digitallines.wait_until_done() master_Task_readin.wait_until_done() if Dev2_analog_channel_number != 0: slave_Task_1_analog_dev2.stop() if Digital_channel_number != 0: slave_Task_2_digitallines.stop() master_Task_readin.stop() if self.has_recording_channel == True: self.collected_data.emit(self.Dataholder) self.finishSignal.emit() print('^^^^^^^^^^^^^^^^^^Daq tasks finish^^^^^^^^^^^^^^^^^^') # set the keys of galvos back for next round if Analog_channel_number != 0: for i in range(len(analog_signals['Sepcification'])): if 'galvosx' in analog_signals['Sepcification'][i]: analog_signals['Sepcification'][i] = self.galvosx_originalkey elif 'galvosy' in analog_signals['Sepcification'][i]: analog_signals['Sepcification'][i] = self.galvosy_originalkey """ # ============================================================================= # Only digital signals # ============================================================================= """ elif self.Only_Digital_signals == True: # some preparations for digital lines Waveforms_length = len(digital_signals['Waveform'][0]) digitalsignalslinenumber = len(digital_signals['Waveform']) # Assume that dev1 is always employed with nidaqmx.Task() as slave_Task_2_digitallines: # adding channels # Set tasks from different devices apart slave_Task_2_digitallines.do_channels.add_do_chan("/Dev1/port0", line_grouping=LineGrouping.CHAN_FOR_ALL_LINES) # Digital clock slave_Task_2_digitallines.timing.cfg_samp_clk_timing(self.sampling_rate, sample_mode= AcquisitionType.FINITE, samps_per_chan=Waveforms_length) # Configure the writer and reader DigitalWriter = nidaqmx.stream_writers.DigitalMultiChannelWriter(slave_Task_2_digitallines.out_stream, auto_start= False) DigitalWriter.auto_start = False # --------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------Begin to execute in DAQ------------------------------------------ print('^^^^^^^^^^^^^^^^^^Daq tasks start^^^^^^^^^^^^^^^^^^') DigitalWriter.write_many_sample_port_uint32(Digital_samples_to_write, timeout = 605.0) slave_Task_2_digitallines.start() slave_Task_2_digitallines.wait_until_done(timeout = 605.0) slave_Task_2_digitallines.stop() print('^^^^^^^^^^^^^^^^^^Daq tasks finish^^^^^^^^^^^^^^^^^^')
write_task.timing.cfg_samp_clk_timing(sample_rate, source=samp_clk_terminal, active_edge=Edge.RISING, samps_per_chan=number_of_samples) read_task.ai_channels.add_ai_voltage_chan(flatten_channel_string( [c.input_channel for c in channels_to_test]), max_val=10, min_val=-10) read_task.timing.cfg_samp_clk_timing(sample_rate, source=samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=number_of_samples) writer = AnalogMultiChannelWriter(write_task.out_stream) reader = AnalogMultiChannelReader(read_task.in_stream) values_to_test = np.array([waveform for _ in range(number_of_channels)], dtype=np.float64) writer.write_many_sample(values_to_test) # Start the read and write tasks before starting the sample clock # source task. read_task.start() write_task.start() sample_clk_task.start() values_read = np.zeros((number_of_channels, number_of_samples), dtype=np.float64) reader.read_many_sample(values_read,
def stim_and_rec(device, t_array, analog_inputs, analog_outputs, N_digital_inputs=0): dt = t_array[1] - t_array[0] sampling_rate = 1. / dt # if analog_outputs.shape[0]>0: output_analog_channels = get_analog_output_channels( device)[:analog_outputs.shape[0]] input_analog_channels = get_analog_input_channels( device)[:analog_inputs.shape[0]] if N_digital_inputs > 0: input_digital_channels = get_digital_input_channels( device)[:N_digital_inputs] with nidaqmx.Task() as write_analog_task, nidaqmx.Task() as read_analog_task,\ nidaqmx.Task() as read_digital_task, nidaqmx.Task() as sample_clk_task: # Use a counter output pulse train task as the sample clock source # for both the AI and AO tasks. sample_clk_task.co_channels.add_co_pulse_chan_freq('{0}/ctr0'.format( device.name), freq=sampling_rate) sample_clk_task.timing.cfg_implicit_timing(samps_per_chan=len(t_array)) samp_clk_terminal = '/{0}/Ctr0InternalOutput'.format(device.name) ### ---- OUTPUTS ---- ## write_analog_task.ao_channels.add_ao_voltage_chan( flatten_channel_string(output_analog_channels), max_val=10, min_val=-10) write_analog_task.timing.cfg_samp_clk_timing( sampling_rate, source=samp_clk_terminal, active_edge=Edge.RISING, samps_per_chan=len(t_array)) ### ---- INPUTS ---- ## read_analog_task.ai_channels.add_ai_voltage_chan( flatten_channel_string(input_analog_channels), max_val=10, min_val=-10) if N_digital_inputs > 0: read_digital_task.di_channels.add_di_chan( flatten_channel_string(input_digital_channels)) read_analog_task.timing.cfg_samp_clk_timing( sampling_rate, source=samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=len(t_array)) if N_digital_inputs > 0: read_digital_task.timing.cfg_samp_clk_timing( sampling_rate, source=samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=len(t_array)) analog_writer = AnalogMultiChannelWriter(write_analog_task.out_stream) analog_reader = AnalogMultiChannelReader(read_analog_task.in_stream) if N_digital_inputs > 0: digital_reader = DigitalMultiChannelReader( read_digital_task.in_stream) analog_writer.write_many_sample(analog_outputs) # Start the read and write tasks before starting the sample clock # source task. read_analog_task.start() if N_digital_inputs > 0: read_digital_task.start() write_analog_task.start() sample_clk_task.start() analog_reader.read_many_sample( analog_inputs, number_of_samples_per_channel=len(t_array), timeout=t_array[-1] + 2 * dt) if N_digital_inputs > 0: digital_inputs = np.zeros((1, len(t_array)), dtype=np.uint32) digital_reader.read_many_sample_port_uint32( digital_inputs, number_of_samples_per_channel=len(t_array), timeout=t_array[-1] + 2 * dt) else: digital_inputs = None return analog_inputs, digital_inputs
def load(self, stim, digstim=None, use_ao_trigger=False): # check if the modes are up to date assert type(stim) is list, 'Stim needs to be a list of numpy arrays' self._check_modes() self.nsamples = int( np.max([np.max(s.shape) for s in stim if not s is None])) aoconversion = self._get_conversion(False) if not self.task_ai is None and self.task_ao is None: self.task_ai.timing.cfg_samp_clk_timing( rate=self.srate, samps_per_chan=self.nsamples) if not self.task_ao is None: self.task_ao.timing.cfg_samp_clk_timing( rate=self.srate, samps_per_chan=self.nsamples) self.task_ao.export_signals.export_signal( nidaqmx.constants.Signal.SAMPLE_CLOCK, 'PFI0') if not self.task_ai is None: if use_ao_trigger: self.task_ai.triggers.start_trigger.cfg_dig_edge_start_trig( 'ao/StartTrigger') self.task_ai.timing.cfg_samp_clk_timing( rate=self.srate, source='PFI0', samps_per_chan=self.nsamples) # Take care of the conversions stims = np.zeros((self.n_output_chan, self.nsamples), dtype=np.float64) # Fix the analog output conversions for i in range(self.n_output_chan): if i < len(stim): if stim[i] is None: continue if not len(stim[i]): continue stims[i] = stim[i] * aoconversion[i] if hasattr(self, 'task_clock'): self.task_clock.timing.cfg_implicit_timing( samps_per_chan=self.nsamples) if not self.task_ao is None: self.task_ao.timing.cfg_samp_clk_timing( self.srate, source=self.samp_clk_terminal, active_edge=nidaqmx.constants.Edge.RISING, samps_per_chan=self.nsamples) if not self.task_ai is None: self.task_ai.timing.cfg_samp_clk_timing( self.srate, source=self.samp_clk_terminal, active_edge=nidaqmx.constants.Edge.FALLING, samps_per_chan=self.nsamples) self.writer = AnalogMultiChannelWriter(self.task_ao.out_stream) self.reader = AnalogMultiChannelReader(self.task_ai.in_stream) self.writer.write_many_sample(stims) self.run_do = False if not digstim is None: if not self.task_do is None: self.task_do.timing.cfg_samp_clk_timing( rate=self.srate, source=self.samp_clk_terminal, active_edge=nidaqmx.constants.Edge.RISING, samps_per_chan=self.nsamples) #this works for 32 bit ports only, all iputs must be on the same port for now. digstims = np.zeros((self.nsamples, 32), dtype=np.uint32) for i in range(len(digstim)): if digstim[i] is None: continue if not len(digstim[i]): continue digstims[:, i] = (digstim[i] > 0) self.di_writer = DigitalMultiChannelWriter( self.task_do.out_stream) self.di_writer.write_many_sample_port_uint32( np.packbits(digstims, bitorder='little').reshape( (1, -1)).view(np.uint32)) self.run_do = True self.acquired = False
def test_many_sample(self, x_series_device, seed): # Reset the pseudorandom number generator with seed. random.seed(seed) number_of_samples = random.randint(20, 100) sample_rate = random.uniform(1000, 5000) # Select a random loopback channel pair on the device. loopback_channel_pairs = self._get_analog_loopback_channels( x_series_device) number_of_channels = random.randint(2, len(loopback_channel_pairs)) channels_to_test = random.sample( loopback_channel_pairs, number_of_channels) with nidaqmx.Task() as write_task, nidaqmx.Task() as read_task, \ nidaqmx.Task() as sample_clk_task: # Use a counter output pulse train task as the sample clock source # for both the AI and AO tasks. sample_clk_task.co_channels.add_co_pulse_chan_freq( '{0}/ctr0'.format(x_series_device.name), freq=sample_rate) sample_clk_task.timing.cfg_implicit_timing( samps_per_chan=number_of_samples) samp_clk_terminal = '/{0}/Ctr0InternalOutput'.format( x_series_device.name) write_task.ao_channels.add_ao_voltage_chan( flatten_channel_string( [c.output_channel for c in channels_to_test]), max_val=10, min_val=-10) write_task.timing.cfg_samp_clk_timing( sample_rate, source=samp_clk_terminal, active_edge=Edge.RISING, samps_per_chan=number_of_samples) read_task.ai_channels.add_ai_voltage_chan( flatten_channel_string( [c.input_channel for c in channels_to_test]), max_val=10, min_val=-10) read_task.timing.cfg_samp_clk_timing( sample_rate, source=samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=number_of_samples) writer = AnalogMultiChannelWriter(write_task.out_stream) reader = AnalogMultiChannelReader(read_task.in_stream) values_to_test = numpy.array( [[random.uniform(-10, 10) for _ in range(number_of_samples)] for _ in range(number_of_channels)], dtype=numpy.float64) writer.write_many_sample(values_to_test) # Start the read and write tasks before starting the sample clock # source task. read_task.start() write_task.start() sample_clk_task.start() values_read = numpy.zeros( (number_of_channels, number_of_samples), dtype=numpy.float64) reader.read_many_sample( values_read, number_of_samples_per_channel=number_of_samples, timeout=2) numpy.testing.assert_allclose( values_read, values_to_test, rtol=0.05, atol=0.005)
def launch(self): if self.outputs is not None: self.write_task = nidaqmx.Task() if self.Nchannel_analog_in > 0: self.read_analog_task = nidaqmx.Task() if self.Nchannel_digital_in > 0: self.read_digital_task = nidaqmx.Task() self.sample_clk_task = nidaqmx.Task() # Use a counter output pulse train task as the sample clock source # for both the AI and AO tasks. self.sample_clk_task.co_channels.add_co_pulse_chan_freq( '{0}/ctr0'.format(self.device.name), freq=self.sampling_rate) self.sample_clk_task.timing.cfg_implicit_timing( samps_per_chan=int(self.max_time / self.dt)) self.samp_clk_terminal = '/{0}/Ctr0InternalOutput'.format( self.device.name) ### ---- OUTPUTS ---- ## if self.outputs is not None: self.write_task.ao_channels.add_ao_voltage_chan( flatten_channel_string(self.output_channels), max_val=10, min_val=-10) self.write_task.timing.cfg_samp_clk_timing( self.sampling_rate, source=self.samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=int(self.max_time / self.dt)) ### ---- INPUTS ---- ## if self.Nchannel_analog_in > 0: self.read_analog_task.ai_channels.add_ai_voltage_chan( flatten_channel_string(self.analog_input_channels), max_val=10, min_val=-10) if self.Nchannel_digital_in > 0: self.read_digital_task.di_channels.add_di_chan( flatten_channel_string(self.digital_input_channels)) if self.Nchannel_analog_in > 0: self.read_analog_task.timing.cfg_samp_clk_timing( self.sampling_rate, source=self.samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=int(self.max_time / self.dt)) if self.Nchannel_digital_in > 0: self.read_digital_task.timing.cfg_samp_clk_timing( self.sampling_rate, source=self.samp_clk_terminal, active_edge=Edge.FALLING, samps_per_chan=int(self.max_time / self.dt)) if self.Nchannel_analog_in > 0: self.analog_reader = AnalogMultiChannelReader( self.read_analog_task.in_stream) self.read_analog_task.register_every_n_samples_acquired_into_buffer_event( self.buffer_size, self.reading_task_callback) if self.Nchannel_digital_in > 0: self.digital_reader = DigitalMultiChannelReader( self.read_digital_task.in_stream) self.read_digital_task.register_every_n_samples_acquired_into_buffer_event( self.buffer_size, self.reading_task_callback) if self.outputs is not None: self.writer = AnalogMultiChannelWriter(self.write_task.out_stream) self.writer.write_many_sample(self.outputs) # Start the read task before starting the sample clock source task. if self.Nchannel_analog_in > 0: self.read_analog_task.start() if self.Nchannel_digital_in > 0: self.read_digital_task.start() if self.outputs is not None: self.write_task.start() self.sample_clk_task.start() if self.filename is not None: np.save(self.filename.replace('.npy', '.start.npy'), np.ones(1) * time.time()) # saving the time stamp of the start ! self.running, self.data_saved = True, False
def __init__(self, samplingrate, analogsignals, digitalsignals, readinchannels): configs = Configuration() configdictionary = {'galvosx':configs.galvoXChannel, 'galvosy':'Dev1/ao1',#configs.galvoYChannel, '640AO':'Dev1/ao3', '488AO':'Dev2/ao1', '532AO':'Dev2/ao0', 'patchAO':configs.patchVoltInChannel, 'cameratrigger':"Dev1/port0/line25", 'galvotrigger':"Dev1/port0/line25", 'blankingall':"Dev1/port0/line4", '640blanking':"Dev1/port0/line4", '532blanking':"Dev1/port0/line6", '488blanking':"Dev1/port0/line3", 'PMT':"Dev1/ai0", 'Vp':"Dev1/ai22", 'Ip':"Dev1/ai20", 'Perfusion_1':"Dev1/port0/line20" } Daq_sample_rate = samplingrate # some preparations for analog lines Totalscansamplesnumber = len(analogsignals['Waveform'][0]) num_rows, num_cols = analogsignals['Waveform'].shape print("row number of analog signals: "+str(num_rows)) # Get the average number and y pixel number information from data self.averagenumber = 0 self.ypixelnumber = 0 for i in range(len(analogsignals['Sepcification'])): if 'galvosx' in analogsignals['Sepcification'][i]: self.averagenumber = int(analogsignals['Sepcification'][i][analogsignals['Sepcification'][i].index('_')+1:len(analogsignals['Sepcification'][i])]) self.galvosx_originalkey = analogsignals['Sepcification'][i] analogsignals['Sepcification'][i] = 'galvosx' elif 'galvosy' in analogsignals['Sepcification'][i]: self.ypixelnumber = int(analogsignals['Sepcification'][i][analogsignals['Sepcification'][i].index('_')+1:len(analogsignals['Sepcification'][i])]) self.galvosy_originalkey = analogsignals['Sepcification'][i] analogsignals['Sepcification'][i] = 'galvosy' # Devide samples from Dev1 or 2 analogwritesamplesdev1_Sepcification = [] analogwritesamplesdev2_Sepcification = [] analogwritesamplesdev1 = [] analogwritesamplesdev2 = [] for i in range(int(num_rows)): if 'Dev1' in configdictionary[analogsignals['Sepcification'][i]]: analogwritesamplesdev1_Sepcification.append(configdictionary[analogsignals['Sepcification'][i]]) analogwritesamplesdev1.append(analogsignals['Waveform'][i]) else: analogwritesamplesdev2_Sepcification.append(configdictionary[analogsignals['Sepcification'][i]]) analogwritesamplesdev2.append(analogsignals['Waveform'][i]) analogsignal_dev1_number = len(analogwritesamplesdev1_Sepcification) analogsignal_dev2_number = len(analogwritesamplesdev2_Sepcification) analogsignalslinenumber = len(analogsignals['Waveform']) digitalsignalslinenumber = len(digitalsignals['Waveform']) # Stack the Analog samples of dev1 and dev2 individually # IN CASE OF ONLY ONE ARRAY, WE NEED TO CONVERT THE SHAPE TO (1,N) BY USING np.array([]) OUTSIDE THE ARRAY!! #------------------------------------------!!_________________________ if analogsignal_dev1_number == 1: writesamples_dev1 = np.array([analogwritesamplesdev1[0]]) elif analogsignal_dev1_number == 0: writesamples_dev1 = [] else: writesamples_dev1 = analogwritesamplesdev1[0] for i in range(1, analogsignal_dev1_number): writesamples_dev1 = np.vstack((writesamples_dev1, analogwritesamplesdev1[i])) if analogsignal_dev2_number == 1: writesamples_dev2 = np.array([analogwritesamplesdev2[0]]) elif analogsignal_dev2_number == 0: writesamples_dev2 = [] else: writesamples_dev2 = analogwritesamplesdev2[0] for i in range(1, analogsignal_dev2_number): writesamples_dev2 = np.vstack((writesamples_dev2, analogwritesamplesdev2[i])) # Stack the digital samples if digitalsignalslinenumber == 1: holder2 = np.array([digitalsignals['Waveform'][0]]) elif digitalsignalslinenumber == 0: holder2 = [] else: holder2 = digitalsignals['Waveform'][0] for i in range(1, digitalsignalslinenumber): holder2 = np.vstack((holder2, digitalsignals['Waveform'][i])) # Set the dtype of digital signals # The same as (0b1 << n) holder2 = np.array(holder2, dtype = 'uint32') for i in range(digitalsignalslinenumber): convernum = int(configdictionary[digitalsignals['Sepcification'][i]][configdictionary[digitalsignals['Sepcification'][i]].index('line')+4:len(configdictionary[digitalsignals['Sepcification'][i]])]) print(convernum) holder2[i] = holder2[i]*(2**(convernum)) # For example, to send commands to line 0 and line 3, you hava to write 1001 to digital port, convert to uint32 that is 9. if digitalsignalslinenumber > 1: holder2 = np.sum(holder2, axis = 0) # sum along the columns, for multiple lines holder2 = np.array([holder2]) # here convert the shape from (n,) to (1,n) # Assume that dev1 is always employed with nidaqmx.Task() as slave_Task_1_analog_dev1, nidaqmx.Task() as slave_Task_1_analog_dev2, nidaqmx.Task() as master_Task_readin, nidaqmx.Task() as slave_Task_2_digitallines: # adding channels # Set tasks from different devices apart for i in range(analogsignal_dev1_number): slave_Task_1_analog_dev1.ao_channels.add_ao_voltage_chan(analogwritesamplesdev1_Sepcification[i]) #if len(digitalsignals['Sepcification']) != 0: #for i in range(len(digitalsignals['Sepcification'])): #slave_Task_2_digitallines.do_channels.add_do_chan(configdictionary[digitalsignals['Sepcification'][i]], line_grouping=LineGrouping.CHAN_PER_LINE)#line_grouping??????????????One Channel For Each Line slave_Task_2_digitallines.do_channels.add_do_chan("/Dev1/port0", line_grouping=LineGrouping.CHAN_FOR_ALL_LINES) Dataholder = np.zeros((len(readinchannels), Totalscansamplesnumber)) if 'PMT' in readinchannels: master_Task_readin.ai_channels.add_ai_voltage_chan(configdictionary['PMT']) if 'Vp' in readinchannels: master_Task_readin.ai_channels.add_ai_voltage_chan(configdictionary['Vp']) if 'Ip' in readinchannels: master_Task_readin.ai_channels.add_ai_current_chan(configdictionary['Ip']) # setting clock # Analog clock USE clock on Dev1 as center clock slave_Task_1_analog_dev1.timing.cfg_samp_clk_timing(Daq_sample_rate, source='ai/SampleClock', sample_mode= AcquisitionType.FINITE, samps_per_chan=Totalscansamplesnumber) #slave_Task_1_analog_dev1.triggers.sync_type.SLAVE = True # Readin clock as master clock master_Task_readin.timing.cfg_samp_clk_timing(Daq_sample_rate, sample_mode= AcquisitionType.FINITE, samps_per_chan=Totalscansamplesnumber) #master_Task_readin.triggers.sync_type.MASTER = True #master_Task_readin.export_signals(Signal.SAMPLE_CLOCK, '/Dev1/PFI1') #master_Task_readin.export_signals(Signal.START_TRIGGER, '') master_Task_readin.export_signals.samp_clk_output_term = configs.clock1Channel#'/Dev1/PFI1'# master_Task_readin.export_signals.start_trig_output_term = configs.trigger1Channel#'/Dev1/PFI2' #slave_Task_1_analog_dev1.samp_clk_output_term #slave_Task_1_analog_dev1.samp_trig_output_term if analogsignal_dev2_number != 0: # By default assume that read master task is in dev1 for i in range(analogsignal_dev2_number): slave_Task_1_analog_dev2.ao_channels.add_ao_voltage_chan(analogwritesamplesdev2_Sepcification[i]) dev2Clock = configs.clock2Channel#/Dev2/PFI1 slave_Task_1_analog_dev2.timing.cfg_samp_clk_timing(Daq_sample_rate, source=dev2Clock, sample_mode= AcquisitionType.FINITE, samps_per_chan=Totalscansamplesnumber) #slave_Task_1_analog_dev2.triggers.sync_type.SLAVE = True #slave_Task_1_analog_dev2.triggers.start_trigger.cfg_dig_edge_start_trig(configs.trigger2Channel)#'/Dev2/PFI7' AnalogWriter = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev1.out_stream, auto_start= False) AnalogWriter.auto_start = False AnalogWriter_dev2 = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev2.out_stream, auto_start= False) AnalogWriter_dev2.auto_start = False # Digital clock if len(digitalsignals['Sepcification']) != 0: # or the source of sample clock could be PFI? or using start trigger: cfg_dig_edge_start_trig slave_task.triggers.start_trigger.cfg_dig_edge_start_trig("/PXI1Slot3/ai/StartTrigger") slave_Task_2_digitallines.timing.cfg_samp_clk_timing(Daq_sample_rate, source='ai/SampleClock', sample_mode= AcquisitionType.FINITE, samps_per_chan=Totalscansamplesnumber) #slave_Task_2_digitallines.triggers.sync_type.SLAVE = True # Configure the writer and reader AnalogWriter = nidaqmx.stream_writers.AnalogMultiChannelWriter(slave_Task_1_analog_dev1.out_stream, auto_start= False) AnalogWriter.auto_start = False if len(digitalsignals['Sepcification']) != 0: DigitalWriter = nidaqmx.stream_writers.DigitalMultiChannelWriter(slave_Task_2_digitallines.out_stream, auto_start= False) DigitalWriter.auto_start = False reader = AnalogMultiChannelReader(master_Task_readin.in_stream) # --------------------------------------------------------------------------------------------------------------------- #-----------------------------------------------------Begin to execute in DAQ------------------------------------------ AnalogWriter.write_many_sample(writesamples_dev1, timeout = 16.0) if analogsignal_dev2_number != 0: AnalogWriter_dev2.write_many_sample(writesamples_dev2, timeout = 16.0) if digitalsignalslinenumber != 0: DigitalWriter.write_many_sample_port_uint32(holder2, timeout = 16.0) if analogsignal_dev2_number != 0: slave_Task_1_analog_dev2.start() slave_Task_1_analog_dev1.start() if digitalsignalslinenumber != 0: slave_Task_2_digitallines.start() master_Task_readin.start() reader.read_many_sample(Dataholder, number_of_samples_per_channel = Totalscansamplesnumber, timeout=16.0) self.data_PMT = [] slave_Task_1_analog_dev1.wait_until_done() if analogsignal_dev2_number != 0: slave_Task_1_analog_dev2.wait_until_done() if digitalsignalslinenumber != 0: slave_Task_2_digitallines.wait_until_done() master_Task_readin.wait_until_done() if 'PMT' in readinchannels: Dataholder_average = np.mean(Dataholder[0,:].reshape(self.averagenumber, -1), axis=0) ScanArrayXnum = int ((Totalscansamplesnumber/self.averagenumber)/self.ypixelnumber) self.data_PMT = np.reshape(Dataholder_average, (self.ypixelnumber, ScanArrayXnum)) self.data_PMT= self.data_PMT*-1 ''' plt.figure() plt.imshow(self.data_PMT, cmap = plt.cm.gray) plt.show() ''' slave_Task_1_analog_dev1.stop() if analogsignal_dev2_number != 0: slave_Task_1_analog_dev2.stop() if digitalsignalslinenumber != 0: slave_Task_2_digitallines.stop() master_Task_readin.stop() ''' slave_Task_1_analog_dev1.close() if analogsignal_dev2_number != 0: slave_Task_1_analog_dev2.close() if digitalsignalslinenumber != 0: slave_Task_2_digitallines.close() master_Task_readin.close() ''' # set the keys of galvos back for next round for i in range(len(analogsignals['Sepcification'])): if 'galvosx' in analogsignals['Sepcification'][i]: analogsignals['Sepcification'][i] = self.galvosx_originalkey elif 'galvosy' in analogsignals['Sepcification'][i]: analogsignals['Sepcification'][i] = self.galvosy_originalkey
def run(self): self.digital_out_data = np.zeros_like(self.tout, dtype=np.uint32) self.digital_out_data = set_bit(self.digital_out_data, self.params['DAQ', 'Output', 'Inhibit line'], self.inhibit) self.digital_out_data = set_bit(self.digital_out_data, self.params['DAQ', 'Output', 'Enable line'], self.enable) self.digital_out_data = set_bit(self.digital_out_data, self.params['DAQ', 'Output', 'LED line'], self.led) if self.params['DAQ', 'Reference trigger'].lower() == 'none': trig = None pretrigdur = 0 else: trig = self.params['DAQ', 'Reference trigger'].lower() pretrigdur = self.params['DAQ', 'Pretrigger duration'] try: with Task() as counter_out, \ Task() as digital_out, \ Task() as analog_in, \ Task() as digital_in: # digital output digital_out.do_channels.add_do_chan(self.params['DAQ', 'Output', 'Digital port'], line_grouping=daq.LineGrouping.CHAN_FOR_ALL_LINES) digital_out.timing.cfg_samp_clk_timing(self.params['Motor', 'Pulse frequency'], sample_mode=daq.AcquisitionType.FINITE, samps_per_chan=len(self.digital_out_data)) if trig is None: digital_out.triggers.start_trigger.cfg_dig_edge_start_trig('ai/StartTrigger', trigger_edge=daq.Edge.RISING) else: digital_out.triggers.start_trigger.cfg_dig_edge_start_trig(trig, trigger_edge=daq.Edge.RISING) # order is inhibit then enable # digital_out.write([False, True], auto_start=True) digital_writer = DigitalSingleChannelWriter(digital_out.out_stream) digital_writer.write_many_sample_port_uint32(self.digital_out_data) totaldur = self.duration + pretrigdur # analog input n_in_samples = int(totaldur * self.params['DAQ', 'Input', 'Sampling frequency']) n_in_pre_samples = int(pretrigdur * self.params['DAQ', 'Input', 'Sampling frequency']) aichans = [self.params['DAQ','Input', c] for c in ['SG0', 'SG1', 'SG2', 'SG3', 'SG4', 'SG5']] for aichan1 in aichans: analog_in.ai_channels.add_ai_voltage_chan(aichan1) analog_in.timing.cfg_samp_clk_timing(self.params['DAQ', 'Input', 'Sampling frequency'], sample_mode=daq.AcquisitionType.FINITE, samps_per_chan=n_in_samples) if trig is not None: analog_in.triggers.reference_trigger.cfg_dig_edge_ref_trig(self.params['DAQ', 'Reference trigger'], n_in_pre_samples, trigger_edge=daq.Edge.RISING) # analog_in.triggers.start_trigger.cfg_dig_edge_start_trig(self.params['DAQ', 'Start trigger'], # trigger_edge=daq.Edge.RISING) reader = AnalogMultiChannelReader(analog_in.in_stream) self.aidata = np.zeros((6, n_in_samples), dtype=np.float64) # digital input n_in_dig_samples = int(totaldur * self.params['DAQ', 'Input', 'Digital sampling frequency']) n_in_dig_pre_samples = int(pretrigdur * self.params['DAQ', 'Input', 'Digital sampling frequency']) digital_in.di_channels.add_di_chan(self.params['DAQ', 'Input', 'Digital input port'], '', line_grouping=daq.LineGrouping.CHAN_FOR_ALL_LINES) digital_in.timing.cfg_samp_clk_timing(self.params['DAQ', 'Input', 'Digital sampling frequency'], sample_mode=daq.AcquisitionType.FINITE, samps_per_chan=n_in_dig_samples) if trig is not None: digital_in.triggers.reference_trigger.cfg_dig_edge_ref_trig(self.params['DAQ', 'Reference trigger'], n_in_dig_pre_samples, trigger_edge=daq.Edge.RISING) else: digital_in.triggers.start_trigger.cfg_dig_edge_start_trig('ai/StartTrigger', trigger_edge=daq.Edge.RISING) # digital_in.triggers.start_trigger.cfg_dig_edge_start_trig(self.params['DAQ', 'Start trigger'], # trigger_edge=daq.Edge.RISING) digital_reader = DigitalSingleChannelReader(digital_in.in_stream) self.didata = np.zeros(n_in_dig_samples, dtype=np.uint32) # counter output if self.params['Movement', 'Position amplitude'] != 0: counter_out.co_channels.add_co_pulse_chan_freq(self.params['DAQ', 'Output', 'Counter name'], units=daq.FrequencyUnits.HZ, idle_state=daq.Level.LOW, initial_delay=0.0, freq=self.params['Motor', 'Pulse frequency'], duty_cycle=0.5) counter_out.timing.cfg_implicit_timing(sample_mode=daq.AcquisitionType.FINITE, samps_per_chan=len(self.duty)) if trig is not None: counter_out.triggers.start_trigger.cfg_dig_edge_start_trig(trig, trigger_edge=daq.Edge.RISING) else: counter_out.triggers.start_trigger.cfg_dig_edge_start_trig('ai/StartTrigger', trigger_edge=daq.Edge.RISING) counter_writer = CounterWriter(counter_out.out_stream) counter_writer.write_many_sample_pulse_frequency(self.freq, self.duty) iscounter = True else: iscounter = False digital_out.start() if iscounter: counter_out.start() digital_in.start() analog_in.start() analog_in.wait_until_done(60) self.endTime = datetime.now() reader.read_many_sample(self.aidata) digital_reader.read_many_sample_port_uint32(self.didata) except DaqError as daqerr: QtWidgets.QMessageBox.critical(None, 'Error', str(daqerr)) finally: pass # digital_out.write([True, False], auto_start=True) self.tin = np.arange(0, n_in_samples) / self.params['DAQ', 'Input', 'Sampling frequency'] self.tin -= self.params['Movement', 'Wait before and after'] + pretrigdur self.tdig = np.arange(0, n_in_dig_samples) / self.params['DAQ', 'Input', 'Digital sampling frequency'] self.tdig -= self.params['Movement', 'Wait before and after'] + pretrigdur self.forces = np.dot(self.aidata.T, self.calibration).T self.pwm = np.bitwise_and(self.didata, 2**self.params['DAQ', 'Input', 'PWM return line']) > 0 self.V3Vpulse = np.bitwise_and(self.didata, 2**self.params['DAQ', 'Input', 'V3V pulse line']) > 0 self.V3Vpulse2 = np.bitwise_and(self.didata, 2**self.params['DAQ', 'Input', 'V3V pulse2']) > 0 self.V3Vpulse3 = np.bitwise_and(self.didata, 2**self.params['DAQ', 'Input', 'V3V pulse3']) > 0
receive_address = ('localhost', 6666) trackingcoords = OSC.OSCServer(receive_address) #bonsai tracking variables qnosex = Queue.LifoQueue(0) qnosey = Queue.LifoQueue(0) #online position storage nosex = np.zeros((1, 1)) nosey = np.zeros((1, 1)) headx = np.zeros((1, 1)) heady = np.zeros((1, 1)) comx = np.zeros((1, 1)) comy = np.zeros((1, 1)) ts = np.zeros((1, 1)) signaldata = np.zeros((channel_num, buffersize), dtype=np.float64) #NI data collection reading variables reader = AnalogMultiChannelReader(ni_data.in_stream) ##START UP PROCEDURES section, section_center = fmon_tracking.calc_partitions( ) #online tracking: gridline deliniation fmon_serial.close_all_valves() #turn off all hardware #Session Summary #Create/Open Data Files ch0_handle = open(ch0_file, 'ab') ch1_handle = open(ch1_file, 'ab') ch2_handle = open(ch2_file, 'ab') ch3_handle = open(ch3_file, 'ab') nx_handle = open(nx_file, 'ab') ny_handle = open(ny_file, 'ab')