Mahesh Phalke / Mbed OS EVAL-AD7689

Dependencies:   sdp_k1_sdram

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ad7689_data_capture.py Source File

ad7689_data_capture.py

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