Handle absolute moves correctly
I've changed the default values in MoveStageAPI so that it correctly handles absolute moves with missing axes. I've also simplified it so it isn't using axes_to_array as that resulted in a confusing code path.
This commit is contained in:
parent
2eade9ed30
commit
90f5a755b1
1 changed files with 24 additions and 24 deletions
|
|
@ -12,39 +12,39 @@ class MoveStageAPI(ActionView):
|
||||||
"absolute": fields.Boolean(
|
"absolute": fields.Boolean(
|
||||||
missing=False, example=False, description="Move to an absolute position"
|
missing=False, example=False, description="Move to an absolute position"
|
||||||
),
|
),
|
||||||
"x": fields.Int(missing=0, example=100),
|
"x": fields.Int(missing=None, example=100),
|
||||||
"y": fields.Int(missing=0, example=100),
|
"y": fields.Int(missing=None, example=100),
|
||||||
"z": fields.Int(missing=0, example=20),
|
"z": fields.Int(missing=None, example=20),
|
||||||
}
|
}
|
||||||
|
|
||||||
def post(self, args):
|
def post(self, args):
|
||||||
"""
|
"""
|
||||||
Move the microscope stage in x, y, z
|
Move the microscope stage in x, y, z
|
||||||
|
|
||||||
|
Any axes that are not specifed will not move.
|
||||||
"""
|
"""
|
||||||
microscope = find_component("org.openflexure.microscope")
|
microscope = find_component("org.openflexure.microscope")
|
||||||
|
|
||||||
# Handle absolute positioning (calculate a relative move from current position and target)
|
if not microscope.stage:
|
||||||
if (args.get("absolute")) and (microscope.stage): # Only if stage exists
|
|
||||||
target_position: List[int] = axes_to_array(args, ["x", "y", "z"])
|
|
||||||
logging.debug("TARGET: %s", (target_position))
|
|
||||||
position: Tuple[int, int, int] = (
|
|
||||||
target_position[i] - microscope.stage.position[i] for i in range(3)
|
|
||||||
)
|
|
||||||
logging.debug("DELTA: %s", (position))
|
|
||||||
|
|
||||||
else:
|
|
||||||
# Get coordinates from payload
|
|
||||||
position = axes_to_array(args, ["x", "y", "z"], [0, 0, 0])
|
|
||||||
|
|
||||||
logging.debug(position)
|
|
||||||
|
|
||||||
# Move if stage exists
|
|
||||||
if microscope.stage:
|
|
||||||
# Explicitally acquire lock with 1s timeout
|
|
||||||
with microscope.stage.lock(timeout=1):
|
|
||||||
microscope.stage.move_rel(position)
|
|
||||||
else:
|
|
||||||
logging.warning("Unable to move. No stage found.")
|
logging.warning("Unable to move. No stage found.")
|
||||||
|
return microscope.state["stage"]["position"]
|
||||||
|
|
||||||
|
absolute_move = args.get("absolute")
|
||||||
|
move = [0, 0, 0] # Default to no motion
|
||||||
|
for i, axis in enumerate(["x", "y", "z"]):
|
||||||
|
if axis in args and args[axis] is not None:
|
||||||
|
if absolute_move:
|
||||||
|
# We emulate absolute moves by calculating a relative move that
|
||||||
|
# will take us to the right position.
|
||||||
|
move[i] = args[axis] - microscope.stage.position[i]
|
||||||
|
else:
|
||||||
|
move[i] = args[axis]
|
||||||
|
|
||||||
|
logging.debug(f"Moving stage by {move}, request was {args}")
|
||||||
|
|
||||||
|
# Explicitly acquire lock with 1s timeout
|
||||||
|
with microscope.stage.lock(timeout=1):
|
||||||
|
microscope.stage.move_rel(move)
|
||||||
|
|
||||||
return microscope.state["stage"]["position"]
|
return microscope.state["stage"]["position"]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue