Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions software/control/core_displacement_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def __init__(self, x_offset=0, y_offset=0, x_scaling=1, y_scaling=1, N_average=1
self.t_array = np.array([])
self.x_array = np.array([])
self.y_array = np.array([])
# Cache the coordinate grids across frames of the same size (see update_measurement).
self._grid_shape = None
self._xgrid = None
self._ygrid = None

def update_measurement(self, image):

Expand All @@ -43,23 +47,36 @@ def update_measurement(self, image):
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

h, w = image.shape
x, y = np.meshgrid(range(w), range(h))
# Recomputing the meshgrid every frame is the bulk of the per-frame CPU cost; on the main
# (UI) thread this is what froze the autofocus live view. Cache it across same-size frames.
if self._grid_shape != (h, w):
self._xgrid, self._ygrid = np.meshgrid(np.arange(w), np.arange(h))
self._grid_shape = (h, w)

I = image.astype(float)
I = I - np.amin(I)
I[I / np.amax(I) < 0.2] = 0
x = np.sum(x * I) / np.sum(I)
y = np.sum(y * I) / np.sum(I)
peak = np.amax(I)
if peak > 0:
I[I / peak < 0.2] = 0
total = np.sum(I)
if total > 0:
x = np.sum(self._xgrid * I) / total
y = np.sum(self._ygrid * I) / total
else:
x = y = 0.0 # blank/uniform frame: no spot -> avoid 0/0 nan spam

x = x - self.x_offset
y = y - self.y_offset
x = x * self.x_scaling
y = y * self.y_scaling

self.t_array = np.append(self.t_array, t)
self.x_array = np.append(self.x_array, x)
self.y_array = np.append(self.y_array, y)
# Keep only the last N samples. The original appended without trimming, so the arrays (and
# the plot data emitted every frame) grew without bound -> the UI thread eventually froze.
self.t_array = np.append(self.t_array, t)[-self.N :]
self.x_array = np.append(self.x_array, x)[-self.N :]
self.y_array = np.append(self.y_array, y)[-self.N :]

self.signal_plots.emit(self.t_array[-self.N :], np.vstack((self.x_array[-self.N :], self.y_array[-self.N :])))
self.signal_plots.emit(self.t_array, np.vstack((self.x_array, self.y_array)))
self.signal_readings.emit([np.mean(self.x_array[-self.N_average :]), np.mean(self.y_array[-self.N_average :])])

def update_settings(self, x_offset, y_offset, x_scaling, y_scaling, N_average, N):
Expand Down
16 changes: 16 additions & 0 deletions software/control/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -2759,10 +2759,15 @@ def init_ui(self):
self.analog_gain_spinbox.setValue(self.laserAutofocusController.laser_af_properties.focus_camera_analog_gain)
analog_gain_layout.addWidget(self.analog_gain_spinbox)

# Restore Full FOV button: undo the laser-AF spot crop so the whole focus-camera
# sensor is visible again (useful for re-locating the spot before re-initializing).
self.btn_restore_full_fov = QPushButton("Restore Full FOV")

# Add to live group
live_layout.addWidget(self.btn_live)
live_layout.addLayout(exposure_layout)
live_layout.addLayout(analog_gain_layout)
live_layout.addWidget(self.btn_restore_full_fov)
live_group.setLayout(live_layout)

# Non-threshold property group
Expand Down Expand Up @@ -2864,6 +2869,7 @@ def init_ui(self):
self.run_spot_detection_button.clicked.connect(self.run_spot_detection)
self.initialize_button.clicked.connect(self.apply_and_initialize)
self.characterization_checkbox.toggled.connect(self.toggle_characterization_mode)
self.btn_restore_full_fov.clicked.connect(self.restore_full_fov)

def _add_spinbox(
self,
Expand Down Expand Up @@ -2926,6 +2932,16 @@ def update_exposure_time(self, value):
def update_analog_gain(self, value):
self.signal_newAnalogGain.emit(value)

def restore_full_fov(self):
"""Reset the focus camera ROI to the full sensor FOV (undo the laser-AF spot crop)."""
try:
camera = self.laserAutofocusController.camera
width, height = camera.get_resolution()
camera.set_region_of_interest(0, 0, width, height)
self._log.info(f"Restored focus camera to full FOV: {width} x {height}")
except Exception as e:
self._log.error(f"Failed to restore full FOV: {e}")

def update_values(self):
"""Update all widget values from the controller properties"""
self.clear_labels()
Expand Down
Loading