diff --git a/src/aare/daq/daq.py b/src/aare/daq/daq.py index 6f7f9da9..f489afee 100644 --- a/src/aare/daq/daq.py +++ b/src/aare/daq/daq.py @@ -1742,45 +1742,45 @@ class AareDAQ: ) self.save_screenshot_db(sample_id, f"{sample_id}_{angle}_{zoom_value:.0f}") - if targets_found_this_attempt == 0: - logger.error(f"No targets found in this attempt {attempt}") - raise LoopCenteringFailed - - else: + # if targets_found_this_attempt == 0: + # logger.error(f"No targets found in this attempt {attempt}") + # raise LoopCenteringFailed + # + # else: + # if targets_found_this_attempt >= len(base_angles): + # logger.debug( + # f"sucessfully found {targets_found_this_attempt} targets in attempt {attempt + 1} ") + # break + #if found_flag is not None and found_angle is not None: + logger.debug(f"found a target at angle {found_angle} in attempt {attempt + 1}") + base_angles = (0, 90) + logger.debug(f"new base angles: {base_angles}") + attempt += 1 + logger.debug(f"attempt {attempt} of {max_attempt}") + if attempt >= max_attempt: if targets_found_this_attempt >= len(base_angles): logger.debug( f"sucessfully found {targets_found_this_attempt} targets in attempt {attempt + 1} ") break - if found_flag is not None and found_angle is not None: - logger.debug(f"found a target at angle {found_angle} in attempt {attempt + 1}") - base_angles = (found_angle, found_angle + 45) - logger.debug(f"new base angles: {base_angles}") - attempt += 1 - logger.debug(f"attempt {attempt} of {max_attempt}") - if attempt >= max_attempt: - logger.error(f"{attempt} exceeds max attempts {max_attempt}") - raise LoopCenteringFailed + logger.error(f"{attempt} exceeds max attempts {max_attempt}") + logger.debug(f"time to loop center: {time.perf_counter() - start}") + raise LoopCenteringFailed #i += 1 logger.debug("alc success") logger.debug(f"current sample: {self.__cfg.current_sample}, sample_id of scan: {sample_id}") - self.__aare.sample_centered(self.__cfg.current_sample) if sample_id is not None: logger.info(f"sample {sample_id} centered") - self.save_screenshot_db(sample_id, f"{sample_id}_centered") self._append_smargon_trace(sample_id=sample_id, event="alc_success") logger.debug(f"time to loop center: {time.perf_counter() - start}") - return True + return LoopCenteringResult(success=True) except Exception as e: total_detections = sum(found_classes_count.values()) if total_detections == 0: - logger.error("ALC: No objects found in any zoom") - self.__aare.alc_failed(self.sample, alc_comment="No objects detected") + alc_comment="No objects detected" else: - self.__aare.alc_failed( - self.sample, - alc_comment=( + alc_comment = ( "Failed to centre but detected objects - " f"Crystal: {found_classes_count.get(2, 0)}, " f"Loop_face: {found_classes_count.get(3, 0)}, " @@ -1788,27 +1788,23 @@ class AareDAQ: f"Pin: {found_classes_count.get(1, 0)}, " f"Ice: {found_classes_count.get(4, 0)}, " f"Needle: {found_classes_count.get(5, 0)}" - ), - ) - logger.error("Failed to centre but detected objects - " - f"Crystal: {found_classes_count.get(2,0)}, " - f"Loop_face: {found_classes_count.get(3,0)}, " - f"Loop_all: {found_classes_count.get(0,0)}, " - f"Pin: {found_classes_count.get(1,0)}, " - f"Ice: {found_classes_count.get(4,0)}, " - f"Needle: {found_classes_count.get(5,0)}") - logger.error(traceback.format_exc()) - + ) + logger.error(f"ALC exception: {alc_comment}") + logger.error(traceback.format_exc()) logger.error(f"Error in loop centering: {e}") - return False + return LoopCenteringResult( + success=False, + comment=alc_comment, + error=e + ) - def auto_loop_center(self, sample_id: int | None = None) -> float: + def auto_loop_center(self, sample:Optional[SampleShortInfo]=None) -> float: """ Automatically center the loop using ML-based detection. This performs a multi-step sequence including rotation and centering. Args: - sample_id: Optional database ID of the sample. If None, uses current sample. + sample: the current SampleShortInfo. If None, the current sample (self.sample) is used. Returns: Time taken for the centering process (seconds) @@ -1819,10 +1815,14 @@ class AareDAQ: start = time.perf_counter() try: self.__cfg.try_set_busy(timeout=360) - if sample_id is None: - sample_id = self.__cfg.current_sample.db_id + if sample is None: + if self.sample is None: + raise LoopCenteringFailed("No sample available for loop centering." + "If you have mounted a manual sample," + "please add it in the manual sample panel.") + sample = self.sample - if not self._execute_loop_centering(sample_id): + if not self._execute_loop_centering(sample): raise LoopCenteringFailed self.__cfg.state_busy = False