mirror of
https://github.com/huggingface/lerobot.git
synced 2026-05-11 14:49:43 +00:00
improve serial_reading
This commit is contained in:
@@ -38,6 +38,7 @@ def parse_raw16(line: bytes) -> list[int] | None:
|
||||
|
||||
def read_raw_from_serial(ser) -> list[int] | None:
|
||||
"""Read latest sample from serial; if buffer is backed up, keep only the newest."""
|
||||
try:
|
||||
last = None
|
||||
while ser.in_waiting > 0:
|
||||
b = ser.readline()
|
||||
@@ -51,6 +52,9 @@ def read_raw_from_serial(ser) -> list[int] | None:
|
||||
if b:
|
||||
last = parse_raw16(b)
|
||||
return last
|
||||
except (OSError, serial.SerialException) as e:
|
||||
logger.warning(f"Serial read error: {e}")
|
||||
return None
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -104,13 +108,19 @@ class ExoskeletonArm:
|
||||
logger.warning(f"failed to load calibration: {e}")
|
||||
|
||||
def read_raw(self) -> list[int] | None:
|
||||
if not self._ser:
|
||||
if not self._ser or not self._ser.is_open:
|
||||
return None
|
||||
return read_raw_from_serial(self._ser)
|
||||
|
||||
def get_angles(self) -> dict[str, float]:
|
||||
def get_angles(self, raw: list[int] | None = None) -> dict[str, float]:
|
||||
"""Convert raw ADC values to joint angles.
|
||||
|
||||
Args:
|
||||
raw: Optional raw ADC values. If None, reads from serial.
|
||||
"""
|
||||
if not self.calibration:
|
||||
raise RuntimeError("exoskeleton not calibrated")
|
||||
if raw is None:
|
||||
raw = self.read_raw()
|
||||
return {} if raw is None else exo_raw_to_angles(raw, self.calibration)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user