Skip to content

Commit 6df5186

Browse files
committed
make common cache speed plan
1 parent a93c305 commit 6df5186

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies = [
1818
# it will be auto-pinned to the latest release version by the pre-release workflow
1919
#
2020
"bluesky",
21-
"dls-dodal>=1.56.0",
21+
"dls-dodal@git+https://github.com/DiamondLightSource/dodal.git@1495-make-apple2-and-pgm-start-flying-in-sync",
2222
"ophyd-async[sim]",
2323
"scanspec",
2424
]

src/sm_bluesky/common/plan_stubs/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from .detectors import set_area_detector_acquire_time
22
from .motions import (
33
MotorTable,
4+
cache_speed,
45
check_within_limit,
56
get_motor_positions,
67
get_velocity_and_step_size,
78
move_motor_with_look_up,
9+
restore_speed,
810
set_slit_size,
911
)
1012

@@ -16,4 +18,6 @@
1618
"check_within_limit",
1719
"get_motor_positions",
1820
"get_velocity_and_step_size",
21+
"cache_speed",
22+
"restore_speed",
1923
]

src/sm_bluesky/common/plan_stubs/motions.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from collections.abc import Hashable, Iterator
1+
import uuid
2+
from collections.abc import Generator, Hashable, Iterator
23
from typing import Any
34

45
import bluesky.plan_stubs as bps
56
from bluesky.plan_stubs import abs_set
6-
from bluesky.utils import MsgGenerator, plan
7+
from bluesky.utils import Msg, MsgGenerator, plan
78
from dodal.devices.slits import Slits
89
from ophyd_async.epics.motor import Motor
910
from pydantic import RootModel
@@ -172,3 +173,26 @@ def get_velocity_and_step_size(
172173
ideal_velocity = round(max_velocity, 3)
173174

174175
return ideal_velocity, ideal_step_size
176+
177+
178+
def cache_speed(
179+
devices_and_speeds: list[Motor],
180+
) -> Generator[Msg, Any, dict[Motor, float]]:
181+
speeds = {}
182+
for axis in devices_and_speeds:
183+
speed = yield from bps.rd(axis.velocity)
184+
speeds[axis] = speed
185+
return speeds
186+
187+
188+
@plan
189+
def restore_speed(
190+
devices_and_speeds: dict[Motor, float],
191+
group: str | None = None,
192+
wait_for_all: bool = True,
193+
) -> MsgGenerator:
194+
reset_group = f"reset-{group if group else str(uuid.uuid4())[:6]}"
195+
for device, speed in devices_and_speeds.items():
196+
yield from bps.abs_set(device.velocity, speed, group=reset_group)
197+
if wait_for_all:
198+
yield from bps.wait(reset_group)

src/sm_bluesky/common/plans/fast_scan.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from ophyd_async.epics.motor import Motor
1515

1616
from sm_bluesky.common.helper import add_extra_names_to_meta
17-
from sm_bluesky.common.plan_stubs import check_within_limit
17+
from sm_bluesky.common.plan_stubs import cache_speed, check_within_limit, restore_speed
1818
from sm_bluesky.log import LOGGER
1919

2020

@@ -182,13 +182,6 @@ def inner_fast_scan_grid(
182182
)
183183

184184

185-
@plan
186-
def reset_speed(old_speed, motor: Motor) -> MsgGenerator:
187-
LOGGER.info(f"Clean up: setting motor speed to {old_speed}.")
188-
if old_speed:
189-
yield from bps.abs_set(motor.velocity, old_speed)
190-
191-
192185
def clean_up():
193186
LOGGER.info("Clean up")
194187
# possibly use to move back to starting position.
@@ -235,7 +228,7 @@ def _fast_scan_1d(
235228
"""
236229

237230
# read the current speed and store it
238-
old_speed: float = yield from bps.rd(motor.velocity)
231+
old_speed: dict[Motor, float] = yield from cache_speed([motor])
239232

240233
def inner_fast_scan_1d(
241234
dets: list[Any],
@@ -245,7 +238,7 @@ def inner_fast_scan_1d(
245238
motor_speed: float | None = None,
246239
):
247240
if not motor_speed:
248-
motor_speed = old_speed
241+
motor_speed = old_speed[motor]
249242

250243
LOGGER.info(
251244
f"Starting 1d fly scan with {motor.name}:"
@@ -270,5 +263,5 @@ def inner_fast_scan_1d(
270263

271264
yield from finalize_wrapper(
272265
plan=inner_fast_scan_1d(dets, motor, start, end, motor_speed),
273-
final_plan=reset_speed(old_speed, motor),
266+
final_plan=restore_speed(old_speed),
274267
)

0 commit comments

Comments
 (0)