Analog Devices / Mbed OS EVAL-AD717x-AD411x-IIO

Dependencies:   sdp_k1_sdram

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ad717x_data_capture.py Source File

ad717x_data_capture.py

00001 import numpy
00002 from serial import Serial
00003 from time import sleep
00004 from pynput import keyboard
00005 import sys
00006 import select
00007 import os
00008 import csv
00009 from adi.ad717x import *
00010 
00011 # Global variables
00012 line = 0
00013 writer = 0
00014 run_continuous = False
00015 iterations = 0
00016 data_capture_abort = False
00017 chn_count = 0
00018 data_list = []
00019 
00020 def key_press_event(key):
00021     global data_capture_abort
00022     data_capture_abort = True
00023 
00024 def init_data_capture():
00025     global device
00026     global data_list
00027     global chn_count
00028     global listener
00029 
00030     ######## User configuration ##########
00031     # Configure the backend for PC to IIOD interface
00032     uri = "serial:COM16,230400"  # For UART, baud rate must be same as set in the FW. COM port is physical Or VCOM.
00033     device_name = "ad4111"      # Name of the device must be same as set in the FW.
00034     ######################################
00035 
00036     # Create an IIO device context
00037     device = ad717x(uri, device_name)
00038     device._ctx.set_timeout(100000)
00039 
00040     ######## User configuration ##########
00041     # Channels to be captured e.g. [0]: 1chn, [0,1]: 2chns, [0,1,2,3]: 4chns, [0,1,2,3,4,5,6,7]: 8chns
00042     device.rx_enabled_channels = [0,1,2,3]
00043 
00044     # The block of samples to be captured. Total samples are received in multiple iterations or blocks
00045     samples_block = 400    # The samples needs to be captured in smaller blocks due to limitations
00046                            # of buffer size (RAM) in the firmware and IIO client timeout factor.
00047                            # Request to capture samples more than buffer size, will be ignored by firmware.
00048                            # Large time taken to read the samples from device, may cause timeout on IIO client.
00049     ######################################
00050 
00051     # Get the channels count from user
00052     chn_count = len(device.rx_enabled_channels)
00053 
00054     # Store the rx buffer size and rx data type based on input channels
00055     device.rx_buffer_size = int(samples_block / chn_count)    # Per channel sample count
00056     device._rx_data_type = np.int32     # size of sample
00057 
00058     listener = keyboard.Listener(on_press=key_press_event)
00059     listener.start()
00060 
00061 def read_user_inputs():
00062     global iterations
00063     global run_continuous
00064     global device
00065     samples_count = int(input("Enter the number of samples to be captured \n\
00066                                 0: Unlimited \n\
00067                                 <50-1000000>: "))
00068 
00069     if (samples_count == 0):
00070         run_continuous = True
00071     else:
00072         run_continuous = False
00073         iterations = (int)(samples_count / device.rx_buffer_size)
00074         if (samples_count % device.rx_buffer_size):
00075             iterations = iterations + 1
00076 
00077 def init_data_logger():
00078     global writer
00079     global chn_count
00080     file_name = "adc_data_capture.csv"
00081     current_dir = os.getcwd()
00082     output_file = os.path.join(current_dir, file_name)
00083     result_file = open(output_file, 'w', newline="")
00084     writer = csv.writer(result_file)
00085     row = []
00086     # Write the channels list header
00087     for chn in range(0,chn_count):
00088         item = "Ch {}".format(chn)
00089         row.insert(chn, item)
00090     writer.writerow(row)
00091 
00092 
00093 def read_buffered_data():
00094     global line
00095     global writer
00096     global device
00097 
00098     # Receive data from device
00099     data = device.rx()
00100     if (line == 0):
00101         print("Data capture started >>")
00102     print("."*line, end="\r")
00103 
00104     if (chn_count == 1):
00105         # Convert 1-D array to 2-D array
00106         data_arr = np.reshape(data, (-1,1))
00107     else:
00108         # Transpose data from N-D data array
00109         data_arr = np.transpose(data)
00110 
00111     writer.writerows(data_arr)
00112 
00113     line = line+1
00114     if (line == 100):
00115         line = 1
00116         print("\n", end="\r")
00117 
00118 def do_data_capture():
00119     global iterations
00120     global run_continuous
00121     global data_capture_abort
00122     done = False
00123     if (run_continuous == True):
00124         print("Press any key to stop data capture..")
00125         sleep(2)
00126         data_capture_abort = False
00127         while not data_capture_abort:
00128             read_buffered_data()
00129     else:
00130         for val in range(0,iterations):
00131             read_buffered_data()
00132 
00133     print("\r\nData capture finished\r\n")
00134 
00135 def exit():
00136     global listener
00137     global writer
00138     global device
00139 
00140     # Delete the objects
00141     del listener
00142     del writer
00143     del device
00144 
00145 def main():
00146     init_data_capture()
00147     init_data_logger()
00148     read_user_inputs()
00149     do_data_capture()
00150     exit()
00151 
00152 if __name__ == "__main__":
00153     main()