diff --git a/scanner.py b/scanner.py index f36a1a9..dea21da 100644 --- a/scanner.py +++ b/scanner.py @@ -25,7 +25,9 @@ PATTERN = re.compile( 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.-]+)" + r"QZR=(?P[\d.-]+)\s+QZX=(?P[\d.-]+)" ) +Va=16.376152 Vp=5.82 | Ia=30.656820 Ip=5.84 | ZR=0.534176 ZX=-0.000190 R=516.000 | freq=1000.0 ampl=100.0 | BW=0.500| irq=59 0c-7b 18-83 | nv=4.494 ni=8.487 | QZR=0.654321 QZX=12.123456 config_path = '/home/bart/python-scanner/config.yaml' # XXXDB #config_path = 'config.yaml' @@ -146,10 +148,9 @@ def extract_to_dataframe(line): int(match.group("adc_imax"), 16), # row 10 float(match.group("nv")), # row 11 float(match.group("ni")), # row 12 + float(match.group("QZR")), # row 13 + float(match.group("QZX")), # row 14 ] - row[1] = row[1]/(2*3.14159*row[0]*0.00005 + 1) # compensate Va for pole at 20kHz - row[5] = row[1]/row[3] * math.cos(0.01745*(row[2]-row[4])) - row[6] = row[1]/row[3] * math.sin(0.01745*(row[2]-row[4])) data_rows.append(row) def process_line(line): @@ -161,7 +162,7 @@ def process_line(line): def get_dataframe(): return pd.DataFrame( data_rows, - columns=["Va", "Vp", "Ia", "Ip", "ZR", "ZX", "adc_vmin", "adc_vmax", "adc_imin", "adc_imax", "nv", "ni"] + columns=["Va", "Vp", "Ia", "Ip", "QZR", "QZX", "adc_vmin", "adc_vmax", "adc_imin", "adc_imax", "nv", "ni"] ) class TelnetReader: @@ -218,73 +219,56 @@ class TelnetReader: def process_line(self, line): global state global Inoise_baseline -# print(f"RAW: {line}") + print(line) if not line.startswith("Va="): # skip lines that are not to be analyzed return - if state["remaining_receive_lines"] > 0 : - state["remaining_receive_lines"] -= 1 # we are waiting for settling - just skip the line - else: - # prepare new frequency measurement - if state["initializing"] == 1: - # since we just start a frequency scan, let's set the R, Max and Amplitude - response = f"\rr516\r" # set Resistance value for scaling HAL sensor - self.tn.write(response.encode("utf-8")) - response = f"m1600\r" # set max amplitude value - self.tn.write(response.encode("utf-8")) - # for noise scans the amplitude is zero, else the state["ampl"] - if state["noise_scan"]==True: - response = f"a0\r" # zero amplitude for noise measurement - else: - response = f"a{state["ampl"]:.1f}\r" # set amplitude - 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 - else: # regular loop (not initializing) - extract_to_dataframe(line) # capture the measurement - if state["freq"]>3.0: - Inoise_baseline = 0.8*Inoise_baseline + 0.2*data_rows[-1][12] # remember last baseline around 3Hz (assuming top-down scanning) - if data_rows and (data_rows[-1][11]>state["allowed_noise_level"] or data_rows[-1][12]>state["allowed_noise_level"] or (state["freq"]<3.0 and data_rows[-1][12]>1.25*Inoise_baseline)): - # Too much noise: add to blacklist - print("Too much noise - adding to blacklist") - blacklist.append({"Freq": state["freq"], "NrToSkip": state["skip_scans_for_noisy_freqs"]}) - 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) - else: - 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: - freq_multiplier = state["freq_step_multiply"] # calc next freq - else: - if state["freq"] > 0.01: - freq_multiplier = state["freq_step_multiply"] ** 2 # skip 1/2 steps to speed up - 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"]3.0: + Inoise_baseline = 0.8*Inoise_baseline + 0.2*data_rows[-1][12] # remember last baseline around 3Hz (assuming top-down scanning) + if data_rows and (data_rows[-1][11]>state["allowed_noise_level"] or data_rows[-1][12]>state["allowed_noise_level"] or (state["freq"]<3.0 and data_rows[-1][12]>1.25*Inoise_baseline)): + # Too much noise: add to blacklist + print("Too much noise - adding to blacklist") + blacklist.append({"Freq": state["freq"], "NrToSkip": state["skip_scans_for_noisy_freqs"]}) + 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) + else: + 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: + freq_multiplier = state["freq_step_multiply"] # calc next freq else: - # program the health monitor to go to the next frequency: if state["freq"] > 0.01: - response = f"\rf{state["freq"]:.1f}\r" # new freq + freq_multiplier = state["freq_step_multiply"] ** 2 # skip 1/2 steps to speed up else: - response = f"\rf{state["freq"]:.3f}\r" # new freq - self.tn.write(response.encode("utf-8")) - bandwidth = state["freq"]/20 - if bandwidth > 0.5 : - bandwidth = 0.5 - response = f"b{bandwidth:.3f}\r" # new freq - self.tn.write(response.encode("utf-8")) - state["remaining_receive_lines"] = 2 + 4/bandwidth - print(line) -# print(state) - state["initializing"] = 0 + freq_multiplier = state["freq_step_multiply"] ** 4 # skip 3/4 steps to speed up + state["freq"] *= freq_multiplier + while (any(item["Freq"] == state["freq"] for item in blacklist)): + print("skipping freq") + state["freq"] *= freq_multiplier # skip all frequencies that are blacklisted + if state["freq"] <= state["stop_freq"]: + print("Reached stop frequency") + else: + # program the health monitor to go to the next frequency: + response = f"\rQ{state["freq"]:.3f}\r" # new freq + self.tn.write(response.encode("utf-8")) +# print(state) + state["initializing"] = 0 # Example: # value = self.extract_value(line) @@ -311,7 +295,6 @@ if __name__ == "__main__": load_yaml_config(state) # Load configuration settings state["freq"] = state["start_freq"]; - state["remaining_receive_lines"] = 0; state["initializing"] = 1; reader.read_loop()