fix
This commit is contained in:
@ -31,8 +31,8 @@ from .constants import WAIT_AFTER_SEND_SEC
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# Default PI regulator coefficients (match firmware defaults)
|
# Default PI regulator coefficients (match firmware defaults)
|
||||||
DEFAULT_PI_P = 1
|
DEFAULT_PI_P = 2560 # 10 * 256
|
||||||
DEFAULT_PI_I = 1
|
DEFAULT_PI_I = 128 # 0.5 * 256
|
||||||
|
|
||||||
|
|
||||||
class LaserController:
|
class LaserController:
|
||||||
@ -150,7 +150,7 @@ class LaserController:
|
|||||||
pi_coeff2_i=self._pi2_i,
|
pi_coeff2_i=self._pi2_i,
|
||||||
message_id=self._message_id,
|
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",
|
logger.debug("Manual mode set: T1=%.2f T2=%.2f I1=%.2f I2=%.2f",
|
||||||
validated['temp1'], validated['temp2'],
|
validated['temp1'], validated['temp2'],
|
||||||
validated['current1'], validated['current2'])
|
validated['current1'], validated['current2'])
|
||||||
@ -228,7 +228,7 @@ class LaserController:
|
|||||||
pi_coeff2_p=self._pi2_p,
|
pi_coeff2_p=self._pi2_p,
|
||||||
pi_coeff2_i=self._pi2_i,
|
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",
|
logger.info("Variation task started: type=%s min=%.3f max=%.3f step=%.3f",
|
||||||
validated['variation_type'].name,
|
validated['variation_type'].name,
|
||||||
validated['min_value'],
|
validated['min_value'],
|
||||||
@ -238,7 +238,7 @@ class LaserController:
|
|||||||
def stop_task(self) -> None:
|
def stop_task(self) -> None:
|
||||||
"""Stop the current task by sending DEFAULT_ENABLE (reset)."""
|
"""Stop the current task by sending DEFAULT_ENABLE (reset)."""
|
||||||
cmd = Protocol.encode_default_enable()
|
cmd = Protocol.encode_default_enable()
|
||||||
self._send(cmd)
|
self._send_and_read_state(cmd)
|
||||||
logger.info("Task stopped (DEFAULT_ENABLE sent)")
|
logger.info("Task stopped (DEFAULT_ENABLE sent)")
|
||||||
|
|
||||||
def get_measurements(self) -> Optional[Measurements]:
|
def get_measurements(self) -> Optional[Measurements]:
|
||||||
@ -303,7 +303,7 @@ class LaserController:
|
|||||||
def reset(self) -> None:
|
def reset(self) -> None:
|
||||||
"""Send a hardware reset command to the device."""
|
"""Send a hardware reset command to the device."""
|
||||||
cmd = Protocol.encode_default_enable()
|
cmd = Protocol.encode_default_enable()
|
||||||
self._send(cmd)
|
self._send_and_read_state(cmd)
|
||||||
logger.info("Device reset command sent")
|
logger.info("Device reset command sent")
|
||||||
|
|
||||||
# ---- Internal helpers -------------------------------------------------
|
# ---- Internal helpers -------------------------------------------------
|
||||||
@ -315,6 +315,23 @@ class LaserController:
|
|||||||
self._protocol.send_raw(cmd)
|
self._protocol.send_raw(cmd)
|
||||||
time.sleep(WAIT_AFTER_SEND_SEC)
|
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 -----------------------------------------
|
# ---- Context manager support -----------------------------------------
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
[build-system]
|
[build-system]
|
||||||
requires = ["setuptools"]
|
requires = ["setuptools"]
|
||||||
build-backend = "setuptools.backends.legacy:build"
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "laser_control"
|
name = "laser_control"
|
||||||
|
|||||||
Reference in New Issue
Block a user