29 Commits

Author SHA1 Message Date
Jan
451afb1a23 MainCode/adalm1000_logger.py aktualisiert
# -*- coding: utf-8 -*-
import os
import time
import csv
import threading
from datetime import datetime
import numpy as np
import matplotlib
matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from collections import deque
from queue import Queue, Full, Empty

from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QGridLayout, QLabel, 
                             QPushButton, QLineEdit, QCheckBox, QFrame, QMessageBox, QFileDialog)
from PyQt5.QtCore import Qt, QTimer, pyqtSignal, pyqtSlot, QObject, QThread
import pysmu

class DeviceDisconnectedError(Exception):
    pass

class MeasurementThread(QThread):
    update_signal = pyqtSignal(float, float, float)
    error_signal = pyqtSignal(str)

    def __init__(self, device, interval=0.1):
        super().__init__()
        self.device = device
        self.interval = interval
        self._running = False
        self.filter_window_size = 10
        self.voltage_window = []
        self.current_window = []
        self.start_time = time.time()
        self.measurement_queue = Queue(maxsize=1)

    def run(self):
        self._running = True
        while self._running:
            try:
                samples = self.device.read(self.filter_window_size, 500, True)
                if not samples:
                    raise DeviceDisconnectedError("No samples received")
                
                current_time = time.time() - self.start_time
                
                # Get voltage from Channel B (HI_Z mode) and current from Channel A
                raw_voltage = np.mean([s[1][0] for s in samples])  # Channel B voltage
                raw_current = np.mean([s[0][1] for s in samples])  # Channel A current
                
                # Update filter windows
                self.voltage_window.append(raw_voltage)
                self.current_window.append(raw_current)
                
                if len(self.voltage_window) > self.filter_window_size:
                    self.voltage_window.pop(0)
                    self.current_window.pop(0)
                
                voltage = np.mean(self.voltage_window)
                current = np.mean(self.current_window)
                
                # Emit update
                self.update_signal.emit(voltage, current, current_time)
                
                # Store measurement
                try:
                    self.measurement_queue.put_nowait((voltage, current))
                except Full:
                    pass
                
                time.sleep(max(0.05, self.interval))
                
            except Exception as e:
                self.error_signal.emit(f"Read error: {str(e)}")
                time.sleep(1)
                continue

    def stop(self):
        self._running = False
        self.wait(500)

class TestSequenceWorker(QObject):
    finished = pyqtSignal()
    update_phase = pyqtSignal(str)
    update_status = pyqtSignal(str)
    test_completed = pyqtSignal()
    error_occurred = pyqtSignal(str)
    
    def __init__(self, device, test_current, charge_cutoff, discharge_cutoff, rest_time, continuous_mode, parent):
        super().__init__()
        self.device = device
        self.test_current = test_current
        self.charge_cutoff = charge_cutoff
        self.discharge_cutoff = discharge_cutoff
        self.rest_time = rest_time * 3600  # Convert hours to seconds
        self.continuous_mode = continuous_mode
        self.parent = parent
        self._running = True
        self.voltage_timeout = 0.5  # seconds

    def get_latest_measurement(self):
        """Thread-safe measurement reading with timeout"""
        try:
            return self.parent.measurement_thread.measurement_queue.get(
                timeout=self.voltage_timeout
            )
        except Empty:
            return (None, None)  # Return tuple for unpacking

    def charge_phase(self):
        """Handle the battery charging phase"""
        self.update_phase.emit("Charge")
        self.update_status.emit(f"Charging to {self.charge_cutoff}V @ {self.test_current:.3f}A")
        
        try:
            # Configure channels - Channel A sources current, Channel B measures voltage
            self.device.channels['B'].mode = pysmu.Mode.HI_Z
            self.device.channels['A'].mode = pysmu.Mode.SIMV
            self.device.channels['A'].constant(self.test_current)
            
            # Small delay to allow current to stabilize
            time.sleep(0.1)
            
            while self._running:
                voltage, current = self.get_latest_measurement()
                if voltage is None:
                    continue
                    
                # Update parent's data for logging/display
                with self.parent.plot_mutex:
                    if len(self.parent.voltage_data) > 0:
                        self.parent.voltage_data[-1] = voltage
                        self.parent.current_data[-1] = current
                
                if voltage >= self.charge_cutoff:
                    break
                    
                time.sleep(0.1)
                
        finally:
            self.device.channels['A'].mode = pysmu.Mode.HI_Z
            self.device.channels['A'].constant(0)

    def discharge_phase(self):
        """Handle the battery discharging phase"""
        self.update_phase.emit("Discharge")
        self.update_status.emit(f"Discharging to {self.discharge_cutoff}V @ {self.test_current:.3f}A")
        
        try:
            # Configure channels - Channel A sinks current, Channel B measures voltage
            self.device.channels['B'].mode = pysmu.Mode.HI_Z
            self.device.channels['A'].mode = pysmu.Mode.SIMV
            self.device.channels['A'].constant(-self.test_current)
            
            # Small delay to allow current to stabilize
            time.sleep(0.1)
            
            while self._running:
                voltage, current = self.get_latest_measurement()
                if voltage is None:
                    continue
                    
                # Update parent's data for logging/display
                with self.parent.plot_mutex:
                    if len(self.parent.voltage_data) > 0:
                        self.parent.voltage_data[-1] = voltage
                        self.parent.current_data[-1] = current
                
                if voltage <= self.discharge_cutoff:
                    break
                    
                time.sleep(0.1)
                
        finally:
            self.device.channels['A'].mode = pysmu.Mode.HI_Z
            self.device.channels['A'].constant(0)
        
    def rest_phase(self, phase_name):
        """Handle rest period between phases"""
        self.update_phase.emit(f"Resting ({phase_name})")
        rest_end = time.time() + self.rest_time
        
        while time.time() < rest_end and self._running:
            time_left = max(0, rest_end - time.time())
            self.update_status.emit(f"Resting | Time left: {time_left/60:.1f} min")
            time.sleep(1)
            
    def stop(self):
        """Request the thread to stop"""
        self._running = False
        self.device.channels['A'].mode = pysmu.Mode.HI_Z
        self.device.channels['A'].constant(0)

    def run(self):
        """Main test sequence loop"""
        try:
            while self._running and (self.continuous_mode or self.parent.cycle_count == 0):
                # Reset stop request at start of each cycle
                self.parent.request_stop = False
                self.parent.cycle_count += 1

                # 1. Charge phase (constant current)
                self.charge_phase()
                if not self._running or self.parent.request_stop:
                    break
                    
                # 2. Rest period after charge
                self.rest_phase("Post-Charge")
                if not self._running or self.parent.request_stop:
                    break
                    
                # 3. Discharge phase (capacity measurement)
                self.discharge_phase()
                if not self._running or self.parent.request_stop:
                    break
                    
                # 4. Rest period after discharge (only if not stopping)
                if self._running and not self.parent.request_stop:
                    self.rest_phase("Post-Discharge")
                    
                # Calculate Coulomb efficiency if not stopping
                if not self.parent.request_stop and self.parent.charge_capacity > 0:
                    self.parent.coulomb_efficiency = (self.parent.capacity_ah / self.parent.charge_capacity) * 100
                    
            # Test completed
            self.test_completed.emit()
            
        except Exception as e:
            self.error_occurred.emit(f"Test sequence error: {str(e)}")
        finally:
            self.finished.emit()

