This commit is contained in:
awe
2026-02-18 18:26:41 +03:00
parent 584dea1623
commit 662a42776f
2 changed files with 24 additions and 7 deletions

View File

@ -31,8 +31,8 @@ from .constants import WAIT_AFTER_SEND_SEC
logger = logging.getLogger(__name__)
# Default PI regulator coefficients (match firmware defaults)
DEFAULT_PI_P = 1
DEFAULT_PI_I = 1
DEFAULT_PI_P = 2560 # 10 * 256
DEFAULT_PI_I = 128 # 0.5 * 256
class LaserController:
@ -150,7 +150,7 @@ class LaserController:
pi_coeff2_i=self._pi2_i,
message_id=self._message_id,
)
self._send(cmd)
self._send_and_read_state(cmd)
logger.debug("Manual mode set: T1=%.2f T2=%.2f I1=%.2f I2=%.2f",
validated['temp1'], validated['temp2'],
validated['current1'], validated['current2'])
@ -228,7 +228,7 @@ class LaserController:
pi_coeff2_p=self._pi2_p,
pi_coeff2_i=self._pi2_i,
)
self._send(cmd)
self._send_and_read_state(cmd)
logger.info("Variation task started: type=%s min=%.3f max=%.3f step=%.3f",
validated['variation_type'].name,
validated['min_value'],
@ -238,7 +238,7 @@ class LaserController:
def stop_task(self) -> None:
"""Stop the current task by sending DEFAULT_ENABLE (reset)."""
cmd = Protocol.encode_default_enable()
self._send(cmd)
self._send_and_read_state(cmd)
logger.info("Task stopped (DEFAULT_ENABLE sent)")
def get_measurements(self) -> Optional[Measurements]:
@ -303,7 +303,7 @@ class LaserController:
def reset(self) -> None:
"""Send a hardware reset command to the device."""
cmd = Protocol.encode_default_enable()
self._send(cmd)
self._send_and_read_state(cmd)
logger.info("Device reset command sent")
# ---- Internal helpers -------------------------------------------------
@ -315,6 +315,23 @@ class LaserController:
self._protocol.send_raw(cmd)
time.sleep(WAIT_AFTER_SEND_SEC)
def _send_and_read_state(self, cmd: bytes) -> int:
"""Send command and read the 2-byte STATE response the device always returns.
Commands DECODE_ENABLE, TASK_ENABLE and DEFAULT_ENABLE each trigger a
STATE reply from the firmware. If we don't consume those bytes here,
they accumulate in the serial buffer and corrupt the next DATA read.
Returns the decoded state code (0x0000 = OK).
"""
self._send(cmd)
raw = self._protocol.receive_raw(2)
if raw and len(raw) == 2:
state = Protocol.decode_state(raw)
logger.debug("STATE response after command: 0x%04x", state)
return state
return 0
# ---- Context manager support -----------------------------------------
def __enter__(self):

View File

@ -1,6 +1,6 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.backends.legacy:build"
build-backend = "setuptools.build_meta"
[project]
name = "laser_control"