Skip to content

Commit 0ccc8f7

Browse files
committed
Cancellable moves
1 parent 3fd1c74 commit 0ccc8f7

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/labthings_sangaboard/proscan.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from labthings_fastapi.descriptors.property import PropertyDescriptor
99
from labthings_fastapi.thing import Thing
1010
from labthings_fastapi.decorators import thing_action, thing_property
11+
from labthings_fastapi.dependencies.invocation import CancelHook, InvocationCancelledError
1112
from typing import Iterator, Literal, Optional
1213
from contextlib import contextmanager
1314
from collections.abc import Sequence, Mapping
@@ -80,13 +81,21 @@ def thing_state(self):
8081
)
8182

8283
@thing_action
83-
def move_relative(self, x: Optional[int]=0, y: Optional[int]=0, z: Optional[int]=0):
84+
def move_relative(self, cancel: CancelHook, block_cancellation: bool=False, x: Optional[int]=0, y: Optional[int]=0, z: Optional[int]=0):
8485
"""Make a relative move. Keyword arguments should be axis names."""
8586
with self._action_lock:
8687
self.moving = True
8788
self.query(f"GR {x} {y} {z}")
88-
while self.is_moving:
89-
time.sleep(0.05)
89+
if block_cancellation:
90+
while self.is_moving:
91+
time.sleep(0.05)
92+
else:
93+
try:
94+
while self.is_moving:
95+
cancel.sleep(0.05)
96+
except InvocationCancelledError:
97+
logging.info("Aborting move due to invocation cancellation, using `I`,")
98+
self.query("I")
9099
self.moving=False
91100

92101
@thing_property
@@ -95,7 +104,7 @@ def is_moving(self):
95104
return self.int_query("$,S")>0
96105

97106
@thing_action
98-
def move_absolute(self, **kwargs: Mapping[str, int]):
107+
def move_absolute(self, cancel: CancelHook, block_cancellation: bool=False, **kwargs: Mapping[str, int]):
99108
"""Make an absolute move. Keyword arguments should be axis names."""
100109
with self._action_lock:
101110
current_pos = self.position
@@ -104,7 +113,7 @@ def move_absolute(self, **kwargs: Mapping[str, int]):
104113
for k, v in kwargs.items()
105114
if k in self.axis_names
106115
}
107-
self.move_relative(**displacement)
116+
self.move_relative(cancel, block_cancellation=block_cancellation, **displacement)
108117

109118
@thing_action
110119
def abort_move(self):

0 commit comments

Comments
 (0)