-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(can): replace uart script (#8450)
* message payloads. * identify uses payload object. * Create can script. * add todo. * tests. * add mock package. * lint. * memoize. * clean up asyncio stuff. * remove deprecated identify script. can_comm supercedes it. * cosmetics. * async. * add scripts to readme * lint
- Loading branch information
amitlissack
authored
Oct 4, 2021
1 parent
d7eaddc
commit 63eb764
Showing
16 changed files
with
708 additions
and
172 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
1 change: 1 addition & 0 deletions
1
hardware/opentrons_hardware/drivers/can_bus/messages/__init__.py
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 @@ | ||
"""Can bus message definitions.""" |
93 changes: 93 additions & 0 deletions
93
hardware/opentrons_hardware/drivers/can_bus/messages/message_definitions.py
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,93 @@ | ||
"""Defintion of CAN messages.""" | ||
from dataclasses import dataclass | ||
from typing import Type | ||
|
||
from typing_extensions import Literal | ||
|
||
from opentrons_hardware.utils import BinarySerializable | ||
from ..constants import MessageId | ||
from . import payloads | ||
|
||
|
||
@dataclass | ||
class HeartbeatRequest: # noqa: D101 | ||
message_id: Literal[MessageId.heartbeat_request] = MessageId.heartbeat_request | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class HeartbeatResponse: # noqa: D101 | ||
message_id: Literal[MessageId.heartbeat_response] = MessageId.heartbeat_response | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class DeviceInfoRequest: # noqa: D101 | ||
message_id: Literal[MessageId.device_info_request] = MessageId.device_info_request | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class DeviceInfoResponse: # noqa: D101 | ||
message_id: Literal[MessageId.device_info_response] = MessageId.device_info_response | ||
payload_type: Type[BinarySerializable] = payloads.DeviceInfoResponseBody | ||
|
||
|
||
@dataclass | ||
class StopRequest: # noqa: D101 | ||
message_id: Literal[MessageId.stop_request] = MessageId.stop_request | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class GetStatusRequest: # noqa: D101 | ||
message_id: Literal[MessageId.get_status_request] = MessageId.get_status_request | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class GetStatusResponse: # noqa: D101 | ||
message_id: Literal[MessageId.get_status_response] = MessageId.get_status_response | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class MoveRequest: # noqa: D101 | ||
message_id: Literal[MessageId.move_request] = MessageId.move_request | ||
payload_type: Type[BinarySerializable] = payloads.MoveRequest | ||
|
||
|
||
@dataclass | ||
class SetupRequest: # noqa: D101 | ||
message_id: Literal[MessageId.setup_request] = MessageId.setup_request | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class GetSpeedRequest: # noqa: D101 | ||
message_id: Literal[MessageId.get_speed_request] = MessageId.get_speed_request | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class GetSpeedResponse: # noqa: D101 | ||
message_id: Literal[MessageId.get_speed_response] = MessageId.get_speed_response | ||
payload_type: Type[BinarySerializable] = payloads.GetSpeedResponse | ||
|
||
|
||
@dataclass | ||
class WriteToEEPromRequest: # noqa: D101 | ||
message_id: Literal[MessageId.write_eeprom] = MessageId.write_eeprom | ||
payload_type: Type[BinarySerializable] = payloads.WriteToEEPromRequest | ||
|
||
|
||
@dataclass | ||
class ReadFromEEPromRequest: # noqa: D101 | ||
message_id: Literal[MessageId.read_eeprom_request] = MessageId.read_eeprom_request | ||
payload_type: Type[BinarySerializable] = payloads.EmptyMessage | ||
|
||
|
||
@dataclass | ||
class ReadFromEEPromResponse: # noqa: D101 | ||
message_id: Literal[MessageId.read_eeprom_response] = MessageId.read_eeprom_response | ||
payload_type: Type[BinarySerializable] = payloads.ReadFromEEPromResponse |
44 changes: 44 additions & 0 deletions
44
hardware/opentrons_hardware/drivers/can_bus/messages/messages.py
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,44 @@ | ||
"""Message types.""" | ||
from functools import lru_cache | ||
from typing import Union, Optional | ||
|
||
from typing_extensions import get_args | ||
|
||
from opentrons_hardware.drivers.can_bus.messages import message_definitions as defs | ||
from opentrons_hardware.drivers.can_bus.constants import MessageId | ||
|
||
MessageDefinition = Union[ | ||
defs.HeartbeatRequest, | ||
defs.HeartbeatResponse, | ||
defs.DeviceInfoRequest, | ||
defs.DeviceInfoResponse, | ||
defs.StopRequest, | ||
defs.GetStatusRequest, | ||
defs.GetStatusResponse, | ||
defs.MoveRequest, | ||
defs.SetupRequest, | ||
defs.GetSpeedRequest, | ||
defs.GetSpeedResponse, | ||
defs.WriteToEEPromRequest, | ||
defs.ReadFromEEPromRequest, | ||
defs.ReadFromEEPromResponse, | ||
] | ||
|
||
|
||
@lru_cache(maxsize=None) | ||
def get_definition(message_id: MessageId) -> Optional[MessageDefinition]: | ||
"""Get the message type for a message id. | ||
Args: | ||
message_id: A message id | ||
Returns: The message definition for a type | ||
""" | ||
# Dumb linear search, but the result is memoized. | ||
for i in get_args(MessageDefinition): | ||
if i.message_id == message_id: | ||
# get args returns Tuple[Any...] | ||
return i # type: ignore | ||
|
||
return None |
55 changes: 55 additions & 0 deletions
55
hardware/opentrons_hardware/drivers/can_bus/messages/payloads.py
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,55 @@ | ||
"""Payloads of can bus messages.""" | ||
from dataclasses import dataclass | ||
|
||
from opentrons_hardware import utils | ||
|
||
|
||
@dataclass | ||
class EmptyMessage(utils.BinarySerializable): | ||
"""An empty payload.""" | ||
|
||
pass | ||
|
||
|
||
@dataclass | ||
class DeviceInfoResponseBody(utils.BinarySerializable): | ||
"""Device info response.""" | ||
|
||
node_id: utils.UInt8Field | ||
version: utils.UInt32Field | ||
|
||
|
||
@dataclass | ||
class GetStatusResponse(utils.BinarySerializable): | ||
"""Get status response.""" | ||
|
||
status: utils.UInt8Field | ||
data: utils.UInt32Field | ||
|
||
|
||
@dataclass | ||
class MoveRequest(utils.BinarySerializable): | ||
"""Move request.""" | ||
|
||
steps: utils.UInt32Field | ||
|
||
|
||
@dataclass | ||
class GetSpeedResponse(utils.BinarySerializable): | ||
"""Get speed response.""" | ||
|
||
mm_sec: utils.UInt32Field | ||
|
||
|
||
@dataclass | ||
class WriteToEEPromRequest(utils.BinarySerializable): | ||
"""Write to eeprom request.""" | ||
|
||
serial_number: utils.UInt8Field | ||
|
||
|
||
@dataclass | ||
class ReadFromEEPromResponse(utils.BinarySerializable): | ||
"""Read from ee prom response.""" | ||
|
||
serial_number: utils.UInt8Field |
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,25 @@ | ||
"""ArgumentParser setup for a can device.""" | ||
from argparse import ArgumentParser | ||
|
||
|
||
def add_can_args(parser: ArgumentParser) -> ArgumentParser: | ||
"""Add CAN interface arguments to ArgumentParser. | ||
Args: | ||
parser: ArgumentParser | ||
Returns: ArgumentParser with added arguments for CAN interface. | ||
""" | ||
parser.add_argument( | ||
"--interface", | ||
type=str, | ||
required=True, | ||
help="the interface to use (ie: virtual, pcan, socketcan)", | ||
) | ||
parser.add_argument( | ||
"--bitrate", type=int, default=250000, required=False, help="the bitrate" | ||
) | ||
parser.add_argument( | ||
"--channel", type=str, default=None, required=False, help="optional channel" | ||
) | ||
return parser |
Oops, something went wrong.