init uv, evaluation pipeline
This commit is contained in:
156
evaluation/calibration/frame.py
Normal file
156
evaluation/calibration/frame.py
Normal file
@@ -0,0 +1,156 @@
|
||||
import time
|
||||
import statistics
|
||||
import threading
|
||||
from tkinter import messagebox, ttk
|
||||
|
||||
class CalibrationFrame(ttk.Frame):
|
||||
def __init__(self, master, serial_reader, **kwargs):
|
||||
super().__init__(master, **kwargs)
|
||||
self.serial_reader = serial_reader
|
||||
self.calibration_in_progress = False
|
||||
|
||||
# UI Components
|
||||
self.status_label = ttk.Label(self, text="Ready for calibration")
|
||||
self.status_label.pack(pady=10)
|
||||
|
||||
self.calibration_factor_label = ttk.Label(self, text="Calibration Factor: Not set")
|
||||
self.calibration_factor_label.pack(pady=5)
|
||||
|
||||
self.std_label = ttk.Label(self, text="Standard Deviation: Not calculated")
|
||||
self.std_label.pack(pady=5)
|
||||
|
||||
self.calibrate_button = ttk.Button(self, text="Start Calibration", command=self.start_calibration)
|
||||
self.calibrate_button.pack(pady=10)
|
||||
|
||||
self.reset_button = ttk.Button(self, text="Reset Calibration", command=self.reset_calibration)
|
||||
self.reset_button.pack(pady=5)
|
||||
|
||||
self.progress_bar = ttk.Progressbar(self, length=300, mode='determinate')
|
||||
self.progress_bar.pack(pady=5)
|
||||
|
||||
def start_calibration(self):
|
||||
if self.calibration_in_progress:
|
||||
return
|
||||
|
||||
# Check if serial reader is connected - try multiple ways to check connection
|
||||
is_connected = self.serial_reader.is_connected
|
||||
|
||||
if not is_connected:
|
||||
messagebox.showerror("Error", "Please connect to device first!")
|
||||
return
|
||||
|
||||
# Start calibration routine
|
||||
self.calibration_in_progress = True
|
||||
self.calibrate_button.config(state='disabled')
|
||||
self.progress_bar['value'] = 0
|
||||
|
||||
# Show initial prompt
|
||||
result = messagebox.askokcancel(
|
||||
"Calibration Procedure",
|
||||
"Step 1: Remove all weights from the scale and press OK to continue."
|
||||
)
|
||||
|
||||
if not result:
|
||||
self.calibration_in_progress = False
|
||||
self.calibrate_button.config(state='normal')
|
||||
return
|
||||
|
||||
# Wait a moment for scale to settle
|
||||
self.status_label.config(text="Waiting for scale to settle...")
|
||||
self.update_idletasks()
|
||||
time.sleep(2)
|
||||
|
||||
# Show second prompt
|
||||
result = messagebox.askokcancel(
|
||||
"Calibration Procedure",
|
||||
"Step 2: Place the 100g calibration weight on the scale and press OK to start measurement."
|
||||
)
|
||||
|
||||
if not result:
|
||||
self.calibration_in_progress = False
|
||||
self.calibrate_button.config(state='normal')
|
||||
self.status_label.config(text="Calibration cancelled")
|
||||
return
|
||||
|
||||
# Start measurement thread
|
||||
try:
|
||||
self.status_label.config(text="Collecting calibration data (30 seconds)...")
|
||||
|
||||
# Collect data for 30 seconds
|
||||
start_time = time.time()
|
||||
duration = 30.0
|
||||
|
||||
self.serial_reader.calibrating = True
|
||||
|
||||
while time.time() - start_time < duration:
|
||||
# Update progress bar
|
||||
elapsed = time.time() - start_time
|
||||
progress = (elapsed / duration) * 100
|
||||
self.progress_bar['value'] = progress
|
||||
self.update_idletasks()
|
||||
|
||||
self.progress_bar['value'] = 100
|
||||
|
||||
calibration_readings = self.serial_reader.calib_window
|
||||
self.serial_reader.calibrating = False
|
||||
|
||||
if len(calibration_readings) == 0:
|
||||
messagebox.showerror("Error", "No readings collected. Check device connection.")
|
||||
self.calibration_in_progress = False
|
||||
self.calibrate_button.config(state='normal')
|
||||
self.status_label.config(text="Calibration failed")
|
||||
return
|
||||
|
||||
# Calculate statistics
|
||||
mean_reading = statistics.mean(calibration_readings)
|
||||
std_reading = statistics.stdev(calibration_readings) if len(calibration_readings) > 1 else 0
|
||||
|
||||
# Calculate calibration factor (100g / mean_reading)
|
||||
calibration_factor = 100.0 / mean_reading
|
||||
|
||||
# Set calibration factor in serial reader - try multiple ways
|
||||
self.serial_reader.calib_factor = calibration_factor
|
||||
|
||||
# Update UI
|
||||
self.status_label.config(text="Calibration complete!")
|
||||
self.calibration_factor_label.config(text=f"Calibration Factor: {calibration_factor:.2f}")
|
||||
self.std_label.config(text=f"Standard Deviation: {std_reading:.2f} (raw units)")
|
||||
|
||||
# Show results
|
||||
messagebox.showinfo(
|
||||
"Calibration Complete",
|
||||
f"Calibration successful!\n\n"
|
||||
f"Samples collected: {len(calibration_readings)}\n"
|
||||
f"Mean reading: {mean_reading:.2f} (raw units)\n"
|
||||
f"Standard deviation: {std_reading:.2f} (raw units)\n"
|
||||
f"Calibration factor: {calibration_factor:.2f}\n\n"
|
||||
f"The scale is now calibrated for 100g."
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
messagebox.showerror("Error", f"Calibration failed: {str(e)}")
|
||||
self.status_label.config(text="Calibration failed")
|
||||
|
||||
finally:
|
||||
self.calibration_in_progress = False
|
||||
self.calibrate_button.config(state='normal')
|
||||
self.progress_bar['value'] = 0
|
||||
|
||||
def reset_calibration(self):
|
||||
"""Reset calibration factor and UI display"""
|
||||
# Reset calibration factor in serial reader
|
||||
if hasattr(self.serial_reader, 'set_calibration_factor'):
|
||||
self.serial_reader.set_calibration_factor(1.0)
|
||||
elif hasattr(self.serial_reader, 'calibration_factor'):
|
||||
self.serial_reader.calibration_factor = 1.0
|
||||
elif hasattr(self.serial_reader, 'calib_factor'):
|
||||
self.serial_reader.calib_factor = 1.0
|
||||
elif hasattr(self.serial_reader, '_calibration_factor'):
|
||||
self.serial_reader._calibration_factor = 1.0
|
||||
|
||||
# Reset UI
|
||||
self.status_label.config(text="Ready for calibration")
|
||||
self.calibration_factor_label.config(text="Calibration Factor: Not set")
|
||||
self.std_label.config(text="Standard Deviation: Not calculated")
|
||||
self.current_reading_label.config(text="Current reading: --")
|
||||
self.progress_bar['value'] = 0
|
||||
Reference in New Issue
Block a user