From 7b92686263e91d82c6e18282a91a88536ab1e93f Mon Sep 17 00:00:00 2001 From: appleb_m Date: Thu, 21 May 2026 10:18:04 +0200 Subject: [PATCH] DAQ: MAJOR: Refactored mounting service --- src/aare/daq/daq.py | 37 ++++++++----- src/aare/daq/operations/mounting/models.py | 5 +- src/aare/daq/operations/mounting/service.py | 61 ++++++++++++++++----- 3 files changed, 74 insertions(+), 29 deletions(-) diff --git a/src/aare/daq/daq.py b/src/aare/daq/daq.py index cb4f7f94..57fcd1c4 100644 --- a/src/aare/daq/daq.py +++ b/src/aare/daq/daq.py @@ -57,6 +57,7 @@ from aare.daq.operations.face_detection import FaceDetectionContext, FaceDetecti from aare.daq.operations.loop_centering import LoopCenteringService, LoopCenteringContext from aare.daq.operations.loop_centering.models import LoopCenteringSettings from aare.daq.operations.mounting import MountingContext, MountingService +from aare.daq.operations.mounting.models import MountingResult from aare.devices.area_detector import AutoEnum from aare.devices.jfjoch import JFJochWrapper @@ -680,7 +681,7 @@ class AareDAQ: #where possible/between operations. For example in raster, we may use XtalSnapshot to take a screenshot, then #return to Datacollection. #Busy stats are handled by the public function call i.e. sample() or by automation i.e. measure() - def _execute_mount_and_prepare(self, sample: SampleShortInfo) -> bool: + def _execute_mount_and_prepare(self, sample: SampleShortInfo | None) -> bool: """ Operation handler for executing mounting and take screenshot. @@ -710,28 +711,36 @@ class AareDAQ: if sample is not None and sample.db_id is not None: self.__aare.send_sample_event(sample, SampleEventType.MOUNTING) - mounting_result = self._create_mounting_service().execute(target=sample) + mounting_result: MountingResult = self._create_mounting_service().execute(target=sample) if not mounting_result.success: raise mounting_result.error or MountingFailed( mounting_result.comment or "Mount failed" ) - if previous_sample is not None and previous_sample.db_id is not None: + previous_sample = mounting_result.previous_sample or previous_sample + mounted_sample = mounting_result.mounted_sample or sample + + if ( + mounting_result.did_unmount_previous + and previous_sample is not None + and previous_sample.db_id is not None + ): self.__aare.send_sample_event(previous_sample, SampleEventType.UNMOUNTED) - self.__set_state(BeamlineStateEnum.SampleAlignment) + + self.__set_state(BeamlineStateEnum.SampleAlignment) + + if mounted_sample is not None and mounted_sample.db_id is not None: + self.__aare.send_sample_event(mounted_sample, SampleEventType.MOUNTED) + self.save_screenshot_db(mounted_sample.db_id, f"{mounted_sample.db_id}_mounted") - if sample is not None and sample.db_id is not None: - self.__aare.send_sample_event(sample, SampleEventType.MOUNTED) - self.save_screenshot_db(sample.db_id, f"{sample.db_id}_mounted") - self.__set_state(BeamlineStateEnum.SampleAlignment) return True except TransformationInvalidException as e: self._last_mount_error_message = str(e) or "Mount failed" logger.error(f"Mount failed due to invalid transformation: {e}") self._handle_operation_error( - operation=DAQOperation.MOUNT, + operation=DAQOperation.MOUNT if sample is not None else DAQOperation.UNMOUNT, sample=sample, error=e, event_type=SampleEventType.MOUNTFAILED, @@ -766,7 +775,7 @@ class AareDAQ: logger.exception("Failed to set state to SampleAlignment") finally: self._handle_operation_error( - operation=DAQOperation.MOUNT, + operation=DAQOperation.MOUNT if sample is not None else DAQOperation.UNMOUNT, sample=sample, error=e, event_type=SampleEventType.MOUNTFAILED @@ -1503,17 +1512,17 @@ class AareDAQ: if target is None: logger.debug("Unmounting sample") - type = "Unmount" + operation_name = "Unmount" else: logger.debug(f"Mounting sample: {target}") - type = "Mount" + operation_name = "Mount" if not self._execute_mount_and_prepare(target): current_sample = target or self.__cfg.current_sample sample_name = self._sample_mount_display_name(current_sample) if target is None: - raise UnmountingFailed(f"Failed to {type.lower()} {sample_name}") - raise MountingFailed(f"Failed to {type.lower()} {sample_name}") + raise UnmountingFailed(f"Failed to {operation_name.lower()} {sample_name}") + raise MountingFailed(f"Failed to {operation_name.lower()} {sample_name}") logger.info(f"Sample operation completed: {target}") self.__cfg.state_busy = False diff --git a/src/aare/daq/operations/mounting/models.py b/src/aare/daq/operations/mounting/models.py index ee260b68..a29783df 100644 --- a/src/aare/daq/operations/mounting/models.py +++ b/src/aare/daq/operations/mounting/models.py @@ -1,6 +1,7 @@ from dataclasses import dataclass from aare.common.coordinate import AerotechCoordinate +from aare.common.models import SampleShortInfo from aare.daq.config import BeamlineConfig from aare.daq.devices import BeamlineDevices @@ -15,8 +16,8 @@ class MountingContext: @dataclass class MountingResult: success: bool - mounted_sample: object | None = None - previous_sample: object | None = None + mounted_sample: SampleShortInfo | None = None + previous_sample: SampleShortInfo | None = None did_unmount_previous: bool = False error: Exception | None = None comment: str | None = None diff --git a/src/aare/daq/operations/mounting/service.py b/src/aare/daq/operations/mounting/service.py index 9290ecdb..b5bb78f5 100644 --- a/src/aare/daq/operations/mounting/service.py +++ b/src/aare/daq/operations/mounting/service.py @@ -61,8 +61,9 @@ class MountingService: if count == end_automation_after_fail_count: self.logger.error(f"Mounting failed {count} times, stopping automation") + self.logger.error("Unmounting sample and drying") try: - self.execute(target=None) + self._unmount_current_sample(timeout=60.0) self.dry(park=True) except Exception as e: self.logger.exception(f"Failed to dry after mount failure: {e}") @@ -110,6 +111,7 @@ class MountingService: TellEventValueEnum.ROBOT_CLEAR_AFTER_MOUNT, TellEventValueEnum.DRY, TellEventValueEnum.COLD, + TellEventValueEnum.SUCCESS, ): self.logger.info(f"Robot is {safe_val} - freeing beamline for user") return @@ -126,14 +128,21 @@ class MountingService: self.ctx.devs.tell.wait_not_busy() self.ctx.devs.tell.set_in_mount_position(True) + def _unmount_current_sample(self, timeout: float = 60.0): + previous_sample = self.ctx.cfg.current_sample + if previous_sample is not None: + self.logger.debug(f"Unmounting sample: {previous_sample}") + self.ctx.devs.tell.unmount(wait=True, timeout=timeout) + self.ctx.cfg.current_sample = None + return previous_sample + def dry(self, *, park: bool) -> None: self.ctx.devs.tell.check_enable_motion() self.ctx.devs.tell.wait_not_busy() self.ctx.devs.tell.set_in_mount_position(True) if self.ctx.cfg.current_sample: - self.ctx.devs.tell.unmount(wait=True, timeout=60.0) - self.ctx.cfg.current_sample = None + self._unmount_current_sample(timeout=60.0) if park: self.ctx.devs.tell.dry(wait_cold=-1, wait=True) @@ -141,33 +150,49 @@ class MountingService: self.ctx.devs.tell.dry(wait=True) def execute(self, *, target) -> MountingResult: + previous_sample = self.ctx.cfg.current_sample + try: self._prepare_mount_hardware() - previous_sample = self.ctx.cfg.current_sample if target is None: - if previous_sample is not None: - self.logger.debug(f"Unmounting sample: {previous_sample}") try: - self.ctx.devs.tell.unmount(wait=True, timeout=60.0) - self.ctx.cfg.current_sample = None - return MountingResult(success=True) + unmounted_sample = self._unmount_current_sample(timeout=60.0) + return MountingResult( + success=True, + mounted_sample=None, + previous_sample=unmounted_sample, + did_unmount_previous=unmounted_sample is not None, + ) except Exception as e: raise UnmountingFailed(f"Failed to unmount: {e}") from e log_msg = f"Mounting sample: {target}" if previous_sample is not None: - log_msg = f"Unmounting sample: {previous_sample} \n and {log_msg}" + log_msg = f"Unmounting sample: {previous_sample}\nand {log_msg}" self.logger.debug(log_msg) self._mount_handler(target) self._mount_fail_handler(mount_error=False) self.ctx.cfg.current_sample = target - return MountingResult(success=True) + + return MountingResult( + success=True, + mounted_sample=target, + previous_sample=previous_sample, + did_unmount_previous=previous_sample is not None, + ) except CriticalTellException as e: self.logger.error(f"Critical Tell error: {e}") - return MountingResult(success=False, error=e, comment=str(e)) + return MountingResult( + success=False, + mounted_sample=None, + previous_sample=previous_sample, + did_unmount_previous=False, + error=e, + comment=str(e), + ) except Exception as e: self.logger.error(f"Mount failed: {e}") @@ -176,8 +201,18 @@ class MountingService: except Exception as fail_handler_error: return MountingResult( success=False, + mounted_sample=None, + previous_sample=previous_sample, + did_unmount_previous=False, error=fail_handler_error, comment=str(fail_handler_error), ) - return MountingResult(success=False, error=e, comment=str(e)) \ No newline at end of file + return MountingResult( + success=False, + mounted_sample=None, + previous_sample=previous_sample, + did_unmount_previous=False, + error=e, + comment=str(e), + ) \ No newline at end of file