Spaces:
Running
Running
RemiFabre commited on
Commit ·
21fc1c2
1
Parent(s): 36864e3
control loop wip
Browse files
src/reachy_mini_conversation_demo/main.py
CHANGED
|
@@ -31,7 +31,7 @@ def main():
|
|
| 31 |
logger = setup_logger(args.debug)
|
| 32 |
logger.info("Starting Reachy Mini Conversation Demo")
|
| 33 |
|
| 34 |
-
robot = ReachyMini()
|
| 35 |
|
| 36 |
camera_worker, head_tracker, vision_manager = handle_vision_stuff(args, robot)
|
| 37 |
|
|
|
|
| 31 |
logger = setup_logger(args.debug)
|
| 32 |
logger.info("Starting Reachy Mini Conversation Demo")
|
| 33 |
|
| 34 |
+
robot = ReachyMini(use_sim=True)
|
| 35 |
|
| 36 |
camera_worker, head_tracker, vision_manager = handle_vision_stuff(args, robot)
|
| 37 |
|
src/reachy_mini_conversation_demo/moves.py
CHANGED
|
@@ -13,7 +13,8 @@ import threading
|
|
| 13 |
import time
|
| 14 |
from collections import deque
|
| 15 |
from dataclasses import dataclass
|
| 16 |
-
from
|
|
|
|
| 17 |
|
| 18 |
import numpy as np
|
| 19 |
from reachy_mini import ReachyMini
|
|
@@ -219,12 +220,11 @@ class MovementManager:
|
|
| 219 |
|
| 220 |
# Configuration
|
| 221 |
self.idle_inactivity_delay = 5.0 # seconds
|
| 222 |
-
self.target_frequency =
|
| 223 |
self.target_period = 1.0 / self.target_frequency
|
| 224 |
|
| 225 |
self._stop_event = threading.Event()
|
| 226 |
self._thread: Optional[threading.Thread] = None
|
| 227 |
-
self._state_lock = threading.RLock()
|
| 228 |
self._is_listening = False
|
| 229 |
self._last_commanded_pose: FullBodyPose = clone_full_body_pose(
|
| 230 |
self.state.last_primary_pose
|
|
@@ -234,30 +234,49 @@ class MovementManager:
|
|
| 234 |
self._antenna_blend_duration = 0.4 # seconds to blend back after listening
|
| 235 |
self._last_listening_blend_time = time.monotonic()
|
| 236 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
def queue_move(self, move: Move) -> None:
|
| 238 |
"""Add a move to the primary move queue."""
|
| 239 |
-
|
| 240 |
-
self.move_queue.append(move)
|
| 241 |
-
self.state.update_activity()
|
| 242 |
-
logger.info(
|
| 243 |
-
f"Queued move with duration {move.duration}s, queue size: {len(self.move_queue)}"
|
| 244 |
-
)
|
| 245 |
|
| 246 |
def clear_queue(self) -> None:
|
| 247 |
"""Clear all queued moves and stop current move."""
|
| 248 |
-
|
| 249 |
-
self.move_queue.clear()
|
| 250 |
-
self.state.current_move = None
|
| 251 |
-
self.state.move_start_time = None
|
| 252 |
-
self.state.is_playing_move = False
|
| 253 |
-
logger.info("Cleared move queue and stopped current move")
|
| 254 |
|
| 255 |
def set_speech_offsets(
|
| 256 |
self, offsets: Tuple[float, float, float, float, float, float]
|
| 257 |
) -> None:
|
| 258 |
"""Set speech head offsets (secondary move)."""
|
| 259 |
-
with self.
|
| 260 |
-
self.
|
|
|
|
| 261 |
|
| 262 |
def set_offsets(
|
| 263 |
self, offsets: Tuple[float, float, float, float, float, float]
|
|
@@ -269,38 +288,116 @@ class MovementManager:
|
|
| 269 |
self, offsets: Tuple[float, float, float, float, float, float]
|
| 270 |
) -> None:
|
| 271 |
"""Set face tracking offsets (secondary move)."""
|
| 272 |
-
with self.
|
| 273 |
-
self.
|
|
|
|
| 274 |
|
| 275 |
def set_moving_state(self, duration: float) -> None:
|
| 276 |
"""Set legacy moving state for goto moves."""
|
| 277 |
-
|
| 278 |
-
self.state.moving_start = time.time()
|
| 279 |
-
self.state.moving_for = duration
|
| 280 |
-
self.state.update_activity()
|
| 281 |
|
| 282 |
def is_idle(self):
|
| 283 |
"""Check if the robot is idle based on inactivity delay."""
|
| 284 |
-
with self.
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
def mark_user_activity(self) -> None:
|
| 292 |
"""Record recent user activity to delay idle behaviours."""
|
| 293 |
-
|
| 294 |
-
self.state.update_activity()
|
| 295 |
|
| 296 |
def set_listening(self, listening: bool) -> None:
|
| 297 |
"""Toggle listening mode, freezing antennas when active."""
|
| 298 |
-
with self.
|
| 299 |
-
if self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
return
|
| 301 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
self._last_listening_blend_time = time.monotonic()
|
| 303 |
-
if
|
| 304 |
# Capture the last antenna command so we keep that pose during listening
|
| 305 |
self._listening_antennas = (
|
| 306 |
float(self._last_commanded_pose[1][0]),
|
|
@@ -308,124 +405,120 @@ class MovementManager:
|
|
| 308 |
)
|
| 309 |
self._antenna_unfreeze_blend = 0.0
|
| 310 |
self.state.update_activity()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
def _manage_move_queue(self, current_time: float) -> None:
|
| 313 |
"""Manage the primary move queue (sequential execution)."""
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
)
|
| 329 |
|
| 330 |
def _manage_breathing(self, current_time: float) -> None:
|
| 331 |
"""Manage automatic breathing when idle."""
|
| 332 |
# Start breathing after inactivity delay if no moves in queue
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
):
|
| 362 |
-
self.state.current_move = None
|
| 363 |
-
self.state.move_start_time = None
|
| 364 |
-
logger.info("Stopping breathing due to new move activity")
|
| 365 |
|
| 366 |
def _get_primary_pose(self, current_time: float) -> FullBodyPose:
|
| 367 |
"""Get the primary full body pose from current move or neutral."""
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
antennas_tuple
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
head_copy,
|
| 388 |
-
antennas_tuple,
|
| 389 |
-
float(body_yaw),
|
| 390 |
-
)
|
| 391 |
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 396 |
)
|
| 397 |
else:
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
self.state.
|
| 401 |
-
|
| 402 |
)
|
| 403 |
|
| 404 |
-
if self.state.last_primary_pose is not None:
|
| 405 |
-
primary_full_body_pose = clone_full_body_pose(
|
| 406 |
-
self.state.last_primary_pose
|
| 407 |
-
)
|
| 408 |
-
else:
|
| 409 |
-
neutral_head_pose = create_head_pose(0, 0, 0, 0, 0, 0, degrees=True)
|
| 410 |
-
primary_full_body_pose = (neutral_head_pose, (0.0, 0.0), 0.0)
|
| 411 |
-
self.state.last_primary_pose = clone_full_body_pose(
|
| 412 |
-
primary_full_body_pose
|
| 413 |
-
)
|
| 414 |
-
|
| 415 |
return primary_full_body_pose
|
| 416 |
|
| 417 |
def _get_secondary_pose(self) -> FullBodyPose:
|
| 418 |
"""Get the secondary full body pose from speech and face tracking offsets."""
|
| 419 |
# Combine speech sway offsets + face tracking offsets for secondary pose
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
]
|
| 429 |
|
| 430 |
secondary_head_pose = create_head_pose(
|
| 431 |
x=secondary_offsets[0],
|
|
@@ -444,12 +537,10 @@ class MovementManager:
|
|
| 444 |
if self.camera_worker is not None:
|
| 445 |
# Get face tracking offsets from camera worker thread
|
| 446 |
offsets = self.camera_worker.get_face_tracking_offsets()
|
| 447 |
-
|
| 448 |
-
self.state.face_tracking_offsets = offsets
|
| 449 |
else:
|
| 450 |
# No camera worker, use neutral offsets
|
| 451 |
-
|
| 452 |
-
self.state.face_tracking_offsets = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
|
| 453 |
|
| 454 |
def start(self) -> None:
|
| 455 |
"""Start the move worker loop in a thread."""
|
|
@@ -470,48 +561,63 @@ class MovementManager:
|
|
| 470 |
|
| 471 |
Single set_target() call with pose fusion.
|
| 472 |
"""
|
| 473 |
-
logger.debug("Starting enhanced movement control loop (
|
| 474 |
|
| 475 |
loop_count = 0
|
| 476 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
while not self._stop_event.is_set():
|
| 479 |
-
|
| 480 |
loop_count += 1
|
| 481 |
current_time = time.time()
|
| 482 |
|
| 483 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
self._manage_move_queue(current_time)
|
| 485 |
|
| 486 |
-
#
|
| 487 |
self._manage_breathing(current_time)
|
| 488 |
|
| 489 |
-
#
|
| 490 |
self._update_face_tracking(current_time)
|
| 491 |
|
| 492 |
-
#
|
| 493 |
primary_full_body_pose = self._get_primary_pose(current_time)
|
| 494 |
-
|
| 495 |
-
# 5. Get secondary pose from speech and face tracking offsets
|
| 496 |
secondary_full_body_pose = self._get_secondary_pose()
|
| 497 |
-
|
| 498 |
-
# 6. Combine primary and secondary poses
|
| 499 |
global_full_body_pose = combine_full_body(
|
| 500 |
primary_full_body_pose, secondary_full_body_pose
|
| 501 |
)
|
| 502 |
|
| 503 |
-
#
|
| 504 |
head, antennas, body_yaw = global_full_body_pose
|
| 505 |
now_monotonic = time.monotonic()
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
# Blend antenna outputs back to the live motion when leaving listening mode
|
| 515 |
if listening:
|
| 516 |
antennas_cmd = listening_antennas
|
| 517 |
new_blend = 0.0
|
|
@@ -526,18 +632,17 @@ class MovementManager:
|
|
| 526 |
listening_antennas[1] * (1.0 - new_blend) + antennas[1] * new_blend,
|
| 527 |
)
|
| 528 |
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
# 8. Single set_target call - the one and only place we control the robot
|
| 541 |
try:
|
| 542 |
self.current_robot.set_target(
|
| 543 |
head=head, antennas=antennas_cmd, body_yaw=body_yaw
|
|
@@ -545,27 +650,38 @@ class MovementManager:
|
|
| 545 |
except Exception as e:
|
| 546 |
logger.error(f"Failed to set robot target: {e}")
|
| 547 |
else:
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
)
|
| 552 |
|
| 553 |
-
#
|
| 554 |
-
computation_time = time.
|
| 555 |
-
|
|
|
|
|
|
|
|
|
|
| 556 |
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
|
|
|
|
|
|
| 564 |
logger.debug(
|
| 565 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
)
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
|
|
|
| 570 |
|
| 571 |
logger.debug("Movement control loop stopped")
|
|
|
|
| 13 |
import time
|
| 14 |
from collections import deque
|
| 15 |
from dataclasses import dataclass
|
| 16 |
+
from queue import Empty, Queue
|
| 17 |
+
from typing import Any, Optional, Tuple
|
| 18 |
|
| 19 |
import numpy as np
|
| 20 |
from reachy_mini import ReachyMini
|
|
|
|
| 220 |
|
| 221 |
# Configuration
|
| 222 |
self.idle_inactivity_delay = 5.0 # seconds
|
| 223 |
+
self.target_frequency = 100.0 # Hz
|
| 224 |
self.target_period = 1.0 / self.target_frequency
|
| 225 |
|
| 226 |
self._stop_event = threading.Event()
|
| 227 |
self._thread: Optional[threading.Thread] = None
|
|
|
|
| 228 |
self._is_listening = False
|
| 229 |
self._last_commanded_pose: FullBodyPose = clone_full_body_pose(
|
| 230 |
self.state.last_primary_pose
|
|
|
|
| 234 |
self._antenna_blend_duration = 0.4 # seconds to blend back after listening
|
| 235 |
self._last_listening_blend_time = time.monotonic()
|
| 236 |
|
| 237 |
+
# Cross-thread signalling
|
| 238 |
+
self._command_queue: Queue[tuple[str, Any]] = Queue()
|
| 239 |
+
self._speech_offsets_lock = threading.Lock()
|
| 240 |
+
self._pending_speech_offsets: Tuple[float, float, float, float, float, float] = (
|
| 241 |
+
0.0,
|
| 242 |
+
0.0,
|
| 243 |
+
0.0,
|
| 244 |
+
0.0,
|
| 245 |
+
0.0,
|
| 246 |
+
0.0,
|
| 247 |
+
)
|
| 248 |
+
self._speech_offsets_dirty = False
|
| 249 |
+
|
| 250 |
+
self._face_offsets_lock = threading.Lock()
|
| 251 |
+
self._pending_face_offsets: Tuple[float, float, float, float, float, float] = (
|
| 252 |
+
0.0,
|
| 253 |
+
0.0,
|
| 254 |
+
0.0,
|
| 255 |
+
0.0,
|
| 256 |
+
0.0,
|
| 257 |
+
0.0,
|
| 258 |
+
)
|
| 259 |
+
self._face_offsets_dirty = False
|
| 260 |
+
|
| 261 |
+
self._shared_state_lock = threading.Lock()
|
| 262 |
+
self._shared_last_activity_time = self.state.last_activity_time
|
| 263 |
+
self._shared_is_listening = self._is_listening
|
| 264 |
+
|
| 265 |
def queue_move(self, move: Move) -> None:
|
| 266 |
"""Add a move to the primary move queue."""
|
| 267 |
+
self._command_queue.put(("queue_move", move))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
def clear_queue(self) -> None:
|
| 270 |
"""Clear all queued moves and stop current move."""
|
| 271 |
+
self._command_queue.put(("clear_queue", None))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
def set_speech_offsets(
|
| 274 |
self, offsets: Tuple[float, float, float, float, float, float]
|
| 275 |
) -> None:
|
| 276 |
"""Set speech head offsets (secondary move)."""
|
| 277 |
+
with self._speech_offsets_lock:
|
| 278 |
+
self._pending_speech_offsets = offsets
|
| 279 |
+
self._speech_offsets_dirty = True
|
| 280 |
|
| 281 |
def set_offsets(
|
| 282 |
self, offsets: Tuple[float, float, float, float, float, float]
|
|
|
|
| 288 |
self, offsets: Tuple[float, float, float, float, float, float]
|
| 289 |
) -> None:
|
| 290 |
"""Set face tracking offsets (secondary move)."""
|
| 291 |
+
with self._face_offsets_lock:
|
| 292 |
+
self._pending_face_offsets = offsets
|
| 293 |
+
self._face_offsets_dirty = True
|
| 294 |
|
| 295 |
def set_moving_state(self, duration: float) -> None:
|
| 296 |
"""Set legacy moving state for goto moves."""
|
| 297 |
+
self._command_queue.put(("set_moving_state", duration))
|
|
|
|
|
|
|
|
|
|
| 298 |
|
| 299 |
def is_idle(self):
|
| 300 |
"""Check if the robot is idle based on inactivity delay."""
|
| 301 |
+
with self._shared_state_lock:
|
| 302 |
+
last_activity = self._shared_last_activity_time
|
| 303 |
+
listening = self._shared_is_listening
|
| 304 |
+
|
| 305 |
+
if listening:
|
| 306 |
+
return False
|
| 307 |
+
|
| 308 |
+
current_time = time.time()
|
| 309 |
+
time_since_activity = current_time - last_activity
|
| 310 |
+
return time_since_activity >= self.idle_inactivity_delay
|
| 311 |
|
| 312 |
def mark_user_activity(self) -> None:
|
| 313 |
"""Record recent user activity to delay idle behaviours."""
|
| 314 |
+
self._command_queue.put(("mark_activity", None))
|
|
|
|
| 315 |
|
| 316 |
def set_listening(self, listening: bool) -> None:
|
| 317 |
"""Toggle listening mode, freezing antennas when active."""
|
| 318 |
+
with self._shared_state_lock:
|
| 319 |
+
if self._shared_is_listening == listening:
|
| 320 |
+
return
|
| 321 |
+
self._command_queue.put(("set_listening", listening))
|
| 322 |
+
|
| 323 |
+
def _poll_signals(self, current_time: float) -> None:
|
| 324 |
+
"""Apply queued commands and pending offset updates."""
|
| 325 |
+
self._apply_pending_offsets()
|
| 326 |
+
|
| 327 |
+
while True:
|
| 328 |
+
try:
|
| 329 |
+
command, payload = self._command_queue.get_nowait()
|
| 330 |
+
except Empty:
|
| 331 |
+
break
|
| 332 |
+
self._handle_command(command, payload, current_time)
|
| 333 |
+
|
| 334 |
+
def _apply_pending_offsets(self) -> None:
|
| 335 |
+
"""Apply the most recent speech/face offset updates."""
|
| 336 |
+
speech_offsets: Optional[Tuple[float, float, float, float, float, float]] = None
|
| 337 |
+
with self._speech_offsets_lock:
|
| 338 |
+
if self._speech_offsets_dirty:
|
| 339 |
+
speech_offsets = self._pending_speech_offsets
|
| 340 |
+
self._speech_offsets_dirty = False
|
| 341 |
+
|
| 342 |
+
if speech_offsets is not None:
|
| 343 |
+
self.state.speech_offsets = speech_offsets
|
| 344 |
+
self.state.update_activity()
|
| 345 |
+
|
| 346 |
+
face_offsets: Optional[Tuple[float, float, float, float, float, float]] = None
|
| 347 |
+
with self._face_offsets_lock:
|
| 348 |
+
if self._face_offsets_dirty:
|
| 349 |
+
face_offsets = self._pending_face_offsets
|
| 350 |
+
self._face_offsets_dirty = False
|
| 351 |
+
|
| 352 |
+
if face_offsets is not None:
|
| 353 |
+
self.state.face_tracking_offsets = face_offsets
|
| 354 |
+
self.state.update_activity()
|
| 355 |
+
|
| 356 |
+
def _handle_command(self, command: str, payload: Any, current_time: float) -> None:
|
| 357 |
+
"""Handle a single cross-thread command."""
|
| 358 |
+
if command == "queue_move":
|
| 359 |
+
if isinstance(payload, Move):
|
| 360 |
+
self.move_queue.append(payload)
|
| 361 |
+
self.state.update_activity()
|
| 362 |
+
duration = getattr(payload, "duration", None)
|
| 363 |
+
if duration is not None:
|
| 364 |
+
try:
|
| 365 |
+
duration_str = f"{float(duration):.2f}"
|
| 366 |
+
except (TypeError, ValueError):
|
| 367 |
+
duration_str = str(duration)
|
| 368 |
+
else:
|
| 369 |
+
duration_str = "?"
|
| 370 |
+
logger.info(
|
| 371 |
+
"Queued move with duration %ss, queue size: %s",
|
| 372 |
+
duration_str,
|
| 373 |
+
len(self.move_queue),
|
| 374 |
+
)
|
| 375 |
+
else:
|
| 376 |
+
logger.warning("Ignored queue_move command with invalid payload: %s", payload)
|
| 377 |
+
elif command == "clear_queue":
|
| 378 |
+
self.move_queue.clear()
|
| 379 |
+
self.state.current_move = None
|
| 380 |
+
self.state.move_start_time = None
|
| 381 |
+
self.state.is_playing_move = False
|
| 382 |
+
logger.info("Cleared move queue and stopped current move")
|
| 383 |
+
elif command == "set_moving_state":
|
| 384 |
+
try:
|
| 385 |
+
duration = float(payload)
|
| 386 |
+
except (TypeError, ValueError):
|
| 387 |
+
logger.warning("Invalid moving state duration: %s", payload)
|
| 388 |
return
|
| 389 |
+
self.state.moving_start = current_time
|
| 390 |
+
self.state.moving_for = max(0.0, duration)
|
| 391 |
+
self.state.update_activity()
|
| 392 |
+
elif command == "mark_activity":
|
| 393 |
+
self.state.update_activity()
|
| 394 |
+
elif command == "set_listening":
|
| 395 |
+
desired_state = bool(payload)
|
| 396 |
+
if self._is_listening == desired_state:
|
| 397 |
+
return
|
| 398 |
+
self._is_listening = desired_state
|
| 399 |
self._last_listening_blend_time = time.monotonic()
|
| 400 |
+
if desired_state:
|
| 401 |
# Capture the last antenna command so we keep that pose during listening
|
| 402 |
self._listening_antennas = (
|
| 403 |
float(self._last_commanded_pose[1][0]),
|
|
|
|
| 405 |
)
|
| 406 |
self._antenna_unfreeze_blend = 0.0
|
| 407 |
self.state.update_activity()
|
| 408 |
+
else:
|
| 409 |
+
logger.warning("Unknown command received by MovementManager: %s", command)
|
| 410 |
+
|
| 411 |
+
def _publish_shared_state(self) -> None:
|
| 412 |
+
"""Expose idle-related state for external threads."""
|
| 413 |
+
with self._shared_state_lock:
|
| 414 |
+
self._shared_last_activity_time = self.state.last_activity_time
|
| 415 |
+
self._shared_is_listening = self._is_listening
|
| 416 |
|
| 417 |
def _manage_move_queue(self, current_time: float) -> None:
|
| 418 |
"""Manage the primary move queue (sequential execution)."""
|
| 419 |
+
if self.state.current_move is None or (
|
| 420 |
+
self.state.move_start_time is not None
|
| 421 |
+
and current_time - self.state.move_start_time
|
| 422 |
+
>= self.state.current_move.duration
|
| 423 |
+
):
|
| 424 |
+
self.state.current_move = None
|
| 425 |
+
self.state.move_start_time = None
|
| 426 |
+
|
| 427 |
+
if self.move_queue:
|
| 428 |
+
self.state.current_move = self.move_queue.popleft()
|
| 429 |
+
self.state.move_start_time = current_time
|
| 430 |
+
logger.info(
|
| 431 |
+
f"Starting new move, duration: {self.state.current_move.duration}s"
|
| 432 |
+
)
|
|
|
|
| 433 |
|
| 434 |
def _manage_breathing(self, current_time: float) -> None:
|
| 435 |
"""Manage automatic breathing when idle."""
|
| 436 |
# Start breathing after inactivity delay if no moves in queue
|
| 437 |
+
if self.state.current_move is None and not self.move_queue:
|
| 438 |
+
time_since_activity = current_time - self.state.last_activity_time
|
| 439 |
+
if not self._is_listening and time_since_activity >= self.idle_inactivity_delay:
|
| 440 |
+
try:
|
| 441 |
+
_, current_antennas = self.current_robot.get_current_joint_positions()
|
| 442 |
+
current_head_pose = self.current_robot.get_current_head_pose()
|
| 443 |
+
|
| 444 |
+
breathing_move = BreathingMove(
|
| 445 |
+
interpolation_start_pose=current_head_pose,
|
| 446 |
+
interpolation_start_antennas=current_antennas,
|
| 447 |
+
interpolation_duration=1.0,
|
| 448 |
+
)
|
| 449 |
+
self.move_queue.append(breathing_move)
|
| 450 |
+
self.state.update_activity()
|
| 451 |
+
logger.info(
|
| 452 |
+
f"Started breathing after {time_since_activity:.1f}s of inactivity"
|
| 453 |
+
)
|
| 454 |
+
except Exception as e:
|
| 455 |
+
logger.error(f"Failed to start breathing: {e}")
|
| 456 |
+
|
| 457 |
+
if (
|
| 458 |
+
self.state.current_move is not None
|
| 459 |
+
and isinstance(self.state.current_move, BreathingMove)
|
| 460 |
+
and self.move_queue
|
| 461 |
+
):
|
| 462 |
+
self.state.current_move = None
|
| 463 |
+
self.state.move_start_time = None
|
| 464 |
+
logger.info("Stopping breathing due to new move activity")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 465 |
|
| 466 |
def _get_primary_pose(self, current_time: float) -> FullBodyPose:
|
| 467 |
"""Get the primary full body pose from current move or neutral."""
|
| 468 |
+
# When a primary move is playing, sample it and cache the resulting pose
|
| 469 |
+
if self.state.current_move is not None and self.state.move_start_time is not None:
|
| 470 |
+
move_time = current_time - self.state.move_start_time
|
| 471 |
+
head, antennas, body_yaw = self.state.current_move.evaluate(move_time)
|
| 472 |
+
|
| 473 |
+
if head is None:
|
| 474 |
+
head = create_head_pose(0, 0, 0, 0, 0, 0, degrees=True)
|
| 475 |
+
if antennas is None:
|
| 476 |
+
antennas = np.array([0.0, 0.0])
|
| 477 |
+
if body_yaw is None:
|
| 478 |
+
body_yaw = 0.0
|
| 479 |
+
|
| 480 |
+
antennas_tuple = (float(antennas[0]), float(antennas[1]))
|
| 481 |
+
head_copy = head.copy()
|
| 482 |
+
primary_full_body_pose = (
|
| 483 |
+
head_copy,
|
| 484 |
+
antennas_tuple,
|
| 485 |
+
float(body_yaw),
|
| 486 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
+
self.state.is_playing_move = True
|
| 489 |
+
self.state.is_moving = True
|
| 490 |
+
self.state.last_primary_pose = clone_full_body_pose(primary_full_body_pose)
|
| 491 |
+
else:
|
| 492 |
+
# Otherwise reuse the last primary pose so we avoid jumps between moves
|
| 493 |
+
self.state.is_playing_move = False
|
| 494 |
+
self.state.is_moving = (
|
| 495 |
+
current_time - self.state.moving_start < self.state.moving_for
|
| 496 |
+
)
|
| 497 |
+
|
| 498 |
+
if self.state.last_primary_pose is not None:
|
| 499 |
+
primary_full_body_pose = clone_full_body_pose(
|
| 500 |
+
self.state.last_primary_pose
|
| 501 |
)
|
| 502 |
else:
|
| 503 |
+
neutral_head_pose = create_head_pose(0, 0, 0, 0, 0, 0, degrees=True)
|
| 504 |
+
primary_full_body_pose = (neutral_head_pose, (0.0, 0.0), 0.0)
|
| 505 |
+
self.state.last_primary_pose = clone_full_body_pose(
|
| 506 |
+
primary_full_body_pose
|
| 507 |
)
|
| 508 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
return primary_full_body_pose
|
| 510 |
|
| 511 |
def _get_secondary_pose(self) -> FullBodyPose:
|
| 512 |
"""Get the secondary full body pose from speech and face tracking offsets."""
|
| 513 |
# Combine speech sway offsets + face tracking offsets for secondary pose
|
| 514 |
+
secondary_offsets = [
|
| 515 |
+
self.state.speech_offsets[0] + self.state.face_tracking_offsets[0],
|
| 516 |
+
self.state.speech_offsets[1] + self.state.face_tracking_offsets[1],
|
| 517 |
+
self.state.speech_offsets[2] + self.state.face_tracking_offsets[2],
|
| 518 |
+
self.state.speech_offsets[3] + self.state.face_tracking_offsets[3],
|
| 519 |
+
self.state.speech_offsets[4] + self.state.face_tracking_offsets[4],
|
| 520 |
+
self.state.speech_offsets[5] + self.state.face_tracking_offsets[5],
|
| 521 |
+
]
|
|
|
|
| 522 |
|
| 523 |
secondary_head_pose = create_head_pose(
|
| 524 |
x=secondary_offsets[0],
|
|
|
|
| 537 |
if self.camera_worker is not None:
|
| 538 |
# Get face tracking offsets from camera worker thread
|
| 539 |
offsets = self.camera_worker.get_face_tracking_offsets()
|
| 540 |
+
self.state.face_tracking_offsets = offsets
|
|
|
|
| 541 |
else:
|
| 542 |
# No camera worker, use neutral offsets
|
| 543 |
+
self.state.face_tracking_offsets = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
|
|
|
|
| 544 |
|
| 545 |
def start(self) -> None:
|
| 546 |
"""Start the move worker loop in a thread."""
|
|
|
|
| 561 |
|
| 562 |
Single set_target() call with pose fusion.
|
| 563 |
"""
|
| 564 |
+
logger.debug("Starting enhanced movement control loop (100Hz)")
|
| 565 |
|
| 566 |
loop_count = 0
|
| 567 |
+
prev_loop_start_perf = time.perf_counter()
|
| 568 |
+
freq_mean = 0.0
|
| 569 |
+
freq_m2 = 0.0
|
| 570 |
+
freq_min = float("inf")
|
| 571 |
+
freq_count = 0
|
| 572 |
+
last_freq = 0.0
|
| 573 |
+
print_interval_loops = max(1, int(self.target_frequency * 2))
|
| 574 |
+
potential_freq = 0.0
|
| 575 |
|
| 576 |
while not self._stop_event.is_set():
|
| 577 |
+
loop_start_perf = time.perf_counter()
|
| 578 |
loop_count += 1
|
| 579 |
current_time = time.time()
|
| 580 |
|
| 581 |
+
if loop_count > 1:
|
| 582 |
+
period = loop_start_perf - prev_loop_start_perf
|
| 583 |
+
if period > 0:
|
| 584 |
+
last_freq = 1.0 / period
|
| 585 |
+
freq_count += 1
|
| 586 |
+
delta = last_freq - freq_mean
|
| 587 |
+
freq_mean += delta / freq_count
|
| 588 |
+
freq_m2 += delta * (last_freq - freq_mean)
|
| 589 |
+
freq_min = min(freq_min, last_freq)
|
| 590 |
+
prev_loop_start_perf = loop_start_perf
|
| 591 |
+
|
| 592 |
+
# 1. Poll external signals and commands
|
| 593 |
+
self._poll_signals(current_time)
|
| 594 |
+
|
| 595 |
+
# 2. Sequential move management
|
| 596 |
self._manage_move_queue(current_time)
|
| 597 |
|
| 598 |
+
# 3. Automatic behaviours (breathing)
|
| 599 |
self._manage_breathing(current_time)
|
| 600 |
|
| 601 |
+
# 4. Update vision offsets
|
| 602 |
self._update_face_tracking(current_time)
|
| 603 |
|
| 604 |
+
# 5. Build pose snapshots
|
| 605 |
primary_full_body_pose = self._get_primary_pose(current_time)
|
|
|
|
|
|
|
| 606 |
secondary_full_body_pose = self._get_secondary_pose()
|
|
|
|
|
|
|
| 607 |
global_full_body_pose = combine_full_body(
|
| 608 |
primary_full_body_pose, secondary_full_body_pose
|
| 609 |
)
|
| 610 |
|
| 611 |
+
# 6. Blend listening state for antennas
|
| 612 |
head, antennas, body_yaw = global_full_body_pose
|
| 613 |
now_monotonic = time.monotonic()
|
| 614 |
+
listening = self._is_listening
|
| 615 |
+
listening_antennas = self._listening_antennas
|
| 616 |
+
blend = self._antenna_unfreeze_blend
|
| 617 |
+
blend_duration = self._antenna_blend_duration
|
| 618 |
+
last_update = self._last_listening_blend_time
|
| 619 |
+
self._last_listening_blend_time = now_monotonic
|
| 620 |
+
|
|
|
|
|
|
|
| 621 |
if listening:
|
| 622 |
antennas_cmd = listening_antennas
|
| 623 |
new_blend = 0.0
|
|
|
|
| 632 |
listening_antennas[1] * (1.0 - new_blend) + antennas[1] * new_blend,
|
| 633 |
)
|
| 634 |
|
| 635 |
+
if listening:
|
| 636 |
+
self._antenna_unfreeze_blend = 0.0
|
| 637 |
+
else:
|
| 638 |
+
self._antenna_unfreeze_blend = new_blend
|
| 639 |
+
if new_blend >= 1.0:
|
| 640 |
+
self._listening_antennas = (
|
| 641 |
+
float(antennas[0]),
|
| 642 |
+
float(antennas[1]),
|
| 643 |
+
)
|
| 644 |
+
|
| 645 |
+
# 7. Single set_target call - the only control point
|
|
|
|
| 646 |
try:
|
| 647 |
self.current_robot.set_target(
|
| 648 |
head=head, antennas=antennas_cmd, body_yaw=body_yaw
|
|
|
|
| 650 |
except Exception as e:
|
| 651 |
logger.error(f"Failed to set robot target: {e}")
|
| 652 |
else:
|
| 653 |
+
self._last_commanded_pose = clone_full_body_pose(
|
| 654 |
+
(head, antennas_cmd, body_yaw)
|
| 655 |
+
)
|
|
|
|
| 656 |
|
| 657 |
+
# 8. Timing bookkeeping + adaptive sleep for 100Hz
|
| 658 |
+
computation_time = time.perf_counter() - loop_start_perf
|
| 659 |
+
potential_freq = (
|
| 660 |
+
1.0 / computation_time if computation_time > 0 else float("inf")
|
| 661 |
+
)
|
| 662 |
+
sleep_time = max(0.0, self.target_period - computation_time)
|
| 663 |
|
| 664 |
+
self._publish_shared_state()
|
| 665 |
+
|
| 666 |
+
if sleep_time > 0:
|
| 667 |
+
time.sleep(sleep_time)
|
| 668 |
+
|
| 669 |
+
# 9. Periodic telemetry
|
| 670 |
+
if loop_count % print_interval_loops == 0 and freq_count > 0:
|
| 671 |
+
variance = freq_m2 / freq_count if freq_count > 0 else 0.0
|
| 672 |
+
lowest = freq_min if freq_min != float("inf") else 0.0
|
| 673 |
logger.debug(
|
| 674 |
+
"Loop freq - avg: %.2fHz, variance: %.4f, min: %.2fHz, last: %.2fHz, potential: %.2fHz, target: %.1fHz",
|
| 675 |
+
freq_mean,
|
| 676 |
+
variance,
|
| 677 |
+
lowest,
|
| 678 |
+
last_freq,
|
| 679 |
+
potential_freq,
|
| 680 |
+
self.target_frequency,
|
| 681 |
)
|
| 682 |
+
freq_mean = 0.0
|
| 683 |
+
freq_m2 = 0.0
|
| 684 |
+
freq_min = float("inf")
|
| 685 |
+
freq_count = 0
|
| 686 |
|
| 687 |
logger.debug("Movement control loop stopped")
|