IIO firmware for the AD4110
Dependencies: tempsensors sdp_k1_sdram
scripts/ad4110_sensor_measurement.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_sensor_measurement.py |
Janani Sunil | 0:6ca37a8f8ba9 | 2 | # @brief Sensor Measurement steps for the demo mode configurations in AD4110 |
Janani Sunil | 0:6ca37a8f8ba9 | 3 | # |
Janani Sunil | 0:6ca37a8f8ba9 | 4 | # Copyright (c) 2022 Analog Devices, Inc. |
Janani Sunil | 0:6ca37a8f8ba9 | 5 | # All rights reserved. |
Janani Sunil | 0:6ca37a8f8ba9 | 6 | # |
Janani Sunil | 0:6ca37a8f8ba9 | 7 | # This software is proprietary to Analog Devices, Inc. and its licensors. |
Janani Sunil | 0:6ca37a8f8ba9 | 8 | # By using this software you agree to the terms of the associated |
Janani Sunil | 0:6ca37a8f8ba9 | 9 | # Analog Devices Software License Agreement. |
Janani Sunil | 0:6ca37a8f8ba9 | 10 | # |
Janani Sunil | 0:6ca37a8f8ba9 | 11 | |
Janani Sunil | 0:6ca37a8f8ba9 | 12 | 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 | from adi.ad4110 import * |
Janani Sunil | 0:6ca37a8f8ba9 | 16 | from ad4110_xattr import * |
Janani Sunil | 0:6ca37a8f8ba9 | 17 | |
Janani Sunil | 0:6ca37a8f8ba9 | 18 | # Delays in seconds |
Janani Sunil | 0:6ca37a8f8ba9 | 19 | short_time = 0.5 |
Janani Sunil | 0:6ca37a8f8ba9 | 20 | long_time = 1 |
Janani Sunil | 0:6ca37a8f8ba9 | 21 | |
Janani Sunil | 0:6ca37a8f8ba9 | 22 | def key_press_event(key): |
Janani Sunil | 0:6ca37a8f8ba9 | 23 | global key_pressed |
Janani Sunil | 0:6ca37a8f8ba9 | 24 | key_pressed = True |
Janani Sunil | 0:6ca37a8f8ba9 | 25 | |
Janani Sunil | 0:6ca37a8f8ba9 | 26 | def init_sensor_measurement(): |
Janani Sunil | 0:6ca37a8f8ba9 | 27 | global device |
Janani Sunil | 0:6ca37a8f8ba9 | 28 | global demo_config |
Janani Sunil | 0:6ca37a8f8ba9 | 29 | global listener |
Janani Sunil | 0:6ca37a8f8ba9 | 30 | |
Janani Sunil | 0:6ca37a8f8ba9 | 31 | ######## User configuration ########## |
Janani Sunil | 0:6ca37a8f8ba9 | 32 | # Configure the backend for PC to IIOD interface |
Janani Sunil | 0:6ca37a8f8ba9 | 33 | 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 | 34 | device_name = "ad4110" # Name of the device must be same as set in the FW. |
Janani Sunil | 0:6ca37a8f8ba9 | 35 | ###################################### |
Janani Sunil | 0:6ca37a8f8ba9 | 36 | |
Janani Sunil | 0:6ca37a8f8ba9 | 37 | # Create an IIO device context |
Janani Sunil | 0:6ca37a8f8ba9 | 38 | device = ad4110_xattr(uri, device_name) |
Janani Sunil | 0:6ca37a8f8ba9 | 39 | device._ctx.set_timeout(100000) |
Janani Sunil | 0:6ca37a8f8ba9 | 40 | device.rx_output_type = "raw" |
Janani Sunil | 0:6ca37a8f8ba9 | 41 | |
Janani Sunil | 0:6ca37a8f8ba9 | 42 | # Get current user device config from the firmware |
Janani Sunil | 0:6ca37a8f8ba9 | 43 | demo_config = device.demo_config |
Janani Sunil | 0:6ca37a8f8ba9 | 44 | print("\r\nDemo Config: {}\r\n".format(demo_config)) |
Janani Sunil | 0:6ca37a8f8ba9 | 45 | |
Janani Sunil | 0:6ca37a8f8ba9 | 46 | listener = keyboard.Listener(on_press=key_press_event) |
Janani Sunil | 0:6ca37a8f8ba9 | 47 | listener.start() |
Janani Sunil | 0:6ca37a8f8ba9 | 48 | |
Janani Sunil | 0:6ca37a8f8ba9 | 49 | def perform_sensor_measurement(): |
Janani Sunil | 0:6ca37a8f8ba9 | 50 | global key_pressed |
Janani Sunil | 0:6ca37a8f8ba9 | 51 | |
Janani Sunil | 0:6ca37a8f8ba9 | 52 | print("\r\n*** Press any key to stop the measurement ***\r\n") |
Janani Sunil | 0:6ca37a8f8ba9 | 53 | sleep(long_time) |
Janani Sunil | 0:6ca37a8f8ba9 | 54 | |
Janani Sunil | 0:6ca37a8f8ba9 | 55 | # Print the header |
Janani Sunil | 0:6ca37a8f8ba9 | 56 | header = "" |
Janani Sunil | 0:6ca37a8f8ba9 | 57 | for chn in device.channel: |
Janani Sunil | 0:6ca37a8f8ba9 | 58 | header = header + chn.name + ' ' |
Janani Sunil | 0:6ca37a8f8ba9 | 59 | print(header) |
Janani Sunil | 0:6ca37a8f8ba9 | 60 | |
Janani Sunil | 0:6ca37a8f8ba9 | 61 | key_pressed = False |
Janani Sunil | 0:6ca37a8f8ba9 | 62 | while not key_pressed: |
Janani Sunil | 0:6ca37a8f8ba9 | 63 | result_str = "" |
Janani Sunil | 0:6ca37a8f8ba9 | 64 | for chn in device.channel: |
Janani Sunil | 0:6ca37a8f8ba9 | 65 | sleep(short_time) |
Janani Sunil | 0:6ca37a8f8ba9 | 66 | adc_raw = chn.raw |
Janani Sunil | 0:6ca37a8f8ba9 | 67 | adc_scale = chn.scale |
Janani Sunil | 0:6ca37a8f8ba9 | 68 | adc_offset = chn.offset |
Janani Sunil | 0:6ca37a8f8ba9 | 69 | if (demo_config == 'Voltage'): |
Janani Sunil | 0:6ca37a8f8ba9 | 70 | actual_voltage = (adc_raw + adc_offset) * adc_scale / 1000 |
Janani Sunil | 0:6ca37a8f8ba9 | 71 | result_str = result_str + str(round(actual_voltage,4)) + 'V ' |
Janani Sunil | 0:6ca37a8f8ba9 | 72 | elif ((demo_config == 'Current') or (demo_config == 'Field Power Supply')): |
Janani Sunil | 0:6ca37a8f8ba9 | 73 | actual_current = (adc_raw + adc_offset) * adc_scale |
Janani Sunil | 0:6ca37a8f8ba9 | 74 | result_str = result_str + str(round(actual_current,4)) + 'mA ' |
Janani Sunil | 0:6ca37a8f8ba9 | 75 | elif (demo_config == 'RTD') or (demo_config == 'Thermocouple'): |
Janani Sunil | 0:6ca37a8f8ba9 | 76 | actual_temperature = (adc_raw * adc_scale) / 1000 |
Janani Sunil | 0:6ca37a8f8ba9 | 77 | result_str = result_str + str(round(actual_temperature,4)) + 'C ' |
Janani Sunil | 0:6ca37a8f8ba9 | 78 | print(result_str) |
Janani Sunil | 0:6ca37a8f8ba9 | 79 | |
Janani Sunil | 0:6ca37a8f8ba9 | 80 | def exit(): |
Janani Sunil | 0:6ca37a8f8ba9 | 81 | global listener |
Janani Sunil | 0:6ca37a8f8ba9 | 82 | global device |
Janani Sunil | 0:6ca37a8f8ba9 | 83 | |
Janani Sunil | 0:6ca37a8f8ba9 | 84 | # Delete the objects |
Janani Sunil | 0:6ca37a8f8ba9 | 85 | del listener |
Janani Sunil | 0:6ca37a8f8ba9 | 86 | del device |
Janani Sunil | 0:6ca37a8f8ba9 | 87 | |
Janani Sunil | 0:6ca37a8f8ba9 | 88 | def main(): |
Janani Sunil | 0:6ca37a8f8ba9 | 89 | init_sensor_measurement() |
Janani Sunil | 0:6ca37a8f8ba9 | 90 | perform_sensor_measurement() |
Janani Sunil | 0:6ca37a8f8ba9 | 91 | exit() |
Janani Sunil | 0:6ca37a8f8ba9 | 92 | |
Janani Sunil | 0:6ca37a8f8ba9 | 93 | if __name__ == "__main__": |
Janani Sunil | 0:6ca37a8f8ba9 | 94 | main() |