-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api): driver and simulator for FLEX Stacker #17120
Open
ahiuchingau
wants to merge
4
commits into
edge
Choose a base branch
from
EXEC-980-flex-stacker-driver-skeleton-in-opentrons-driver
base: edge
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+525
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
90ab7b6
lets make command builder terminator have a default value
ahiuchingau 1a92220
make number of retries a default arg for serial communicaition
ahiuchingau f773220
add Flex Stakcer opentrons driver
ahiuchingau dc75210
Merge branch 'edge' into EXEC-980-flex-stacker-driver-skeleton-in-ope…
ahiuchingau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from .abstract import StackerDriver | ||
from .driver import FlexStackerDriver | ||
from .simulator import SimulatingDriver | ||
|
||
__all__ = [ | ||
"StackerDriver", | ||
"FlexStackerDriver", | ||
"SimulatingDriver", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from typing import Protocol | ||
|
||
from .types import StackerAxis, PlatformStatus, Direction, MoveParams, StackerInfo | ||
|
||
|
||
class StackerDriver(Protocol): | ||
"""Protocol for the Stacker driver.""" | ||
|
||
async def connect(self) -> None: | ||
"""Connect to stacker.""" | ||
... | ||
|
||
async def disconnect(self) -> None: | ||
"""Disconnect from stacker.""" | ||
... | ||
|
||
async def is_connected(self) -> bool: | ||
"""Check connection to stacker.""" | ||
... | ||
|
||
async def update_firmware(self, firmware_file_path: str) -> None: | ||
"""Updates the firmware on the device.""" | ||
... | ||
|
||
async def get_device_info(self) -> StackerInfo: | ||
"""Get Device Info.""" | ||
... | ||
|
||
async def set_serial_number(self, sn: str) -> None: | ||
"""Set Serial Number.""" | ||
... | ||
|
||
async def stop_motor(self) -> None: | ||
"""Stop motor movement.""" | ||
... | ||
|
||
async def get_limit_switch(self, axis: StackerAxis, direction: Direction) -> bool: | ||
"""Get limit switch status. | ||
|
||
:return: True if limit switch is triggered, False otherwise | ||
""" | ||
... | ||
|
||
async def get_platform_sensor(self, direction: Direction) -> bool: | ||
"""Get platform sensor status. | ||
|
||
:return: True if platform is present, False otherwise | ||
""" | ||
... | ||
|
||
async def get_platform_status(self) -> PlatformStatus: | ||
"""Get platform status.""" | ||
... | ||
|
||
async def get_hopper_door_closed(self) -> bool: | ||
"""Get whether or not door is closed. | ||
|
||
:return: True if door is closed, False otherwise | ||
""" | ||
... | ||
|
||
async def move_in_mm( | ||
self, axis: StackerAxis, distance: float, params: MoveParams | None = None | ||
) -> None: | ||
"""Move axis.""" | ||
... | ||
|
||
async def move_to_limit_switch( | ||
self, axis: StackerAxis, direction: Direction, params: MoveParams | None = None | ||
) -> None: | ||
"""Move until limit switch is triggered.""" | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
import asyncio | ||
import re | ||
from typing import Optional | ||
|
||
from opentrons.drivers.command_builder import CommandBuilder | ||
from opentrons.drivers.asyncio.communication import AsyncResponseSerialConnection | ||
|
||
from .abstract import StackerDriver | ||
from .types import ( | ||
GCODE, | ||
StackerAxis, | ||
PlatformStatus, | ||
Direction, | ||
StackerInfo, | ||
HardwareRevision, | ||
MoveParams, | ||
LimitSwitchStatus, | ||
) | ||
|
||
|
||
FS_BAUDRATE = 115200 | ||
DEFAULT_FS_TIMEOUT = 40 | ||
FS_ACK = "OK\n" | ||
FS_ERROR_KEYWORD = "err" | ||
FS_ASYNC_ERROR_ACK = "async" | ||
DEFAULT_COMMAND_RETRIES = 0 | ||
GCODE_ROUNDING_PRECISION = 2 | ||
|
||
|
||
class FlexStackerDriver(StackerDriver): | ||
"""FLEX Stacker driver.""" | ||
|
||
@classmethod | ||
def parse_device_info(cls, response: str) -> StackerInfo: | ||
"""Parse stacker info.""" | ||
_RE = re.compile( | ||
f"^{GCODE.DEVICE_INFO} FW:(?P<fw>.+) HW:Opentrons-flex-stacker-(?P<hw>.+) SerialNo:(?P<sn>.+)" | ||
) | ||
m = _RE.match(response) | ||
if not m: | ||
raise ValueError(f"Incorrect Response for device info: {response}") | ||
return StackerInfo( | ||
m.group("fw"), HardwareRevision(m.group("hw")), m.group("sn") | ||
) | ||
|
||
@classmethod | ||
def parse_limit_switch_status(cls, response: str) -> LimitSwitchStatus: | ||
"""Parse limit switch statuses.""" | ||
field_names = LimitSwitchStatus.get_fields() | ||
pattern = r"/s".join([rf"{name}:(?P<{name}>\d)" for name in field_names]) | ||
_RE = re.compile(f"^{GCODE.GET_LIMIT_SWITCH} {pattern}") | ||
m = _RE.match(response) | ||
if not m: | ||
raise ValueError(f"Incorrect Response for limit siwtch status: {response}") | ||
return LimitSwitchStatus(*(bool(int(m.group(name))) for name in field_names)) | ||
|
||
@classmethod | ||
def parse_platform_sensor_status(cls, response: str) -> PlatformStatus: | ||
"""Parse platform statuses.""" | ||
field_names = PlatformStatus.get_fields() | ||
pattern = r"/s".join([rf"{name}:(?P<{name}>\d)" for name in field_names]) | ||
_RE = re.compile(f"^{GCODE.GET_PLATFORM_SENSOR} {pattern}") | ||
m = _RE.match(response) | ||
if not m: | ||
raise ValueError(f"Incorrect Response for platform status: {response}") | ||
return PlatformStatus(*(bool(int(m.group(name))) for name in field_names)) | ||
|
||
@classmethod | ||
def parse_door_closed(cls, response: str) -> bool: | ||
"""Parse door closed.""" | ||
_RE = re.compile(r"^M122 (\d)") | ||
match = _RE.match(response) | ||
if not match: | ||
raise ValueError(f"Incorrect Response for door closed: {response}") | ||
return bool(int(match.group(1))) | ||
|
||
@classmethod | ||
def append_move_params( | ||
cls, command: CommandBuilder, params: MoveParams | None | ||
) -> CommandBuilder: | ||
"""Append move params.""" | ||
if params is not None: | ||
if params.max_speed is not None: | ||
command.add_float("V", params.max_speed, GCODE_ROUNDING_PRECISION) | ||
if params.acceleration is not None: | ||
command.add_float("A", params.acceleration, GCODE_ROUNDING_PRECISION) | ||
if params.max_speed_discont is not None: | ||
command.add_float( | ||
"D", params.max_speed_discont, GCODE_ROUNDING_PRECISION | ||
) | ||
return command | ||
|
||
@classmethod | ||
async def create( | ||
cls, port: str, loop: Optional[asyncio.AbstractEventLoop] | ||
) -> "FlexStackerDriver": | ||
"""Create a FLEX Stacker driver.""" | ||
connection = await AsyncResponseSerialConnection.create( | ||
port=port, | ||
baud_rate=FS_BAUDRATE, | ||
timeout=DEFAULT_FS_TIMEOUT, | ||
number_of_retries=DEFAULT_COMMAND_RETRIES, | ||
ack=FS_ACK, | ||
loop=loop, | ||
error_keyword=FS_ERROR_KEYWORD, | ||
async_error_ack=FS_ASYNC_ERROR_ACK, | ||
) | ||
return cls(connection) | ||
|
||
def __init__(self, connection: AsyncResponseSerialConnection) -> None: | ||
""" | ||
Constructor | ||
|
||
Args: | ||
connection: Connection to the FLEX Stacker | ||
""" | ||
self._connection = connection | ||
|
||
async def connect(self) -> None: | ||
"""Connect to stacker.""" | ||
await self._connection.open() | ||
|
||
async def disconnect(self) -> None: | ||
"""Disconnect from stacker.""" | ||
await self._connection.close() | ||
|
||
async def is_connected(self) -> bool: | ||
"""Check connection to stacker.""" | ||
return await self._connection.is_open() | ||
|
||
async def get_device_info(self) -> StackerInfo: | ||
"""Get Device Info.""" | ||
response = await self._connection.send_command( | ||
GCODE.DEVICE_INFO.build_command() | ||
) | ||
return self.parse_device_info(response) | ||
|
||
async def set_serial_number(self, sn: str) -> None: | ||
"""Set Serial Number.""" | ||
await self._connection.send_command( | ||
GCODE.SET_SERIAL_NUMBER.build_command().add_element(sn) | ||
) | ||
|
||
async def stop_motor(self) -> None: | ||
"""Stop motor movement.""" | ||
await self._connection.send_command(GCODE.STOP_MOTOR.build_command()) | ||
|
||
async def get_limit_switch(self, axis: StackerAxis, direction: Direction) -> bool: | ||
"""Get limit switch status. | ||
|
||
:return: True if limit switch is triggered, False otherwise | ||
""" | ||
response = await self.get_limit_switches_status() | ||
return response.get(axis, direction) | ||
|
||
async def get_limit_switches_status(self) -> LimitSwitchStatus: | ||
"""Get limit switch statuses for all axes.""" | ||
response = await self._connection.send_command( | ||
GCODE.GET_LIMIT_SWITCH.build_command() | ||
) | ||
return self.parse_limit_switch_status(response) | ||
|
||
async def get_platform_sensor_status(self) -> PlatformStatus: | ||
"""Get platform sensor status. | ||
|
||
:return: True if platform is detected, False otherwise | ||
""" | ||
response = await self._connection.send_command( | ||
GCODE.GET_PLATFORM_SENSOR.build_command() | ||
) | ||
return self.parse_platform_sensor_status(response) | ||
|
||
async def get_hopper_door_closed(self) -> bool: | ||
"""Get whether or not door is closed. | ||
|
||
:return: True if door is closed, False otherwise | ||
""" | ||
response = await self._connection.send_command( | ||
GCODE.GET_DOOR_SWITCH.build_command() | ||
) | ||
return self.parse_door_closed(response) | ||
|
||
async def move_in_mm( | ||
self, axis: StackerAxis, distance: float, params: MoveParams | None = None | ||
) -> None: | ||
"""Move axis.""" | ||
command = self.append_move_params( | ||
GCODE.MOVE_TO.build_command().add_float( | ||
axis.name, distance, GCODE_ROUNDING_PRECISION | ||
), | ||
params, | ||
) | ||
await self._connection.send_command(command) | ||
|
||
async def move_to_limit_switch( | ||
self, axis: StackerAxis, direction: Direction, params: MoveParams | None = None | ||
) -> None: | ||
"""Move until limit switch is triggered.""" | ||
command = self.append_move_params( | ||
GCODE.MOVE_TO_SWITCH.build_command().add_int(axis.name, direction.value), | ||
params, | ||
) | ||
await self._connection.send_command(command) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.