Skip to content

Commit 9b567b2

Browse files
authored
Calculate longest motor move time and set timeout (#1164)
* feat: calculate longest motor move time and set timeout * tests: amend move_to_start test to check timeout * chore: split out serial signal reads
1 parent d381c3b commit 9b567b2

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

src/ophyd_async/epics/pmac/_pmac_trajectory.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
AsyncStatus,
1111
FlyerController,
1212
error_if_none,
13+
gather_dict,
1314
observe_value,
1415
set_and_wait_for_value,
1516
wait_for_value,
@@ -202,12 +203,25 @@ async def _move_to_start(
202203
coord = self.pmac.coord[motor_info.cs_number]
203204
coros = []
204205
await coord.defer_moves.set(True)
206+
207+
motor_readbacks = await gather_dict(
208+
{motor: motor.user_readback.get_value() for motor in ramp_up_position}
209+
)
210+
211+
move_times = [
212+
abs(position - motor_readbacks[motor])
213+
/ motor_info.motor_max_velocity[motor]
214+
for motor, position in ramp_up_position.items()
215+
]
216+
217+
longest_time = max(move_times)
218+
205219
for motor, position in ramp_up_position.items():
206220
coros.append(
207221
set_and_wait_for_value(
208222
coord.cs_axis_setpoint[motor_info.motor_cs_index[motor]],
209223
position,
210-
set_timeout=10,
224+
set_timeout=longest_time + DEFAULT_TIMEOUT,
211225
wait_for_set_completion=False,
212226
)
213227
)

tests/unit_tests/epics/pmac/test_pmac_trajectory.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from scanspec.specs import Fly, Line
66

77
from ophyd_async.core import (
8+
DEFAULT_TIMEOUT,
89
get_mock,
10+
set_and_wait_for_value,
911
set_mock_value,
1012
)
1113
from ophyd_async.epics.motor import Motor
@@ -54,22 +56,39 @@ async def test_pmac_move_to_start(sim_motors: tuple[PmacIO, Motor, Motor]):
5456
ramp_up_position = {sim_x_motor: np.float64(-1.2), sim_y_motor: np.float64(-0.6)}
5557
pmac_trajectory = PmacTrajectoryTriggerLogic(pmac_io)
5658

57-
await pmac_trajectory._move_to_start(motor_info, ramp_up_position)
58-
59-
coord_mock_calls = get_mock(coord).mock_calls
60-
61-
assert coord_mock_calls[0] == call.defer_moves.put(True, wait=True)
62-
assert coord_mock_calls[1] == (
63-
"cs_axis_setpoint.7.put",
64-
(np.float64(-1.2)),
65-
{"wait": True},
66-
)
67-
assert coord_mock_calls[2] == (
68-
"cs_axis_setpoint.8.put",
69-
(np.float64(-0.6)),
70-
{"wait": True},
71-
)
72-
assert coord_mock_calls[3] == call.defer_moves.put(False, wait=True)
59+
# Wrap set_and_wait_for_value to check passed arguments
60+
with patch(
61+
"ophyd_async.epics.pmac._pmac_trajectory.set_and_wait_for_value",
62+
wraps=set_and_wait_for_value,
63+
) as spy_set_and_wait_for_value:
64+
await pmac_trajectory._move_to_start(motor_info, ramp_up_position)
65+
66+
coord_mock_calls = get_mock(coord).mock_calls
67+
68+
assert coord_mock_calls[0] == call.defer_moves.put(True, wait=True)
69+
assert coord_mock_calls[1] == (
70+
"cs_axis_setpoint.7.put",
71+
(np.float64(-1.2)),
72+
{"wait": True},
73+
)
74+
assert coord_mock_calls[2] == (
75+
"cs_axis_setpoint.8.put",
76+
(np.float64(-0.6)),
77+
{"wait": True},
78+
)
79+
assert coord_mock_calls[3] == call.defer_moves.put(False, wait=True)
80+
81+
# Longest move time should be sim_motor_x, so calculate this
82+
expected_timeout = DEFAULT_TIMEOUT + (
83+
abs(ramp_up_position[sim_x_motor])
84+
/ motor_info.motor_max_velocity[sim_x_motor]
85+
)
86+
87+
# All motors should have the same move timeout
88+
assert all(
89+
call_.kwargs["set_timeout"] == expected_timeout
90+
for call_ in spy_set_and_wait_for_value.mock_calls
91+
)
7392

7493

7594
async def test_pmac_trajectory_kickoff(

0 commit comments

Comments
 (0)