Fix homunculus teleoperator input lag (#2196)

Removes input lag by making changes to the serial
reading loop
- remove serial flush as this only clears
output buffer
- read all data in the input buffer in per loop
and use the latest line as the state to clear
the input buffer
previously was only reading one line per loop,
which in combination with teleoperator script loop
busy_wait function (which is slowing the
_read_loops down) was causing a backlog in input
buffer

Co-authored-by: Martino Russi <77496684+nepyope@users.noreply.github.com>
This commit is contained in:
Ryan Pennings
2025-10-16 20:39:05 +11:00
committed by GitHub
parent a6ff3cfebb
commit 845b359d39
2 changed files with 18 additions and 4 deletions

View File

@@ -270,8 +270,15 @@ class HomunculusArm(Teleoperator):
raw_values = None
with self.serial_lock:
if self.serial.in_waiting > 0:
self.serial.flush()
raw_values = self.serial.readline().decode("utf-8").strip().split(" ")
lines = []
while self.serial.in_waiting > 0:
line = self.serial.read_until().decode("utf-8").strip()
if line:
lines.append(line.split(" "))
if lines:
raw_values = lines[-1]
if raw_values is None or len(raw_values) != 21: # 16 raw + 5 angle values
continue

View File

@@ -304,8 +304,15 @@ class HomunculusGlove(Teleoperator):
positions = None
with self.serial_lock:
if self.serial.in_waiting > 0:
self.serial.flush()
positions = self.serial.readline().decode("utf-8").strip().split(" ")
lines = []
while self.serial.in_waiting > 0:
line = self.serial.read_until().decode("utf-8").strip()
if line:
lines.append(line.split(" "))
if lines:
positions = lines[-1]
if positions is None or len(positions) != len(self.joints):
continue