def AI_start_delay(device_name): if 'PFI0' not in DAQmxGetDevTerminals(device_name): return None task = Task() clock_terminal = '/' + device_name + '/PFI0' rate = DAQmxGetDevAIMaxSingleChanRate(device_name) Vmin, Vmax = DAQmxGetDevAIVoltageRngs(device_name)[0:2] num_samples = 1000 chan = device_name + '/ai0' task.CreateAIVoltageChan(chan, "", c.DAQmx_Val_RSE, Vmin, Vmax, c.DAQmx_Val_Volts, None) task.CfgSampClkTiming("", rate, c.DAQmx_Val_Rising, c.DAQmx_Val_ContSamps, num_samples) task.CfgDigEdgeStartTrig(clock_terminal, c.DAQmx_Val_Rising) start_trig_delay = float64() delay_from_sample_clock = float64() sample_timebase_rate = float64() task.GetStartTrigDelay(start_trig_delay) task.GetDelayFromSampClkDelay(delay_from_sample_clock) task.GetSampClkTimebaseRate(sample_timebase_rate) task.ClearTask() total_delay_in_ticks = start_trig_delay.value + delay_from_sample_clock.value total_delay_in_seconds = total_delay_in_ticks / sample_timebase_rate.value return total_delay_in_seconds
def AI_start_delay(device_name): """Empirically determines the analog inputs' start delay. Args: device_name (str): NI-MAX device name Returns: float: Analog input start delay in seconds. `None` if analog inputs not supported. """ if 'PFI0' not in DAQmxGetDevTerminals(device_name): return None task = Task() clock_terminal = '/' + device_name + '/PFI0' rate = DAQmxGetDevAIMaxSingleChanRate(device_name) Vmin, Vmax = DAQmxGetDevAIVoltageRngs(device_name)[0:2] num_samples = 1000 chan = device_name + '/ai0' supp_types = DAQmxGetPhysicalChanAITermCfgs(chan) if supp_types & c.DAQmx_Val_Bit_TermCfg_RSE: input_type = c.DAQmx_Val_RSE elif supp_types & c.DAQmx_Val_Bit_TermCfg_Diff: input_type = c.DAQmx_Val_Diff elif supp_types & c.DAQmx_Val_Bit_TermCfg_PseudoDIFF: input_type = c.DAQmx_Val_PseudoDiff task.CreateAIVoltageChan( chan, "", input_type, Vmin, Vmax, c.DAQmx_Val_Volts, None ) task.CfgSampClkTiming( "", rate, c.DAQmx_Val_Rising, c.DAQmx_Val_ContSamps, num_samples ) task.CfgDigEdgeStartTrig(clock_terminal, c.DAQmx_Val_Rising) start_trig_delay = float64() delay_from_sample_clock = float64() sample_timebase_rate = float64() try: task.GetStartTrigDelay(start_trig_delay) except PyDAQmx.DAQmxFunctions.AttributeNotSupportedInTaskContextError: # device does not have a Start Trigger Delay property # is likely a dynamic signal acquisition device with filter # delays instead. start_trig_delay.value = 0 try: task.GetDelayFromSampClkDelay(delay_from_sample_clock) except PyDAQmx.DAQmxFunctions.AttributeNotSupportedInTaskContextError: # seems simultaneous sampling devices do not have this property, # so assume it is zero delay_from_sample_clock.value = 0 task.GetSampClkTimebaseRate(sample_timebase_rate) task.ClearTask() total_delay_in_ticks = start_trig_delay.value + delay_from_sample_clock.value total_delay_in_seconds = total_delay_in_ticks / sample_timebase_rate.value return total_delay_in_seconds
def write_do(taskhandle, indata, intimeout=0): ''' Write given data to given task in taskhandle = Task handle indata = Samples to write as list of integers intimeout = Timeout for writing all samples, default 0 = try once out ErrorText = Error text, None if no errors ''' errortext = None #logger.info("Indata: {}".format(indata)) if isinstance(indata, int): samplecount = 1 datatype = uInt8 data = datatype(indata) else: samplecount = len(indata) datatype = uInt8 * samplecount data = datatype(*indata) result = int32() samples = int32(samplecount) autostart = bool32(False) timeout = float64(intimeout) try: taskhandle.WriteDigitalU8(samples, autostart, timeout, DAQmx_Val_GroupByChannel, data, byref(result), None) except DAQError as err: errortext = err return errortext
def write_analog_io_trigger(taskhandle, value, intimeout=100): ''' Write data from given task in Task handle Timeout for writing, default 100 out write data ''' samples = 2 datatype = float64 * samples data = datatype() for i in range(samples): data[i] = float(value) #logger.info("testlog, write analog io trigger -funtion for loop 1: {}".format(value)) for i in data: logger.info( "testlog, write analog io trigger -funtion, for loop 2: {}".format( str(i))) timeout = float64(intimeout) #logger.info("write analog io trigger -funtion, test1: {}".format(data)) taskhandle.WriteAnalogF64(2, False, timeout, DAQmx_Val_GroupByChannel, data, int32(), None) taskhandle.StartTask() taskhandle.WaitUntilTaskDone(-1) return data
def AI_filter_delay(device_name): """Determine the filter delay for dynamic signal acquistion devices. Returns the delay in clock cycles. Absolute delay will vary with sample rate. Args: device_name (str): NI-MAX device name Returns: int: Number of analog input delays ticks between task start and acquisition start. """ if 'PFI0' not in DAQmxGetDevTerminals(device_name): return None task = Task() clock_terminal = '/' + device_name + '/PFI0' rate = DAQmxGetDevAIMaxSingleChanRate(device_name) Vmin, Vmax = DAQmxGetDevAIVoltageRngs(device_name)[0:2] num_samples = 1000 chan = device_name + '/ai0' task.CreateAIVoltageChan( chan, "", c.DAQmx_Val_PseudoDiff, Vmin, Vmax, c.DAQmx_Val_Volts, None ) task.CfgSampClkTiming( "", rate, c.DAQmx_Val_Rising, c.DAQmx_Val_ContSamps, num_samples ) task.CfgDigEdgeStartTrig(clock_terminal, c.DAQmx_Val_Rising) start_filter_delay = float64() sample_timebase_rate = float64() # get delay in number of clock samples task.SetAIFilterDelayUnits("", c.DAQmx_Val_SampleClkPeriods) task.GetAIFilterDelay("", start_filter_delay) task.GetSampClkTimebaseRate(sample_timebase_rate) task.ClearTask() return int(start_filter_delay.value)
def wrapped(name): import warnings with warnings.catch_warnings(): # PyDAQmx warns about a positive return value, but actually this is how you # are supposed to figure out the size of the array required. warnings.simplefilter("ignore") # Pass in null pointer and 0 len to ask for what array size is needed: npts = func(name, byref(float64()), 0) # Create that array result = (float64 * npts)() func(name, result, npts) result = [result[i] for i in range(npts)] return result
def connect_analog_io_port_RSE(devport, samples, rate): ''' Initialize task for reading from analog input port for Voltage measurement in devport = Device/port e.g. Dev1/ai2 out task = Task handle ''' logger.info("temp: {}".format(devport)) max_num_samples = samples task = Task() task.CreateAIVoltageChan(devport, '', DAQmx_Val_RSE, -10.0, 10.0, DAQmx_Val_Volts, None) task.CfgSampClkTiming('', float64(rate), DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, uInt64(max_num_samples)) task.StartTask() return task
def write_analog_io(taskhandle, value, intimeout=100): ''' Write data from given task in Task handle Timeout for writing, default 100 out write data ''' samples = 1 data = float(value) timeout = float64(intimeout) taskhandle.WriteAnalogScalarF64(0, timeout, data, None) sleep(0.5) logger.info( "testlog, write analog io -function, Voltage level: {}".format(data)) return data
def read_di(taskhandle, intimeout=0): ''' Read data from given task in Task handle Timeout for reading, default 0 = try once out Read data ''' value = uInt8() result = int32() bytespersample = int32() samples = int32(1) readarraysize = uInt32(1) timeout = float64(intimeout) taskhandle.ReadDigitalU8(samples, timeout, DAQmx_Val_GroupByScanNumber, value, readarraysize, byref(result), None) return value.value
def read_analog_io_RSE(taskhandle, samples, intimeout=100): ''' Initialize task for reading from analog input port for Voltage measurement in devport = Device/port e.g. Dev1/ai2 Timeout for reading, default 100 out task = Task handle ''' datatype = ctypes.c_double * samples data = datatype() result = int32() readarraysize = uInt32(samples) timeout = float64(intimeout) taskhandle.ReadAnalogF64(samples, timeout, DAQmx_Val_GroupByChannel, data, readarraysize, byref(result), None) #sleep(0.1) for i in data: logger.info("Voltage level: {}".format(str(i))) return repr(data)
def read_analog_io_Diff(taskhandle, samples, intimeout=100): ''' Read data from given task in Task handle Timeout for reading, default 100 out Read data ''' datatype = ctypes.c_double * samples data = datatype() result = int32() readarraysize = uInt32(samples * 2) timeout = float64(intimeout) taskhandle.ReadAnalogF64(samples, timeout, DAQmx_Val_GroupByChannel, data, readarraysize, byref(result), None) #sleep(0.1) for i in data: logger.info("Voltage level: {}".format(str(i))) return repr(data)
def trigger_analog_output(devport, edge_selection): ''' Using PFI to Trigger an Analog Output Generation in devport = Device/port e.g. Dev2/PFI0 out task = Task handle ''' logger.info("testlog, Device: {}".format(devport)) max_num_samples = 2 task = Task() task.CreateAOVoltageChan(devport, '', -10.0, 10.0, DAQmx_Val_Volts, None) task.CfgSampClkTiming('', float64(100), DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, uInt64(max_num_samples)) if edge_selection == 'R': task.CfgDigEdgeStartTrig("/Dev2/PFI1", DAQmx_Val_Rising) #logger.info("testlog, trigger analog output -function, Rising: {}") else: task.CfgDigEdgeStartTrig("/Dev2/PFI1", DAQmx_Val_Falling) #logger.info("testlog, trigger analog output -function, Falling: {}") return task
def trigger_analog_input(devport, samples, rate, edge_selection): ''' Using PFI to Trigger an Analog Input Acquisition in devport = Device/port e.g. Dev2/PFI0 out task = Task handle ''' logger.info("testlog, Device: {}".format(devport)) max_num_samples = samples task = Task() task.CreateAIVoltageChan(devport, '', DAQmx_Val_RSE, -10.0, 10.0, DAQmx_Val_Volts, None) task.CfgSampClkTiming('', float64(rate), DAQmx_Val_Rising, DAQmx_Val_FiniteSamps, uInt64(max_num_samples)) #logger.info("trigger analog input -function, 1: {}") if edge_selection == 'R': task.CfgDigEdgeStartTrig("/Dev2/PFI0", DAQmx_Val_Rising) logger.info("testlog, trigger analog input -function, Rising: {}") else: task.CfgDigEdgeStartTrig("/Dev2/PFI0", DAQmx_Val_Falling) logger.info("testlog, trigger analog input -function, Falling: {}") task.StartTask() return task
def wrapped(name): result = float64() func(name, byref(result)) return result.value