diff --git a/src/mx_bluesky/hyperion/experiment_plans/change_aperture_then_move_plan.py b/src/mx_bluesky/hyperion/experiment_plans/change_aperture_then_move_plan.py index 69e845077..930f4428b 100644 --- a/src/mx_bluesky/hyperion/experiment_plans/change_aperture_then_move_plan.py +++ b/src/mx_bluesky/hyperion/experiment_plans/change_aperture_then_move_plan.py @@ -26,8 +26,9 @@ def change_aperture_then_move_to_xtal( best_hit.bounding_box_mm[1] - best_hit.bounding_box_mm[0] ) with TRACER.start_span("change_aperture"): - yield from _set_aperture_for_bbox_mm( - aperture_scatterguard, bounding_box_size + yield from set_aperture_for_bbox_mm( + aperture_scatterguard, + bounding_box_size, ) else: LOGGER.warning("No bounding box size received") @@ -49,25 +50,35 @@ def change_aperture_then_move_to_xtal( ) -def _set_aperture_for_bbox_mm( - aperture_device: ApertureScatterguard, bbox_size_mm: list[float] | numpy.ndarray +def set_aperture_for_bbox_mm( + aperture_device: ApertureScatterguard, + bbox_size_mm: list[float] | numpy.ndarray, ): - # TODO confirm correction factor see https://github.com/DiamondLightSource/mx-bluesky/issues/618 - ASSUMED_BOX_SIZE_MM = 0.020 - bbox_size_boxes = [round(mm / ASSUMED_BOX_SIZE_MM) for mm in bbox_size_mm] - yield from set_aperture_for_bbox_size(aperture_device, bbox_size_boxes) + """Sets aperture size based on bbox_size. + This function determines the aperture size needed to accomodate the bounding box + of a crystal. The x-axis length of the bounding box is used, setting the aperture + to Medium if this is less than 50um, and Large otherwise. + + Args: + aperture_device: The aperture scatter gaurd device we are controlling. + bbox_size_mm: The [x,y,z] lengths, in mm, of a bounding box + containing a crystal. This describes (in no particular order): + * The maximum width a crystal occupies + * The maximum height a crystal occupies + * The maximum depth a crystal occupies + constructing a three dimensional cuboid, completely encapsulating the crystal. + + Yields: + Iterator[MsgGenerator] + """ -def set_aperture_for_bbox_size( - aperture_device: ApertureScatterguard, - bbox_size: list[int] | numpy.ndarray, -): # bbox_size is [x,y,z], for i03 we only care about x new_selected_aperture = ( - ApertureValue.MEDIUM if bbox_size[0] < 2 else ApertureValue.LARGE + ApertureValue.MEDIUM if bbox_size_mm[0] < 0.05 else ApertureValue.LARGE ) LOGGER.info( - f"Setting aperture to {new_selected_aperture} based on bounding box size {bbox_size}." + f"Setting aperture to {new_selected_aperture} based on bounding box size {bbox_size_mm}." ) @bpp.set_run_key_decorator("change_aperture") diff --git a/tests/system_tests/hyperion/test_aperturescatterguard_system.py b/tests/system_tests/hyperion/test_aperturescatterguard_system.py index b92499b03..c05a82ce8 100644 --- a/tests/system_tests/hyperion/test_aperturescatterguard_system.py +++ b/tests/system_tests/hyperion/test_aperturescatterguard_system.py @@ -11,7 +11,7 @@ from ophyd_async.core import DeviceCollector from mx_bluesky.hyperion.experiment_plans.change_aperture_then_move_plan import ( - set_aperture_for_bbox_size, + set_aperture_for_bbox_mm, ) @@ -29,7 +29,17 @@ def ap_sg(): @pytest.mark.s03() -def test_aperture_change_callback(ap_sg: ApertureScatterguard): +@pytest.mark.parametrize( + "bbox, expected_aperture", + [ + ([0.05, 0.05, 0.05], "LARGE_APERTURE"), + ([0.02, 0.02, 0.02], "MEDIUM_APERTURE"), + ], + ids=["large_aperture", "medium_aperture"], +) +def test_aperture_change_callback( + ap_sg: ApertureScatterguard, bbox: list[float], expected_aperture: str +): from bluesky.run_engine import RunEngine from mx_bluesky.hyperion.external_interaction.callbacks.aperture_change_callback import ( @@ -39,5 +49,5 @@ def test_aperture_change_callback(ap_sg: ApertureScatterguard): cb = ApertureChangeCallback() RE = RunEngine({}) RE.subscribe(cb) - RE(set_aperture_for_bbox_size(ap_sg, [2, 2, 2])) - assert cb.last_selected_aperture == "LARGE_APERTURE" + RE(set_aperture_for_bbox_mm(ap_sg, bbox)) + assert cb.last_selected_aperture == expected_aperture