def main(): daq_device = None ctr_device = None status = ScanStatus.IDLE descriptor_index = 0 interface_type = InterfaceType.USB low_encoder = 0 encoder_count = 2 sample_rate = 1000.0 # Hz samples_per_channel = 10000 scan_options = ScanOption.CONTINUOUS scan_flags = CInScanFlag.DEFAULT encoder_type = CounterMeasurementType.ENCODER encoder_mode = CounterMeasurementMode.ENCODER_X1 | CounterMeasurementMode.ENCODER_CLEAR_ON_Z edge_detection = CounterEdgeDetection.RISING_EDGE tick_size = CounterTickSize.TICK_20ns debounce_mode = CounterDebounceMode.NONE debounce_time = CounterDebounceTime.DEBOUNCE_0ns config_flags = CConfigScanFlag.DEFAULT try: # Get descriptors for all of the available DAQ devices. devices = get_daq_device_inventory(interface_type) number_of_devices = len(devices) if number_of_devices == 0: raise Exception('Error: No DAQ devices found') print('Found', number_of_devices, 'DAQ device(s):') for i in range(number_of_devices): print(' ', devices[i].product_name, ' (', devices[i].unique_id, ')', sep='') # Create the DAQ device object associated with the specified descriptor index. daq_device = DaqDevice(devices[descriptor_index]) # Get the CtrDevice object and verify that it is valid. ctr_device = daq_device.get_ctr_device() if ctr_device is None: raise Exception('Error: The DAQ device does not support counters') # Verify that the specified device supports hardware pacing for counters. ctr_info = ctr_device.get_info() if not ctr_info.has_pacer(): raise Exception( '\nError: The specified DAQ device does not support hardware paced counter input' ) # Establish a connection to the DAQ device. descriptor = daq_device.get_descriptor() print('\nConnecting to', descriptor.dev_string, '- please wait...') daq_device.connect() # Get the encoder counter channels. encoder_counters = get_supported_encoder_counters(ctr_info) if len(encoder_counters) == 0: raise Exception( '\nError: The specified DAQ device does not support encoder channels' ) # Verify that the low_encoder number is valid. first_encoder = encoder_counters[0] if low_encoder < first_encoder: low_encoder = first_encoder if low_encoder > first_encoder + len(encoder_counters) - 1: low_encoder = first_encoder # Verify that the encoder count is valid. if encoder_count > len(encoder_counters): encoder_count = len(encoder_counters) # Set the high_encoder channel. high_encoder = low_encoder + encoder_count - 1 if high_encoder > first_encoder + len(encoder_counters) - 1: high_encoder = first_encoder + len(encoder_counters) - 1 # update the actual number of encoders being used encoder_count = high_encoder - low_encoder + 1 # Clear the counter, and configure the counter as an encoder. for encoder in range(low_encoder, high_encoder + 1): ctr_device.c_config_scan(encoder, encoder_type, encoder_mode, edge_detection, tick_size, debounce_mode, debounce_time, config_flags) # Allocate a buffer to receive the data. data = create_int_buffer(encoder_count, samples_per_channel) print('\n', descriptor.dev_string, ' ready', sep='') print(' Function demonstrated: ctr_device.c_config_scan()') print(' Counter(s):', low_encoder, '-', high_encoder) print(' Sample rate:', sample_rate, 'Hz') print(' Scan options:', display_scan_options(scan_options)) try: input('\nHit ENTER to continue\n') except (NameError, SyntaxError): pass # Start the scan ctr_device.c_in_scan(low_encoder, high_encoder, samples_per_channel, sample_rate, scan_options, scan_flags, data) system('clear') try: while True: try: status, transfer_status = ctr_device.get_scan_status() reset_cursor() print('Please enter CTRL + C to terminate the process\n') print('Active DAQ device: ', descriptor.dev_string, ' (', descriptor.unique_id, ')\n', sep='') print('actual scan rate = ', '{:.6f}'.format(sample_rate), 'Hz\n') index = transfer_status.current_index print('currentScanCount = ', transfer_status.current_scan_count) print('currentTotalCount = ', transfer_status.current_total_count) print('currentIndex = ', index, '\n') for encoder_index in range(encoder_count): print('chan =', (encoder_index + low_encoder), ': ', '{:.6f}'.format(data[index + encoder_index])) sleep(0.1) if status != ScanStatus.RUNNING: break except (ValueError, NameError, SyntaxError): break except KeyboardInterrupt: pass except Exception as e: print('\n', e) finally: if daq_device: if status == ScanStatus.RUNNING: ctr_device.scan_stop() if daq_device.is_connected(): daq_device.disconnect() daq_device.release()
def main(): low_counter = 0 high_counter = 1 samples_per_channel = 2000 # Two second buffer (sample_rate * 2) sample_rate = 1000.0 # Hz scan_options = ScanOption.CONTINUOUS scan_flags = CInScanFlag.DEFAULT interface_type = InterfaceType.USB daq_device = None ctr_device = None scan_status = ScanStatus.IDLE try: # Get descriptors for all of the available DAQ devices. devices = get_daq_device_inventory(interface_type) number_of_devices = len(devices) # Verify at least one DAQ device is detected. if number_of_devices == 0: raise Exception('Error: No DAQ devices found') print('Found', number_of_devices, 'DAQ device(s):') for i in range(number_of_devices): print(' ', devices[i].product_name, ' (', devices[i].unique_id, ')', sep='') # Create a DaqDevice object from the first descriptor. daq_device = DaqDevice(devices[0]) ctr_device = daq_device.get_ctr_device() # Verify the specified DAQ device supports counters. if ctr_device is None: raise Exception('Error: The DAQ device does not support counters') # Verify the specified DAQ device supports hardware pacing for counters. ctr_info = ctr_device.get_info() if not ctr_info.has_pacer(): raise Exception( 'Error: The DAQ device does not support paced counter inputs') # Verify that the selected counters support event counting. verify_counters_support_events(ctr_info, low_counter, high_counter) number_of_counters = high_counter - low_counter + 1 descriptor = daq_device.get_descriptor() print('\nConnecting to', descriptor.dev_string, '- please wait...') # Establish a connection to the device. daq_device.connect() print('\n', descriptor.dev_string, 'ready') print(' Function demonstrated: CtrDevice.c_in_scan') print(' Counter(s):', low_counter, '-', high_counter) print(' Samples per channel:', samples_per_channel) print(' Sample rate:', sample_rate, 'Hz') print(' Scan options:', display_scan_options(scan_options)) try: input('\nHit ENTER to continue') except (NameError, SyntaxError): pass # Create a buffer for input data. data = create_int_buffer(number_of_counters, samples_per_channel) # Start the input scan. sample_rate = ctr_device.c_in_scan(low_counter, high_counter, samples_per_channel, sample_rate, scan_options, scan_flags, data) system('clear') print('Please enter CTRL + C to terminate the process\n') print('Active DAQ device: ', descriptor.dev_string, ' (', descriptor.unique_id, ')\n', sep='') print(' Actual scan rate: ', sample_rate, 'Hz') try: while True: try: # Read and display the current scan status. scan_status, transfer_status = ctr_device.get_scan_status() if scan_status != ScanStatus.RUNNING: break print(' Current scan count: ', transfer_status.current_scan_count) print(' Current total count:', transfer_status.current_total_count) print(' Current index: ', transfer_status.current_index) print('') # Read and display the data for each counter. for counter_index in range(number_of_counters): counter_value = data[transfer_status.current_index - counter_index] print(' Counter ', (counter_index + low_counter), ':', str(counter_value).rjust(12), sep='') stdout.flush() sleep(0.1) # Clear the previous status before displaying the next status. for line in range(4 + number_of_counters): stdout.write(CURSOR_UP + ERASE_LINE) except (ValueError, NameError, SyntaxError): break except KeyboardInterrupt: pass except Exception as e: print('\n', e) finally: if daq_device: # Stop the scan. if scan_status == ScanStatus.RUNNING: ctr_device.scan_stop() # Disconnect from the DAQ device. if daq_device.is_connected(): daq_device.disconnect() # Release the DAQ device resource. daq_device.release() return
async def run(self, parsed_args, output): aq_device = None ctr_device = None descriptor_index = 0 interface_type = InterfaceType.USB low_encoder = 0 encoder_count = 5 sample_rate = 1000.0 # 1000 # Hz samples_per_channel = 10000 # 10000 scan_options = ScanOption.CONTINUOUS scan_flags = CInScanFlag.DEFAULT encoder_type = CounterMeasurementType.ENCODER encoder_mode = CounterMeasurementMode.ENCODER_X4 edge_detection = CounterEdgeDetection.RISING_EDGE tick_size = CounterTickSize.TICK_20ns debounce_mode = CounterDebounceMode.TRIGGER_AFTER_STABLE debounce_time = CounterDebounceTime.DEBOUNCE_7500ns config_flags = CConfigScanFlag.DEFAULT daq_device = None try: # Get descriptors for all of the available DAQ devices. devices = get_daq_device_inventory(interface_type) number_of_devices = len(devices) if number_of_devices == 0: raise ValueError('Error: No DAQ devices found') # Create the DAQ device object associated with the specified descriptor index. daq_device = DaqDevice(devices[descriptor_index]) # Get the CtrDevice object and verify that it is valid. ctr_device = daq_device.get_ctr_device() if ctr_device is None: raise ValueError( 'Error: The DAQ device does not support counters') # Verify that the specified device supports hardware pacing for counters. ctr_info = ctr_device.get_info() if not ctr_info.has_pacer(): raise ValueError( 'Error: The specified DAQ device does not support hardware paced counter input' ) # Establish a connection to the DAQ device. descriptor = daq_device.get_descriptor() daq_device.connect() # Get the encoder counter channels. encoder_counters = get_supported_encoder_counters(ctr_info) if len(encoder_counters) == 0: raise ValueError( 'Error: The specified DAQ device does not support encoder channels' ) # Verify that the low_encoder number is valid. first_encoder = encoder_counters[0] if low_encoder < first_encoder: low_encoder = first_encoder if low_encoder > first_encoder + len(encoder_counters) - 1: low_encoder = first_encoder # Verify that the encoder count is valid. if encoder_count > len(encoder_counters): encoder_count = len(encoder_counters) # Set the high_encoder channel. high_encoder = low_encoder + encoder_count - 1 if high_encoder > first_encoder + len(encoder_counters) - 1: high_encoder = first_encoder + len(encoder_counters) - 1 # update the actual number of encoders being used encoder_count = high_encoder - low_encoder + 1 # Clear the counter, and configure the counter as an encoder. for encoder in range(low_encoder, high_encoder + 1): ctr_device.c_config_scan(encoder, encoder_type, encoder_mode, edge_detection, tick_size, debounce_mode, debounce_time, config_flags) # Allocate a buffer to receive the data. data = create_int_buffer(encoder_count, samples_per_channel) # Start the scan ctr_device.c_in_scan(low_encoder, high_encoder, samples_per_channel, sample_rate, scan_options, scan_flags, data) # prev_samples_per_channel = 0 # cur_samples_per_channel = 0 prev_index = 0 XVect = [] YVect = [] ZVect = [] EVect = [] AllVect = [] startX = 0.0 startY = 0.0 startZ = 0.0 startE = 0.0 # newZ = [] prev_index = 0 start_time = time_now() cur_time = start_time + 1 while not self.stop_requested: status, transfer_status = ctr_device.get_scan_status() # not sure if can await the above line index = transfer_status.current_index # print(index) # edge starting condition if index == -1: AllVect = [0.0, 0.0, 0.0, 0.0, 0.0] # normal condition else: # has not looped around if prev_index < index: AllVect = data[prev_index:index] # has wrapped around else: AllVect = data[prev_index:] + data[0:index] prev_index = index # print(AllVect) XVect = AllVect[3::5] YVect = AllVect[1::5] Z1Vect = AllVect[0::5] Z2Vect = AllVect[2::5] EVect = AllVect[4::5] end_time = cur_time + (len(XVect) - 1) * 1e3 # cur_time is in us [XVect, startX] = processX(XVect, startX) [YVect, startY] = processY(YVect, startY) [EVect, startE] = processE(EVect, startE) [ZVect, startZ] = processZ(Z1Vect, Z2Vect, startZ, cur_time, end_time) Xarray = np.array(XVect) Yarray = np.array(YVect) Zarray = np.array(ZVect) Earray = np.array(EVect) * -1 #invert for positive extrusion Xarray = np.vstack(Xarray) Yarray = np.vstack(Yarray) Zarray = np.vstack(Zarray) Earray = np.vstack(Earray) time_array = np.linspace(cur_time, end_time, len(XVect)) time_array = np.vstack(time_array) # print("before all output") All_Output = np.hstack( (time_array, Xarray, Yarray, Zarray, Earray)) # print("after all output") # print(All_Output) await output.write(np.array(All_Output)) # print("after writing") # print(len(YVect)) # for i in range(len(YVect)): # print(str(YVect[i])+ "\t"+str(newY[i])) await asyncio.sleep(1) cur_time = end_time + 1e3 if status != ScanStatus.RUNNING: break # except (ValueError, NameError, SyntaxError): # break # print("next while loop cycle") except ValueError as e: print(str(e)) except Exception as e: raise e finally: if daq_device: if ctr_device: ctr_device.scan_stop() if daq_device.is_connected(): daq_device.disconnect() daq_device.release()
def main(): counter_number = 0 interface_type = InterfaceType.USB daq_device = None try: # Get descriptors for all of the available DAQ devices. devices = get_daq_device_inventory(interface_type) number_of_devices = len(devices) # Verify at least one DAQ device is detected. if number_of_devices == 0: raise Exception('Error: No DAQ devices found') print('Found', number_of_devices, 'DAQ device(s):') for i in range(number_of_devices): print(' ', devices[i].product_name, ' (', devices[i].unique_id, ')', sep='') # Create a DaqDevice object from the first descriptor. daq_device = DaqDevice(devices[0]) ctr_device = daq_device.get_ctr_device() # Verify the specified DAQ device supports counters. if ctr_device is None: raise Exception('Error: The DAQ device does not support counters') descriptor = daq_device.get_descriptor() print('\nConnecting to', descriptor.dev_string, '- please wait...') # Establish a connection to the device. daq_device.connect() ctr_info = ctr_device.get_info() dev_num_counters = ctr_info.get_num_ctrs() if counter_number >= dev_num_counters: counter_number = dev_num_counters - 1 print('\n', descriptor.dev_string, 'ready') print(' Function demonstrated: CtrDevice.c_in') print(' Counter:', counter_number) try: input('\nHit ENTER to continue') except (NameError, SyntaxError): pass system('clear') ctr_device.c_clear(counter_number) try: while True: try: # Read and display the data. counter_value = ctr_device.c_in(counter_number) reset_cursor() print('Please enter CTRL + C to terminate the process\n') print('Active DAQ device: ', descriptor.dev_string, ' (', descriptor.unique_id, ')\n', sep='') print(' Counter ', counter_number, ':', str(counter_value).rjust(12), sep='') stdout.flush() sleep(0.1) except (ValueError, NameError, SyntaxError): break except KeyboardInterrupt: pass except Exception as e: print('\n', e) finally: if daq_device: # Disconnect from the DAQ device. if daq_device.is_connected(): daq_device.disconnect() # Release the DAQ device resource. daq_device.release()