Skip to content

Commit

Permalink
(#117) (#45) fix sim motor
Browse files Browse the repository at this point in the history
  • Loading branch information
dperl-dls committed May 7, 2024
1 parent dbc2d02 commit 1798b41
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
39 changes: 20 additions & 19 deletions src/ophyd_async/sim/demo/sim_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def set(self, new_position: float, timeout: float | None = None):
"""
Asynchronously move the motor to a new position.
"""
update, move_status = await self._move(new_position)
update, move_status = await self._move(new_position, timeout)
async for current_position in observe_value(
self.user_readback, done_status=move_status
):
Expand All @@ -79,7 +79,7 @@ async def set(self, new_position: float, timeout: float | None = None):
current=current_position,
)

async def _move(self, new_position: float):
async def _move(self, new_position: float, timeout: float | None = None):
"""
Start the motor moving to a new position.
Expand All @@ -100,23 +100,24 @@ async def _move(self, new_position: float):
)

async def update_position():
while True:
time_elapsed = round(time.monotonic() - start, 2)

# update position based on time elapsed
if time_elapsed >= travel_time:
# successfully reached our target position
await self._user_readback.put(new_position)
break
else:
current_position = (
old_position + distance * time_elapsed / travel_time
)

await self._user_readback.put(current_position)

# 10hz update loop
await asyncio.sleep(0.1)
async with asyncio.timeout(timeout):
while True:
time_elapsed = round(time.monotonic() - start, 2)

# update position based on time elapsed
if time_elapsed >= travel_time:
# successfully reached our target position
await self._user_readback.put(new_position)
break
else:
current_position = (
old_position + distance * time_elapsed / travel_time
)

await self._user_readback.put(current_position)

# 10hz update loop
await asyncio.sleep(0.1)

# set up a task that updates the motor position at ~10hz
self._move_status = AsyncStatus(asyncio.create_task(update_position()))
Expand Down
19 changes: 10 additions & 9 deletions tests/sim/demo/test_sim_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bluesky.plans import spiral_square
from bluesky.run_engine import RunEngine

from ophyd_async.core.async_status import AsyncStatusBase
from ophyd_async.core.device import DeviceCollector
from ophyd_async.sim.demo.sim_motor import SimMotor

Expand Down Expand Up @@ -73,16 +74,16 @@ async def test_timeout():
move_status = m1.set(10, timeout=0.1)
await asyncio.sleep(0.2)

# verify status of inner task set up to run _move.update_position()
assert isinstance(m1._move_task, asyncio.Task)
assert m1._move_task.done
assert m1._move_task.cancelled

# verify status of outer task set up to run _move()
assert move_status.task.done
# check inner status
assert m1._move_status is not None
assert m1._move_status.done
assert not move_status.success
assert move_status.task.cancelled

new_pos = await m1.user_readback.get_value()
assert new_pos < 10
# check outer status
assert isinstance(move_status, AsyncStatusBase)
assert move_status.done
assert not move_status.success
assert move_status.task.cancelled
new_pos = await m1.user_readback.get_value()
assert new_pos < 10

0 comments on commit 1798b41

Please sign in to comment.