diff --git a/MainCode/adalm1000_logger.py b/MainCode/adalm1000_logger.py index 06e6694..88efa5f 100644 --- a/MainCode/adalm1000_logger.py +++ b/MainCode/adalm1000_logger.py @@ -75,9 +75,9 @@ class MeasurementThread(QThread): raw_voltage = np.mean([s[1][0] for s in samples]) raw_current = np.mean([s[0][1] for s in samples]) - if voltage_window.size() >= self.filter_window_size: - voltage_window.append - current_window.append + if len(voltage_window) >= self.filter_window_size: + voltage_window.popleft() + current_window.popleft() voltage_window.append(raw_voltage) current_window.append(raw_current) @@ -614,14 +614,19 @@ class BatteryTester(QMainWindow): self.cleanup_device() try: - # Delay to avoid "Device busy" errors - time.sleep(1.5) - - self.session = pysmu.Session(ignore_dataflow=True, queue_size=10000) - if not self.session.devices: - raise Exception("No ADALM1000 detected - check connection") + print("Waiting before initializing session...") + time.sleep(1.5) # Delay helps avoid "device busy" issues + self.session = pysmu.Session(ignore_dataflow=True, queue_size=10000) + + # 🔍 Log detected devices + print(f"Devices found: {self.session.devices}") + + if not self.session.devices: + raise Exception("No ADALM1000 detected - check USB connection") + self.dev = self.session.devices[0] + # Reset channels self.dev.channels['A'].mode = pysmu.Mode.HI_Z self.dev.channels['B'].mode = pysmu.Mode.HI_Z @@ -630,6 +635,7 @@ class BatteryTester(QMainWindow): self.session.start(0) + # Update UI self.status_light.setStyleSheet("background-color: green; border-radius: 10px;") self.connection_label.setText("Connected") self.status_bar.setText("Device connected | Ready for measurement") @@ -644,17 +650,19 @@ class BatteryTester(QMainWindow): def cleanup_device(self): """Clean up device resources.""" + print("Cleaning up device session...") + # Stop measurement thread if self.measurement_thread is not None: try: self.measurement_thread.stop() - if not self.measurement_thread.wait(1000): # 1 second timeout + if not self.measurement_thread.wait(1000): print("Warning: Measurement thread didn't stop cleanly") self.measurement_thread = None except Exception as e: print(f"Error stopping measurement thread: {e}") - - # Clean up session + + # Stop and delete session if hasattr(self, 'session'): try: if self.session_active: @@ -662,12 +670,13 @@ class BatteryTester(QMainWindow): self.session.end() self.session_active = False del self.session + print("Session ended successfully") except Exception as e: - print(f"Error cleaning up session: {e}") + print(f"Error ending session: {e}") finally: self.session_active = False - # Reset UI status + # Reset UI indicators self.status_light.setStyleSheet("background-color: red; border-radius: 10px;") self.connection_label.setText("Disconnected") self.start_button.setEnabled(False) @@ -878,10 +887,10 @@ class BatteryTester(QMainWindow): def update_measurements(self, voltage: float, current: float, current_time: float): """Update measurements in the UI.""" - if self.time_data.size() > 10000: # Limit data points - self.time_data.append - self.voltage_data.append - self.current_data.append + if len(self.time_data) > 10000: # Limit data points + self.time_data.popleft() + self.voltage_data.popleft() + self.current_data.popleft() self.time_data.append(current_time) self.voltage_data.append(voltage) @@ -893,7 +902,7 @@ class BatteryTester(QMainWindow): self.time_label.setText(self.format_time(current_time)) # Update plot periodically - if self.time_data.size() % 10 == 0: + if len(self.time_data) % 10 == 0: self.update_plot() # Log data if test is running