Skip to content

Commit e3beb01

Browse files
Merge pull request #15 from OpenwaterHealth/next
Next
2 parents 9523a9c + 20ff2e8 commit e3beb01

1 file changed

Lines changed: 67 additions & 60 deletions

File tree

motion_connector.py

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,70 +1508,77 @@ def set_laser_power_from_config(self, interface):
15081508
skip_entries.add(_OPT_DRIVE_CL)
15091509

15101510
self._console_mutex.lock()
1511-
for idx, laser_param in enumerate(self.laser_params, start=1):
1512-
muxIdx = laser_param["muxIdx"]
1513-
channel = laser_param["channel"]
1514-
i2cAddr = laser_param["i2cAddr"]
1515-
offset = laser_param["offset"]
1516-
dataToSend = bytearray(laser_param["dataToSend"])
1517-
1518-
if (channel, offset) in skip_entries:
1511+
try:
1512+
for idx, laser_param in enumerate(self.laser_params, start=1):
1513+
muxIdx = laser_param["muxIdx"]
1514+
channel = laser_param["channel"]
1515+
i2cAddr = laser_param["i2cAddr"]
1516+
offset = laser_param["offset"]
1517+
dataToSend = bytearray(laser_param["dataToSend"])
1518+
1519+
if (channel, offset) in skip_entries:
1520+
logger.info(
1521+
f"[Connector] Skipping JSON entry ch={channel} off=0x{offset:02X} "
1522+
f"(overridden by user config)"
1523+
)
1524+
continue
1525+
15191526
logger.info(
1520-
f"[Connector] Skipping JSON entry ch={channel} off=0x{offset:02X} "
1521-
f"(overridden by user config)"
1527+
f"[Connector] ({idx}/{len(self.laser_params)}) "
1528+
f"Writing I2C: muxIdx={muxIdx}, channel={channel}, "
1529+
f"i2cAddr=0x{i2cAddr:02X}, offset=0x{offset:02X}, "
1530+
f"data={list(dataToSend)}"
15221531
)
1523-
continue
15241532

1525-
logger.info(
1526-
f"[Connector] ({idx}/{len(self.laser_params)}) "
1527-
f"Writing I2C: muxIdx={muxIdx}, channel={channel}, "
1528-
f"i2cAddr=0x{i2cAddr:02X}, offset=0x{offset:02X}, "
1529-
f"data={list(dataToSend)}"
1530-
)
1533+
if not interface.console_module.write_i2c_packet(
1534+
mux_index=muxIdx,
1535+
channel=channel,
1536+
device_addr=i2cAddr,
1537+
reg_addr=offset,
1538+
data=dataToSend,
1539+
):
1540+
logger.error(
1541+
f"Failed to set laser power (muxIdx={muxIdx}, channel={channel})"
1542+
)
1543+
return False
15311544

1532-
if not interface.console_module.write_i2c_packet(
1533-
mux_index=muxIdx,
1534-
channel=channel,
1535-
device_addr=i2cAddr,
1536-
reg_addr=offset,
1537-
data=dataToSend,
1538-
):
1539-
logger.error(
1540-
f"Failed to set laser power (muxIdx={muxIdx}, channel={channel})"
1541-
)
1542-
return False
1545+
# ------------------------------------------------------------------
1546+
# Write user-config DRIVE CL overrides after the JSON pass.
1547+
# Default scale matches the static FpgaModel value (1.86 mA/LSB).
1548+
# Data format: 16-bit LSB-first (isMsbFirst=false in FpgaModel).
1549+
# thresh is a raw uint16 register value; gain is a float scale factor
1550+
# used only for the QML FpgaData scale override.
1551+
# ------------------------------------------------------------------
15431552

