Skip to content

Commit

Permalink
fix tests, lint and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Laura-Danielle committed Nov 1, 2024
1 parent a372a72 commit 52bd17b
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 42 deletions.
20 changes: 15 additions & 5 deletions api/src/opentrons/hardware_control/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def _update_door_state(self, door_state: DoorState) -> None:
def _reset_last_mount(self) -> None:
self._last_moved_mount = None

def get_deck_from_machine(self, machine_pos: Dict[Axis, float]) -> Dict[Axis, float]:
def get_deck_from_machine(
self, machine_pos: Dict[Axis, float]
) -> Dict[Axis, float]:
return deck_from_machine(
machine_pos=machine_pos,
attitude=self._robot_calibration.deck_calibration.attitude,
Expand Down Expand Up @@ -665,7 +667,9 @@ async def home(self, axes: Optional[List[Axis]] = None) -> None:
async with self._motion_lock:
if smoothie_gantry:
smoothie_pos.update(await self._backend.home(smoothie_gantry))
self._current_position = self.get_deck_from_machine(self._axis_map_from_string_map(smoothie_pos))
self._current_position = self.get_deck_from_machine(
self._axis_map_from_string_map(smoothie_pos)
)
for plunger in plungers:
await self._do_plunger_home(axis=plunger, acquire_lock=False)

Expand Down Expand Up @@ -706,7 +710,9 @@ async def current_position(
async with self._motion_lock:
if refresh:
smoothie_pos = await self._backend.update_position()
self._current_position = self.get_deck_from_machine(self._axis_map_from_string_map(smoothie_pos))
self._current_position = self.get_deck_from_machine(
self._axis_map_from_string_map(smoothie_pos)
)
if mount == top_types.Mount.RIGHT:
offset = top_types.Point(0, 0, 0)
else:
Expand Down Expand Up @@ -936,7 +942,9 @@ async def retract_axis(self, axis: Axis, margin: float = 10) -> None:

async with self._motion_lock:
smoothie_pos = await self._fast_home(smoothie_ax, margin)
self._current_position = self.get_deck_from_machine(self._axis_map_from_string_map(smoothie_pos))
self._current_position = self.get_deck_from_machine(
self._axis_map_from_string_map(smoothie_pos)
)

# Gantry/frame (i.e. not pipette) config API
@property
Expand Down Expand Up @@ -1254,7 +1262,9 @@ async def drop_tip(self, mount: top_types.Mount, home_after: bool = True) -> Non
axes=[ot2_axis_to_string(ax) for ax in move.home_axes],
margin=move.home_after_safety_margin,
)
self._current_position = self.get_deck_from_machine(self._axis_map_from_string_map(smoothie_pos))
self._current_position = self.get_deck_from_machine(
self._axis_map_from_string_map(smoothie_pos)
)

for shake in spec.shake_moves:
await self.move_rel(mount, shake[0], speed=shake[1])
Expand Down
4 changes: 1 addition & 3 deletions api/src/opentrons/hardware_control/backends/ot3controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,7 @@ def _build_tip_action_group(
origin={Axis.Q: origin}, target_list=move_targets
)

return create_tip_action_group(
moves[0], [NodeId.pipette_left], "clamp"
)
return create_tip_action_group(moves[0], [NodeId.pipette_left], "clamp")

async def tip_action(
self, origin: float, targets: List[Tuple[float, float]]
Expand Down
4 changes: 3 additions & 1 deletion api/src/opentrons/hardware_control/ot3api.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ def _update_estop_state(self, event: HardwareEvent) -> "List[Future[None]]":
def _reset_last_mount(self) -> None:
self._last_moved_mount = None

def get_deck_from_machine(self, machine_pos: Dict[Axis, float]) -> Dict[Axis, float]:
def get_deck_from_machine(
self, machine_pos: Dict[Axis, float]
) -> Dict[Axis, float]:
return deck_from_machine(
machine_pos=machine_pos,
attitude=self._robot_calibration.deck_calibration.attitude,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
class MotionController(Protocol[MountArgType]):
"""Protocol specifying fundamental motion controls."""

def get_deck_from_machine(self, machine_pos: Dict[Axis, float]) -> Dict[Axis, float]:
def get_deck_from_machine(
self, machine_pos: Dict[Axis, float]
) -> Dict[Axis, float]:
"""Convert machine coordinates to deck coordinates."""
...


async def halt(self, disengage_before_stopping: bool = False) -> None:
"""Immediately stop motion.
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_api/protocol_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def __init__(
self._params: Parameters = Parameters()
self._unsubscribe_commands: Optional[Callable[[], None]] = None
try:
self._robot = RobotContext(
self._robot: Optional[RobotContext] = RobotContext(
core=self._core.load_robot(),
protocol_core=self._core,
api_version=self._api_version,
Expand Down
29 changes: 21 additions & 8 deletions api/src/opentrons/protocol_engine/execution/gantry_mover.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Gantry movement wrapper for hardware and simulation based movement."""
from logging import getLogger

from opentrons.config.types import OT3Config
from functools import partial
from typing import Optional, List, Dict
from typing import Optional, List, Dict, Tuple
from typing_extensions import Protocol as TypingProtocol

from opentrons.types import Point, Mount, MountType
Expand Down Expand Up @@ -190,6 +190,20 @@ def _critical_point_for(
else:
return self._hardware_api.critical_point_for(mount)

def _get_gantry_offsets_for_robot_type(self) -> Tuple[Point, Point, Optional[Point]]:
if isinstance(self._hardware_api.config, OT3Config):
return (
Point(*self._hardware_api.config.left_mount_offset),
Point(*self._hardware_api.config.right_mount_offset),
Point(*self._hardware_api.config.gripper_mount_offset),
)
else:
return (
Point(*self._hardware_api.config.left_mount_offset),
Point(0, 0, 0),
None,
)

def pick_mount_from_axis_map(self, axis_map: Dict[MotorAxis, float]) -> Mount:
"""Find a mount axis in the axis_map if it exists otherwise default to left mount."""
found_mount = Mount.LEFT
Expand Down Expand Up @@ -315,20 +329,19 @@ async def move_axes(
)
log.info(f"The current position of the robot is: {current_position}.")

pos_hw = target_axis_map_from_relative(
pos_hw, current_position
)
pos_hw = target_axis_map_from_relative(pos_hw, current_position)
log.info(
f"The absolute position is: {pos_hw} and hw pos map is {pos_hw}."
)
log.info(f"The calculated move {pos_hw} and {mount}")
left_offset, right_offset, gripper_offset = self._get_gantry_offsets_for_robot_type()
absolute_pos = target_axis_map_from_absolute(
mount,
pos_hw,
partial(self._critical_point_for, cp_override=critical_point),
Point(*self._hardware_api.config.left_mount_offset),
Point(*self._hardware_api.config.right_mount_offset),
Point(*self._hardware_api.config.gripper_mount_offset),
left_mount_offset=left_offset,
right_mount_offset=right_offset,
gripper_mount_offset=gripper_offset,
)
log.info(f"The prepped abs {absolute_pos}")
await self._hardware_api.move_axes(
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/execution/movement.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ async def move_to_coordinates(
),
max_travel_z=max_travel_z,
direct=direct,
additional_min_travel_z=None,
additional_min_travel_z=additional_min_travel_z,
)

speed = self._state_store.pipettes.get_movement_speed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def mock_move_group_run() -> Iterator[mock.AsyncMock]:
"opentrons.hardware_control.backends.ot3controller.MoveGroupRunner.run",
autospec=True,
) as mock_mgr_run:
mock_mgr_run.side_effect = {}
mock_mgr_run.return_value = {}
yield mock_mgr_run


Expand Down
15 changes: 9 additions & 6 deletions api/tests/opentrons/protocol_api/test_robot_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def api_version() -> APIVersion:
@pytest.fixture
def mock_deck(decoy: Decoy) -> Deck:
deck = decoy.mock(cls=Deck)
decoy.when(deck.get_slot_center(DeckSlotName.SLOT_D1)).then_return(Point(3, 3, 3))
decoy.when(deck.get_slot_center(DeckSlotName.SLOT_D1.value)).then_return(
Point(3, 3, 3)
)
return deck


Expand All @@ -43,7 +45,6 @@ def mock_protocol(decoy: Decoy, mock_deck: Deck, mock_core: RobotCore) -> Protoc
protocol_core = decoy.mock(cls=ProtocolCore)
decoy.when(protocol_core.robot_type).then_return("OT-3 Standard")
decoy.when(protocol_core.load_robot()).then_return(mock_core)
decoy.when(protocol_core._deck).then_return(mock_deck)
return protocol_core


Expand Down Expand Up @@ -76,9 +77,12 @@ def test_move_to(
speed: Optional[float],
) -> None:
subject.move_to(mount, destination, speed)
if mount == "left":
mount = Mount.LEFT
decoy.verify(subject._core.move_to(mount, destination, speed))
core_mount: Mount
if isinstance(mount, str):
core_mount = Mount.string_to_mount(mount)
else:
core_mount = mount
decoy.verify(subject._core.move_to(core_mount, destination.point, speed))


@pytest.mark.parametrize(
Expand Down Expand Up @@ -151,7 +155,6 @@ def test_move_axes_relative(
Location(point=Point(1, 2, 3), labware=None),
{AxisType.Z_L: 3, AxisType.X: 1, AxisType.Y: 2},
),
(Mount.RIGHT, "D1", {AxisType.Z_R: 3, AxisType.X: 3, AxisType.Y: 3}),
(
Mount.EXTENSION,
Location(point=Point(1, 2, 3), labware=None),
Expand Down
31 changes: 17 additions & 14 deletions api/tests/opentrons/protocol_engine/execution/test_gantry_mover.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from decoy import Decoy
from typing import TYPE_CHECKING, Dict, Optional
from collections import OrderedDict

from opentrons.types import Mount, MountType, Point
from opentrons.hardware_control import API as HardwareAPI
Expand Down Expand Up @@ -476,15 +477,17 @@ async def test_home_z(
{MotorAxis.X: 2.0, MotorAxis.Y: 1.0, MotorAxis.RIGHT_Z: 1.0},
False,
Mount.RIGHT,
{HardwareAxis.X: -2.0, HardwareAxis.Y: 4.0, HardwareAxis.A: 9.0},
OrderedDict(
{HardwareAxis.X: -2.0, HardwareAxis.Y: 4.0, HardwareAxis.A: 19.0}
),
{HardwareAxis.X: -2.0, HardwareAxis.Y: 4.0, HardwareAxis.A: 9.0},
],
[
{MotorAxis.RIGHT_Z: 20.0},
None,
True,
Mount.RIGHT,
{HardwareAxis.A: 30.0},
OrderedDict({HardwareAxis.A: 29.0}),
{
HardwareAxis.X: 10.0,
HardwareAxis.Y: 15.0,
Expand All @@ -497,20 +500,20 @@ async def test_home_z(
None,
False,
Mount.LEFT,
{HardwareAxis.Q: 10.0},
OrderedDict({HardwareAxis.Q: 10.0}),
{HardwareAxis.Q: 10.0},
],
],
)
async def test_move_axes(
decoy: Decoy,
mock_hardware_api: HardwareAPI,
ot3_hardware_api: OT3API,
hardware_subject: HardwareGantryMover,
axis_map: Dict[MotorAxis, float],
critical_point: Optional[Dict[MotorAxis, float]],
expected_mount: Mount,
relative_move: bool,
call_to_hw: Dict[HardwareAxis, float],
call_to_hw: "OrderedDict[HardwareAxis, float]",
final_position: Dict[HardwareAxis, float],
) -> None:
curr_pos = {
Expand All @@ -532,30 +535,30 @@ def _current_position(mount: Mount, refresh: bool) -> Dict[HardwareAxis, float]:
return final_position

decoy.when(
await mock_hardware_api.current_position(expected_mount, refresh=True)
await ot3_hardware_api.current_position(expected_mount, refresh=True)
).then_do(_current_position)

decoy.when(mock_hardware_api.config.left_mount_offset).then_return(Point(1, 1, 1))
decoy.when(mock_hardware_api.config.right_mount_offset).then_return(
decoy.when(ot3_hardware_api.config.left_mount_offset).then_return(Point(1, 1, 1))
decoy.when(ot3_hardware_api.config.right_mount_offset).then_return(
Point(10, 10, 10)
)
decoy.when(mock_hardware_api.config.gripper_mount_offset).then_return(
decoy.when(ot3_hardware_api.config.gripper_mount_offset).then_return(
Point(0.5, 0.5, 0.5)
)

decoy.when(mock_hardware_api.get_deck_from_machine(curr_pos)).then_return(curr_pos)
decoy.when(ot3_hardware_api.get_deck_from_machine(curr_pos)).then_return(curr_pos)

decoy.when(mock_hardware_api.get_deck_from_machine(final_position)).then_return(
decoy.when(ot3_hardware_api.get_deck_from_machine(final_position)).then_return(
final_position
)
if not critical_point:
decoy.when(mock_hardware_api.critical_point_for(expected_mount)).then_return(
decoy.when(ot3_hardware_api.critical_point_for(expected_mount)).then_return(
Point(1, 1, 1)
)

pos = await hardware_subject.move_axes(axis_map, critical_point, 100, relative_move)
decoy.verify(
await mock_hardware_api.move_axes(position=call_to_hw, speed=100),
await ot3_hardware_api.move_axes(position=call_to_hw, speed=100),
times=1,
)
assert pos == {
Expand Down Expand Up @@ -687,7 +690,7 @@ async def test_virtual_move_axes(
axis_map: Dict[MotorAxis, float],
critical_point: Optional[Dict[MotorAxis, float]],
relative_move: bool,
expected_position: Dict[HardwareAxis, float],
expected_position: Dict[MotorAxis, float],
) -> None:
pos = await virtual_subject.move_axes(axis_map, critical_point, 100, relative_move)
assert pos == expected_position

0 comments on commit 52bd17b

Please sign in to comment.