From 253e3b82caeb08ba0611a63c38ded32977139c31 Mon Sep 17 00:00:00 2001 From: David Perl Date: Mon, 4 Mar 2024 13:06:54 +0000 Subject: [PATCH] DiamondLightSource/hyperion#1068 update hyperion plans to use ophyd-async edge detect --- .../oav_grid_detection_plan.py | 44 ++++--------------- .../experiment_plans/pin_tip_centring_plan.py | 2 +- .../experiment_plans/test_pin_tip_centring.py | 11 ++++- 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/src/hyperion/experiment_plans/oav_grid_detection_plan.py b/src/hyperion/experiment_plans/oav_grid_detection_plan.py index 4a30972a2..8599eaaa3 100644 --- a/src/hyperion/experiment_plans/oav_grid_detection_plan.py +++ b/src/hyperion/experiment_plans/oav_grid_detection_plan.py @@ -8,7 +8,6 @@ import bluesky.preprocessors as bpp import numpy as np from blueapi.core import BlueskyContext -from bluesky.preprocessors import finalize_wrapper from dodal.devices.backlight import Backlight from dodal.devices.oav.oav_detector import OAV from dodal.devices.oav.pin_image_recognition import PinTipDetection @@ -17,8 +16,8 @@ from hyperion.device_setup_plans.setup_oav import ( get_move_required_so_that_beam_is_at_pixel, pre_centring_setup_oav, - wait_for_tip_to_be_found, ) +from hyperion.experiment_plans.pin_tip_centring_plan import trigger_and_return_pin_tip from hyperion.log import LOGGER from hyperion.parameters.constants import ( OAV_REFRESH_DELAY, @@ -43,29 +42,8 @@ def create_devices(context: BlueskyContext) -> OavGridDetectionComposite: return device_composite_from_context(context, OavGridDetectionComposite) -def grid_detection_plan( - composite: OavGridDetectionComposite, - parameters: OAVParameters, - snapshot_template: str, - snapshot_dir: str, - grid_width_microns: float, - box_size_microns=20, -): - yield from finalize_wrapper( - grid_detection_main_plan( - composite, - parameters, - snapshot_template, - snapshot_dir, - grid_width_microns, - box_size_microns, - ), - reset_oav(composite.oav), - ) - - @bpp.run_decorator() -def grid_detection_main_plan( +def grid_detection_plan( composite: OavGridDetectionComposite, parameters: OAVParameters, snapshot_template: str, @@ -87,13 +65,14 @@ def grid_detection_main_plan( """ oav: OAV = composite.oav smargon: Smargon = composite.smargon + pin_tip_detection: PinTipDetection = composite.pin_tip_detection LOGGER.info("OAV Centring: Starting grid detection centring") yield from bps.wait() # Set relevant PVs to whatever the config dictates. - yield from pre_centring_setup_oav(oav, parameters, oav.mxsc) + yield from pre_centring_setup_oav(oav, parameters, pin_tip_detection) LOGGER.info("OAV Centring: Camera set up") @@ -113,13 +92,14 @@ def grid_detection_main_plan( # need to wait for the OAV image to update # See #673 for improvements yield from bps.sleep(OAV_REFRESH_DELAY) - - tip_x_px, tip_y_px = yield from wait_for_tip_to_be_found(oav.mxsc.pin_tip) + tip_x_px, tip_y_px = yield from trigger_and_return_pin_tip(pin_tip_detection) LOGGER.info(f"Tip is at x,y: {tip_x_px},{tip_y_px}") - top_edge = np.array((yield from bps.rd(oav.mxsc.top))) - bottom_edge = np.array((yield from bps.rd(oav.mxsc.bottom))) + top_edge = np.array((yield from bps.rd(pin_tip_detection.triggered_top_edge))) + bottom_edge = np.array( + (yield from bps.rd(pin_tip_detection.triggered_bottom_edge)) + ) full_image_height_px = yield from bps.rd(oav.cam.array_size.array_size_y) @@ -198,9 +178,3 @@ def grid_detection_main_plan( ) LOGGER.info(f"Step sizes: {box_size_um, box_size_um, box_size_um}") - - -def reset_oav(oav: OAV): - """Changes the MJPG stream to look at the camera without the edge detection and turns off the edge detcetion plugin.""" - yield from bps.abs_set(oav.snapshot.input_plugin, "OAV.CAM") - yield from bps.abs_set(oav.mxsc.enable_callbacks, 0) diff --git a/src/hyperion/experiment_plans/pin_tip_centring_plan.py b/src/hyperion/experiment_plans/pin_tip_centring_plan.py index 020f6ac9e..b44e032b4 100644 --- a/src/hyperion/experiment_plans/pin_tip_centring_plan.py +++ b/src/hyperion/experiment_plans/pin_tip_centring_plan.py @@ -44,7 +44,7 @@ def trigger_and_return_pin_tip( pin_tip: PinTipDetect | PinTipDetection, ) -> Generator[Msg, None, Pixel]: yield from bps.trigger(pin_tip, wait=True) - tip_x_y_px = yield from bps.rd(pin_tip) + tip_x_y_px = yield from bps.rd(pin_tip.triggered_tip) LOGGER.info(f"Pin tip found at {tip_x_y_px}") return tip_x_y_px # type: ignore diff --git a/tests/unit_tests/experiment_plans/test_pin_tip_centring.py b/tests/unit_tests/experiment_plans/test_pin_tip_centring.py index e093d14b5..9d7a16f5d 100644 --- a/tests/unit_tests/experiment_plans/test_pin_tip_centring.py +++ b/tests/unit_tests/experiment_plans/test_pin_tip_centring.py @@ -1,12 +1,14 @@ from functools import partial from unittest.mock import AsyncMock, MagicMock, patch +import numpy as np import pytest from bluesky.plan_stubs import null -from bluesky.run_engine import RunEngine +from bluesky.run_engine import RunEngine, RunEngineResult from dodal.devices.areadetector.plugins.MXSC import MXSC from dodal.devices.oav.oav_detector import OAV, OAVConfigParams from dodal.devices.oav.pin_image_recognition import PinTipDetection +from dodal.devices.oav.pin_image_recognition.utils import SampleLocation from dodal.devices.smargon import Smargon from ophyd.sim import NullStatus @@ -39,6 +41,7 @@ def test_given_the_pin_tip_is_already_in_view_when_get_tip_into_view_then_tip_re oav.mxsc.pin_tip.trigger.assert_called_once() assert smargon.x.user_readback.get() == 0 + assert isinstance(result, RunEngineResult) assert result.plan_result == (100, 200) @@ -63,6 +66,7 @@ def set_pin_tip_when_x_moved(*args, **kwargs): result = RE(move_pin_into_view(oav.mxsc.pin_tip, smargon)) assert smargon.x.user_readback.get() == DEFAULT_STEP_SIZE + assert isinstance(result, RunEngineResult) assert result.plan_result == (100, 200) @@ -104,7 +108,10 @@ def test_trigger_and_return_pin_tip_works_for_AD_pin_tip_detection( def test_trigger_and_return_pin_tip_works_for_ophyd_pin_tip_detection( ophyd_pin_tip_detection: PinTipDetection, RE: RunEngine ): - ophyd_pin_tip_detection._get_tip_position = AsyncMock(return_value=(100, 200)) + mock_trigger_result = SampleLocation(100, 200, np.array([]), np.array([])) + ophyd_pin_tip_detection._get_tip_and_edge_data = AsyncMock( + return_value=mock_trigger_result + ) re_result = RE(trigger_and_return_pin_tip(ophyd_pin_tip_detection)) assert re_result.plan_result == (100, 200) # type: ignore