1544-
# ------------------------------------------------------------------
1545-
# Write user-config DRIVE CL overrides after the JSON pass.
1546-
# Default scale matches the static FpgaModel value (1.86 mA/LSB).
1547-
# Data format: 16-bit LSB-first (isMsbFirst=false in FpgaModel).
1548-
# thresh is a raw uint16 register value; gain is a float scale factor
1549-
# used only for the QML FpgaData scale override (not for raw calculation).
1550-
# ------------------------------------------------------------------
1553+
def _write_drive_cl(ch: int, thresh, gain: float, label: str) -> bool:
1554+
if thresh is None:
1555+
return True
1556+
set_value = thresh
1557+
gain_f = float(gain) if gain is not None else 0.0
1558+
if gain_f != 0.0:
1559+
set_value = thresh/gain_f
1560+
raw = max(0, min(0xFFFF, int(round(set_value)))) # uint16 raw value
1561+
data = bytearray([raw & 0xFF, (raw >> 8) & 0xFF]) # LSB first
15511562

1552-
def _write_drive_cl(ch: int, thresh, gain: float, label: str) -> bool:
1553-
if thresh is None:
1554-
return True
1555-
raw = max(0, min(0xFFFF, int(thresh))) # uint16 raw value
1556-
data = bytearray([raw & 0xFF, (raw >> 8) & 0xFF]) # LSB first
1557-
gain_f = float(gain) if gain is not None else 0.0
1558-
logger.info(
1559-
f"[Connector] Writing user-config {label} DRIVE CL: "
1560-
f"raw={raw}, gain={gain_f}{list(data)}"
1561-
)
1562-
return interface.console_module.write_i2c_packet(
1563-
mux_index=1, channel=ch, device_addr=0x41, reg_addr=0x10, data=data
1564-
)
1563+
logger.info(
1564+
f"[Connector] Writing user-config {label} DRIVE CL: "
1565+
f"raw={raw}, gain={gain_f}{list(data)}"
1566+
)
1567+
return interface.console_module.write_i2c_packet(
1568+
mux_index=1, channel=ch, device_addr=0x41, reg_addr=0x10, data=data
1569+
)
15651570

1566-
if not _write_drive_cl(6, ee_thresh, ee_gain, "Safety EE"):
1567-
logger.error("Failed to write user-config Safety EE DRIVE CL")
1568-
return False
1569-
if not _write_drive_cl(7, opt_thresh, opt_gain, "Safety OPT"):
1570-
logger.error("Failed to write user-config Safety OPT DRIVE CL")
1571-
return False
1571+
if not _write_drive_cl(6, ee_thresh, ee_gain, "Safety EE"):
1572+
logger.error("Failed to write user-config Safety EE DRIVE CL")
1573+
return False
1574+
if not _write_drive_cl(7, opt_thresh, opt_gain, "Safety OPT"):
1575+
logger.error("Failed to write user-config Safety OPT DRIVE CL")
1576+
return False
1577+
1578+
logger.info("Laser power set successfully.")
1579+
finally:
1580+
self._console_mutex.unlock()
15721581

1573-
logger.info("Laser power set successfully.")
1574-
self._console_mutex.unlock()
15751582
return True
15761583

15771584
@pyqtProperty(str, notify=csvOutputDirectoryChanged)
@@ -3422,7 +3429,7 @@ def tec_status(self):
34223429

34233430
self.tecStatusChanged.emit()
34243431

3425-
return True
3432+
return ok
34263433

34273434
except Exception as e:
34283435
logger.error(f"Error in TEC status operation: {e}")
@@ -3534,7 +3541,7 @@ def run(self):
35343541
# 1. TEC status poll
35353542
#
35363543
# This updates _tec_* fields inside connector and emits tecStatusChanged
3537-
self.connector.tec_status()
3544+
ok_tec = self.connector.tec_status()
35383545

35393546
#
35403547
# 2. PDU Mon poll
@@ -3571,8 +3578,8 @@ def run(self):
35713578

35723579
ok_se = (statuses["SE"] & 0x0F) == 0
35733580
ok_so = (statuses["SO"] & 0x0F) == 0
3574-
3575-
if ok_se and ok_so:
3581+
3582+
if ok_se and ok_so and ok_tec:
35763583
if self.connector._safetyFailure:
35773584
self.connector._safetyFailure = False
35783585
self.connector.safetyFailureStateChanged.emit(False)

0 commit comments

Comments
 (0)