MainCode/adalm1000_logger.py aktualisiert

working but very sluggish
D
This commit is contained in:
Jan 2025-08-08 19:19:47 +02:00
parent 716955900b
commit 8086784b69

View File

@ -88,6 +88,10 @@ class DeviceManager:
self.start_time = None self.start_time = None
self.last_measurement_time = None self.last_measurement_time = None
def start_measurement(self):
if not self.measurement_thread.isRunning():
self.measurement_thread.start()
def reset_data(self): def reset_data(self):
"""Reset all data buffers and statistics for the device""" """Reset all data buffers and statistics for the device"""
self.time_data.clear() self.time_data.clear()
@ -647,7 +651,7 @@ class BatteryTester(QMainWindow):
# Status update timer # Status update timer
self.status_timer = QTimer() self.status_timer = QTimer()
self.status_timer.timeout.connect(self.update_status_and_plot) self.status_timer.timeout.connect(self.update_status_and_plot)
self.status_timer.start(1000) #every second self.status_timer.start(200) #every 200 ms
def print_device_status(self): def print_device_status(self):
"""Debug method to print current device states""" """Debug method to print current device states"""
@ -1325,6 +1329,10 @@ class BatteryTester(QMainWindow):
first_serial = next(iter(self.devices.keys())) first_serial = next(iter(self.devices.keys()))
self.active_device = self.devices[first_serial] self.active_device = self.devices[first_serial]
if self.active_device:
if not self.active_device.measurement_thread.isRunning():
self.active_device.measurement_thread.start()
# Update UI # Update UI
self.device_combo.clear() self.device_combo.clear()
for serial in self.devices: for serial in self.devices:
@ -1535,24 +1543,31 @@ class BatteryTester(QMainWindow):
self.status_bar.showMessage(f"Switched to device: {serial}") self.status_bar.showMessage(f"Switched to device: {serial}")
self.set_connection_status(f"Connected: {serial}", self.status_colors["connected"]) self.set_connection_status(f"Connected: {serial}", self.status_colors["connected"])
if not self.active_device.measurement_thread.isRunning():
self.active_device.measurement_thread.start()
def update_ui_from_active_device(self): def update_ui_from_active_device(self):
dev = self.active_device dev = self.active_device
if not dev: if not dev:
return return
with self.plot_mutex: with self.plot_mutex:
# Kopiere aktuelle Daten # Copy current data
x = list(dev.display_time_data) x = list(dev.display_time_data)
y_v = list(dev.display_voltage_data) y_v = list(dev.display_voltage_data)
y_c = list(dev.display_current_data) y_c = list(dev.display_current_data)
# Aktualisiere Plot # Update plot
self.line_voltage.set_data(x, y_v) self.line_voltage.set_data(x, y_v)
self.line_current.set_data(x, y_c) self.line_current.set_data(x, y_c)
self.auto_scale_axes()
# Fix: Pass required arguments to auto_scale_axes
if x and y_v and y_c: # Only call if data exists
self.auto_scale_axes(x, y_v, y_c)
self.canvas.draw_idle() self.canvas.draw_idle()
# Aktualisiere Labels # Update labels
if dev.voltage_data: if dev.voltage_data:
v = dev.voltage_data[-1] v = dev.voltage_data[-1]
i = dev.current_data[-1] i = dev.current_data[-1]
@ -1572,7 +1587,6 @@ class BatteryTester(QMainWindow):
try: try:
device = self.devices.get(serial) device = self.devices.get(serial)
if not device: if not device:
logger.error(f"Device {serial} not found")
return return
# Ensure timing is initialized # Ensure timing is initialized
@ -1593,12 +1607,11 @@ class BatteryTester(QMainWindow):
logger.warning(f"Invalid values - V: {voltage:.3f}, I: {current:.3f}") logger.warning(f"Invalid values - V: {voltage:.3f}, I: {current:.3f}")
return return
# Update data buffers with self.plot_mutex:
device.time_data.append(elapsed) device.time_data.append(elapsed)
device.voltage_data.append(voltage) device.voltage_data.append(voltage)
device.current_data.append(current) device.current_data.append(current)
# Update display buffers
device.display_time_data.append(elapsed) device.display_time_data.append(elapsed)
device.display_voltage_data.append(voltage) device.display_voltage_data.append(voltage)
device.display_current_data.append(current) device.display_current_data.append(current)
@ -1623,12 +1636,8 @@ class BatteryTester(QMainWindow):
self.capacity_label.setText(f"{device.capacity_ah:.4f}") self.capacity_label.setText(f"{device.capacity_ah:.4f}")
self.energy_label.setText(f"{device.energy:.4f}") self.energy_label.setText(f"{device.energy:.4f}")
# Force plot update
self.update_plot()
except Exception as e: except Exception as e:
logger.error(f"Critical error in update_measurements: {str(e)}") logger.error(f"Update error: {str(e)}")
traceback.print_exc()
def adjust_downsampling(self): def adjust_downsampling(self):
current_length = len(self.time_data) current_length = len(self.time_data)
@ -2693,65 +2702,51 @@ class BatteryTester(QMainWindow):
self.update_plot() self.update_plot()
def update_plot(self): def update_plot(self):
"""Debugged plot update"""
if not self.active_device: if not self.active_device:
logger.warning("No active device in update_plot")
return return
try:
dev = self.active_device dev = self.active_device
# Get minimal data under lock
with self.plot_mutex: with self.plot_mutex:
if not dev.display_time_data: if not dev.display_time_data:
logger.warning(f"No data to plot for {dev.serial}")
return return
# Copy only the last 1000 points
x_data = list(dev.display_time_data)[-1000:]
y1_data = list(dev.display_voltage_data)[-1000:]
y2_data = list(dev.display_current_data)[-1000:]
x_data = list(dev.display_time_data) # Update plot data outside lock
y1_data = list(dev.display_voltage_data)
y2_data = list(dev.display_current_data)
self.line_voltage.set_data(x_data, y1_data) self.line_voltage.set_data(x_data, y1_data)
self.line_current.set_data(x_data, y2_data) self.line_current.set_data(x_data, y2_data)
# Auto-scale only when significant changes occur # Auto-scale only when needed
if len(x_data) > 1 and x_data[-1] - x_data[0] > 1.0: if len(x_data) > 1 and x_data[-1] - x_data[0] > 1.0:
self.auto_scale_axes() self.auto_scale_axes(x_data, y1_data, y2_data)
self.canvas.draw_idle() self.canvas.draw_idle()
logger.debug(f"Plot updated for {dev.serial} with {len(x_data)} points")
except Exception as e: def auto_scale_axes(self, x_data, y1_data, y2_data):
logger.error(f"Plot update failed: {str(e)}") """Safe auto-scaling that handles empty data"""
if not x_data:
def auto_scale_axes(self):
"""Auto-scale plot axes with appropriate padding and strict boundaries"""
if not self.active_device or not self.active_device.time_data:
return return
dev = self.active_device try:
min_time = 0 # Voltage scaling
max_time = dev.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 voltage_padding = 0.2
if dev.voltage_data: min_voltage = max(0, min(y1_data) - voltage_padding)
min_voltage = max(0, min(dev.voltage_data) - voltage_padding) max_voltage = min(5.0, max(y1_data) + voltage_padding)
max_voltage = min(5.0, max(dev.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 scaling
current_padding = 0.05 current_padding = 0.05
if dev.current_data: min_current = max(-0.25, min(y2_data) - current_padding)
min_current = max(-0.25, min(dev.current_data) - current_padding) max_current = min(0.25, max(y2_data) + current_padding)
max_current = min(0.25, max(dev.current_data) + current_padding)
current_ylim2 = self.ax2.get_ylim() # Apply new limits
if (abs(current_ylim2[0] - min_current) > 0.02 or abs(current_ylim2[1] - max_current) > 0.02): self.ax.set_xlim(min(x_data), max(x_data) * 1.05)
self.ax.set_ylim(min_voltage, max_voltage)
self.ax2.set_ylim(min_current, max_current) self.ax2.set_ylim(min_current, max_current)
except ValueError: # Handle empty sequences
pass
@pyqtSlot(str) @pyqtSlot(str)
def handle_device_error(self, error_msg): def handle_device_error(self, error_msg):