class BatteryTester(QMainWindow):
    def __init__(self):
        self.plot_mutex = threading.Lock()
        super().__init__()

        # Color scheme
        self.bg_color = "#2E3440"
        self.fg_color = "#D8DEE9"
        self.accent_color = "#5E81AC"
        self.warning_color = "#BF616A"
        self.success_color = "#A3BE8C"
        
        # Device and measurement state
        self.session_active = False
        self.measuring = False
        self.test_running = False
        self.continuous_mode = False  
        self.request_stop = False  
        self.interval = 0.1  
        self.log_dir = os.path.expanduser("~/adalm1000/logs")
        os.makedirs(self.log_dir, exist_ok=True)
        
        # Data buffers
        self.time_data = deque()
        self.voltage_data = deque()
        self.current_data = deque()
        self.phase_data = deque()
        
        # Initialize UI and device
        self.setup_ui()
        self.init_device()
        
        # Set window properties
        self.setWindowTitle("ADALM1000 - Battery Capacity Tester (CC Test)")
        self.resize(1000, 800)
        self.setMinimumSize(800, 700)
        
        # Status update timer
        self.status_timer = QTimer()
        self.status_timer.timeout.connect(self.update_status)
        self.status_timer.start(1000)  # Update every second

    def setup_ui(self):
        """Configure the user interface"""
        # Main widget and layout
        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        self.main_layout = QVBoxLayout(self.central_widget)
        self.main_layout.setContentsMargins(10, 10, 10, 10)
        
        # Header area
        header_frame = QFrame()
        header_frame.setFrameShape(QFrame.NoFrame)
        header_layout = QHBoxLayout(header_frame)
        header_layout.setContentsMargins(0, 0, 0, 0)
        
        self.title_label = QLabel("ADALM1000 Battery Capacity Tester (CC Test)")
        self.title_label.setStyleSheet(f"font-size: 14pt; font-weight: bold; color: {self.accent_color};")
        header_layout.addWidget(self.title_label, 1)
        
        # Status indicator
        self.status_light = QLabel()
        self.status_light.setFixedSize(20, 20)
        self.status_light.setStyleSheet("background-color: red; border-radius: 10px;")
        header_layout.addWidget(self.status_light)
        
        self.connection_label = QLabel("Disconnected")
        header_layout.addWidget(self.connection_label)
        
        # Reconnect button
        self.reconnect_btn = QPushButton("Reconnect")
        self.reconnect_btn.clicked.connect(self.reconnect_device)
        header_layout.addWidget(self.reconnect_btn)
        
        self.main_layout.addWidget(header_frame)
        
        # Measurement display
        display_frame = QFrame()
        display_frame.setFrameShape(QFrame.StyledPanel)
        display_frame.setStyleSheet(f"QFrame {{ border: 1px solid {self.accent_color}; border-radius: 5px; }}")
        display_layout = QGridLayout(display_frame)
        
        # Measurement values
        measurement_labels = [
            ("Voltage", "V"),       ("Current", "A"),       ("Test Phase", ""),
            ("Elapsed Time", "s"),      ("Discharge Capacity", "Ah"), ("Charge Capacity", "Ah"),
            ("Coulomb Eff.", "%"),      ("Cycle Count", ""),        ("Battery Temp", "°C"),
            ("Internal R", "Ω"),        ("Power", "W"),             ("Energy", "Wh")
        ]

        # 4 Zeilen × 3 Spalten Anordnung
        for i, (label, unit) in enumerate(measurement_labels):
            row = i // 3  # 0-3 (4 Zeilen)
            col = (i % 3) * 3  # 0, 3, 6 (3 Spalten mit je 3 Widgets)
            
            # Label für den Messwertnamen
            lbl = QLabel(f"{label}:")
            lbl.setStyleSheet(f"color: {self.fg_color}; font-size: 11px;")
            display_layout.addWidget(lbl, row, col)
            
            # Label für den Messwert
            value_lbl = QLabel("0.000")
            value_lbl.setStyleSheet(f"""
                color: {self.fg_color}; 
                font-weight: bold; 
                font-size: 12px;
                min-width: 60px;
            """)
            display_layout.addWidget(value_lbl, row, col + 1)
            
            # Einheit falls vorhanden
            if unit:
                unit_lbl = QLabel(unit)
                unit_lbl.setStyleSheet(f"color: {self.fg_color}; font-size: 11px;")
                display_layout.addWidget(unit_lbl, row, col + 2)

        # Spaltenabstände anpassen
        for i in range(9):  # 3 Spalten × 3 Widgets
            display_layout.setColumnStretch(i, 1 if i % 3 == 1 else 0)  # Nur Wert-Spalten dehnen

        # Referenzen aktualisieren
        self.voltage_label = display_layout.itemAtPosition(0, 1).widget()
        self.current_label = display_layout.itemAtPosition(0, 4).widget()
        self.phase_label = display_layout.itemAtPosition(0, 7).widget()
        self.time_label = display_layout.itemAtPosition(1, 1).widget()
        self.capacity_label = display_layout.itemAtPosition(1, 4).widget()
        self.charge_capacity_label = display_layout.itemAtPosition(1, 7).widget()
        self.efficiency_label = display_layout.itemAtPosition(2, 1).widget()
        self.cycle_label = display_layout.itemAtPosition(2, 4).widget()
        self.temp_label = display_layout.itemAtPosition(2, 7).widget()
        self.resistance_label = display_layout.itemAtPosition(3, 1).widget()
        self.power_label = display_layout.itemAtPosition(3, 4).widget()
        self.energy_label = display_layout.itemAtPosition(3, 7).widget()
        
        self.main_layout.addWidget(display_frame)
        
        # Control area
        controls_frame = QFrame()
        controls_frame.setFrameShape(QFrame.NoFrame)
        controls_layout = QHBoxLayout(controls_frame)
        controls_layout.setContentsMargins(0, 0, 0, 0)
        
        # Parameters frame
        params_frame = QFrame()
        params_frame.setFrameShape(QFrame.StyledPanel)
        params_frame.setStyleSheet(f"QFrame {{ border: 1px solid {self.accent_color}; border-radius: 5px; }}")
        params_layout = QGridLayout(params_frame)
        
        # Battery capacity
        self.capacity = 0.2
        self.capacity_label_input = QLabel("Battery Capacity (Ah):")
        self.capacity_label_input.setStyleSheet(f"color: {self.fg_color};")
        params_layout.addWidget(self.capacity_label_input, 0, 0)
        self.capacity_input = QLineEdit("0.2")
        self.capacity_input.setStyleSheet(f"background-color: #3B4252; color: {self.fg_color};")
        self.capacity_input.setFixedWidth(60)
        params_layout.addWidget(self.capacity_input, 0, 1)
        
        # Charge cutoff
        self.charge_cutoff = 1.43
        self.charge_cutoff_label = QLabel("Charge Cutoff (V):")
        self.charge_cutoff_label.setStyleSheet(f"color: {self.fg_color};")
        params_layout.addWidget(self.charge_cutoff_label, 1, 0)
        self.charge_cutoff_input = QLineEdit("1.43")
        self.charge_cutoff_input.setStyleSheet(f"background-color: #3B4252; color: {self.fg_color};")
        self.charge_cutoff_input.setFixedWidth(60)
        params_layout.addWidget(self.charge_cutoff_input, 1, 1)
        
        # Discharge cutoff
        self.discharge_cutoff = 0.9
        self.discharge_cutoff_label = QLabel("Discharge Cutoff (V):")
        self.discharge_cutoff_label.setStyleSheet(f"color: {self.fg_color};")
        params_layout.addWidget(self.discharge_cutoff_label, 2, 0)
        self.discharge_cutoff_input = QLineEdit("0.9")
        self.discharge_cutoff_input.setStyleSheet(f"background-color: #3B4252; color: {self.fg_color};")
        self.discharge_cutoff_input.setFixedWidth(60)
        params_layout.addWidget(self.discharge_cutoff_input, 2, 1)
        
        # Rest time
        self.rest_time = 0.25
        self.rest_time_label = QLabel("Rest Time (hours):")
        self.rest_time_label.setStyleSheet(f"color: {self.fg_color};")
        params_layout.addWidget(self.rest_time_label, 3, 0)
        self.rest_time_input = QLineEdit("0.25")
        self.rest_time_input.setStyleSheet(f"background-color: #3B4252; color: {self.fg_color};")
        self.rest_time_input.setFixedWidth(60)
        params_layout.addWidget(self.rest_time_input, 3, 1)
        
        # C-rate for test
        self.c_rate = 0.1
        self.c_rate_label = QLabel("Test C-rate:")
        self.c_rate_label.setStyleSheet(f"color: {self.fg_color};")
        params_layout.addWidget(self.c_rate_label, 0, 2)
        self.c_rate_input = QLineEdit("0.1")
        self.c_rate_input.setStyleSheet(f"background-color: #3B4252; color: {self.fg_color};")
        self.c_rate_input.setFixedWidth(40)
        params_layout.addWidget(self.c_rate_input, 0, 3)
        
        c_rate_note = QLabel("(e.g., 0.2 for C/5)")
        c_rate_note.setStyleSheet(f"color: {self.fg_color};")
        params_layout.addWidget(c_rate_note, 0, 4)
        
        controls_layout.addWidget(params_frame, 1)

        # Test conditions input
        self.test_conditions_label = QLabel("Test Conditions/Chemistry:")
        self.test_conditions_label.setStyleSheet(f"color: {self.fg_color};")
        params_layout.addWidget(self.test_conditions_label, 4, 0)
        self.test_conditions_input = QLineEdit("")
        self.test_conditions_input.setStyleSheet(f"background-color: #3B4252; color: {self.fg_color};")
        self.test_conditions_input.setFixedWidth(120)
        params_layout.addWidget(self.test_conditions_input, 4, 1)
        
        # Button frame
        button_frame = QFrame()
        button_frame.setFrameShape(QFrame.NoFrame)
        button_layout = QVBoxLayout(button_frame)
        button_layout.setContentsMargins(0, 0, 0, 0)
        
        self.start_button = QPushButton("START TEST")
        self.start_button.setStyleSheet(f"""
            QPushButton {{
                background-color: {self.accent_color};
                color: {self.fg_color};
                font-weight: bold;
                padding: 6px;
                border-radius: 4px;
            }}
            QPushButton:disabled {{
                background-color: #4C566A;
                color: #D8DEE9;
            }}
        """)
        self.start_button.clicked.connect(self.start_test)
        button_layout.addWidget(self.start_button)
        
        self.stop_button = QPushButton("STOP TEST")
        self.stop_button.setStyleSheet(f"""
            QPushButton {{
                background-color: {self.warning_color};
                color: {self.fg_color};
                font-weight: bold;
                padding: 6px;
                border-radius: 4px;
            }}
            QPushButton:disabled {{
                background-color: #4C566A;
                color: #D8DEE9;
            }}
        """)
        self.stop_button.clicked.connect(self.stop_test)
        self.stop_button.setEnabled(False)
        button_layout.addWidget(self.stop_button)
        
        # Continuous mode checkbox
        self.continuous_mode_check = QCheckBox("Continuous Mode")
        self.continuous_mode_check.setChecked(True)
        self.continuous_mode_check.setStyleSheet(f"color: {self.fg_color};")
        button_layout.addWidget(self.continuous_mode_check)
        
        controls_layout.addWidget(button_frame)
        self.main_layout.addWidget(controls_frame)
        
        # Plot area
        self.setup_plot()
        
        # Status bar
        self.status_bar = self.statusBar()
        self.status_bar.setStyleSheet(f"color: {self.fg_color};")
        self.status_bar.showMessage("Ready")
        
        # Apply dark theme
        self.setStyleSheet(f"""
            QMainWindow {{
                background-color: {self.bg_color};
            }}
            QLabel {{
                color: {self.fg_color};
            }}
            QLineEdit {{
                background-color: #3B4252;
                color: {self.fg_color};
                border: 1px solid #4C566A;
                border-radius: 3px;
                padding: 2px;
            }}
        """)

    def setup_plot(self):
        """Configure the matplotlib plot"""
        self.fig = Figure(figsize=(8, 5), dpi=100, facecolor=self.bg_color)
        self.fig.subplots_adjust(left=0.1, right=0.88, top=0.9, bottom=0.15)
        self.ax = self.fig.add_subplot(111)
        self.ax.set_facecolor('#3B4252')

        # Set initial voltage range
        voltage_padding = 0.2
        min_voltage = max(0, 0.9 - voltage_padding)
        max_voltage = 1.43 + voltage_padding
        self.ax.set_ylim(min_voltage, max_voltage)
        
        # Voltage plot
        self.line_voltage, = self.ax.plot([], [], color='#00BFFF', label='Voltage (V)', linewidth=2)
        self.ax.set_ylabel("Voltage (V)", color='#00BFFF')
        self.ax.tick_params(axis='y', labelcolor='#00BFFF')

        # Current plot (right axis)
        self.ax2 = self.ax.twinx()
        current_padding = 0.05
        test_current = 0.1 * 0.2  # Default values
        max_current = test_current * 1.5
        self.ax2.set_ylim(-max_current - current_padding, max_current + current_padding)
        
        self.line_current, = self.ax2.plot([], [], 'r-', label='Current (A)', linewidth=2)
        self.ax2.set_ylabel("Current (A)", color='r')
        self.ax2.tick_params(axis='y', labelcolor='r')

        self.ax.set_xlabel('Time (s)', color=self.fg_color)
        self.ax.set_title('Battery Test (CC)', color=self.fg_color)
        self.ax.tick_params(axis='x', colors=self.fg_color)
        self.ax.grid(True, color='#4C566A')

        # Position legends
        self.ax.legend(loc='upper left', bbox_to_anchor=(0.01, 0.99))
        self.ax2.legend(loc='upper right', bbox_to_anchor=(0.99, 0.99))

        # Embed plot
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setStyleSheet(f"background-color: {self.bg_color};")
        self.main_layout.addWidget(self.canvas, 1)

    def init_device(self):
        """Initialize the ADALM1000 device with continuous measurement"""
        try:
            # Clean up any existing session
            if hasattr(self, 'session'):
                try:
                    self.session.end()
                    del self.session
                except:
                    pass
                    
            time.sleep(1)
            
            self.session = pysmu.Session(ignore_dataflow=True, queue_size=10000)
            if not self.session.devices:
                raise Exception("No ADALM1000 detected - check connections")

            self.dev = self.session.devices[0]
            self.dev.channels['A'].mode = pysmu.Mode.HI_Z
            self.dev.channels['B'].mode = pysmu.Mode.HI_Z
            self.dev.channels['A'].constant(0)
            self.dev.channels['B'].constant(0)
            
            self.session.start(0)

            self.status_light.setStyleSheet(f"background-color: green; border-radius: 10px;")
            self.connection_label.setText("Connected")
            self.status_bar.showMessage("Device connected | Ready to measure")
            self.session_active = True
            self.start_button.setEnabled(True)

            # Start measurement thread
            self.measurement_thread = MeasurementThread(self.dev, self.interval)
            self.measurement_thread.update_signal.connect(self.update_measurements)
            self.measurement_thread.error_signal.connect(self.handle_device_error)
            
            # Start the QThread directly (no need for threading.Thread)
            self.measurement_thread.start()

        except Exception as e:
            self.handle_device_error(str(e))

    @pyqtSlot(float, float, float)
    def update_measurements(self, voltage, current, current_time):
        """Update measurements from the measurement thread"""
        self.time_data.append(current_time)
        self.voltage_data.append(voltage)
        self.current_data.append(current)
        
        # Update display
        self.voltage_label.setText(f"{voltage:.4f}")
        self.current_label.setText(f"{current:.4f}")
        self.time_label.setText(self.format_time(current_time))
        
        # Throttle plot updates to avoid recursive repaint
        now = time.time()
        if not hasattr(self, '_last_plot_update'):
            self._last_plot_update = 0
        
        if now - self._last_plot_update > 0.1:  # Update plot max 10 times per second
            self._last_plot_update = now
            QTimer.singleShot(0, self.update_plot)

    def update_status(self):
        """Update status information periodically"""
        if self.test_running:
            # Update capacity calculations if in test mode
            if self.measuring and self.time_data:
                current_time = time.time() - self.start_time
                delta_t = current_time - self.last_update_time
                self.last_update_time = current_time
                
                if self.test_phase == "Discharge":
                    current_current = abs(self.current_data[-1])
                    self.capacity_ah += current_current * delta_t / 3600
                    self.capacity_label.setText(f"{self.capacity_ah:.4f}")
                elif self.test_phase == "Charge":
                    current_current = abs(self.current_data[-1])
                    self.charge_capacity += current_current * delta_t / 3600
                    self.charge_capacity_label.setText(f"{self.charge_capacity:.4f}")

    def start_test(self):
        """Start the full battery test cycle"""
        if not self.test_running:
            try:
                # Get parameters from UI
                self.capacity = float(self.capacity_input.text())
                self.charge_cutoff = float(self.charge_cutoff_input.text())
                self.discharge_cutoff = float(self.discharge_cutoff_input.text())
                self.rest_time = float(self.rest_time_input.text())
                self.c_rate = float(self.c_rate_input.text())
                
                # Validate inputs
                if self.capacity <= 0:
                    raise ValueError("Battery capacity must be positive")
                if self.charge_cutoff <= self.discharge_cutoff:
                    raise ValueError("Charge cutoff must be higher than discharge cutoff")
                if self.c_rate <= 0:
                    raise ValueError("C-rate must be positive")
                
                test_current = self.c_rate * self.capacity
                if test_current > 0.2:
                    raise ValueError("Current must be ≤200mA (0.2A) for ADALM1000")
                
                # Clear previous data
                self.time_data.clear()
                self.voltage_data.clear()
                self.current_data.clear()
                self.phase_data.clear()
                self.capacity_ah = 0.0
                self.charge_capacity = 0.0
                self.coulomb_efficiency = 0.0
                self.cycle_count = 0

                # Reset plot with proper ranges
                self.reset_plot()
                
                # Generate filename and create log file
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
                self.base_filename = os.path.join(self.log_dir, f"battery_test_{timestamp}")
                self.create_cycle_log_file()
                
                # Start test
                self.test_running = True
                self.start_time = time.time()
                self.last_update_time = time.time()
                self.test_phase = "Initial Discharge"
                self.phase_label.setText(self.test_phase)
                
                self.start_button.setEnabled(False)
                self.stop_button.setEnabled(True)
                self.status_bar.showMessage(f"Test started | Discharging to {self.discharge_cutoff}V @ {test_current:.3f}A")
                
                # Start test sequence in a QThread
                self.test_sequence_thread = QThread()
                self.test_sequence_worker = TestSequenceWorker(
                    self.dev,
                    test_current,
                    self.charge_cutoff,
                    self.discharge_cutoff,
                    self.rest_time,
                    self.continuous_mode_check.isChecked(),
                    self  # Pass reference to main window for callbacks
                )
                self.test_sequence_worker.moveToThread(self.test_sequence_thread)
                
                # Connect signals
                self.test_sequence_worker.update_phase.connect(self.update_test_phase)
                self.test_sequence_worker.update_status.connect(self.status_bar.showMessage)
                self.test_sequence_worker.test_completed.connect(self.finalize_test)
                self.test_sequence_worker.error_occurred.connect(self.handle_test_error)
                self.test_sequence_worker.finished.connect(self.test_sequence_thread.quit)
                self.test_sequence_worker.finished.connect(self.test_sequence_worker.deleteLater)
                self.test_sequence_thread.finished.connect(self.test_sequence_thread.deleteLater)
                
                # Start the thread and the worker's run method
                self.test_sequence_thread.start()
                QTimer.singleShot(0, self.test_sequence_worker.run)
                
                # Start capacity calculation timer if not already running
                if not self.status_timer.isActive():
                    self.status_timer.start(1000)
                
            except Exception as e:
                QMessageBox.critical(self, "Error", str(e))
                # Ensure buttons are in correct state if error occurs
                self.start_button.setEnabled(True)
                self.stop_button.setEnabled(False)

    def create_cycle_log_file(self):
        """Create a new log file for the current cycle"""
        try:
            # Close previous file if exists
            if hasattr(self, 'current_cycle_file') and self.current_cycle_file:
                try:
                    self.current_cycle_file.close()
                except Exception as e:
                    print(f"Error closing previous log file: {e}")
            
            # Ensure log directory exists
            os.makedirs(self.log_dir, exist_ok=True)
            
            if not os.access(self.log_dir, os.W_OK):
                QMessageBox.critical(self, "Error", f"No write permissions in {self.log_dir}")
                return False
            
            # Generate unique filename
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            self.filename = os.path.join(self.log_dir, f"battery_test_{timestamp}.csv")
            
            # Open new file
            try:
                self.current_cycle_file = open(self.filename, 'w', newline='')
                
                # Write header with test parameters
                test_current = self.c_rate * self.capacity
                test_conditions = self.test_conditions_input.text() if hasattr(self, 'test_conditions_input') else "N/A"
                
                self.current_cycle_file.write(f"# ADALM1000 Battery Test Log\n")
                self.current_cycle_file.write(f"# Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
                self.current_cycle_file.write(f"# Battery Capacity: {self.capacity} Ah\n")
                self.current_cycle_file.write(f"# Test Current: {test_current:.3f} A (C/{1/self.c_rate:.1f})\n")
                self.current_cycle_file.write(f"# Charge Cutoff: {self.charge_cutoff} V\n")
                self.current_cycle_file.write(f"# Discharge Cutoff: {self.discharge_cutoff} V\n")
                self.current_cycle_file.write(f"# Rest Time: {self.rest_time} hours\n")
                self.current_cycle_file.write(f"# Test Conditions/Chemistry: {test_conditions}\n")
                self.current_cycle_file.write("#\n")
                
                # Write data header
                self.log_writer = csv.writer(self.current_cycle_file)
                self.log_writer.writerow([
                    "Time(s)", "Voltage(V)", "Current(A)", "Phase", 
                    "Discharge_Capacity(Ah)", "Charge_Capacity(Ah)", 
                    "Coulomb_Eff(%)", "Cycle"
                ])
                self.log_buffer = []
                return True
            except Exception as e:
                QMessageBox.critical(self, "Error", f"Failed to create log file: {e}")
                return False
        except Exception as e:
            print(f"Error in create_cycle_log_file: {e}")
            return False

    def format_time(self, seconds):
        """Convert seconds to hh:mm:ss format"""
        hours = int(seconds // 3600)
        minutes = int((seconds % 3600) // 60)
        seconds = int(seconds % 60)
        return f"{hours:02d}:{minutes:02d}:{seconds:02d}"

    def stop_test(self):
        """Request immediate stop of the test"""
        if not self.test_running:
            return
            
        self.request_stop = True
        self.test_running = False
        self.measuring = False
        self.test_phase = "Idle"
        self.phase_label.setText(self.test_phase)
        
        if hasattr(self, 'dev'):
            try:
                self.dev.channels['A'].mode = pysmu.Mode.HI_Z
                self.dev.channels['A'].constant(0)
            except Exception as e:
                print(f"Error resetting device: {e}")
        
        self.time_data.clear()
        self.voltage_data.clear()
        self.current_data.clear()
        self.phase_data.clear()
        
        self.capacity_ah = 0.0
        self.charge_capacity = 0.0
        self.coulomb_efficiency = 0.0
        
        self.reset_plot()
        
        self.status_bar.showMessage("Test stopped - Ready for new test")
        self.stop_button.setEnabled(False)
        self.start_button.setEnabled(True)
        
        self.finalize_test()

    def finalize_test(self):
        """Final cleanup after test completes or is stopped"""
        self.measuring = False
        if hasattr(self, 'dev'):
            try:
                self.dev.channels['A'].constant(0)
            except Exception as e:
                print(f"Error resetting device: {e}")
        test_current = self.c_rate * self.capacity

        # Only try to close if file exists and is open
        if hasattr(self, 'current_cycle_file') and self.current_cycle_file is not None:
            try:
                if self.log_buffer:
                    self.log_writer.writerows(self.log_buffer)
                    self.log_buffer.clear()
                
                # Write test summary
                test_current = self.c_rate * self.capacity
                test_conditions = self.test_conditions_input.text() if hasattr(self, 'test_conditions_input') else "N/A"
                
                self.current_cycle_file.write("\n# TEST SUMMARY\n")
                self.current_cycle_file.write(f"# Test Parameters:\n")
                self.current_cycle_file.write(f"# - Battery Capacity: {self.capacity} Ah\n")
                self.current_cycle_file.write(f"# - Test Current: {test_current:.3f} A (C/{1/self.c_rate:.1f})\n")
                self.current_cycle_file.write(f"# - Charge Cutoff: {self.charge_cutoff} V\n")
                self.current_cycle_file.write(f"# - Discharge Cutoff: {self.discharge_cutoff} V\n")
                self.current_cycle_file.write(f"# - Test Conditions: {test_conditions}\n")
                self.current_cycle_file.write(f"# Results:\n")
                self.current_cycle_file.write(f"# - Cycles Completed: {self.cycle_count}\n")
                self.current_cycle_file.write(f"# - Final Discharge Capacity: {self.capacity_ah:.4f} Ah\n")
                self.current_cycle_file.write(f"# - Final Charge Capacity: {self.charge_capacity:.4f} Ah\n")
                self.current_cycle_file.write(f"# - Coulombic Efficiency: {self.coulomb_efficiency:.1f}%\n")
                
                self.current_cycle_file.close()
            except Exception as e:
                print(f"Error closing log file: {e}")
            finally:
                self.current_cycle_file = None
        
        self.start_button.setEnabled(True)
        self.stop_button.setEnabled(False)
        self.request_stop = False
        
        message = (
            f"Test safely stopped after discharge phase | "
            f"Cycle {self.cycle_count} completed | "
            f"Final capacity: {self.capacity_ah:.3f}Ah"
        )
        self.status_bar.showMessage(message)
        
        QMessageBox.information(
            self,
            "Test Completed",
            f"Test was safely stopped after discharge phase.\n\n"
            f"Test Parameters:\n"
            f"- Capacity: {self.capacity} Ah\n"
            f"- Current: {test_current:.3f} A (C/{1/self.c_rate:.1f})\n"
            f"- Charge Cutoff: {self.charge_cutoff} V\n"
            f"- Discharge Cutoff: {self.discharge_cutoff} V\n"
            f"- Conditions: {test_conditions}\n\n"
            f"Results:\n"
            f"- Cycles: {self.cycle_count}\n"
            f"- Discharge capacity: {self.capacity_ah:.3f}Ah\n"
            f"- Coulombic efficiency: {self.coulomb_efficiency:.1f}%"
        )

    def reset_plot(self):
        """Reset the plot completely for a new test"""
        self.line_voltage.set_data([], [])
        self.line_current.set_data([], [])
        
        self.time_data.clear()
        self.voltage_data.clear()
        self.current_data.clear()
        
        voltage_padding = 0.2
        min_voltage = max(0, self.discharge_cutoff - voltage_padding)
        max_voltage = self.charge_cutoff + voltage_padding
        self.ax.set_xlim(0, 10)
        self.ax.set_ylim(min_voltage, max_voltage)
        
        current_padding = 0.05
        test_current = self.c_rate * self.capacity
        max_current = test_current * 1.5
        self.ax2.set_ylim(-max_current - current_padding, max_current + current_padding)
        
        self.canvas.draw()

    def write_cycle_summary(self):
        """Write cycle summary to the current cycle's log file"""
        if not hasattr(self, 'current_cycle_file') or not self.current_cycle_file:
            return
            
        summary_line = (
            f"Cycle {self.cycle_count} Summary - "
            f"Discharge={self.capacity_ah:.4f}Ah, "
            f"Charge={self.charge_capacity:.4f}Ah, "
            f"Efficiency={self.coulomb_efficiency:.1f}%"
        )
        
        try:
            if self.log_buffer:
                self.log_writer.writerows(self.log_buffer)
                self.log_buffer.clear()
            self.current_cycle_file.write(summary_line + "\n")
            self.current_cycle_file.flush()
        except Exception as e:
            print(f"Error writing cycle summary: {e}")

    def update_plot(self):
        """More reliable plotting with better error handling"""
        try:
            # Create local copies safely
            with self.plot_mutex:
                if not self.time_data or not self.voltage_data or not self.current_data:
                    return
                    
                if len(self.time_data) != len(self.voltage_data) or len(self.time_data) != len(self.current_data):
                    # Find the minimum length to avoid mismatch
                    min_len = min(len(self.time_data), len(self.voltage_data), len(self.current_data))
                    x_data = np.array(self.time_data[-min_len:])
                    y1_data = np.array(self.voltage_data[-min_len:])
                    y2_data = np.array(self.current_data[-min_len:])
                else:
                    x_data = np.array(self.time_data)
                    y1_data = np.array(self.voltage_data)
                    y2_data = np.array(self.current_data)
            
            # Update plot data
            self.line_voltage.set_data(x_data, y1_data)
            self.line_current.set_data(x_data, y2_data)
            
            # Auto-scale when needed
            if len(x_data) > 0 and x_data[-1] > self.ax.get_xlim()[1] * 0.8:
                self.auto_scale_axes()
            
            # Force redraw
            self.canvas.draw_idle()
            
        except Exception as e:
            print(f"Plot error: {e}")
            # Reset plot on error
            self.line_voltage.set_data([], [])
            self.line_current.set_data([], [])
            self.canvas.draw_idle()

    def auto_scale_axes(self):
        """Auto-scale plot axes with appropriate padding and strict boundaries"""
        if not self.time_data:
            return
        
        min_time = 0
        max_time = self.time_data[-1]
        current_xlim = self.ax.get_xlim()
        
        if max_time > current_xlim[1] * 0.95:
            new_max = max_time * 1.05
            self.ax.set_xlim(min_time, new_max)
            self.ax2.set_xlim(min_time, new_max)
        
        voltage_padding = 0.2
        if self.voltage_data:
            min_voltage = max(0, min(self.voltage_data) - voltage_padding)
            max_voltage = min(5.0, max(self.voltage_data) + voltage_padding)
            current_ylim = self.ax.get_ylim()
            if (abs(current_ylim[0] - min_voltage) > 0.1 or abs(current_ylim[1] - max_voltage) > 0.1):
                self.ax.set_ylim(min_voltage, max_voltage)
        
        current_padding = 0.05
        if self.current_data:
            min_current = max(-0.25, min(self.current_data) - current_padding)
            max_current = min(0.25, max(self.current_data) + current_padding)
            current_ylim2 = self.ax2.get_ylim()
            if (abs(current_ylim2[0] - min_current) > 0.02 or abs(current_ylim2[1] - max_current) > 0.02):
                self.ax2.set_ylim(min_current, max_current)

    @pyqtSlot(str)
    def handle_device_error(self, error):
        """Handle device connection errors"""
        error_msg = str(error)
        print(f"Device error: {error_msg}")

        if hasattr(self, 'session'):
            try:
                if self.session_active:
                    self.session.end()
                del self.session
            except Exception as e:
                print(f"Error cleaning up session: {e}")

        self.status_light.setStyleSheet(f"background-color: red; border-radius: 10px;")
        self.connection_label.setText("Disconnected")
        self.status_bar.showMessage(f"Device error: {error_msg}")

        self.session_active = False
        self.test_running = False
        self.continuous_mode = False
        self.measuring = False
        
        self.start_button.setEnabled(False)
        self.stop_button.setEnabled(False)

        self.time_data.clear()
        self.voltage_data.clear()
        self.current_data.clear()
    
    @pyqtSlot(str)
    def update_test_phase(self, phase_text):
        """Update the test phase display"""
        self.test_phase = phase_text
        self.phase_label.setText(phase_text)
    
        # Update log if available
        if hasattr(self, 'log_buffer'):
            current_time = time.time() - self.start_time
            self.log_buffer.append([
                f"{current_time:.3f}",
                "",
                "",
                phase_text,
                f"{self.capacity_ah:.4f}",
                f"{self.charge_capacity:.4f}",
                f"{self.coulomb_efficiency:.1f}" if hasattr(self, 'coulomb_efficiency') else "0.0",
                f"{self.cycle_count}"
            ])
        
    @pyqtSlot(str)
    def handle_test_error(self, error_msg):
        """Handle errors from the test sequence with complete cleanup"""
        try:
            # 1. Notify user
            QMessageBox.critical(self, "Test Error", 
                            f"An error occurred:\n{error_msg}\n\nAttempting to recover...")
            
            # 2. Stop all operations
            self.stop_test()
            
            # 3. Reset UI elements
            if hasattr(self, 'line_voltage'):
                try:
                    self.line_voltage.set_data([], [])
                    self.line_current.set_data([], [])
                    self.ax.set_xlim(0, 1)
                    self.ax2.set_xlim(0, 1)
                    self.canvas.draw()
                except Exception as plot_error:
                    print(f"Plot reset error: {plot_error}")
            
            # 4. Update status
            self.status_bar.showMessage(f"Error: {error_msg} - Reconnecting...")
            self.status_light.setStyleSheet("background-color: orange; border-radius: 10px;")
            
            # 5. Attempt recovery
            QTimer.singleShot(1000, self.attempt_reconnect)  # Delay before reconnect
            
        except Exception as e:
            print(f"Error in error handler: {e}")
            # Fallback - restart application?
            QMessageBox.critical(self, "Fatal Error", 
                            "The application needs to restart due to an unrecoverable error")
            QTimer.singleShot(1000, self.close)

    def attempt_reconnect(self):
        """Attempt to reconnect automatically"""
        QMessageBox.critical(
            self,
            "Device Connection Error",
            "Could not connect to ADALM1000\n\n"
            "1. Check USB cable connection\n"
            "2. The device will attempt to reconnect automatically"
        )
        
        QTimer.singleShot(1000, self.reconnect_device)

    def reconnect_device(self):
        """Reconnect the device with proper cleanup"""
        self.status_bar.showMessage("Attempting to reconnect...")
        
        if hasattr(self, 'session'):
            try:
                if self.session_active:
                    self.session.end()
                del self.session
            except:
                pass
                
        self.test_running = False
        self.continuous_mode = False
        self.measuring = False
        
        if hasattr(self, 'measurement_thread'):
            self.measurement_thread.stop()

        time.sleep(1.5)
        
        try:
            self.init_device()
            if self.session_active:
                self.status_bar.showMessage("Reconnected successfully")
                return
        except Exception as e:
            print(f"Reconnect failed: {e}")
            
        self.status_bar.showMessage("Reconnect failed - will retry...")
        QTimer.singleShot(2000, self.reconnect_device)

    def closeEvent(self, event):
        """Clean up on window close"""
        self.test_running = False
        self.measuring = False
        self.session_active = False
        
        # Stop measurement thread
        if hasattr(self, 'measurement_thread'):
            self.measurement_thread.stop()
        
        # Stop test sequence thread
        if hasattr(self, 'test_sequence_thread'):
            if hasattr(self, 'test_sequence_worker'):
                self.test_sequence_worker.stop()
            self.test_sequence_thread.quit()
            self.test_sequence_thread.wait(500)
        
        # Clean up device session
        if hasattr(self, 'session') and self.session:
            try:
                self.session.end()
            except Exception as e:
                print(f"Error ending session: {e}")

        event.accept()

if __name__ == "__main__":
    app = QApplication([])
    try:
        window = BatteryTester()
        window.show()
        app.exec_()
    except Exception as e:
        QMessageBox.critical(None, "Fatal Error", f"Application failed: {str(e)}") 
fixed by C
2025-07-07 11:56:44 +02:00
Jan
c52779e104 MainCode/adalm1000_logger.py aktualisiert
Diese Änderungen fügen:

    Ein neues Eingabefeld für Testbedingungen/Zellchemie hinzu

    Schreiben alle relevanten Testparameter in den Kopf der Logdatei

    Fügen eine detaillierte Zusammenfassung am Ende der Logdatei hinzu

    Zeigen die Testparameter auch in der Abschlussmeldung an
(D)
2025-07-07 11:51:24 +02:00
Jan
20fe83a319 MainCode/adalm1000_logger.py aktualisiert
Working
(D)
2025-07-07 11:44:10 +02:00
Jan
418885aca8 MainCode/adalm1000_logger.py aktualisiert
Working fine 
(D)
2025-07-03 19:07:53 +02:00
Jan
9746ca12b3 MainCode/adalm1000_logger.py aktualisiert
Sourcing und sinking current doesnt work.
2025-07-03 18:07:32 +02:00
Jan
b5380e5a33 MainCode/adalm1000_logger.py aktualisiert
plot and tata works
description overlapping test untested
2025-07-03 17:40:03 +02:00
Jan
df69d0e832 MainCode/adalm1000_logger.py aktualisiert
Thread Safety Violations:

        Qt objects (like QWidgets) must only be accessed from the main thread (GUI thread)

        Your measurement thread is directly interacting with GUI elements

    Painter Conflicts:

        Multiple threads trying to paint to the same canvas simultaneously

        Recursive repaint calls

    Segmentation Fault:

        Likely caused by improper thread cleanup or accessing deleted objects
2025-07-03 13:45:46 +02:00
Jan
f75d1dbcf7 MainCode/adalm1000_logger.py aktualisiert
error qgridlayout is not defined
2025-07-02 17:33:36 +02:00
Jan
e854fb904f MainCode/adalm1000_logger.py aktualisiert
File "/home/jan/adalm1000/bin/adalm1000_logger.py", line 869
    if (abs(current_ylim[0] - min_voltage) > 0.1 or abs(current_ylim[1] - max_voltage) > 0.1:
                                                                                            ^
SyntaxError: invalid syntax
Fixed (D)
2025-07-02 17:32:10 +02:00
Jan
75be821b4b MainCode/adalm1000_logger.py aktualisiert
(D)
2025-07-02 17:29:17 +02:00
Jan
0305463857 MainCode/adalm1000_logger.py aktualisiert
1. Verbesserte auto_scale_axes() Methode:
(D)
2025-06-13 18:21:17 +02:00
Jan
12fb82ce80 MainCode/adalm1000_logger.py aktualisiert
Got it working, from the push before.
(Deepseek)
2025-06-13 18:16:00 +02:00
Jan
81ccc8ccd7 MainCode/adalm1000_logger.py aktualisiert
Test phase display issue: When continuous mode is disabled and the discharge voltage is already below cutoff, the phase still shows "Discharge" instead of switching to "Idle".

    Graph update issue: The plot sometimes fails to update when starting a new test series.
(Deepseek)
2025-06-13 18:04:59 +02:00
Jan
1a6ebb2fab MainCode/adalm1000_logger.py aktualisiert
Clear log_buffer after every cycle, because logs are getting smaler the more cycles.
(D)
2025-05-28 17:06:24 +02:00
Jan
c697388157 MainCode/adalm1000_logger.py aktualisiert
 User unchecks Continuous Mode during discharge:

    Status updates immediately: "Continuous Mode disabled..."

    Discharge continues until cutoff voltage.

    Test stops after discharge (no rest phase or new cycle).

 User leaves Continuous Mode enabled:

    Test continues looping indefinitely (original behavior).
(D)
2025-05-27 22:09:52 +02:00
Jan
34be33434f MainCode/adalm1000_logger.py aktualisiert
This makes the stop operation more thorough and provides better visual feedback to the user. The plot will now clearly show that the test has been stopped and reset.
(D)
2025-05-26 13:38:12 +02:00
Jan
876fecb466 MainCode/adalm1000_logger.py aktualisiert
This change ensures that:

    The plot data buffers are cleared

    The plot lines are reset to empty data

    The axes are reset to default ranges (0-1)

    The canvas is redrawn to show the cleared plot
(Deepseek)
2025-05-26 13:35:44 +02:00
Jan
25322bc59d MainCode/adalm1000_logger.py aktualisiert
Safer:

    Application shutdown

    Thread cleanup

    Error recovery

    Reconnection scenarios
(Deepseek)
2025-05-25 17:08:26 +02:00
Jan
24cc224138 MainCode/adalm1000_logger.py aktualisiert
Separate Log Files: You've successfully implemented separate log files for each cycle without cycle numbers in filenames.

    1Hz UI Updates: The measurement display updates at 1Hz as requested.

    Throttled Plot Updates: Plot updates are properly throttled to prevent lag.

    Error Handling: Good error handling throughout the code.

    Thread Safety: Proper use of threading for measurements and test sequences.
(Deepseek)
2025-05-24 13:23:14 +02:00
Jan
1c928e22fc MainCode/adalm1000_logger.py aktualisiert
Show an error message when disconnected

    Automatically attempt to reconnect

    Keep retrying until successful

    Provide clear status updates throughout the process
2025-05-24 01:36:51 +02:00
Jan
6db656c71b MainCode/adalm1000_logger.py aktualisiert
change cyclecount to beginning

This will ensure your plot:

    Starts with a reasonable view of your expected voltage range

    Maintains good visibility of the key areas (charge/discharge cutoffs)

    Doesn't zoom out too far when there are measurement spikes

    Has better overall framing of the data
2025-05-24 01:33:36 +02:00
Jan
f50a641211 MainCode/adalm1000_logger.py aktualisiert
Make all measurements update live in the GUI

    Allow the stop button to immediately halt the test at any point in the cycle

    Still maintain proper cleanup and data saving when stopped
(Deepseek)
2025-05-24 01:20:08 +02:00
Jan
13148a64de MainCode/adalm1000_logger.py aktualisiert
Reducing unnecessary GUI updates

    Implementing buffered file I/O

    Throttling plot updates

    Only updating display elements when values change

    Using more efficient drawing methods for the plot
(Deepseek)
2025-05-23 23:41:21 +02:00
Jan
06c99bae38 MainCode/adalm1000_logger.py aktualisiert
Der Stop-Button setzt nur ein Flag (request_stop) statt sofort zu stoppen

    Die Entladephase überprüft dieses Flag und bricht ab, wenn es gesetzt ist

    Nach der Entladephase wird der Test nur beendet, wenn request_stop True ist

    Neue finalize_test Methode für konsistente Aufräumarbeiten

    Klare Statusmeldungen, die den Stop-Request anzeigen
(Deepseek)
2025-05-23 23:34:30 +02:00
Jan
a82cc2c981 MainCode/adalm1000_logger.py aktualisiert
Die Statusmeldung zeigt jetzt klar an, ob:
        Der Test normal weiterläuft ("Next: Charge to X.XV")
        Auf einen Interrupt gewartet wird ("Waiting for interrupt"
    Der Code prüft häufiger auf Interrupts, besonders zwischen den einzelnen Phasen
    Bei einem Interrupt wird der aktuelle Zyklus noch sauber zu Ende geführt, aber kein neuer gestartet
    Die Abschlussmeldung zeigt jetzt an, nach welchem Zyklus der Test unterbrochen wurde
(Deepseek)
2025-05-23 20:55:06 +02:00
Jan
07b86664c0 MainCode/adalm1000_logger.py aktualisiert
Cycling added
    Ich werde eine neue Variable continuous_mode hinzufügen, um den kontinuierlichen Betrieb zu steuern
    Die run_test_sequence() Methode wird modifiziert, um in einer Schleife zu laufen
    Die stop_test() Methode wird erweitert, um den kontinuierlichen Modus zu beenden
(Deepseek)
2025-05-23 20:38:50 +02:00
Jan
516e2a44b2 MainCode/adalm1000_logger.py aktualisiert
Timing angepasst (ChatGPT)
2025-05-23 20:27:54 +02:00
Jan
a9a871bff5 revert 165e27204b
revert MainCode/adalm1000_logger.py aktualisiert

Charge Time handeling geändert (ChatGPT)
2025-05-23 20:26:55 +02:00
Jan
165e27204b MainCode/adalm1000_logger.py aktualisiert
Charge Time handeling geändert (ChatGPT)
2025-05-23 20:22:44 +02:00
2 changed files with 978 additions and 1396 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,812 +0,0 @@
# -*- coding: utf-8 -*-
import os
import time
import csv
import json
import queue
import threading
from datetime import datetime
import tkinter as tk
from tkinter import ttk, messagebox
import pysmu
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class DeviceDisconnectedError(Exception):
pass
class ModernADALM1000Logger:
def __init__(self, root):
# Color scheme
self.bg_color = "#2E3440"
self.fg_color = "#D8DEE9"
self.accent_color = "#5E81AC"
self.warning_color = "#BF616A"
self.success_color = "#A3BE8C"
# Main window configuration
self.root = root
self.root.title("ADALM1000 - Modern Logger")
self.root.geometry("1000x800")
self.root.minsize(800, 700)
self.root.configure(bg=self.bg_color)
# Device and measurement state
self.session_active = False
self.measuring = False
self.interval = 1.0
self.shunt_resistor = 0.1
self.log_dir = os.path.expanduser("~/adalm1000/logs")
self.config_dir = os.path.expanduser("~/adalm1000/config")
os.makedirs(self.config_dir, exist_ok=True)
# Calibration data
self.calibration = {'A': {'gain': 1.0, 'offset': 0.0}, 'B': {'gain': 1.0, 'offset': 0.0}}
self.cal_applied = tk.BooleanVar(value=True)
# Threading and data
self.device_lock = threading.Lock()
self.measurement_event = threading.Event()
self.data_queue = queue.Queue(maxsize=100)
# Data buffers using numpy for better performance
self.time_data = []
self.voltage_a_data = []
self.voltage_b_data = []
self.current_data = []
self.data_index = 0
# Initialize UI and device
self.setup_ui()
self.load_calibration()
self.init_device()
self.root.after(100, self.update_calibration_display)
# Ensure proper cleanup
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
def handle_device_error(self, error):
"""Handle device connection errors and update UI accordingly"""
error_msg = str(error)
print(f"Device error: {error_msg}") # Log to console
# Update UI in safe thread context
self.root.after_idle(lambda: self.status_light.itemconfig(self.status_indicator, fill='red'))
# Safe status light update
if hasattr(self, 'status_light') and hasattr(self, 'status_indicator'):
self.status_light.itemconfig(self.status_indicator, fill='red')
# Update connection label and status
self.connection_label.config(text="Disconnected")
if "No ADALM1000 detected" in error_msg:
self.status_var.set("Device not found - check USB connection")
else:
self.status_var.set(f"Device error: {error_msg}")
# Disable controls
self.session_active = False
self.measuring = False
if hasattr(self, 'start_button'):
self.start_button.config(state=tk.DISABLED, text="START LOGGING")
if hasattr(self, 'calibrate_button'):
self.calibrate_button.config(state=tk.DISABLED)
# Clear plot + buffers
self.line_a.set_data([], [])
self.line_current.set_data([], [])
self.ax.set_xlim(0, 1)
self.ax2.set_xlim(0, 1)
self.canvas.draw()
self.time_data.clear()
self.voltage_a_data.clear()
self.voltage_b_data.clear()
self.current_data.clear()
self.data_index = 0
# Attempt to clean up the session
if hasattr(self, 'session'):
try:
self.session.end()
except:
pass
del self.session
# Enable reconnect option
if hasattr(self, 'reconnect_btn'):
self.reconnect_btn.config(state=tk.NORMAL)
# Show popup message if window still exists
if self.root.winfo_exists():
messagebox.showerror(
"Device Connection Error",
f"Could not connect to ADALM1000:\n\n{error_msg}\n\n"
"1. Check USB cable connection\n"
"2. Try the Reconnect button\n"
"3. Restart the application if problem persists"
)
def setup_ui(self):
"""Configure the user interface"""
self.style = ttk.Style()
self.style.theme_use('clam')
# Configure styles
self.style.configure('.', background=self.bg_color, foreground=self.fg_color)
self.style.configure('TFrame', background=self.bg_color)
self.style.configure('TLabel', background=self.bg_color, foreground=self.fg_color)
self.style.configure('TButton', background=self.accent_color, foreground=self.fg_color,
padding=6, font=('Helvetica', 10, 'bold'))
self.style.map('TButton',
background=[('active', self.accent_color), ('disabled', '#4C566A')],
foreground=[('active', self.fg_color), ('disabled', '#D8DEE9')])
self.style.configure('TEntry', fieldbackground="#3B4252", foreground=self.fg_color)
self.style.configure('Header.TLabel', font=('Helvetica', 14, 'bold'), foreground=self.accent_color)
self.style.configure('Value.TLabel', font=('Helvetica', 12, 'bold'))
self.style.configure('Status.TLabel', font=('Helvetica', 10))
self.style.configure('Warning.TButton', background=self.warning_color)
self.style.configure('TLabelframe', background=self.bg_color, foreground=self.accent_color)
# Main layout
self.content_frame = ttk.Frame(self.root)
self.content_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
# Header area
header_frame = ttk.Frame(self.content_frame)
header_frame.pack(fill=tk.X, pady=(0, 20))
ttk.Label(header_frame, text="ADALM1000 Measurement Logger", style='Header.TLabel').pack(side=tk.LEFT)
# Status indicator
self.status_light = tk.Canvas(header_frame, width=20, height=20, bg=self.bg_color, bd=0, highlightthickness=0)
self.status_light.pack(side=tk.RIGHT, padx=10)
self.status_indicator = self.status_light.create_oval(2, 2, 18, 18, fill='red')
self.connection_label = ttk.Label(header_frame, text="Disconnected")
self.connection_label.pack(side=tk.RIGHT)
# Reconnect button
self.reconnect_btn = ttk.Button(header_frame, text="Reconnect", command=self.reconnect_device)
self.reconnect_btn.pack(side=tk.RIGHT, padx=10)
# Measurement display
display_frame = ttk.LabelFrame(self.content_frame, text=" Live Measurements ", padding=15)
display_frame.pack(fill=tk.BOTH, expand=False)
# Measurement values
measurement_labels = [
("Circuit Voltage (CH A)", "V"),
("Reference Voltage (CH B)", "V"),
("Shunt Voltage (A - B)", "V"),
("Current", "A")
]
for i, (label, unit) in enumerate(measurement_labels):
ttk.Label(display_frame, text=f"{label}:", font=('Helvetica', 11)).grid(row=i, column=0, sticky=tk.W, pady=5)
value_label = ttk.Label(display_frame, text="0.000", style='Value.TLabel')
value_label.grid(row=i, column=1, sticky=tk.W, padx=10)
ttk.Label(display_frame, text=unit).grid(row=i, column=2, sticky=tk.W)
if i == 0:
self.voltage_a_label = value_label
elif i == 1:
self.voltage_b_ref_label = value_label # NEW: CH B direct
elif i == 2:
self.shunt_voltage_label = value_label # NEW: A - B
else:
self.current_label = value_label
# Control area (single row now)
controls_frame = ttk.Frame(self.content_frame)
controls_frame.pack(fill=tk.X, pady=(10, 10), padx=0)
# Left side: Start + Interval + Shunt
left_control_frame = ttk.Frame(controls_frame)
left_control_frame.pack(side=tk.LEFT)
self.start_button = ttk.Button(left_control_frame, text="START LOGGING", command=self.toggle_measurement)
self.start_button.pack(side=tk.LEFT, padx=(0, 12))
ttk.Label(left_control_frame, text="Interval:").pack(side=tk.LEFT, padx=(0, 4))
self.interval_entry = ttk.Entry(left_control_frame, width=6)
self.interval_entry.pack(side=tk.LEFT)
self.interval_entry.insert(0, "1.0")
ttk.Label(left_control_frame, text="s").pack(side=tk.LEFT, padx=(4, 12))
ttk.Label(left_control_frame, text="Shunt:").pack(side=tk.LEFT, padx=(0, 4))
self.shunt_entry = ttk.Entry(left_control_frame, width=6)
self.shunt_entry.pack(side=tk.LEFT)
self.shunt_entry.insert(0, "0.1")
ttk.Label(left_control_frame, text="Ω").pack(side=tk.LEFT, padx=(4, 12))
# Right side: Calibration stuff
cal_control_frame = ttk.Frame(controls_frame)
cal_control_frame.pack(side=tk.RIGHT)
self.cal_check = ttk.Checkbutton(cal_control_frame, text="Apply Calibration", variable=self.cal_applied, command=self.toggle_calibration)
self.cal_check.pack(side=tk.LEFT, padx=(0, 10))
self.cal_status = ttk.Label(cal_control_frame, text="No calibration", foreground=self.warning_color)
self.cal_status.pack(side=tk.LEFT, padx=(0, 10))
self.calibrate_button = ttk.Button(cal_control_frame, text="CALIBRATE", command=self.run_calibration)
self.calibrate_button.pack(side=tk.LEFT)
# Plot area
self.plot_frame = ttk.Frame(self.content_frame)
self.plot_frame.pack(fill=tk.BOTH, expand=True, padx=20, pady=(0, 5))
self.setup_plot()
# Status bar
self.status_var = tk.StringVar()
self.status_var.set("Ready")
self.status_label = ttk.Label(self.root, textvariable=self.status_var, style='Status.TLabel', padding=(0, 5), anchor=tk.W)
self.status_label.place(x=20, relx=0, rely=1.0, anchor='sw', relwidth=0.96, height=28)
def setup_plot(self):
"""Configure the matplotlib plot"""
self.fig = Figure(figsize=(8, 5), dpi=100, facecolor='#2E3440')
self.fig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.15)
self.ax = self.fig.add_subplot(111)
self.ax.set_facecolor('#3B4252')
# Voltage A (left axis)
self.line_a, = self.ax.plot([], [], color='#00BFFF', label='Voltage A (V)', linewidth=2)
self.ax.set_ylabel("Voltage (V)", color='#00BFFF')
self.ax.tick_params(axis='y', labelcolor='#00BFFF')
# Current (right axis)
self.ax2 = self.ax.twinx()
self.line_current, = self.ax2.plot([], [], 'r-', label='Current (A)', linewidth=2)
self.ax2.set_ylabel("Current (A)", color='r')
self.ax2.tick_params(axis='y', labelcolor='r')
self.ax.set_xlabel('Time (s)', color=self.fg_color)
self.ax.set_title('Circuit Voltage and Current', color=self.fg_color)
self.ax.tick_params(axis='x', colors=self.fg_color)
self.ax.grid(True, color='#4C566A')
self.ax.legend(loc='upper left')
self.ax2.legend(loc='upper right')
# Embed plot
self.canvas = FigureCanvasTkAgg(self.fig, master=self.plot_frame)
self.canvas.draw()
canvas_widget = self.canvas.get_tk_widget()
canvas_widget.configure(bg='#2E3440', bd=0, highlightthickness=0)
canvas_widget.pack(side=tk.TOP, fill=tk.X, expand=True, pady=(10, 0))
def init_device(self):
"""Initialize the ADALM1000 device"""
try:
if hasattr(self, 'session'):
try:
if self.session_active:
self.session.end()
del self.session
except Exception as e:
print(f"Cleanup error: {e}")
self.session = pysmu.Session(ignore_dataflow=True, queue_size=10000)
if not self.session.devices:
raise Exception("No ADALM1000 detected - check connections")
self.dev = self.session.devices[0]
self.dev.channels['A'].mode = pysmu.Mode.HI_Z
self.dev.channels['B'].mode = pysmu.Mode.HI_Z
self.session.start(0)
self.status_light.itemconfig(self.status_indicator, fill='green')
self.connection_label.config(text="Connected")
self.status_var.set("Device connected | Ready to measure")
self.session_active = True
self.start_button.config(state=tk.NORMAL)
self.calibrate_button.config(state=tk.NORMAL)
if hasattr(self, 'reconnect_btn'):
self.reconnect_btn.config(state=tk.DISABLED)
if not self.measurement_event.is_set():
self.start_measurement_thread()
except Exception as e:
self.handle_device_error(e)
def reconnect_device(self):
"""Reconnect the device with proper cleanup"""
self.status_var.set("Attempting to reconnect...")
self.measuring = False
self.measurement_event.clear()
# Wait for threads to finish
if hasattr(self, 'measurement_thread'):
self.measurement_thread.join(timeout=1.0)
if hasattr(self, 'queue_listener'):
self.queue_listener.join(timeout=0.5)
# Reset plot + buffers before reinitializing
self.handle_device_error("Reconnecting...")
# Try to reinitialize device
self.init_device()
def start_measurement_thread(self):
"""Start the measurement thread"""
if not self.measurement_event.is_set():
self.measurement_event.set()
self.measurement_thread = threading.Thread(
target=self.measure,
daemon=True
)
self.measurement_thread.start()
self.start_queue_listener()
def start_queue_listener(self):
"""Start listening to the data queue"""
self.queue_listener = threading.Thread(
target=self.listen_to_data_queue,
daemon=True
)
self.queue_listener.start()
def listen_to_data_queue(self):
"""Process data from the queue"""
while self.measurement_event.is_set():
try:
data = self.data_queue.get(timeout=0.1)
self.process_data_item(data)
except queue.Empty:
continue
def process_data_item(self, data):
"""Process a single data item from the queue"""
if data[0] == 'error':
self.handle_error(data[1])
else:
current_time, voltage_a, voltage_b, current = data
shunt_voltage = voltage_a - voltage_b
# Always append new data
self.time_data.append(current_time)
self.voltage_a_data.append(voltage_a)
self.voltage_b_data.append(voltage_b)
self.current_data.append(current)
self.data_index += 1
# Update UI
self.update_measurement_display(voltage_a, voltage_b, shunt_voltage, current)
# Update plot periodically
if self.data_index % 5 == 0 or self.data_index == 1:
self.root.after_idle(self.update_plot)
# Save data if logging
if self.measuring:
self.save_measurement_data(voltage_a, voltage_b, shunt_voltage, current)
def measure(self):
"""Measurement loop using interval-based averaging"""
self.start_time = time.time()
next_time = self.start_time
raw_va = []
raw_vb = []
while self.measurement_event.is_set():
try:
with self.device_lock:
if not self.session_active:
self.data_queue.put(('error', "Device disconnected during measurement"))
break
samples = self.dev.read(5, 500, True)
if not samples or not isinstance(samples[0], tuple) or len(samples[0]) != 2:
raise DeviceDisconnectedError("ADALM1000 is no longer responding")
raw_va.extend([s[0][0] for s in samples])
raw_vb.extend([s[1][0] for s in samples])
now = time.time()
if now >= next_time:
if self.cal_applied.get():
raw_va_cal = [(v * self.calibration['A']['gain']) + self.calibration['A']['offset'] for v in raw_va]
raw_vb_cal = [(v * self.calibration['B']['gain']) + self.calibration['B']['offset'] for v in raw_vb]
else:
raw_va_cal = raw_va
raw_vb_cal = raw_vb
voltage_a = sum(raw_va_cal) / len(raw_va_cal) if raw_va_cal else 0
voltage_b = sum(raw_vb_cal) / len(raw_vb_cal) if raw_vb_cal else 0
shunt_voltage = voltage_a - voltage_b
current = shunt_voltage / self.shunt_resistor if self.shunt_resistor > 0 else 0
current_time = now - self.start_time
self.data_queue.put((current_time, voltage_a, voltage_b, current))
next_time += self.interval
raw_va.clear()
raw_vb.clear()
time.sleep(0.01)
except DeviceDisconnectedError as e:
self.root.after(0, lambda err=e: self.handle_device_error(err))
break
except Exception as e:
self.root.after(0, lambda err=e: self.handle_device_error(f"Measurement error: {err}"))
break
def toggle_measurement(self):
"""Start/Stop measurement logging"""
if not self.measuring:
try:
self.interval = max(0.1, float(self.interval_entry.get()))
self.shunt_resistor = max(0.001, float(self.shunt_entry.get()))
except ValueError as e:
messagebox.showerror("Input Error", f"Invalid number: {str(e)}")
return
# Clear all data buffers when starting new measurement
self.time_data.clear()
self.voltage_a_data.clear()
self.voltage_b_data.clear()
self.current_data.clear()
# Setup new log file
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
self.filename = os.path.join(self.log_dir, f"measurement_{timestamp}.csv")
try:
with open(self.filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(["Time(s)", "Voltage_A(V)", "Voltage_B(V)", "Shunt_Voltage(V)", "Current(A)", "Calibration_Applied"])
except Exception as e:
messagebox.showerror("File Error", f"Cannot create file: {str(e)}")
return
# Reset data buffers
self.time_data.clear()
self.voltage_a_data.clear()
self.voltage_b_data.clear()
self.current_data.clear()
self.data_index = 0
# Start measurement
self.measuring = True
self.start_button.config(text="STOP LOGGING", style='Warning.TButton')
self.status_var.set(f"Logging to: {os.path.basename(self.filename)}")
self.start_time = time.time()
if not self.measurement_event.is_set():
self.start_measurement_thread()
else:
self.measuring = False
self.start_button.config(text="START LOGGING", style='TButton')
self.status_var.set(f"Ready | Last file: {os.path.basename(self.filename)}")
def toggle_calibration(self):
"""Toggle calibration application and update display"""
self.update_calibration_display()
def run_calibration(self):
"""Start calibration procedure with proper initialization"""
if hasattr(self, 'cal_window') and self.cal_window.winfo_exists():
self.cal_window.lift()
return
# Store current state
self.original_measuring = self.measuring
self.original_cal_state = self.cal_applied.get()
self.cal_applied.set(False)
# Create calibration window
self.cal_window = tk.Toplevel(self.root)
self.cal_window.title("Calibration")
self.cal_window.geometry("500x500")
self.cal_window.configure(bg=self.bg_color)
# Calibration instructions
ttk.Label(self.cal_window, text="ADALM1000 Calibration", style='Header.TLabel').pack(pady=10)
steps = [
"1. Short both CHA and CHB inputs",
"2. Click 'Measure Zero' when ready",
"3. Apply known voltage to CHA",
"4. Enter reference voltage and click 'Calibrate CHA'",
"5. Repeat for CHB if needed",
"6. Click 'Save Calibration' when done"
]
for step in steps:
ttk.Label(self.cal_window, text=step, style='TLabel').pack(anchor=tk.W, padx=20, pady=2)
# Calibration controls
btn_frame = ttk.Frame(self.cal_window)
btn_frame.pack(pady=10)
ttk.Button(btn_frame, text="Measure Zero",
command=self.start_zero_calibration).pack(side=tk.LEFT, padx=5)
self.ref_voltage = ttk.Entry(btn_frame, width=8)
self.ref_voltage.pack(side=tk.LEFT, padx=5)
self.ref_voltage.insert(0, "3.3")
ttk.Button(btn_frame, text="Calibrate CHA",
command=lambda: self.start_channel_calibration('A')).pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame, text="Calibrate CHB",
command=lambda: self.start_channel_calibration('B')).pack(side=tk.LEFT, padx=5)
# Save/Load buttons
btn_frame2 = ttk.Frame(self.cal_window)
btn_frame2.pack(pady=10)
self.save_cal_btn = ttk.Button(btn_frame2, text="Save Calibration",
command=self.save_calibration)
self.save_cal_btn.pack(side=tk.LEFT, padx=5)
ttk.Button(btn_frame2, text="Load Calibration",
command=self.load_calibration).pack(side=tk.LEFT, padx=5)
# Results display
self.cal_results = tk.Text(self.cal_window, height=10, width=50,
bg="#3B4252", fg=self.fg_color,
font=('Monospace', 9))
self.cal_results.pack(pady=10)
self.cal_results.insert(tk.END, "Calibration results will appear here...")
self.cal_results.config(state=tk.DISABLED)
self.cal_window.protocol("WM_DELETE_WINDOW", self.on_cal_window_close)
def start_zero_calibration(self):
"""Start zero calibration in a thread with status updates"""
self.update_cal_results("Starting zero measurement...")
threading.Thread(target=self.measure_zero, daemon=True).start()
def start_channel_calibration(self, channel):
"""Start channel calibration in a thread with validation"""
try:
ref_voltage = float(self.ref_voltage.get())
if ref_voltage <= 0:
raise ValueError("Reference voltage must be positive")
self.update_cal_results(f"Starting {channel} calibration with {ref_voltage}V reference...")
threading.Thread(
target=lambda: self.calibrate_channel(channel, ref_voltage),
daemon=True
).start()
except ValueError as e:
self.update_cal_results(f"Error: {str(e)}")
def measure_zero(self):
"""Perform zero offset measurement with averaging"""
try:
with self.device_lock:
# Take multiple samples for stability
samples = []
for _ in range(5): # Take 5 batches of samples
batch = self.dev.read(20, 500, True) # 20 samples, 500ms timeout
if batch:
samples.extend(batch)
time.sleep(0.1)
if not samples:
raise Exception("No valid samples received")
# Calculate averages
voltage_a = np.mean([s[0][0] for s in samples])
voltage_b = np.mean([s[1][0] for s in samples])
# Update calibration
self.calibration['A']['offset'] = -voltage_a
self.calibration['B']['offset'] = -voltage_b
# Update results
result_text = (
f"Zero Calibration Complete:\n"
f"Channel A Offset: {-voltage_a:.6f} V\n"
f"Channel B Offset: {-voltage_b:.6f} V\n\n"
f"Now apply known voltage and calibrate each channel."
)
self.update_cal_results(result_text)
except Exception as e:
self.update_cal_results(f"Zero Calibration Failed:\n{str(e)}")
def calibrate_channel(self, channel, ref_voltage):
"""Perform gain calibration for specified channel"""
try:
with self.device_lock:
# Take multiple samples for stability
samples = []
for _ in range(5): # Take 5 batches of samples
batch = self.dev.read(20, 500, True) # 20 samples, 500ms timeout
if batch:
samples.extend(batch)
time.sleep(0.1)
if not samples:
raise Exception("No valid samples received")
# Calculate average measured voltage
measured = np.mean([s[0][0] if channel == 'A' else s[1][0] for s in samples])
if abs(measured) < 0.01: # Sanity check
raise Exception(f"Measured voltage too low ({measured:.4f}V) - check connection")
# Calculate and apply gain
gain = ref_voltage / measured
self.calibration[channel]['gain'] = gain
# Update results
result_text = (
f"Channel {channel} Calibration Complete:\n"
f"Reference Voltage: {ref_voltage:.4f} V\n"
f"Measured Voltage: {measured:.6f} V\n"
f"Calculated Gain: {gain:.6f}\n\n"
f"Don't forget to Save Calibration!"
)
self.update_cal_results(result_text)
except Exception as e:
self.update_cal_results(f"Channel {channel} Calibration Failed:\n{str(e)}")
def load_calibration(self):
"""Load calibration from file and update display"""
config_file = os.path.join(self.config_dir, "calibration.json")
if os.path.exists(config_file):
try:
with open(config_file, 'r') as f:
data = json.load(f)
if 'A' in data and 'B' in data:
self.calibration = data
self.cal_applied.set(True)
self.update_calibration_display()
return True
except Exception as e:
print(f"Failed to load calibration: {str(e)}")
return False
def update_calibration_display(self):
"""Update calibration status display"""
if self.cal_applied.get():
if self.calibration == {'A': {'gain': 1.0, 'offset': 0.0}, 'B': {'gain': 1.0, 'offset': 0.0}}:
self.cal_status.config(text="No calibration", foreground=self.warning_color)
else:
self.cal_status.config(text="Calibration active", foreground=self.success_color)
else:
self.cal_status.config(text="Calibration available", foreground=self.warning_color)
def update_cal_results(self, text):
"""Thread-safe results display update"""
if hasattr(self, 'cal_results'):
self.cal_results.config(state=tk.NORMAL)
self.cal_results.delete(1.0, tk.END)
self.cal_results.insert(tk.END, text)
self.cal_results.config(state=tk.DISABLED)
self.cal_window.update()
def save_calibration(self):
"""Save calibration to file with error handling"""
try:
config_file = os.path.join(self.config_dir, "calibration.json")
os.makedirs(self.config_dir, exist_ok=True)
with open(config_file, 'w') as f:
json.dump(self.calibration, f, indent=4)
self.update_cal_results(
f"Calibration successfully saved to:\n{config_file}\n\n"
f"Channel A: Gain={self.calibration['A']['gain']:.6f}, Offset={self.calibration['A']['offset']:.6f}V\n"
f"Channel B: Gain={self.calibration['B']['gain']:.6f}, Offset={self.calibration['B']['offset']:.6f}V"
)
return True
except Exception as e:
self.update_cal_results(f"Failed to save calibration:\n{str(e)}")
return False
def on_cal_window_close(self):
"""Handle calibration window closing properly"""
if hasattr(self, 'cal_window'):
# Restore original states
if hasattr(self, 'original_cal_state'):
self.cal_applied.set(self.original_cal_state)
# Clean up window
self.cal_window.destroy()
del self.cal_window
# Restart logging if it was running
if hasattr(self, 'original_measuring') and self.original_measuring:
self.toggle_measurement()
def update_measurement_display(self, voltage_a, voltage_b_ref, shunt_voltage, current):
self.voltage_a_label.config(text=f"{voltage_a:.4f}")
self.voltage_b_ref_label.config(text=f"{voltage_b_ref:.4f}")
self.shunt_voltage_label.config(text=f"{shunt_voltage:.4f}")
self.current_label.config(text=f"{current:.4f}")
status = "Logging" if self.measuring else "Measuring"
if self.cal_applied.get():
status += " | CALIBRATED"
self.status_var.set(f"{status} | Interval: {self.interval}s | Shunt: {self.shunt_resistor}Ω")
def update_plot(self):
"""Update the plot with current data"""
if not self.time_data:
return
self.line_a.set_data(self.time_data, self.voltage_a_data)
self.line_current.set_data(self.time_data, self.current_data)
# Auto-scale the plot
self.ax.relim()
self.ax.autoscale_view()
self.ax2.relim()
self.ax2.autoscale_view()
# Set x-axis to show from 0 to current max time
if len(self.time_data) > 1:
self.ax.set_xlim(0, self.time_data[-1])
self.ax2.set_xlim(0, self.time_data[-1])
self.canvas.draw()
def save_measurement_data(self, voltage_a, voltage_b, shunt_voltage, current):
"""Save measurement data to file"""
with open(self.filename, 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([
f"{time.time()-self.start_time:.3f}",
f"{voltage_a:.6f}",
f"{voltage_b:.6f}",
f"{shunt_voltage:.6f}",
f"{current:.6f}",
str(self.cal_applied.get())
])
def on_close(self):
"""Clean up on window close"""
self.measurement_event.clear()
if hasattr(self, 'measurement_thread'):
self.measurement_thread.join(timeout=1.0)
if hasattr(self, 'queue_listener'):
self.queue_listener.join(timeout=0.5)
if hasattr(self, 'session') and self.session:
try:
if self.session_active:
self.session.end()
except:
pass
# Clean UI state without triggering error popup
self.status_var.set("Session ended. Closing...")
if hasattr(self, 'status_light') and hasattr(self, 'status_indicator'):
self.status_light.itemconfig(self.status_indicator, fill='red')
if hasattr(self, 'connection_label'):
self.connection_label.config(text="Disconnected")
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
try:
app = ModernADALM1000Logger(root)
root.mainloop()
except Exception as e:
if root.winfo_exists():
messagebox.showerror("Fatal Error", f"Application failed: {str(e)}")
else:
print(f"Fatal Error: {e}")
try:
root.destroy()
except:
pass