Skip to content

Commit

Permalink
broadcast request msg
Browse files Browse the repository at this point in the history
  • Loading branch information
ahiuchingau committed Mar 7, 2024
1 parent a9301ec commit 8f47695
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 31 deletions.
16 changes: 7 additions & 9 deletions api/src/opentrons/hardware_control/backends/ot3controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
set_enable_tip_motor,
set_disable_tip_motor,
get_motor_enabled,
get_tip_motor_enabled,
)
from opentrons_hardware.hardware_control.motor_position_status import (
get_motor_position,
Expand Down Expand Up @@ -1179,17 +1178,16 @@ def engaged_axes(self) -> OT3AxisMap[bool]:
async def update_engaged_axes(self) -> None:
"""Update engaged axes."""
motor_nodes = self._motor_nodes()
for node in motor_nodes:
await self.is_motor_engaged(node_to_axis(node))
results = await get_motor_enabled(self._messenger, motor_nodes)
for node, status in results.items():
self._engaged_axes[node_to_axis(node)] = status

async def is_motor_engaged(self, axis: Axis) -> bool:
node = axis_to_node(axis)
if axis == Axis.Q:
result = await get_tip_motor_enabled(self._messenger, node)
else:
result = await get_motor_enabled(self._messenger, node)
self._engaged_axes.update({axis: result})
return result
result = await get_motor_enabled(self._messenger, {node})
engaged = result[node]
self._engaged_axes.update({axis: engaged})
return engaged

async def disengage_axes(self, axes: List[Axis]) -> None:
"""Disengage axes."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Utilities for updating the enable/disable state of an OT3 axis."""
from typing import Set
from typing import Dict, Set
import logging
import asyncio
from opentrons_hardware.drivers.can_bus.can_messenger import (
Expand All @@ -21,6 +21,7 @@
ErrorCode,
MessageId,
)
from opentrons_hardware.firmware_bindings.messages.messages import MessageDefinition

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -87,30 +88,41 @@ async def set_disable_tip_motor(

async def get_motor_enabled(
can_messenger: CanMessenger,
node: NodeId,
timeout: float = 0.5,
) -> bool:
"""Get motor status of a node."""
nodes: Set[NodeId],
timeout: float = 1.0,
) -> Dict[NodeId, bool]:
"""Get motor status of a set of nodes."""
expected = nodes or set()
reported: Dict[NodeId, bool] = {}
event = asyncio.Event()

Check warning on line 97 in hardware/opentrons_hardware/hardware_control/motor_enable_disable.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/hardware_control/motor_enable_disable.py#L95-L97

Added lines #L95 - L97 were not covered by tests

def _filter(arbitration_id: ArbitrationId) -> bool:
return (NodeId(arbitration_id.parts.originating_node_id) == node) and (
MessageId(arbitration_id.parts.message_id) == GetStatusResponse.message_id
)
def _filter(arb_id: ArbitrationId) -> bool:
return MessageId(arb_id.parts.message_id) == GetStatusResponse.message_id

Check warning on line 100 in hardware/opentrons_hardware/hardware_control/motor_enable_disable.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/hardware_control/motor_enable_disable.py#L99-L100

Added lines #L99 - L100 were not covered by tests

async def _wait_for_response(reader: WaitableCallback) -> bool:
def _listener(message: MessageDefinition, arb_id: ArbitrationId) -> None:

Check warning on line 102 in hardware/opentrons_hardware/hardware_control/motor_enable_disable.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/hardware_control/motor_enable_disable.py#L102

Added line #L102 was not covered by tests
"""Listener for receving motor status messages."""
async for response, _ in reader:
if isinstance(response, GetStatusResponse):
return bool(response.payload.status.value)
raise StopAsyncIteration

with WaitableCallback(can_messenger, _filter) as reader:
await can_messenger.send(node_id=node, message=GetStatusRequest())
try:
return await asyncio.wait_for(_wait_for_response(reader), timeout)
except asyncio.TimeoutError:
log.warning("Read motor status timed out")
raise StopAsyncIteration
if isinstance(message, GetStatusResponse):
reported[NodeId(arb_id.parts.originating_node_id)] = bool(

Check warning on line 105 in hardware/opentrons_hardware/hardware_control/motor_enable_disable.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/hardware_control/motor_enable_disable.py#L104-L105

Added lines #L104 - L105 were not covered by tests
message.payload.status.value
)

# found all expected nodes
if expected.issubset(reported):
event.set()

Check warning on line 111 in hardware/opentrons_hardware/hardware_control/motor_enable_disable.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/hardware_control/motor_enable_disable.py#L110-L111

Added lines #L110 - L111 were not covered by tests

can_messenger.add_listener(_listener, _filter)
await can_messenger.send(node_id=NodeId.broadcast, message=GetStatusRequest())
try:
await asyncio.wait_for(event.wait(), timeout)
except asyncio.TimeoutError:
if expected:
log.warning(

Check warning on line 119 in hardware/opentrons_hardware/hardware_control/motor_enable_disable.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/hardware_control/motor_enable_disable.py#L113-L119

Added lines #L113 - L119 were not covered by tests
"Read motor status timed out, missing nodes: "
f"{expected.difference(reported)}"
)
else:
log.debug("Read motor status terminated, no missing nodes.")
return reported

Check warning on line 125 in hardware/opentrons_hardware/hardware_control/motor_enable_disable.py

View check run for this annotation

Codecov / codecov/patch

hardware/opentrons_hardware/hardware_control/motor_enable_disable.py#L124-L125

Added lines #L124 - L125 were not covered by tests


async def get_tip_motor_enabled(
Expand Down

0 comments on commit 8f47695

Please sign in to comment.