Example program for EVAL-AD4130
Dependencies: tempsensors sdp_k1_sdram
scripts/ad4130_calibration.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 serial |
Mahesh Phalke |
2:7b2b268ea49c | 2 | from time import sleep |
Mahesh Phalke |
2:7b2b268ea49c | 3 | from adi import ad4130 |
Mahesh Phalke |
2:7b2b268ea49c | 4 | from ad4130_xattr import * |
Mahesh Phalke |
2:7b2b268ea49c | 5 | |
Mahesh Phalke |
2:7b2b268ea49c | 6 | # Delays in second |
Mahesh Phalke |
2:7b2b268ea49c | 7 | short_time = 0.2 |
Mahesh Phalke |
2:7b2b268ea49c | 8 | |
Mahesh Phalke |
2:7b2b268ea49c | 9 | # Calibration type identifiers |
Mahesh Phalke |
2:7b2b268ea49c | 10 | internal_calibration = '1' |
Mahesh Phalke |
2:7b2b268ea49c | 11 | system_calibration = '2' |
Mahesh Phalke |
2:7b2b268ea49c | 12 | |
Mahesh Phalke |
2:7b2b268ea49c | 13 | # Analog input mapping as configured in the firmware |
Mahesh Phalke |
2:7b2b268ea49c | 14 | ain_mapping = { |
Mahesh Phalke |
2:7b2b268ea49c | 15 | "User Default" : [ 'AIN0-AVSS', 'AIN1-AVSS', 'AIN2-AVSS', 'AIN3-AVSS', |
Mahesh Phalke |
2:7b2b268ea49c | 16 | 'AIN4-AVSS', 'AIN5-AVSS', 'AIN6-AVSS', 'AIN7-AVSS', |
Mahesh Phalke |
2:7b2b268ea49c | 17 | 'AIN8-AVSS', 'AIN9-AVSS', 'AIN10-AVSS', 'AIN11-AVSS', |
Mahesh Phalke |
2:7b2b268ea49c | 18 | 'AIN12-AVSS', 'AIN13-AVSS', 'AIN14-AVSS', 'AIN15-AVSS' |
Mahesh Phalke |
2:7b2b268ea49c | 19 | ], |
Mahesh Phalke |
2:7b2b268ea49c | 20 | "Thermistor" : [ 'AIN4-AIN5' ], |
Mahesh Phalke |
2:7b2b268ea49c | 21 | "Thermocouple" : [ 'AIN2-AIN3', 'AIN4-AIN5' ], |
Mahesh Phalke |
2:7b2b268ea49c | 22 | "2-Wire RTD" : [ 'AIN2-AIN3' ], |
Mahesh Phalke |
2:7b2b268ea49c | 23 | "3-Wire RTD" : [ 'AIN2-AIN3' ], |
Mahesh Phalke |
2:7b2b268ea49c | 24 | "4-Wire RTD" : [ 'AIN2-AIN3' ], |
Mahesh Phalke |
2:7b2b268ea49c | 25 | "Loadcell" : [ 'AIN5-AIN6' ], |
Mahesh Phalke |
2:7b2b268ea49c | 26 | "ECG" : [ 'AIN11-AIN14' ], |
Mahesh Phalke |
2:7b2b268ea49c | 27 | "Noise Test" : [ 'AIN0-AIN1' ], |
Mahesh Phalke |
2:7b2b268ea49c | 28 | } |
Mahesh Phalke |
2:7b2b268ea49c | 29 | |
Mahesh Phalke |
2:7b2b268ea49c | 30 | # IIO Channel name and respective channel index mapping |
Mahesh Phalke |
2:7b2b268ea49c | 31 | chn_mappping = { |
Mahesh Phalke |
2:7b2b268ea49c | 32 | "voltage0" : 0, "voltage1" : 1, "voltage2" : 2, "voltage3" : 3, "voltage4" : 4, |
Mahesh Phalke |
2:7b2b268ea49c | 33 | "voltage5" : 5, "voltage6" : 6, "voltage7" : 7, "voltage8" : 8, "voltage9" : 9, |
Mahesh Phalke |
2:7b2b268ea49c | 34 | "voltage10" : 10, "voltage11" : 11, "voltage12" : 12, "voltage13" : 13, |
Mahesh Phalke |
2:7b2b268ea49c | 35 | "voltage14" : 14, "voltage15" : 15, |
Mahesh Phalke |
2:7b2b268ea49c | 36 | |
Mahesh Phalke |
2:7b2b268ea49c | 37 | "current0" : 0, "current1" : 1, "current2" : 2, "current3" : 3, "current4" : 4, |
Mahesh Phalke |
2:7b2b268ea49c | 38 | "current5" : 5, "current6" : 6, "current7" : 7, "current8" : 8, "current9" : 9, |
Mahesh Phalke |
2:7b2b268ea49c | 39 | "current10" : 10, "current11" : 11, "current12" : 12, "current13" : 13, |
Mahesh Phalke |
2:7b2b268ea49c | 40 | "current14" : 14, "current15" : 15, |
Mahesh Phalke |
2:7b2b268ea49c | 41 | |
Mahesh Phalke |
2:7b2b268ea49c | 42 | "temp0" : 0, "temp1" : 1, "temp2" : 2, "temp3" : 3, "temp4" : 4, |
Mahesh Phalke |
2:7b2b268ea49c | 43 | "temp5" : 5, "temp6" : 6, "temp7" : 7, "temp8" : 8, "temp9" : 9, |
Mahesh Phalke |
2:7b2b268ea49c | 44 | "temp10" : 10, "temp11" : 11, "temp12" : 12, "temp13" : 13, |
Mahesh Phalke |
2:7b2b268ea49c | 45 | "temp14" : 14, "temp15" : 15, |
Mahesh Phalke |
2:7b2b268ea49c | 46 | } |
Mahesh Phalke |
2:7b2b268ea49c | 47 | |
Mahesh Phalke |
2:7b2b268ea49c | 48 | def init_calibration(): |
Mahesh Phalke |
2:7b2b268ea49c | 49 | global device |
Mahesh Phalke |
2:7b2b268ea49c | 50 | global demo_config |
Mahesh Phalke |
2:7b2b268ea49c | 51 | |
Mahesh Phalke |
2:7b2b268ea49c | 52 | ######## User configuration ########## |
Mahesh Phalke |
2:7b2b268ea49c | 53 | # Configure the backend for PC to IIOD interface |
Mahesh Phalke |
2:7b2b268ea49c | 54 | 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 | 55 | device_name = "ad4130-8" # Name of the device must be same as set in the FW. |
Mahesh Phalke |
2:7b2b268ea49c | 56 | ###################################### |
Mahesh Phalke |
2:7b2b268ea49c | 57 | |
Mahesh Phalke |
2:7b2b268ea49c | 58 | # Create an IIO device context |
Mahesh Phalke |
2:7b2b268ea49c | 59 | device = ad4130_xattr(uri, device_name) |
Mahesh Phalke |
2:7b2b268ea49c | 60 | device._ctx.set_timeout(100000) |
Mahesh Phalke |
2:7b2b268ea49c | 61 | |
Mahesh Phalke |
2:7b2b268ea49c | 62 | # Get current user device config from the firmware |
Mahesh Phalke |
2:7b2b268ea49c | 63 | demo_config = device.demo_config |
Mahesh Phalke |
2:7b2b268ea49c | 64 | print("\r\nDemo Config: {}\r\n".format(demo_config)) |
Mahesh Phalke |
2:7b2b268ea49c | 65 | |
Mahesh Phalke |
2:7b2b268ea49c | 66 | def get_calibration_status(calibration_type, chn, chn_index): |
Mahesh Phalke |
2:7b2b268ea49c | 67 | global gain_before_calib, gain_after_calib |
Mahesh Phalke |
2:7b2b268ea49c | 68 | global offset_before_calib, offset_after_calib |
Mahesh Phalke |
2:7b2b268ea49c | 69 | global calibration_status |
Mahesh Phalke |
2:7b2b268ea49c | 70 | |
Mahesh Phalke |
2:7b2b268ea49c | 71 | if (calibration_type == system_calibration): |
Mahesh Phalke |
2:7b2b268ea49c | 72 | calib_status = chn.system_calibration |
Mahesh Phalke |
2:7b2b268ea49c | 73 | else: |
Mahesh Phalke |
2:7b2b268ea49c | 74 | calib_status = chn.internal_calibration |
Mahesh Phalke |
2:7b2b268ea49c | 75 | |
Mahesh Phalke |
2:7b2b268ea49c | 76 | gain_before_calib = calib_status[0:8] |
Mahesh Phalke |
2:7b2b268ea49c | 77 | gain_after_calib = calib_status[8:16] |
Mahesh Phalke |
2:7b2b268ea49c | 78 | offset_before_calib = calib_status[16:24] |
Mahesh Phalke |
2:7b2b268ea49c | 79 | offset_after_calib = calib_status[24:32] |
Mahesh Phalke |
2:7b2b268ea49c | 80 | calibration_status = calib_status[32:] |
Mahesh Phalke |
2:7b2b268ea49c | 81 | |
Mahesh Phalke |
2:7b2b268ea49c | 82 | def perform_calibration(): |
Mahesh Phalke |
2:7b2b268ea49c | 83 | |
Mahesh Phalke |
2:7b2b268ea49c | 84 | if (demo_config == "Power Test"): |
Mahesh Phalke |
2:7b2b268ea49c | 85 | # Power test uses internal ADC channels, on which calibration can't be performed |
Mahesh Phalke |
2:7b2b268ea49c | 86 | print("Invalid demo mode config. Calibration can't be performed on internal ADC channels!!") |
Mahesh Phalke |
2:7b2b268ea49c | 87 | return |
Mahesh Phalke |
2:7b2b268ea49c | 88 | |
Mahesh Phalke |
2:7b2b268ea49c | 89 | # Select calibration type |
Mahesh Phalke |
2:7b2b268ea49c | 90 | calibration_type = input("\r\nSelect Calibration Type:\r\n\ |
Mahesh Phalke |
2:7b2b268ea49c | 91 | {}. Internal Calibration\r\n\ |
Mahesh Phalke |
2:7b2b268ea49c | 92 | {}. System Calibration\r\n".format(internal_calibration, system_calibration)) |
Mahesh Phalke |
2:7b2b268ea49c | 93 | if (calibration_type > system_calibration): |
Mahesh Phalke |
2:7b2b268ea49c | 94 | print("Invalid Input!!") |
Mahesh Phalke |
2:7b2b268ea49c | 95 | return |
Mahesh Phalke |
2:7b2b268ea49c | 96 | |
Mahesh Phalke |
2:7b2b268ea49c | 97 | # Perform calibration for all channels |
Mahesh Phalke |
2:7b2b268ea49c | 98 | for chn in device.channel: |
Mahesh Phalke |
2:7b2b268ea49c | 99 | chn_index = chn_mappping[chn.name] |
Mahesh Phalke |
2:7b2b268ea49c | 100 | print("-------------------------------------------") |
Mahesh Phalke |
2:7b2b268ea49c | 101 | print("Calibrating channel {} ".format(chn_index)) |
Mahesh Phalke |
2:7b2b268ea49c | 102 | |
Mahesh Phalke |
2:7b2b268ea49c | 103 | if (calibration_type == system_calibration): |
Mahesh Phalke |
2:7b2b268ea49c | 104 | # Perform zero-scale (offset) system calibration |
Mahesh Phalke |
2:7b2b268ea49c | 105 | val = input("Apply zero-scale voltage between {} and press enter".format(ain_mapping[demo_config][chn_index])) |
Mahesh Phalke |
2:7b2b268ea49c | 106 | chn.system_calibration = "start_calibration" |
Mahesh Phalke |
2:7b2b268ea49c | 107 | sleep(short_time) |
Mahesh Phalke |
2:7b2b268ea49c | 108 | get_calibration_status(calibration_type, chn, chn_index) |
Mahesh Phalke |
2:7b2b268ea49c | 109 | print("Offset (before calibration): 0x{}".format(offset_before_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 110 | print("Offset (after calibration): 0x{}".format(offset_after_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 111 | if (calibration_status == "calibration_done"): |
Mahesh Phalke |
2:7b2b268ea49c | 112 | print("System offset calibration successfull..\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 113 | else: |
Mahesh Phalke |
2:7b2b268ea49c | 114 | print("System offset calibration failed!!\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 115 | |
Mahesh Phalke |
2:7b2b268ea49c | 116 | # Perform full-scale (gain) system calibration |
Mahesh Phalke |
2:7b2b268ea49c | 117 | val = input("Apply full-scale voltage between {} and press enter".format(ain_mapping[demo_config][chn_index])) |
Mahesh Phalke |
2:7b2b268ea49c | 118 | chn.system_calibration = "start_calibration" |
Mahesh Phalke |
2:7b2b268ea49c | 119 | sleep(short_time) |
Mahesh Phalke |
2:7b2b268ea49c | 120 | get_calibration_status(calibration_type, chn, chn_index) |
Mahesh Phalke |
2:7b2b268ea49c | 121 | print("Gain (before calibration): 0x{}".format(gain_before_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 122 | print("Gain (after calibration): 0x{}".format(gain_after_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 123 | if (calibration_status == "calibration_done"): |
Mahesh Phalke |
2:7b2b268ea49c | 124 | print("System gain calibration successfull..\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 125 | else: |
Mahesh Phalke |
2:7b2b268ea49c | 126 | print("System gain calibration failed!!\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 127 | else: |
Mahesh Phalke |
2:7b2b268ea49c | 128 | # Perform full-scale (gain) internal calibration |
Mahesh Phalke |
2:7b2b268ea49c | 129 | chn.internal_calibration = "start_calibration" |
Mahesh Phalke |
2:7b2b268ea49c | 130 | sleep(short_time) |
Mahesh Phalke |
2:7b2b268ea49c | 131 | get_calibration_status(calibration_type, chn, chn_index) |
Mahesh Phalke |
2:7b2b268ea49c | 132 | print("Gain (before calibration): 0x{}".format(gain_before_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 133 | print("Gain (after calibration): 0x{}".format(gain_after_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 134 | if (calibration_status == "calibration_done"): |
Mahesh Phalke |
2:7b2b268ea49c | 135 | print("Internal gain calibration successfull..\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 136 | elif (calibration_status == "calibration_skipped"): |
Mahesh Phalke |
2:7b2b268ea49c | 137 | print("Internal gain calibration skipped due to PGA=1\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 138 | else: |
Mahesh Phalke |
2:7b2b268ea49c | 139 | print("Internal gain calibration failed..\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 140 | |
Mahesh Phalke |
2:7b2b268ea49c | 141 | # Perform zero-scale (offset) internal calibration |
Mahesh Phalke |
2:7b2b268ea49c | 142 | chn.internal_calibration = "start_calibration" |
Mahesh Phalke |
2:7b2b268ea49c | 143 | sleep(short_time) |
Mahesh Phalke |
2:7b2b268ea49c | 144 | get_calibration_status(calibration_type, chn, chn_index) |
Mahesh Phalke |
2:7b2b268ea49c | 145 | print("Offset (before calibration): 0x{}".format(offset_before_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 146 | print("Offset (after calibration): 0x{}".format(offset_after_calib)) |
Mahesh Phalke |
2:7b2b268ea49c | 147 | if (calibration_status == "calibration_done"): |
Mahesh Phalke |
2:7b2b268ea49c | 148 | print("Internal offset calibration successfull..\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 149 | else: |
Mahesh Phalke |
2:7b2b268ea49c | 150 | print("Internal offset calibration failed!!\r\n") |
Mahesh Phalke |
2:7b2b268ea49c | 151 | |
Mahesh Phalke |
2:7b2b268ea49c | 152 | def exit(): |
Mahesh Phalke |
2:7b2b268ea49c | 153 | global device |
Mahesh Phalke |
2:7b2b268ea49c | 154 | |
Mahesh Phalke |
2:7b2b268ea49c | 155 | # Delete the objects |
Mahesh Phalke |
2:7b2b268ea49c | 156 | del device |
Mahesh Phalke |
2:7b2b268ea49c | 157 | |
Mahesh Phalke |
2:7b2b268ea49c | 158 | def main(): |
Mahesh Phalke |
2:7b2b268ea49c | 159 | init_calibration() |
Mahesh Phalke |
2:7b2b268ea49c | 160 | perform_calibration() |
Mahesh Phalke |
2:7b2b268ea49c | 161 | exit() |
Mahesh Phalke |
2:7b2b268ea49c | 162 | |
Mahesh Phalke |
2:7b2b268ea49c | 163 | if __name__ == "__main__": |
Mahesh Phalke |
2:7b2b268ea49c | 164 | main() |