|
| 1 | +#!/usr/bin/env python |
| 2 | +#------------------------------------------------------------ |
| 3 | +# FILE: DIY2TankSensorCalibrate.py |
| 4 | +# PURPOSE: Get calibration values for the Infinieon TLE5501 E0001 TMR-based angle sensor |
| 5 | +# used in gentankdiy2.py and write them in gentankdiy2.conf file. |
| 6 | +# |
| 7 | +# AUTHOR: Curtis Case @curtis1757 |
| 8 | +# DATE: 10-Oct-2020 |
| 9 | +# Free software. Use at your own risk. |
| 10 | +# MODIFICATIONS: |
| 11 | +#------------------------------------------------------------ |
| 12 | +import time, sys, os, getopt, math |
| 13 | + |
| 14 | +# This program assumes the directory genmonlib is one level higher |
| 15 | +sys.path.append(os.path.dirname(sys.path[0])) # Adds higher directory to python modules path. |
| 16 | + |
| 17 | +try: |
| 18 | + from genmonlib.myconfig import MyConfig |
| 19 | + from genmonlib.gaugediy import GaugeDIY2 |
| 20 | + from genmonlib.mylog import SetupLogger |
| 21 | + from genmonlib.mycommon import MyCommon |
| 22 | + |
| 23 | +except Exception as e1: |
| 24 | + print("\n\nThis program requires the modules located in the genmonlib directory in the github repository.\n") |
| 25 | + print("Please see the project documentation at https://github.com/jgyates/genmon.\n") |
| 26 | + print("Error: " + str(e1)) |
| 27 | + sys.exit(2) |
| 28 | + |
| 29 | +#---------------------input_float----------------------------------------------- |
| 30 | +def input_float(prompt, ndec = 2): |
| 31 | + """Get a float number from the user""" |
| 32 | + |
| 33 | + while True: |
| 34 | + try: |
| 35 | + s = input(prompt) |
| 36 | + v = float(s) |
| 37 | + |
| 38 | + return round(v, ndec) |
| 39 | + except Exception as e1: |
| 40 | + if s == "": return None |
| 41 | + print("{0}, try again".format(e1)) |
| 42 | + |
| 43 | +#---------------------check_ap_table-------------------------------------------- |
| 44 | +def check_ap_table(): |
| 45 | + """Checks to mke sure the angle-percentage table is linear""" |
| 46 | + |
| 47 | + if gauge.ap_table != None and len(gauge.ap_table) > 2: |
| 48 | + increasing = gauge.ap_table[0][0] < gauge.ap_table[-1][0] |
| 49 | + for i in range(1,len(gauge.ap_table)): |
| 50 | + if (increasing and gauge.ap_table[i-1][0] < gauge.ap_table[i][0]) or \ |
| 51 | + (not increasing and gauge.ap_table[i-1][0] < gauge.ap_table[i][0]): |
| 52 | + |
| 53 | + print(("error in calibration table at row {0}, {1:.2f}" + |
| 54 | + " is {2} than {3:.2f}\n" + |
| 55 | + "value not added to calibration table.").format(i, gauge.ap_table[i-1][0], 'greater' if increasing else 'less', gauge.ap_table[i][0])) |
| 56 | + |
| 57 | + guage.ap_table.pop(i) |
| 58 | + |
| 59 | +#---------------------show_ap_table--------------------------------------------- |
| 60 | +def show_ap_table(): |
| 61 | + if gauge.ap_table != None and len(gauge.ap_table) > 0: |
| 62 | + i = 1 |
| 63 | + print("{0:>6s}{1:>8s}{2:>8s}".format('Index', 'Angle', 'Percent')) |
| 64 | + for a, p in gauge.ap_table: |
| 65 | + print("{0:6d}{1:8.2f}{2:8.1f}".format(i, a, p)) |
| 66 | + i+=1 |
| 67 | + else: |
| 68 | + print("calibration table is empty") |
| 69 | + |
| 70 | +#---------------------main------------------------------------------------------ |
| 71 | +if __name__=='__main__': |
| 72 | + HelpStr = """python DIY2TankSensorCalibrate.py [OPTIONS] ... |
| 73 | +
|
| 74 | +Usage: python DIY2TankSensorCalibrate.py [OPTION] ... |
| 75 | +
|
| 76 | +Gets information to modify 'gentankDIY2.conf' file |
| 77 | +
|
| 78 | +Valid [OPTIONS] are: |
| 79 | + -c <config file path> |
| 80 | +""" |
| 81 | + try: |
| 82 | + console = SetupLogger("DIY2_console", log_file = "", stream = True) |
| 83 | + opts, args = getopt.getopt(sys.argv[1:],"hc:",["help","config="]) |
| 84 | + |
| 85 | + except getopt.GetoptError: |
| 86 | + print (HelpStr) |
| 87 | + sys.exit(2) |
| 88 | + |
| 89 | + configfilename = "gentankdiy.conf" |
| 90 | + configfile = os.path.join(MyCommon.DefaultConfPath, configfilename) |
| 91 | + |
| 92 | + for opt, arg in opts: |
| 93 | + if opt == '-h': |
| 94 | + print (HelpStr) |
| 95 | + sys.exit() |
| 96 | + elif opt in ("-c"): |
| 97 | + configfile = os.path.join(arg, configfilename) |
| 98 | + |
| 99 | + |
| 100 | + if not os.path.isfile(configfile): |
| 101 | + console.error("Missing config file: {0}".format(configfile)) |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | + console.error("Using config file %s" % configfile) |
| 105 | + |
| 106 | + config = MyConfig(filename = configfile, section = 'gentankdiy') |
| 107 | + |
| 108 | + gauge = GaugeDIY2(config, console = console, log = console) |
| 109 | + |
| 110 | + if not gauge.InitADC(): |
| 111 | + console.error("Error initializing ADC, exiting") |
| 112 | + exit(2) |
| 113 | + |
| 114 | + average_n = 10 |
| 115 | + |
| 116 | + while True: |
| 117 | + print(""" |
| 118 | +Enter command: |
| 119 | +1 - Show current calibration table |
| 120 | +2 - Clear all entries from calibration table |
| 121 | +3 - Enter lowest and highest readings possible |
| 122 | +4 - Add new entry(s) to calibration table |
| 123 | +5 - Delete entry(s) in calibration table |
| 124 | +6 - Save '{configfile}' |
| 125 | +7 - Show current dial reading using current calibration table |
| 126 | +0 - exit |
| 127 | +""") |
| 128 | + c = str(input("Command: ")).strip() |
| 129 | + |
| 130 | + if c == '1': |
| 131 | + show_ap_table() |
| 132 | + |
| 133 | + elif c == '2': |
| 134 | + gauge.ap_table.clear() |
| 135 | + |
| 136 | + elif c == '3': |
| 137 | + p = input_float("Set dial as low as possible and enter the percent value (typically 5%)\n" + |
| 138 | + "when enter is pressed after the value is entered the dial angle will be read and recorded: ") |
| 139 | + if p != None: |
| 140 | + a = gauge.GetAvgGaugeAngle(average_n, 1.0, True) |
| 141 | + gauge.ap_table = [(a,p)] |
| 142 | + |
| 143 | + p = input_float("Set dial as high as possible and enter the percent value (typically 100%)\n" + |
| 144 | + "when enter is pressed after the value is entered the dial angle will be read and recorded: ") |
| 145 | + if p != None: |
| 146 | + if p < gauge.ap_table[0][1]: |
| 147 | + print("Error: the low entry must be less than the high entry, {0:.1f} is not less than {1:.1f}".format(gauge.ap_table[0][1]), p) |
| 148 | + else: |
| 149 | + a = gauge.GetAvgGaugeAngle(average_n, 1.0, True) |
| 150 | + gauge.ap_table.append((a,p)) |
| 151 | + |
| 152 | + print() |
| 153 | + show_ap_table() |
| 154 | + print("Next use option 4 to add calibration points between {0:.1f} and {1:.1f}".format(gauge.ap_table[0][1], gauge.ap_table[1][1])) |
| 155 | + print("suggested to use major markings on dial, ie 10, 20, 30, 40, 50, 60, 70") |
| 156 | + |
| 157 | + elif c == '4': |
| 158 | + if gauge.ap_table == None or len(gauge.ap_table) < 2: |
| 159 | + print("must use option 3 first to set lowest and highest dial values") |
| 160 | + else: |
| 161 | + if len(gauge.ap_table) >= gauge.MAX_AP_TABLE: |
| 162 | + print("already maximum of {0} entries, delete some (5) or all (2)".format(gauge.MAX_AP_TABLE)) |
| 163 | + else: |
| 164 | + while len(gauge.ap_table) < gauge.MAX_AP_TABLE: |
| 165 | + while True: |
| 166 | + p = input_float("Set dial to desired value to read the associated angle for\n" + |
| 167 | + "(or just 'Return' to stop entering values). When enter is pressed \n" + |
| 168 | + "after a value is entered the dial angle will be read and recorded: ") |
| 169 | + if p == None: break |
| 170 | + if p < 0.0 or p > 100.0: |
| 171 | + print("value must be between 0 and 100") |
| 172 | + else: |
| 173 | + break |
| 174 | + |
| 175 | + if p == None: break |
| 176 | + |
| 177 | + a = gauge.GetAvgGaugeAngle(average_n, 1.0, True) |
| 178 | + gauge.ap_table.append((a,p)) |
| 179 | + |
| 180 | + gauge.ap_table.sort(key = lambda x: x[1]) |
| 181 | + |
| 182 | + check_ap_table() |
| 183 | + |
| 184 | + print("\nadded angle {0:.2f} to represent dial value {1:.1f}% to calibration table\n".format(a, p)) |
| 185 | + |
| 186 | + elif c == '5': |
| 187 | + show_ap_table() |
| 188 | + if gauge.ap_table != None and len(gauge.ap_table) > 0: |
| 189 | + while True: |
| 190 | + i = input_float("Enter Index of entry to be deleted:") |
| 191 | + if i == None: break |
| 192 | + if i >= 1 and i <= len(gauge.ap_table): |
| 193 | + gauge.ap_table.pop(i-1) |
| 194 | + print("updated calibration table:") |
| 195 | + show_ap_table() |
| 196 | + else: |
| 197 | + print("Index must be from 1 to {0}, try again".format(len(gauge.ap_table))) |
| 198 | + |
| 199 | + elif c == '6': |
| 200 | + if gauge.ap_table != None: |
| 201 | + if len(gauge.ap_table) < 2: |
| 202 | + print("must have at least 2 entries in calibration table, file not written") |
| 203 | + else: |
| 204 | + for i in range(1,gauge.MAX_AP_TABLE+1): |
| 205 | + config.WriteValue("ang_pct_pnt_{0}".format(i), "", remove = True) |
| 206 | + |
| 207 | + i = 1 |
| 208 | + for a, p in gauge.ap_table: |
| 209 | + config.WriteValue("ang_pct_pnt_{0}".format(i), "{0:8.2f},{1:6.1f}".format(a, p)) |
| 210 | + i += 1 |
| 211 | + |
| 212 | + print("wrote {0} calibration entries in '{1}'".format(len(gauge.ap_table), configfile)) |
| 213 | + |
| 214 | + elif c == '7': |
| 215 | + a = gauge.read_gauge_angle() |
| 216 | + print("Current dial angle is {0:.2f} degrees which is {1:.1f}%".format(a, gauge.convert_angle_to_percent(a))) |
| 217 | + |
| 218 | + elif c == '0': |
| 219 | + sys.exit(0) |
| 220 | + else: |
| 221 | + print("unknown command '{0}'".format(c)) |
| 222 | + |
| 223 | + sys.exit(1) |
0 commit comments