IIO firmware for the AD4110
Dependencies: tempsensors sdp_k1_sdram
scripts/ad4110_data_capture.py@0:6ca37a8f8ba9, 2022-07-27 (annotated)
- Committer:
- Janani Sunil
- Date:
- Wed Jul 27 17:04:15 2022 +0530
- Revision:
- 0:6ca37a8f8ba9
Initial implementation for AD4110 IIO Firmware
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Janani Sunil | 0:6ca37a8f8ba9 | 1 | # @file ad4110_data_capture.py |
Janani Sunil | 0:6ca37a8f8ba9 | 2 | # @brief Data capturing for the AD4110 device |
Janani Sunil | 0:6ca37a8f8ba9 | 3 | # Copyright (c) 2022 Analog Devices, Inc. |
Janani Sunil | 0:6ca37a8f8ba9 | 4 | # All rights reserved. |
Janani Sunil | 0:6ca37a8f8ba9 | 5 | # |
Janani Sunil | 0:6ca37a8f8ba9 | 6 | # This software is proprietary to Analog Devices, Inc. and its licensors. |
Janani Sunil | 0:6ca37a8f8ba9 | 7 | # By using this software you agree to the terms of the associated |
Janani Sunil | 0:6ca37a8f8ba9 | 8 | # Analog Devices Software License Agreement. |
Janani Sunil | 0:6ca37a8f8ba9 | 9 | # |
Janani Sunil | 0:6ca37a8f8ba9 | 10 | |
Janani Sunil | 0:6ca37a8f8ba9 | 11 | import numpy |
Janani Sunil | 0:6ca37a8f8ba9 | 12 | from serial import Serial |
Janani Sunil | 0:6ca37a8f8ba9 | 13 | from time import sleep |
Janani Sunil | 0:6ca37a8f8ba9 | 14 | from pynput import keyboard |
Janani Sunil | 0:6ca37a8f8ba9 | 15 | import os |
Janani Sunil | 0:6ca37a8f8ba9 | 16 | import csv |
Janani Sunil | 0:6ca37a8f8ba9 | 17 | from adi.ad4110 import * |
Janani Sunil | 0:6ca37a8f8ba9 | 18 | import math |
Janani Sunil | 0:6ca37a8f8ba9 | 19 | |
Janani Sunil | 0:6ca37a8f8ba9 | 20 | # Global variables |
Janani Sunil | 0:6ca37a8f8ba9 | 21 | line = 0 |
Janani Sunil | 0:6ca37a8f8ba9 | 22 | writer = 0 |
Janani Sunil | 0:6ca37a8f8ba9 | 23 | run_continuous = False |
Janani Sunil | 0:6ca37a8f8ba9 | 24 | iterations = 0 |
Janani Sunil | 0:6ca37a8f8ba9 | 25 | data_capture_abort = False |
Janani Sunil | 0:6ca37a8f8ba9 | 26 | chn_count = 0 |
Janani Sunil | 0:6ca37a8f8ba9 | 27 | data_list = [] |
Janani Sunil | 0:6ca37a8f8ba9 | 28 | |
Janani Sunil | 0:6ca37a8f8ba9 | 29 | def key_press_event(key): |
Janani Sunil | 0:6ca37a8f8ba9 | 30 | global data_capture_abort |
Janani Sunil | 0:6ca37a8f8ba9 | 31 | data_capture_abort = True |
Janani Sunil | 0:6ca37a8f8ba9 | 32 | |
Janani Sunil | 0:6ca37a8f8ba9 | 33 | def init_data_capture(): |
Janani Sunil | 0:6ca37a8f8ba9 | 34 | global device |
Janani Sunil | 0:6ca37a8f8ba9 | 35 | global data_list |
Janani Sunil | 0:6ca37a8f8ba9 | 36 | global chn_count |
Janani Sunil | 0:6ca37a8f8ba9 | 37 | global listener |
Janani Sunil | 0:6ca37a8f8ba9 | 38 | global samples_block |
Janani Sunil | 0:6ca37a8f8ba9 | 39 | |
Janani Sunil | 0:6ca37a8f8ba9 | 40 | ######## User configuration ########## |
Janani Sunil | 0:6ca37a8f8ba9 | 41 | # Configure the backend for PC to IIOD interface |
Janani Sunil | 0:6ca37a8f8ba9 | 42 | uri = "serial:COM17,230400" # For UART, baud rate must be same as set in the FW. COM port is physical Or VCOM. |
Janani Sunil | 0:6ca37a8f8ba9 | 43 | device_name = "ad4110" # Name of the device must be same as set in the FW. |
Janani Sunil | 0:6ca37a8f8ba9 | 44 | ###################################### |
Janani Sunil | 0:6ca37a8f8ba9 | 45 | |
Janani Sunil | 0:6ca37a8f8ba9 | 46 | # Create an IIO device context |
Janani Sunil | 0:6ca37a8f8ba9 | 47 | device = ad4110(uri, device_name) |
Janani Sunil | 0:6ca37a8f8ba9 | 48 | device._ctx.set_timeout(100000) |
Janani Sunil | 0:6ca37a8f8ba9 | 49 | |
Janani Sunil | 0:6ca37a8f8ba9 | 50 | ######## User configuration ########## |
Janani Sunil | 0:6ca37a8f8ba9 | 51 | # Channels to be captured e.g. [0]: 1chn, [0,1]: 2chns, [0,1,2,3]: 4chns |
Janani Sunil | 0:6ca37a8f8ba9 | 52 | device.rx_enabled_channels = [0,1,2,3] |
Janani Sunil | 0:6ca37a8f8ba9 | 53 | |
Janani Sunil | 0:6ca37a8f8ba9 | 54 | # The block of samples to be captured. Total samples are received in multiple iterations or blocks |
Janani Sunil | 0:6ca37a8f8ba9 | 55 | samples_block = 400 # The samples needs to be captured in smaller blocks due to limitations |
Janani Sunil | 0:6ca37a8f8ba9 | 56 | # of buffer size (RAM) in the firmware and IIO client timeout factor. |
Janani Sunil | 0:6ca37a8f8ba9 | 57 | # Request to capture samples more than buffer size, will be ignored by firmware. |
Janani Sunil | 0:6ca37a8f8ba9 | 58 | # Large time taken to read the samples from device, may cause timeout on IIO client. |
Janani Sunil | 0:6ca37a8f8ba9 | 59 | ###################################### |
Janani Sunil | 0:6ca37a8f8ba9 | 60 | |
Janani Sunil | 0:6ca37a8f8ba9 | 61 | # Get the channels count from user |
Janani Sunil | 0:6ca37a8f8ba9 | 62 | chn_count = len(device.rx_enabled_channels) |
Janani Sunil | 0:6ca37a8f8ba9 | 63 | |
Janani Sunil | 0:6ca37a8f8ba9 | 64 | # Store the rx buffer size and rx data type based on input channels |
Janani Sunil | 0:6ca37a8f8ba9 | 65 | device.rx_buffer_size = samples_block # Size of the IIO buffer (buffer is submitted during call to rx() method) |
Janani Sunil | 0:6ca37a8f8ba9 | 66 | device._rx_data_type = np.int32 # size of sample |
Janani Sunil | 0:6ca37a8f8ba9 | 67 | |
Janani Sunil | 0:6ca37a8f8ba9 | 68 | listener = keyboard.Listener(on_press=key_press_event) |
Janani Sunil | 0:6ca37a8f8ba9 | 69 | listener.start() |
Janani Sunil | 0:6ca37a8f8ba9 | 70 | |
Janani Sunil | 0:6ca37a8f8ba9 | 71 | def read_user_inputs(): |
Janani Sunil | 0:6ca37a8f8ba9 | 72 | global iterations |
Janani Sunil | 0:6ca37a8f8ba9 | 73 | global run_continuous |
Janani Sunil | 0:6ca37a8f8ba9 | 74 | global device |
Janani Sunil | 0:6ca37a8f8ba9 | 75 | global samples_count |
Janani Sunil | 0:6ca37a8f8ba9 | 76 | |
Janani Sunil | 0:6ca37a8f8ba9 | 77 | samples_count = int(input("Enter the number of samples to be captured \n\ |
Janani Sunil | 0:6ca37a8f8ba9 | 78 | 0: Unlimited \n\ |
Janani Sunil | 0:6ca37a8f8ba9 | 79 | <50-1000000>: ")) |
Janani Sunil | 0:6ca37a8f8ba9 | 80 | |
Janani Sunil | 0:6ca37a8f8ba9 | 81 | if (samples_count == 0): |
Janani Sunil | 0:6ca37a8f8ba9 | 82 | run_continuous = True |
Janani Sunil | 0:6ca37a8f8ba9 | 83 | else: |
Janani Sunil | 0:6ca37a8f8ba9 | 84 | run_continuous = False |
Janani Sunil | 0:6ca37a8f8ba9 | 85 | if (samples_count <= samples_block): |
Janani Sunil | 0:6ca37a8f8ba9 | 86 | device.rx_buffer_size = samples_count |
Janani Sunil | 0:6ca37a8f8ba9 | 87 | iterations = 1 |
Janani Sunil | 0:6ca37a8f8ba9 | 88 | else: |
Janani Sunil | 0:6ca37a8f8ba9 | 89 | iterations = math.ceil(samples_count / samples_block) |
Janani Sunil | 0:6ca37a8f8ba9 | 90 | |
Janani Sunil | 0:6ca37a8f8ba9 | 91 | def init_data_logger(): |
Janani Sunil | 0:6ca37a8f8ba9 | 92 | global writer |
Janani Sunil | 0:6ca37a8f8ba9 | 93 | global chn_count |
Janani Sunil | 0:6ca37a8f8ba9 | 94 | file_name = "adc_data_capture.csv" |
Janani Sunil | 0:6ca37a8f8ba9 | 95 | current_dir = os.getcwd() |
Janani Sunil | 0:6ca37a8f8ba9 | 96 | output_file = os.path.join(current_dir, file_name) |
Janani Sunil | 0:6ca37a8f8ba9 | 97 | result_file = open(output_file, 'w', newline="") |
Janani Sunil | 0:6ca37a8f8ba9 | 98 | writer = csv.writer(result_file) |
Janani Sunil | 0:6ca37a8f8ba9 | 99 | row = [] |
Janani Sunil | 0:6ca37a8f8ba9 | 100 | # Write the channels list header |
Janani Sunil | 0:6ca37a8f8ba9 | 101 | for chn in range(0,chn_count): |
Janani Sunil | 0:6ca37a8f8ba9 | 102 | item = "Ch {}".format(chn) |
Janani Sunil | 0:6ca37a8f8ba9 | 103 | row.insert(chn, item) |
Janani Sunil | 0:6ca37a8f8ba9 | 104 | writer.writerow(row) |
Janani Sunil | 0:6ca37a8f8ba9 | 105 | |
Janani Sunil | 0:6ca37a8f8ba9 | 106 | |
Janani Sunil | 0:6ca37a8f8ba9 | 107 | def read_buffered_data(): |
Janani Sunil | 0:6ca37a8f8ba9 | 108 | global line |
Janani Sunil | 0:6ca37a8f8ba9 | 109 | global writer |
Janani Sunil | 0:6ca37a8f8ba9 | 110 | global device |
Janani Sunil | 0:6ca37a8f8ba9 | 111 | |
Janani Sunil | 0:6ca37a8f8ba9 | 112 | # Receive data from device |
Janani Sunil | 0:6ca37a8f8ba9 | 113 | data = device.rx() |
Janani Sunil | 0:6ca37a8f8ba9 | 114 | if (line == 0): |
Janani Sunil | 0:6ca37a8f8ba9 | 115 | print("Data capture started >>") |
Janani Sunil | 0:6ca37a8f8ba9 | 116 | print("."*line, end="\r") |
Janani Sunil | 0:6ca37a8f8ba9 | 117 | |
Janani Sunil | 0:6ca37a8f8ba9 | 118 | if (chn_count == 1): |
Janani Sunil | 0:6ca37a8f8ba9 | 119 | # Convert 1-D array to 2-D array |
Janani Sunil | 0:6ca37a8f8ba9 | 120 | data_arr = np.reshape(data, (-1,1)) |
Janani Sunil | 0:6ca37a8f8ba9 | 121 | else: |
Janani Sunil | 0:6ca37a8f8ba9 | 122 | # Transpose data from N-D data array |
Janani Sunil | 0:6ca37a8f8ba9 | 123 | data_arr = np.transpose(data) |
Janani Sunil | 0:6ca37a8f8ba9 | 124 | |
Janani Sunil | 0:6ca37a8f8ba9 | 125 | writer.writerows(data_arr) |
Janani Sunil | 0:6ca37a8f8ba9 | 126 | |
Janani Sunil | 0:6ca37a8f8ba9 | 127 | line = line+1 |
Janani Sunil | 0:6ca37a8f8ba9 | 128 | if (line == 100): |
Janani Sunil | 0:6ca37a8f8ba9 | 129 | line = 1 |
Janani Sunil | 0:6ca37a8f8ba9 | 130 | print("\n", end="\r") |
Janani Sunil | 0:6ca37a8f8ba9 | 131 | |
Janani Sunil | 0:6ca37a8f8ba9 | 132 | def do_data_capture(): |
Janani Sunil | 0:6ca37a8f8ba9 | 133 | global iterations |
Janani Sunil | 0:6ca37a8f8ba9 | 134 | global run_continuous |
Janani Sunil | 0:6ca37a8f8ba9 | 135 | global data_capture_abort |
Janani Sunil | 0:6ca37a8f8ba9 | 136 | done = False |
Janani Sunil | 0:6ca37a8f8ba9 | 137 | if (run_continuous == True): |
Janani Sunil | 0:6ca37a8f8ba9 | 138 | print("Press any key to stop data capture..") |
Janani Sunil | 0:6ca37a8f8ba9 | 139 | sleep(2) |
Janani Sunil | 0:6ca37a8f8ba9 | 140 | data_capture_abort = False |
Janani Sunil | 0:6ca37a8f8ba9 | 141 | while not data_capture_abort: |
Janani Sunil | 0:6ca37a8f8ba9 | 142 | read_buffered_data() |
Janani Sunil | 0:6ca37a8f8ba9 | 143 | else: |
Janani Sunil | 0:6ca37a8f8ba9 | 144 | for val in range(0,iterations): |
Janani Sunil | 0:6ca37a8f8ba9 | 145 | read_buffered_data() |
Janani Sunil | 0:6ca37a8f8ba9 | 146 | |
Janani Sunil | 0:6ca37a8f8ba9 | 147 | print("\r\nData capture finished\r\n") |
Janani Sunil | 0:6ca37a8f8ba9 | 148 | |
Janani Sunil | 0:6ca37a8f8ba9 | 149 | def exit(): |
Janani Sunil | 0:6ca37a8f8ba9 | 150 | global listener |
Janani Sunil | 0:6ca37a8f8ba9 | 151 | global writer |
Janani Sunil | 0:6ca37a8f8ba9 | 152 | global device |
Janani Sunil | 0:6ca37a8f8ba9 | 153 | |
Janani Sunil | 0:6ca37a8f8ba9 | 154 | # Delete the objects |
Janani Sunil | 0:6ca37a8f8ba9 | 155 | del listener |
Janani Sunil | 0:6ca37a8f8ba9 | 156 | del writer |
Janani Sunil | 0:6ca37a8f8ba9 | 157 | del device |
Janani Sunil | 0:6ca37a8f8ba9 | 158 | |
Janani Sunil | 0:6ca37a8f8ba9 | 159 | def main(): |
Janani Sunil | 0:6ca37a8f8ba9 | 160 | init_data_capture() |
Janani Sunil | 0:6ca37a8f8ba9 | 161 | init_data_logger() |
Janani Sunil | 0:6ca37a8f8ba9 | 162 | read_user_inputs() |
Janani Sunil | 0:6ca37a8f8ba9 | 163 | do_data_capture() |
Janani Sunil | 0:6ca37a8f8ba9 | 164 | exit() |
Janani Sunil | 0:6ca37a8f8ba9 | 165 | |
Janani Sunil | 0:6ca37a8f8ba9 | 166 | if __name__ == "__main__": |
Janani Sunil | 0:6ca37a8f8ba9 | 167 | main() |