Spaces:
Running
Running
RemiFabre commited on
Commit ·
95eacb8
1
Parent(s): e37e836
timing: unify all duration measurements on time.monotonic() to avoid clock jumps and drift
Browse files
src/reachy_mini_conversation_demo/moves.py
CHANGED
|
@@ -186,7 +186,7 @@ class MovementState:
|
|
| 186 |
|
| 187 |
def update_activity(self) -> None:
|
| 188 |
"""Update the last activity time."""
|
| 189 |
-
self.last_activity_time = time.
|
| 190 |
|
| 191 |
|
| 192 |
class MovementManager:
|
|
@@ -209,9 +209,12 @@ class MovementManager:
|
|
| 209 |
self.head_tracker = head_tracker
|
| 210 |
self.camera_worker = camera_worker
|
| 211 |
|
|
|
|
|
|
|
|
|
|
| 212 |
# Movement state
|
| 213 |
self.state = MovementState()
|
| 214 |
-
self.state.last_activity_time =
|
| 215 |
neutral_pose = create_head_pose(0, 0, 0, 0, 0, 0, degrees=True)
|
| 216 |
self.state.last_primary_pose = (neutral_pose, (0.0, 0.0), 0.0)
|
| 217 |
|
|
@@ -232,7 +235,7 @@ class MovementManager:
|
|
| 232 |
self._listening_antennas: Tuple[float, float] = self._last_commanded_pose[1]
|
| 233 |
self._antenna_unfreeze_blend = 1.0
|
| 234 |
self._antenna_blend_duration = 0.4 # seconds to blend back after listening
|
| 235 |
-
self._last_listening_blend_time =
|
| 236 |
|
| 237 |
# Cross-thread signalling
|
| 238 |
self._command_queue: Queue[tuple[str, Any]] = Queue()
|
|
@@ -305,7 +308,7 @@ class MovementManager:
|
|
| 305 |
if listening:
|
| 306 |
return False
|
| 307 |
|
| 308 |
-
current_time =
|
| 309 |
time_since_activity = current_time - last_activity
|
| 310 |
return time_since_activity >= self.idle_inactivity_delay
|
| 311 |
|
|
@@ -396,7 +399,7 @@ class MovementManager:
|
|
| 396 |
if self._is_listening == desired_state:
|
| 397 |
return
|
| 398 |
self._is_listening = desired_state
|
| 399 |
-
self._last_listening_blend_time =
|
| 400 |
if desired_state:
|
| 401 |
# Capture the last antenna command so we keep that pose during listening
|
| 402 |
self._listening_antennas = (
|
|
@@ -564,7 +567,7 @@ class MovementManager:
|
|
| 564 |
logger.debug("Starting enhanced movement control loop (100Hz)")
|
| 565 |
|
| 566 |
loop_count = 0
|
| 567 |
-
|
| 568 |
freq_mean = 0.0
|
| 569 |
freq_m2 = 0.0
|
| 570 |
freq_min = float("inf")
|
|
@@ -574,12 +577,12 @@ class MovementManager:
|
|
| 574 |
potential_freq = 0.0
|
| 575 |
|
| 576 |
while not self._stop_event.is_set():
|
| 577 |
-
|
| 578 |
loop_count += 1
|
| 579 |
-
current_time =
|
| 580 |
|
| 581 |
if loop_count > 1:
|
| 582 |
-
period =
|
| 583 |
if period > 0:
|
| 584 |
last_freq = 1.0 / period
|
| 585 |
freq_count += 1
|
|
@@ -587,7 +590,7 @@ class MovementManager:
|
|
| 587 |
freq_mean += delta / freq_count
|
| 588 |
freq_m2 += delta * (last_freq - freq_mean)
|
| 589 |
freq_min = min(freq_min, last_freq)
|
| 590 |
-
|
| 591 |
|
| 592 |
# 1. Poll external signals and commands
|
| 593 |
self._poll_signals(current_time)
|
|
@@ -610,7 +613,7 @@ class MovementManager:
|
|
| 610 |
|
| 611 |
# 6. Blend listening state for antennas
|
| 612 |
head, antennas, body_yaw = global_full_body_pose
|
| 613 |
-
now_monotonic =
|
| 614 |
listening = self._is_listening
|
| 615 |
listening_antennas = self._listening_antennas
|
| 616 |
blend = self._antenna_unfreeze_blend
|
|
@@ -655,7 +658,7 @@ class MovementManager:
|
|
| 655 |
)
|
| 656 |
|
| 657 |
# 8. Timing bookkeeping + adaptive sleep for 100Hz
|
| 658 |
-
computation_time =
|
| 659 |
potential_freq = (
|
| 660 |
1.0 / computation_time if computation_time > 0 else float("inf")
|
| 661 |
)
|
|
|
|
| 186 |
|
| 187 |
def update_activity(self) -> None:
|
| 188 |
"""Update the last activity time."""
|
| 189 |
+
self.last_activity_time = time.monotonic()
|
| 190 |
|
| 191 |
|
| 192 |
class MovementManager:
|
|
|
|
| 209 |
self.head_tracker = head_tracker
|
| 210 |
self.camera_worker = camera_worker
|
| 211 |
|
| 212 |
+
# Single timing source for durations
|
| 213 |
+
self._now = time.monotonic
|
| 214 |
+
|
| 215 |
# Movement state
|
| 216 |
self.state = MovementState()
|
| 217 |
+
self.state.last_activity_time = self._now()
|
| 218 |
neutral_pose = create_head_pose(0, 0, 0, 0, 0, 0, degrees=True)
|
| 219 |
self.state.last_primary_pose = (neutral_pose, (0.0, 0.0), 0.0)
|
| 220 |
|
|
|
|
| 235 |
self._listening_antennas: Tuple[float, float] = self._last_commanded_pose[1]
|
| 236 |
self._antenna_unfreeze_blend = 1.0
|
| 237 |
self._antenna_blend_duration = 0.4 # seconds to blend back after listening
|
| 238 |
+
self._last_listening_blend_time = self._now()
|
| 239 |
|
| 240 |
# Cross-thread signalling
|
| 241 |
self._command_queue: Queue[tuple[str, Any]] = Queue()
|
|
|
|
| 308 |
if listening:
|
| 309 |
return False
|
| 310 |
|
| 311 |
+
current_time = self._now()
|
| 312 |
time_since_activity = current_time - last_activity
|
| 313 |
return time_since_activity >= self.idle_inactivity_delay
|
| 314 |
|
|
|
|
| 399 |
if self._is_listening == desired_state:
|
| 400 |
return
|
| 401 |
self._is_listening = desired_state
|
| 402 |
+
self._last_listening_blend_time = self._now()
|
| 403 |
if desired_state:
|
| 404 |
# Capture the last antenna command so we keep that pose during listening
|
| 405 |
self._listening_antennas = (
|
|
|
|
| 567 |
logger.debug("Starting enhanced movement control loop (100Hz)")
|
| 568 |
|
| 569 |
loop_count = 0
|
| 570 |
+
prev_loop_start = self._now()
|
| 571 |
freq_mean = 0.0
|
| 572 |
freq_m2 = 0.0
|
| 573 |
freq_min = float("inf")
|
|
|
|
| 577 |
potential_freq = 0.0
|
| 578 |
|
| 579 |
while not self._stop_event.is_set():
|
| 580 |
+
loop_start = self._now()
|
| 581 |
loop_count += 1
|
| 582 |
+
current_time = loop_start
|
| 583 |
|
| 584 |
if loop_count > 1:
|
| 585 |
+
period = loop_start - prev_loop_start
|
| 586 |
if period > 0:
|
| 587 |
last_freq = 1.0 / period
|
| 588 |
freq_count += 1
|
|
|
|
| 590 |
freq_mean += delta / freq_count
|
| 591 |
freq_m2 += delta * (last_freq - freq_mean)
|
| 592 |
freq_min = min(freq_min, last_freq)
|
| 593 |
+
prev_loop_start = loop_start
|
| 594 |
|
| 595 |
# 1. Poll external signals and commands
|
| 596 |
self._poll_signals(current_time)
|
|
|
|
| 613 |
|
| 614 |
# 6. Blend listening state for antennas
|
| 615 |
head, antennas, body_yaw = global_full_body_pose
|
| 616 |
+
now_monotonic = self._now()
|
| 617 |
listening = self._is_listening
|
| 618 |
listening_antennas = self._listening_antennas
|
| 619 |
blend = self._antenna_unfreeze_blend
|
|
|
|
| 658 |
)
|
| 659 |
|
| 660 |
# 8. Timing bookkeeping + adaptive sleep for 100Hz
|
| 661 |
+
computation_time = self._now() - loop_start
|
| 662 |
potential_freq = (
|
| 663 |
1.0 / computation_time if computation_time > 0 else float("inf")
|
| 664 |
)
|