Skip to content

Commit 148a21c

Browse files
Enumerate tx7332s on set_solution (OpenwaterHealth#223)
* Enumerate tx7332s on set_solution Closes OpenwaterHealth#222 * Fix type annotation error * Update test_solution.py * accept autofix * accept autofix * accept fix * Fix type union * fix for linting --------- Co-authored-by: Ebrahim Ebrahim <ebrahim.ebrahim@kitware.com>
1 parent 0db543a commit 148a21c

4 files changed

Lines changed: 37 additions & 24 deletions

File tree

notebooks/test_solution.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
# name: python3
1313
# ---
1414

15+
# +
1516
import numpy as np
1617

1718
import openlifu
1819

20+
# -
21+
1922
pulse = openlifu.Pulse(frequency=500e3, amplitude=1, duration=2e-5)
2023
pt = openlifu.Point(position=(0,0,30), units="mm")
2124
sequence = openlifu.Sequence(
@@ -42,9 +45,8 @@
4245
solution
4346

4447
ifx = openlifu.LIFUInterface(test_mode=True)
45-
ifx.txdevice.enum_tx7332_devices(_num_transmitters=2)
4648

47-
ifx.set_solution(solution)
49+
ifx.set_solution(solution.to_dict())
4850

4951
txm = ifx.txdevice.tx_registers
5052
r = {'DELAY CONTROL': {}, 'DELAY DATA': {}, 'PULSE CONTROL': {}, 'PULSE DATA': {}}

src/openlifu/io/LIFUInterface.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from __future__ import annotations
2+
13
import asyncio
24
import logging
3-
from typing import Dict, Union
5+
from typing import Dict
46

57
from openlifu.io.LIFUHVController import HVController
68
from openlifu.io.LIFUSignal import LIFUSignal
@@ -104,7 +106,7 @@ def is_device_connected(self) -> tuple:
104106
return tx_connected, hv_connected
105107

106108
def set_solution(self,
107-
solution: Union[Solution, Dict],
109+
solution: Solution|Dict,
108110
profile_index:int=1,
109111
profile_increment:bool=True) -> bool:
110112
"""

src/openlifu/io/LIFUTXDevice.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from openlifu.io.LIFUUart import LIFUUart
1313
from openlifu.util.units import getunitconversion
1414

15-
NUM_TRANSMITTERS = 2
15+
DEFAULT_NUM_TRANSMITTERS = 2
1616
ADDRESS_GLOBAL_MODE = 0x0
1717
ADDRESS_STANDBY = 0x1
1818
ADDRESS_DYNPWR_2 = 0x6
@@ -761,10 +761,16 @@ def enter_dfu(self) -> bool:
761761
logger.error("Unexpected error during process: %s", e)
762762
raise # Re-raise the exception for the caller to handle
763763

764-
def enum_tx7332_devices(self, _num_transmitters=2) -> int:
764+
def enum_tx7332_devices(self, num_devices: int | None = None) -> int:
765765
"""
766766
Enumerate TX7332 devices connected to the TX device.
767767
768+
Args:
769+
num_transmitters (int): The number of transmitters expected to be enumerated. If None, the number of
770+
transmitters will be determined from the response. If provided and the number enumerated does not
771+
match the expected number, an error will be raised. If the UART is in demo mode, this argument is
772+
used to set the number of transmitters for the demo (or set to a default if omitted/None)
773+
768774
Returns:
769775
n_transmitters: number of devices detected.
770776
@@ -774,24 +780,23 @@ def enum_tx7332_devices(self, _num_transmitters=2) -> int:
774780
"""
775781
try:
776782
if self.uart.demo_mode:
777-
n_transmitters = _num_transmitters
778-
self.tx_registers = TxDeviceRegisters(num_transmitters=n_transmitters)
779-
return n_transmitters
780-
781-
if not self.uart.is_connected():
782-
raise ValueError("TX Device not connected")
783-
784-
r = self.uart.send_packet(id=None, packetType=OW_TX7332, command=OW_TX7332_ENUM)
785-
self.uart.clear_buffer()
786-
# r.print_packet()
787-
if r.packet_type != OW_ERROR and r.reserved > 0:
788-
n_transmitters = r.reserved
783+
num_detected_devices = num_devices
789784
else:
790-
logger.info("Error enumerating TX devices.")
785+
if not self.uart.is_connected():
786+
raise ValueError("TX Device not connected")
791787

792-
self.tx_registers = TxDeviceRegisters(num_transmitters=n_transmitters)
793-
logger.info("TX Device Count: %d", n_transmitters)
794-
return n_transmitters
788+
r = self.uart.send_packet(id=None, packetType=OW_TX7332, command=OW_TX7332_ENUM)
789+
self.uart.clear_buffer()
790+
# r.print_packet()
791+
if r.packet_type != OW_ERROR and r.reserved > 0:
792+
num_detected_devices = r.reserved
793+
else:
794+
logger.info("Error enumerating TX devices.")
795+
if num_devices is not None and num_detected_devices != num_devices:
796+
raise ValueError(f"Expected {num_devices} devices, but detected {num_detected_devices} devices")
797+
self.tx_registers = TxDeviceRegisters(num_transmitters=num_detected_devices)
798+
logger.info("TX Device Count: %d", num_detected_devices)
799+
return num_detected_devices
795800
except ValueError as v:
796801
logger.error("ValueError: %s", v)
797802
raise # Re-raise the exception for the caller to handle
@@ -1222,6 +1227,10 @@ def set_solution(self,
12221227
if apodizations.ndim == 1:
12231228
apodizations = apodizations.reshape(1, -1)
12241229
n = delays.shape[0]
1230+
n_elements = delays.shape[1]
1231+
n_required_devices = int(n_elements / NUM_CHANNELS)
1232+
self.enum_tx7332_devices(num_devices=n_required_devices)
1233+
12251234
if n != apodizations.shape[0]:
12261235
raise ValueError("Delays and apodizations must have the same number of rows")
12271236
if n > 1:
@@ -1787,7 +1796,7 @@ class TxDeviceRegisters:
17871796
_profiles_list: List[Tx7332PulseProfile] = field(default_factory=list)
17881797
active_delay_profile: int | None = None
17891798
active_profile: int | None = None
1790-
num_transmitters: int = NUM_TRANSMITTERS
1799+
num_transmitters: int = DEFAULT_NUM_TRANSMITTERS
17911800

17921801
def __post_init__(self):
17931802
self.transmitters = tuple([Tx7332Registers(bf_clk=self.bf_clk) for _ in range(self.num_transmitters)])

tests/test_sonication_control_mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def example_solution() -> Solution:
3232
def test_lifuinterface_mock(example_solution:Solution):
3333
"""Test that LIFUInterface can be used in mock mode (i.e. test_mode=True)"""
3434
lifu_interface = LIFUInterface(test_mode=True)
35-
lifu_interface.txdevice.enum_tx7332_devices(_num_transmitters=2)
35+
lifu_interface.txdevice.enum_tx7332_devices(num_devices=2)
3636
lifu_interface.set_solution(example_solution)
3737
lifu_interface.start_sonication()
3838
status = lifu_interface.get_status()

0 commit comments

Comments
 (0)