This commit is contained in:
Vincent Hanewinkel 2025-08-14 23:37:07 +02:00
parent 72df5f7691
commit e2b8a4afcb

View File

@ -1,4 +1,5 @@
import os, sys, time, csv, statistics, threading, queue
import numpy as np
scriptDir = os.path.dirname(os.path.realpath(__file__))
DEFAULT_OUTDIR = os.path.join(scriptDir, "logs")
@ -41,10 +42,10 @@ class DeviceWorker:
def _reader_loop(self):
print(f"[{self.serial}] Reader gestartet (interval={self.interval}, n={self.filter_window_size})")
filter_window_size = 10
interval = 0.1
last_log = 0.0
interval = self.interval
last_log = -999.0
current_direction = 1 # falls du das brauchst wie im Single-Logger
last_log = 0.0
while not self._stop_evt.is_set():
if not self._running:
time.sleep(0.05); continue
@ -57,12 +58,14 @@ class DeviceWorker:
continue
# WICHTIG: gleiche Indizes wie in deinem Logger!
raw_voltage = float(np.mean([s[1][0] for s in samples])) # VB
raw_current = float(np.mean([s[0][1] for s in samples])) * current_direction # IA
vb = float(np.mean([s[1][0] for s in samples])) # Spannung B
ia = float(np.mean([s[0][1] for s in samples]))
print(f"[{self.serial}] got {len(samples)} samples; vb={vb:.3f}, ia={ia:.3f}")
now = time.time()
if now - last_log >= 1.0: # 1 Hz loggen
self._writer_q.put((now, raw_voltage, raw_current))
self._writer_q.put((now, vb, ia))
last_log = now
except Exception as e:
@ -81,18 +84,18 @@ class DeviceWorker:
self._log_path = os.path.join(self.outdir, f"{time.strftime('%Y%m%d_%H%M%S')}_{self.serial}.csv")
print(f"[{self.serial}] Writer startet → {self._log_path}")
try:
with open(self._log_path, "w", newline="") as f:
w = csv.writer(f)
w.writerow(["timestamp", "A", "B"]) # Datei wird JETZT angelegt
while not (self._stop_evt.is_set() and self._writer_q.empty()):
try:
ts, vA, vB = self._writer_q.get(timeout=0.5)
except queue.Empty:
continue
w.writerow([ts, vA, vB])
written = 0
with open(self._log_path, "w", newline="") as f:
w = csv.writer(f)
w.writerow(["timestamp", "VB", "IA"]) # Datei JETZT sichtbar
while not (self._stop_evt.is_set() and self._writer_q.empty()):
try:
ts, vb, ia = self._writer_q.get(timeout=0.5)
except queue.Empty:
continue
w.writerow([ts, vb, ia])
written += 1
if written % 5 == 0:
f.flush()
except Exception as e:
print(f"[{self.serial}] Writer-Fehler ({self._log_path}): {e}")
finally:
print(f"[{self.serial}] Writer beendet: {self._log_path}")
print(f"[{self.serial}] rows written: {written}")
print(f"[{self.serial}] Writer beendet: {self._log_path}")