From 715b8ba4179393403045ba6d330455dbec28ae0a Mon Sep 17 00:00:00 2001 From: appleb_m Date: Wed, 29 Apr 2026 17:20:57 +0200 Subject: [PATCH] mount_handling: added mount_handler and mount_fail_handler as well as redis mount_count, WIP --- src/aare/daq/config.py | 12 ++++++++++ src/aare/daq/daq.py | 53 ++++++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/aare/daq/config.py b/src/aare/daq/config.py index 1633866f..ca337072 100644 --- a/src/aare/daq/config.py +++ b/src/aare/daq/config.py @@ -717,6 +717,18 @@ class BeamlineConfig: else: self.__client.set(f"{self.__bl}:last_best_b_factor", last_best_b_factor) + def get_mount_fail_count(self) -> int: + value = self.__client.get(f"{self.__bl}:mount_fail_count") + return int(value) if value else 0 + + def record_mount_failure(self) -> int: + # Atomic increment + return self.__client.incr(f"{self.__bl}:mount_fail_count") + + def record_mount_success(self): + # Atomic delete/reset + self.__client.delete(f"{self.__bl}:mount_fail_count") + @property def simple_input_parameters(self) -> SimpleStrategyInputModel | None: tmp = self.__client.get(f"{self.__bl}:simple_input_params") diff --git a/src/aare/daq/daq.py b/src/aare/daq/daq.py index 00601291..ab244875 100644 --- a/src/aare/daq/daq.py +++ b/src/aare/daq/daq.py @@ -1,8 +1,6 @@ import copy import secrets import time -import traceback - from datetime import datetime from math import ceil, floor from pathlib import Path @@ -12,14 +10,13 @@ import cv2 import numpy as np from aareDB import SampleEventType from jfjoch_client.exceptions import NotFoundException - from jfjoch_client import ScanResult, ScanResultImagesInner import aare.common.face_detection as fd from aare.daq import workflows from aare.daq.aaredb import AareWrapper -from aare.common.autofocus_tools import focus_measure_edges -from aare.daq.config import BeamlineConfig, ABR_POS_MOUNT, ABR_OMEGA_MOUNT + +from aare.daq.config import BeamlineConfig, ABR_POS_MOUNT from aare.daq.config import BeamlineStateEnum from aare.daq.devices import BeamlineDevices from aare.daq.mlbox import MlBox, MLBoxPredictionResult, MLBoxPredictionsResult @@ -922,17 +919,41 @@ class AareDAQ: raise Exception("Goniometer is not in position based on magnet position sensor readout") - def __mount_handler(self, target : SampleShortInfo, mount_attempted: bool = False): + def __mount_fail_handler( + self, + mount_error: bool = False, + dry_after_fail_count: int = 3, + end_automation_after_fail_count: int = 5 + ): + if mount_error: + count = self.__cfg.record_mount_failure() + if count > end_automation_after_fail_count: + logger.error(f"Mounting continued to fail after stopping automation, contact your local contact") + logger.debug(f"Clearing mount fail count") + self.__cfg.record_mount_success() + raise CriticalTellException(f"Mounting continued to fail after stopping automation, contact your local contact.") + elif count == end_automation_after_fail_count: + logger.error(f"Mounting failed {count} times, stopping automation") + raise CriticalTellException(f"Mount failed {count} times in a row, stopping automation.") + elif count == dry_after_fail_count: + logger.warning(f"Mount failed {count} times in a row; drying") + try: + self.park_and_dry() + except Exception as e: + logger.exception(f"Failed to dry after mount failure: {e}") + else: + return + else: + logger.debug("Mounting succeeded, resetting mount failure counter") + self.__cfg.record_mount_success() + + def __mount_handler(self, target : SampleShortInfo): value = self.__devs.tell.mount(address=target.tell_address(), force=True, auto_unmount=True, read_dm=False, wait=True, timeout=360.0) logger.debug(f"Mount response: {value.value}") if value == TellEventValueEnum.NO_PIN_IN_GRIPPER: - if mount_attempted: - raise MountingFailed("No Pin in Gripper") - else: - logger.warning("No Pin in Gripper - retrying mounting") - self.__mount_handler(target, mount_attempted=True) + raise MountingFailed("No Pin in Gripper") elif value == TellEventValueEnum.PIN_IS_LOST_GRIPPER: logger.error("Pin is lost") raise MountingFailed("Pin is lost") @@ -970,14 +991,16 @@ class AareDAQ: f"and {log_msg}") logger.debug(log_msg) self.__mount_handler(target) + self.__mount_fail_handler(mount_error=False) self.__cfg.current_sample = target - except TellMountFailedException as e: - logger.error(f"Failed to mount: {target} due to exception: {e}") + except CriticalTellException as e: + logger.error(f"Critical Tell error: {e}") + self.__mount_fail_handler(mount_error=True) raise except Exception as e: logger.error(f"Mount failed: {e}") - - raise MountingFailed(f"Failed to mount: {e}") + self.__mount_fail_handler(mount_error=True) + raise def recovery_unmount_sample(self) -> None: