Analog Devices / Mbed OS EVAL-AD4110

Dependencies:   tempsensors sdp_k1_sdram

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ad4110_data_capture.py Source File

ad4110_data_capture.py

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