Skip to content

Commit

Permalink
refactor(hardware): Scripts use hardware can context manager (#10274)
Browse files Browse the repository at this point in the history
* conftest

* can_comm

* can_control

* can_mon

* gripper

* gripper_currents

* monitor_sensors

* move

* sensor_pass

* sensors

* set_current

* update_fw

* lint

* bug in can_control
  • Loading branch information
amitlissack authored May 12, 2022
1 parent 22f8dbd commit 90d8b0b
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 151 deletions.
16 changes: 9 additions & 7 deletions hardware/opentrons_hardware/scripts/can_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from logging.config import dictConfig
from typing import Type, Sequence, Callable, TypeVar

from opentrons_hardware.drivers.can_bus import build
from opentrons_hardware.firmware_bindings.constants import (
MessageId,
NodeId,
Expand All @@ -20,7 +21,6 @@
from opentrons_hardware.drivers.can_bus.abstract_driver import AbstractCanDriver
from opentrons_hardware.firmware_bindings.messages.messages import get_definition

from opentrons_hardware.drivers.can_bus.build import build_driver
from opentrons_hardware.scripts.can_args import add_can_args, build_settings
from opentrons_hardware.firmware_bindings.utils import (
BinarySerializable,
Expand Down Expand Up @@ -213,10 +213,8 @@ async def ui_task(can_driver: AbstractCanDriver) -> None:
print(in_red(str(e)))


async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
driver = await build_driver(build_settings(args))

async def run_ui(driver: AbstractCanDriver) -> None:
"""Run the UI."""
loop = asyncio.get_event_loop()
fut = asyncio.gather(
loop.create_task(listen_task(driver)), loop.create_task(ui_task(driver))
Expand All @@ -227,8 +225,12 @@ async def run(args: argparse.Namespace) -> None:
fut.cancel()
except asyncio.CancelledError:
pass
finally:
driver.shutdown()


async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
async with build.driver(build_settings(args)) as driver:
await (run_ui(driver))


def in_red(s: str) -> str:
Expand Down
38 changes: 15 additions & 23 deletions hardware/opentrons_hardware/scripts/can_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
from logging.config import dictConfig
from typing import TextIO, Optional

from opentrons_hardware.drivers.can_bus import build, CanMessenger
from opentrons_hardware.drivers.can_bus.abstract_driver import AbstractCanDriver
from opentrons_hardware.drivers.can_bus.can_messenger import (
CanMessenger,
)
from opentrons_hardware.firmware_bindings.message import CanMessage

from opentrons_hardware.drivers.can_bus.build import build_driver
from opentrons_hardware.firmware_bindings.message import CanMessage
from opentrons_hardware.scripts.can_args import add_can_args, build_settings

from .can_mon import task as monitor_task
Expand Down Expand Up @@ -76,24 +73,19 @@ async def input_task(

async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
driver = await build_driver(build_settings(args))

messenger = CanMessenger(driver)
messenger.start()

try:
all_fut = asyncio.gather(
monitor_task(messenger, args.output),
input_task(driver, args.input, args.output),
)
await all_fut
except KeyboardInterrupt:
all_fut.cancel()
except asyncio.CancelledError:
pass
finally:
await messenger.stop()
driver.shutdown()
async with build.driver(build_settings(args)) as driver, CanMessenger(
driver
) as messenger:
try:
all_fut = asyncio.gather(
monitor_task(messenger, args.output),
input_task(driver, args.input, args.output),
)
await all_fut
except KeyboardInterrupt:
all_fut.cancel()
except asyncio.CancelledError:
pass


LOG_CONFIG = {
Expand Down
30 changes: 11 additions & 19 deletions hardware/opentrons_hardware/scripts/can_mon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from logging.config import dictConfig
from typing import List, TextIO


from opentrons_hardware.drivers.can_bus.can_messenger import (
from opentrons_hardware.drivers.can_bus import (
build,
CanMessenger,
WaitableCallback,
)
Expand All @@ -19,7 +19,6 @@
NodeId,
)

from opentrons_hardware.drivers.can_bus.build import build_driver
from opentrons_hardware.scripts.can_args import add_can_args, build_settings

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -114,22 +113,15 @@ async def task(

async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
driver = await build_driver(build_settings(args))

messenger = CanMessenger(driver)
messenger.start()

loop = asyncio.get_event_loop()
fut = loop.create_task(task(messenger, args.output))
try:
await fut
except KeyboardInterrupt:
fut.cancel()
except asyncio.CancelledError:
pass
finally:
await messenger.stop()
driver.shutdown()
async with build.can_messenger(build_settings(args)) as messenger:
loop = asyncio.get_event_loop()
fut = loop.create_task(task(messenger, args.output))
try:
await fut
except KeyboardInterrupt:
fut.cancel()
except asyncio.CancelledError:
pass


LOG_CONFIG = {
Expand Down
27 changes: 14 additions & 13 deletions hardware/opentrons_hardware/scripts/gripper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from typing import Callable
from logging.config import dictConfig

from opentrons_hardware.drivers.can_bus import build
from opentrons_hardware.firmware_bindings.messages import payloads
from opentrons_hardware.firmware_bindings.messages.message_definitions import (
SetupRequest,
Expand All @@ -15,7 +17,6 @@
from opentrons_hardware.drivers.can_bus.can_messenger import CanMessenger
from opentrons_hardware.firmware_bindings.constants import NodeId
from opentrons_hardware.scripts.can_args import add_can_args, build_settings
from opentrons_hardware.drivers.can_bus.build import build_driver
from opentrons_hardware.hardware_control.gripper_settings import (
set_pwm_param,
set_reference_voltage,
Expand Down Expand Up @@ -82,19 +83,12 @@ def output_details(i: int, freq: int, duty_cycle: int, v_ref: float) -> None:
print(f"PWM: {freq}Hz {duty_cycle}%\n")


async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
os.system("cls")
os.system("clear")

print("Gripper testing beings... \n")
async def run_test(messenger: CanMessenger) -> None:
"""Run the for test."""
print("Gripper testing begins... \n")
pwm_freq = prompt_int_input("PWM frequency in Hz (int)")
pwm_duty = prompt_int_input("PWM duty cycle in % (int)")

driver = await build_driver(build_settings(args))
messenger = CanMessenger(driver=driver)
messenger.start()

"""Setup gripper"""
try:
await messenger.send(node_id=NodeId.gripper, message=SetupRequest())
Expand All @@ -119,8 +113,15 @@ async def run(args: argparse.Namespace) -> None:
finally:
print("\nTesting finishes...\n")
await messenger.send(node_id=NodeId.gripper, message=DisableMotorRequest())
await messenger.stop()
driver.shutdown()


async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
os.system("cls")
os.system("clear")

async with build.can_messenger(build_settings(args)) as messenger:
await run_test(messenger)


log = logging.getLogger(__name__)
Expand Down
27 changes: 14 additions & 13 deletions hardware/opentrons_hardware/scripts/gripper_currents.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from typing import Callable
from logging.config import dictConfig

from opentrons_hardware.drivers.can_bus import build
from opentrons_hardware.firmware_bindings.messages.message_definitions import (
SetupRequest,
DisableMotorRequest,
Expand All @@ -17,7 +19,6 @@
from opentrons_hardware.drivers.can_bus.can_messenger import CanMessenger
from opentrons_hardware.firmware_bindings.constants import NodeId
from opentrons_hardware.scripts.can_args import add_can_args, build_settings
from opentrons_hardware.drivers.can_bus.build import build_driver
from opentrons_hardware.hardware_control.gripper_settings import (
set_pwm_param,
set_reference_voltage,
Expand Down Expand Up @@ -73,22 +74,15 @@ async def execute_move(messenger: CanMessenger) -> None:
)


async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
os.system("cls")
os.system("clear")

print("Gripper testing beings... \n")
async def run_test(messenger: CanMessenger) -> None:
"""Run test."""
print("Gripper testing begins... \n")
print("Hints: \033[96mdefaults values\033[0m \n")
v_ref = prompt_float_input(
"Set reference voltage in A (float, \033[96m0.5A\033[0m)"
)
pwm_freq = prompt_int_input("Set PWM frequency in Hz (int, \033[96m32000Hz\033[0m)")

driver = await build_driver(build_settings(args))
messenger = CanMessenger(driver=driver)
messenger.start()

try:
await messenger.send(node_id=NodeId.gripper, message=SetupRequest())
await set_reference_voltage(messenger, v_ref)
Expand All @@ -114,8 +108,15 @@ async def run(args: argparse.Namespace) -> None:
finally:
print("\nTesting finishes...\n")
await messenger.send(node_id=NodeId.gripper, message=DisableMotorRequest())
await messenger.stop()
driver.shutdown()


async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
os.system("cls")
os.system("clear")

async with build.can_messenger(build_settings(args)) as messenger:
await run_test(messenger)


log = logging.getLogger(__name__)
Expand Down
12 changes: 5 additions & 7 deletions hardware/opentrons_hardware/scripts/monitor_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import argparse
import datetime

from opentrons_hardware.drivers.can_bus.can_messenger import (
from opentrons_hardware.drivers.can_bus import (
build,
CanMessenger,
WaitableCallback,
)
Expand All @@ -19,7 +20,6 @@
sensor_fixed_point_conversion,
)

from opentrons_hardware.drivers.can_bus.build import build_driver
from opentrons_hardware.scripts.can_args import add_can_args, build_settings


Expand Down Expand Up @@ -67,14 +67,12 @@ async def do_run(

async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
driver = await build_driver(build_settings(args))
target = constants.NodeId["pipette_" + args.mount]
sensor = constants.SensorType[args.sensor]

messenger = CanMessenger(driver)
messenger.start()
with WaitableCallback(messenger) as reader:
return await do_run(messenger, reader, target, sensor, args.threshold)
async with build.can_messenger(build_settings(args)) as messenger:
with WaitableCallback(messenger) as reader:
return await do_run(messenger, reader, target, sensor, args.threshold)


def main() -> None:
Expand Down
29 changes: 11 additions & 18 deletions hardware/opentrons_hardware/scripts/move.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import logging
from numpy import float64
from logging.config import dictConfig
from typing import Optional

from opentrons_hardware.drivers.can_bus import CanDriver
from opentrons_hardware.drivers.can_bus.can_messenger import CanMessenger
from opentrons_hardware.drivers.can_bus import build, CanMessenger
from opentrons_hardware.firmware_bindings.constants import NodeId

from opentrons_hardware.firmware_bindings.messages.message_definitions import (
Expand All @@ -16,8 +14,7 @@
)
from opentrons_hardware.hardware_control.motion import MoveGroupSingleAxisStep
from opentrons_hardware.hardware_control.move_group_runner import MoveGroupRunner
from opentrons_hardware.scripts.can_args import add_can_args

from opentrons_hardware.scripts.can_args import add_can_args, build_settings

log = logging.getLogger(__name__)

Expand All @@ -43,15 +40,8 @@
}


async def run(interface: str, bitrate: int, channel: Optional[str] = None) -> None:
"""Entry point for script."""
log.info(f"Connecting to {interface} {bitrate} {channel}")
driver = await CanDriver.build(
bitrate=bitrate, interface=interface, channel=channel
)
messenger = CanMessenger(driver=driver)
messenger.start()

async def run_move(messenger: CanMessenger) -> None:
"""Run the move."""
await messenger.send(node_id=NodeId.broadcast, message=SetupRequest())
await messenger.send(node_id=NodeId.broadcast, message=EnableMotorRequest())

Expand Down Expand Up @@ -99,9 +89,12 @@ async def run(interface: str, bitrate: int, channel: Optional[str] = None) -> No
await runner.run(can_messenger=messenger)
except asyncio.CancelledError:
pass
finally:
await messenger.stop()
driver.shutdown()


async def run(args: argparse.Namespace) -> None:
"""Entry point for script."""
async with build.can_messenger(build_settings(args)) as messenger:
await run_move(messenger)


def main() -> None:
Expand All @@ -113,7 +106,7 @@ def main() -> None:

args = parser.parse_args()

asyncio.run(run(args.interface, args.bitrate, args.channel))
asyncio.run(run(args))


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 90d8b0b

Please sign in to comment.