Skip to content

Commit

Permalink
DiamondLightSource/hyperion#1068 update hyperion plans to use ophyd-a…
Browse files Browse the repository at this point in the history
…sync edge detect
  • Loading branch information
dperl-dls committed Mar 4, 2024
1 parent 0dfe032 commit 253e3b8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 38 deletions.
44 changes: 9 additions & 35 deletions src/hyperion/experiment_plans/oav_grid_detection_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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")

Expand All @@ -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)

Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/hyperion/experiment_plans/pin_tip_centring_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions tests/unit_tests/experiment_plans/test_pin_tip_centring.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)


Expand All @@ -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)


Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 253e3b8

Please sign in to comment.