Skip to content

Commit

Permalink
Merge pull request #750 from ImperialCollegeLondon/fix-dummy-stepper-…
Browse files Browse the repository at this point in the history
…motor

Fix: `DummyStepperMotor` returns `None` if moving
  • Loading branch information
alexdewar authored Dec 19, 2024
2 parents 6680116 + b2589ee commit 15cac7d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
16 changes: 5 additions & 11 deletions finesse/hardware/plugins/stepper_motor/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(
self._move_end_timer.timeout.connect(self._on_move_end)

self._steps_per_rotation = steps_per_rotation
self._step = 0
self._step = self._new_step = 0

super().__init__()

Expand All @@ -57,15 +57,8 @@ def is_moving(self) -> bool:
return self._move_end_timer.isActive()

@property
def step(self) -> int | None:
"""The number of steps that correspond to a full rotation.
As this can only be requested when the motor is stationary, if the motor is
moving then None will be returned.
"""
if self.is_moving:
return None

def step(self) -> int:
"""The current state of the device's step counter."""
return self._step

@step.setter
Expand All @@ -76,7 +69,7 @@ def step(self, step: int) -> None:
step: Which step position to move to
"""
logging.info(f"Moving stepper motor to step {step}")
self._step = step
self._new_step = step
self._move_end_timer.start()

def stop_moving(self) -> None:
Expand All @@ -87,5 +80,6 @@ def stop_moving(self) -> None:

def _on_move_end(self) -> None:
"""Run when the timer signals that the move has finished."""
self._step = self._new_step
logging.info("Move finished")
self.send_move_end_message()
16 changes: 10 additions & 6 deletions tests/hardware/plugins/stepper_motor/test_dummy_stepper_motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ def test_move_to_number(
qtbot,
) -> None:
"""Check move_to, when an angle is given."""
assert stepper.step == 0
assert stepper._new_step == 0

with patch.object(stepper._move_end_timer, "start") as start_mock:
with raises:
stepper.move_to(10.0 * float(target))
assert stepper.step == target
assert stepper._new_step == target
start_mock.assert_called_once_with()


Expand Down Expand Up @@ -114,12 +114,16 @@ def test_stop_moving(stepper: DummyStepperMotor, qtbot) -> None:
move_end_mock.assert_called_once()


def test_on_move_end(
stepper: DummyStepperMotor, sendmsg_mock: MagicMock, qtbot
) -> None:
"""Test the _on_move_end() method when notification is requested."""
def test_move(stepper: DummyStepperMotor, sendmsg_mock: MagicMock, qtbot) -> None:
"""Test that moving the motor works."""
assert stepper.step == 0
with patch.object(stepper, "_move_end_timer"):
stepper.step = 100
assert stepper.step == 0 # the old value should still be returned

# Trigger move end timer
stepper._move_end_timer.timeout.emit()
assert stepper.step == 100

# Check message is sent with final angle moved to
sendmsg_mock.assert_called_once_with(
Expand Down

0 comments on commit 15cac7d

Please sign in to comment.