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