From 5d20d50822527414d8da114dc98dbb8bf972788d Mon Sep 17 00:00:00 2001 From: Gertjan Date: Tue, 17 Mar 2026 12:33:19 +0100 Subject: [PATCH] Interference avoidance is tested and working (on-desk) --- .gitignore | 1 + scanner.py | 88 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 21d0b89..fa1a06e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .venv/ +config.yaml \ No newline at end of file diff --git a/scanner.py b/scanner.py index 153c6ae..4b2e645 100644 --- a/scanner.py +++ b/scanner.py @@ -6,7 +6,7 @@ import re import telnetlib3 import csv import time -import pyodbc +import pyodbc # XXXDB import math from datetime import datetime import yaml @@ -14,24 +14,21 @@ import os data_rows = [] # global 2-D list state = {} +blacklist = [] +ALLOWED_NOISE_LEVEL = 100.0 PATTERN = re.compile( - r"Va=(?P-?\d+\.?\d*)\s+" - r"Vp=(?P-?\d+\.?\d*)\s+\|\s+" - r"Ia=(?P-?\d+\.?\d*)\s+" - r"Ip=(?P-?\d+\.?\d*)\s+\|\s+" - r"ZR=(?P-?\d+\.?\d*)\s+" - r"ZX=(?P-?\d+\.?\d*)" - r".*?irq=\d+\s+" # Skip to 'irq=', match the first digits and space - r"(?P[0-9a-fA-F]+)-" # First hex - r"(?P[0-9a-fA-F]+)\s+" # Second hex - r"(?P[0-9a-fA-F]+)-" # Third hex - r"(?P[0-9a-fA-F]+)" # Fourth hex - r"nv=(?P-?\d+\.?\d*)\s+" # voltage noise/interference - r"ni=(?P-?\d+\.?\d*)\s+" # current noise/interference + r"Va=(?P[\d.-]+)\s+Vp=(?P[\d.-]+)\s+\|\s+" + r"Ia=(?P[\d.-]+)\s+Ip=(?P[\d.-]+)\s+\|\s+" + r"ZR=(?P[\d.-]+)\s+ZX=(?P[\d.-]+).*?\|\s+" # Skips R=... + r".*?irq=\w+\s+" # Skips the first irq value (59) + r"(?P[0-9a-fA-F]+)-(?P[0-9a-fA-F]+)\s+" + r"(?P[0-9a-fA-F]+)-(?P[0-9a-fA-F]+).*?\|\s+" + r"nv=(?P[\d.-]+)\s+ni=(?P[\d.-]+)" ) -config_path = '/home/bart/python-scanner/config.yaml' +config_path = '/home/bart/python-scanner/config.yaml' # XXXDB +#config_path = 'config.yaml' def load_yaml_config(state_dict): if os.path.exists(config_path): @@ -200,18 +197,20 @@ class TelnetReader: except KeyboardInterrupt: print("Interrupted by user.") print(data_rows) - if state["freq"]>0.00001: # check if scan endeed normally (e.g. no abort due to clipping) - #dump_csv("measurements.csv") - dump_into_database() - #append_line_to_file(0, "scan_freq.csv") - #append_line_to_file(1, "scan_Va.csv") - #append_line_to_file(2, "scan_Vp.csv") - #append_line_to_file(3, "scan_Ia.csv") - #append_line_to_file(4, "scan_Ip.csv") - #append_line_to_file(5, "scan_ZR.csv") - #append_line_to_file(6, "scan_ZX.csv") - #append_Nyquist_run("scan_Nyquist.csv") - #state["noise_scan"] = not state["noise_scan"] + if state["freq"]>0.00001: # check if scan ended normally (e.g. no abort due to clipping) + if False: + dump_into_database() + else: + dump_csv("measurements.csv") + #append_line_to_file(0, "scan_freq.csv") + #append_line_to_file(1, "scan_Va.csv") + #append_line_to_file(2, "scan_Vp.csv") + #append_line_to_file(3, "scan_Ia.csv") + #append_line_to_file(4, "scan_Ip.csv") + #append_line_to_file(5, "scan_ZR.csv") + #append_line_to_file(6, "scan_ZX.csv") + #append_Nyquist_run("scan_Nyquist.csv") + #state["noise_scan"] = not state["noise_scan"] else: print("scan aborted\r") gc.collect() # clean up internal memory (garbage collect) @@ -240,11 +239,14 @@ class TelnetReader: # there will be a lot of lines, but they will be skipped as they do not match the pattern else: # regular loop (not initializing) extract_to_dataframe(line) # capture the measurement + if data_rows and (data_rows[-1][11]>ALLOWED_NOISE_LEVEL or data_rows[-1][12]>ALLOWED_NOISE_LEVEL): + # Too much noise: add to blacklist + print("Too much noise - adding to blacklist") + blacklist.append({"Freq": state["freq"], "NrToSkip": 10}) + print(f"Blacklist: {blacklist}\r") + del data_rows[-1] # remove this last entry from the list (for DB it is okay, but for CSV things will shift) # print(f"minmax: {data_rows[7]},{data_rows[8]},{data_rows[9]},{data_rows[10] }\n") - if data_rows and (data_rows[-1][7]<5 or data_rows[-1][8]>250 or data_rows[-1][9]<5 or data_rows[-1][10]>250): - #response = f"a{state["ampl"]:.1f}\r" # send ampl to trigger sweep - #self.tn.write(response.encode("utf-8")) - # there will be a lot of lines, but they will be skipped as they do not match the pattern + if data_rows and (data_rows[-1][7]<5 or data_rows[-1][8]>250 or data_rows[-1][9]<5 or data_rows[-1][10]>250): # XXXDB state["freq"] = 0 # force ending of the scan, and write no data in the database # calculate next freq if state["freq"] > 1.0: @@ -255,13 +257,17 @@ class TelnetReader: else: freq_multiplier = state["freq_step_multiply"] ** 4 # skip 3/4 steps to speed up state["freq"] *= freq_multiplier - if (state["freq"]>40) and (state["freq"]<660) and ((state["freq"]%50<2.5) or (-state["freq"]%50<2.5)): - state["freq"] *= freq_multiplier # if near a 50Hz harmonic, skip to the next frequency - while (state["freq"]<0.6) and ((state["freq"]%state["interference_freq"]40) and (state["freq"]<660) and ((state["freq"]%50<2.5) or (-state["freq"]%50<2.5)): +# state["freq"] *= freq_multiplier # if near a 50Hz harmonic, skip to the next frequency +# while (state["freq"]<0.6) and ((state["freq"]%state["interference_freq"] 0.01: response = f"\rf{state["freq"]:.1f}\r" # new freq else: @@ -272,9 +278,9 @@ class TelnetReader: bandwidth = 0.5 response = f"b{bandwidth:.3f}\r" # new freq self.tn.write(response.encode("utf-8")) - state["remaining_receive_lines"] = 1 + 4/bandwidth + state["remaining_receive_lines"] = 2 + 4/bandwidth print(line) - print(state) +# print(state) state["initializing"] = 0 # Example: @@ -292,7 +298,7 @@ class TelnetReader: if __name__ == "__main__": reader = TelnetReader(host="localhost", port=2002) # reader = TelnetReader(host="10.1.122.152", port=2002) -# reader = TelnetReader(host="192.168.1.196", port=2002) +# reader = TelnetReader(host="192.168.1.235", port=2002) try: reader.connect() @@ -306,6 +312,12 @@ if __name__ == "__main__": state["initializing"] = 1; reader.read_loop() + # decrement all freq's in blacklist: + print(f"Before decrementing: {blacklist}") + for item in blacklist: + item["NrToSkip"] -= 1 + blacklist = [item for item in blacklist if item["NrToSkip"] != 0] # remove all zero items + print(f"After removing zeros: {blacklist}") finally: reader.disconnect()