Commit ·
f3abae3
1
Parent(s): 634577b
v0.2.1: Fix daemon crash issue and optimize code
Browse files- PROJECT_PLAN.md +81 -6
- pyproject.toml +1 -1
- reachy_mini_ha_voice/__init__.py +1 -1
- reachy_mini_ha_voice/motion.py +2 -2
- reachy_mini_ha_voice/movement_manager.py +24 -5
- reachy_mini_ha_voice/reachy_controller.py +284 -250
- reachy_mini_ha_voice/voice_assistant.py +1 -1
PROJECT_PLAN.md
CHANGED
|
@@ -93,7 +93,7 @@ reachy_mini_ha_voice/
|
|
| 93 |
│ ├── audio_player.py # 音频播放器
|
| 94 |
│ ├── camera_server.py # MJPEG 摄像头流服务器
|
| 95 |
│ ├── motion.py # 运动控制 (高层 API)
|
| 96 |
-
│ ├── movement_manager.py # 统一运动管理器 (
|
| 97 |
│ ├── models.py # 数据模型
|
| 98 |
│ ├── entity.py # ESPHome 基础实体
|
| 99 |
│ ├── entity_extensions.py # 扩展实体类型
|
|
@@ -136,8 +136,7 @@ dependencies = [
|
|
| 136 |
## 使用流程
|
| 137 |
|
| 138 |
1. **安装应用**
|
| 139 |
-
- 从 Reachy Mini App Store 安装
|
| 140 |
-
- 或 `pip install reachy-mini-ha-voice`
|
| 141 |
|
| 142 |
2. **启动应用**
|
| 143 |
- 应用自动启动 ESPHome 服务器(端口 6053)
|
|
@@ -433,11 +432,14 @@ automation:
|
|
| 433 |
- `CARTOON` - 卡通风格,带回弹效果,活泼可爱
|
| 434 |
|
| 435 |
**已实现功能**:
|
| 436 |
-
- ✅
|
|
|
|
|
|
|
| 437 |
- ✅ 平滑插值动作 (ease in-out 曲线)
|
| 438 |
- ✅ 呼吸动画 - 空闲时 Z 轴微动 + 天线摆动 (`BreathingAnimation`)
|
| 439 |
- ✅ 命令队列模式 - 线程安全的外部 API
|
| 440 |
- ✅ 错误节流 - 防止日志爆炸
|
|
|
|
| 441 |
|
| 442 |
**未实现功能**:
|
| 443 |
- ❌ 动态插值技术切换 (CARTOON/EASE_IN_OUT 等)
|
|
@@ -621,7 +623,8 @@ VAD_DB_OFF = -45 # 停止检测阈值
|
|
| 621 |
|
| 622 |
### 中优先级 (部分实现 🟡)
|
| 623 |
- 🟡 **Phase 15**: 卡通风格运动模式
|
| 624 |
-
- ✅
|
|
|
|
| 625 |
- ✅ 平滑插值动作 + 呼吸动画
|
| 626 |
- ❌ 动态插值技术切换 (CARTOON 等)
|
| 627 |
- 🟡 **Phase 16**: 说话时天线同步
|
|
@@ -649,7 +652,7 @@ VAD_DB_OFF = -45 # 停止检测阈值
|
|
| 649 |
| Phase 1-12 | ✅ 完成 | 100% | 40 个 ESPHome 实体已实现(Phase 11 LED 已禁用) |
|
| 650 |
| Phase 13 | 🟡 部分完成 | 30% | API 基础设施就绪,缺自动触发 |
|
| 651 |
| Phase 14 | ❌ 未完成 | 20% | 仅实现唤醒时转向 |
|
| 652 |
-
| Phase 15 | 🟡 部分完成 |
|
| 653 |
| Phase 16 | 🟡 部分完成 | 50% | 语音驱动头部摆动已实现 |
|
| 654 |
| Phase 17 | ❌ 未完成 | 10% | 摄像头已实现,缺人脸检测 |
|
| 655 |
| Phase 18 | 🟡 部分完成 | 40% | 模式切换已实现,缺教学流程 |
|
|
@@ -658,6 +661,78 @@ VAD_DB_OFF = -45 # 停止检测阈值
|
|
| 658 |
|
| 659 |
**总体完成度**: **Phase 1-12: 100%** | **Phase 13-20: ~35%**
|
| 660 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
### SDK 数据结构参考
|
| 662 |
|
| 663 |
```python
|
|
|
|
| 93 |
│ ├── audio_player.py # 音频播放器
|
| 94 |
│ ├── camera_server.py # MJPEG 摄像头流服务器
|
| 95 |
│ ├── motion.py # 运动控制 (高层 API)
|
| 96 |
+
│ ├── movement_manager.py # 统一运动管理器 (20Hz 控制循环,优化以防止 daemon 崩溃)
|
| 97 |
│ ├── models.py # 数据模型
|
| 98 |
│ ├── entity.py # ESPHome 基础实体
|
| 99 |
│ ├── entity_extensions.py # 扩展实体类型
|
|
|
|
| 136 |
## 使用流程
|
| 137 |
|
| 138 |
1. **安装应用**
|
| 139 |
+
- 从 Reachy Mini App Store 安装`reachy-mini-ha-voice`
|
|
|
|
| 140 |
|
| 141 |
2. **启动应用**
|
| 142 |
- 应用自动启动 ESPHome 服务器(端口 6053)
|
|
|
|
| 432 |
- `CARTOON` - 卡通风格,带回弹效果,活泼可爱
|
| 433 |
|
| 434 |
**已实现功能**:
|
| 435 |
+
- ✅ 20Hz 统一控制循环 (`movement_manager.py`) - 从 100Hz 降低以防止 daemon 崩溃
|
| 436 |
+
- ✅ 姿态变化检测 - 仅在姿态显著变化时发送命令 (阈值 0.001)
|
| 437 |
+
- ✅ 状态查询缓存 - 100ms TTL,减少 daemon 负载
|
| 438 |
- ✅ 平滑插值动作 (ease in-out 曲线)
|
| 439 |
- ✅ 呼吸动画 - 空闲时 Z 轴微动 + 天线摆动 (`BreathingAnimation`)
|
| 440 |
- ✅ 命令队列模式 - 线程安全的外部 API
|
| 441 |
- ✅ 错误节流 - 防止日志爆炸
|
| 442 |
+
- ✅ 连接健康监控 - 自动检测和恢复连接丢失
|
| 443 |
|
| 444 |
**未实现功能**:
|
| 445 |
- ❌ 动态插值技术切换 (CARTOON/EASE_IN_OUT 等)
|
|
|
|
| 623 |
|
| 624 |
### 中优先级 (部分实现 🟡)
|
| 625 |
- 🟡 **Phase 15**: 卡通风格运动模式
|
| 626 |
+
- ✅ 20Hz 统一控制循环架构 (优化以防止 daemon 崩溃)
|
| 627 |
+
- ✅ 姿态变化检测 + 状态查询缓存 (减少 daemon 负载)
|
| 628 |
- ✅ 平滑插值动作 + 呼吸动画
|
| 629 |
- ❌ 动态插值技术切换 (CARTOON 等)
|
| 630 |
- 🟡 **Phase 16**: 说话时天线同步
|
|
|
|
| 652 |
| Phase 1-12 | ✅ 完成 | 100% | 40 个 ESPHome 实体已实现(Phase 11 LED 已禁用) |
|
| 653 |
| Phase 13 | 🟡 部分完成 | 30% | API 基础设施就绪,缺自动触发 |
|
| 654 |
| Phase 14 | ❌ 未完成 | 20% | 仅实现唤醒时转向 |
|
| 655 |
+
| Phase 15 | 🟡 部分完成 | 70% | 20Hz控制循环+姿态变化检测+状态缓存+呼吸动画已实现 |
|
| 656 |
| Phase 16 | 🟡 部分完成 | 50% | 语音驱动头部摆动已实现 |
|
| 657 |
| Phase 17 | ❌ 未完成 | 10% | 摄像头已实现,缺人脸检测 |
|
| 658 |
| Phase 18 | 🟡 部分完成 | 40% | 模式切换已实现,缺教学流程 |
|
|
|
|
| 661 |
|
| 662 |
**总体完成度**: **Phase 1-12: 100%** | **Phase 13-20: ~35%**
|
| 663 |
|
| 664 |
+
---
|
| 665 |
+
|
| 666 |
+
## 🔧 Daemon 崩溃问题修复 (2025-01-05)
|
| 667 |
+
|
| 668 |
+
### 问题描述
|
| 669 |
+
长期运行过程中,`reachy_mini daemon` 会崩溃,导致机器人失去响应。
|
| 670 |
+
|
| 671 |
+
### 根本原因
|
| 672 |
+
1. **100Hz 控制循环过于频繁** - 每 10ms 调用一次 `robot.set_target()`,即使姿态没有变化
|
| 673 |
+
2. **频繁的状态查询** - 每次读取实体状态都调用 `get_status()`、`get_current_head_pose()` 等
|
| 674 |
+
3. **缺少变化检测** - 即使姿态没有变化,也会持续发送相同的命令
|
| 675 |
+
4. **Zenoh 消息队列堵塞** - 累积起来可能每秒 150+ 条消息,daemon 无法及时处理
|
| 676 |
+
|
| 677 |
+
### 修复方案
|
| 678 |
+
|
| 679 |
+
#### 1. 降低控制循环频率 (movement_manager.py)
|
| 680 |
+
```python
|
| 681 |
+
# 从 100Hz 降低到 20Hz
|
| 682 |
+
CONTROL_LOOP_FREQUENCY_HZ = 20 # 减少 80% 的消息量
|
| 683 |
+
```
|
| 684 |
+
|
| 685 |
+
#### 2. 添加姿态变化检测 (movement_manager.py)
|
| 686 |
+
```python
|
| 687 |
+
# 仅在姿态显著变化时发送命令
|
| 688 |
+
if self._last_sent_pose is not None:
|
| 689 |
+
max_diff = max(abs(pose[k] - self._last_sent_pose.get(k, 0.0)) for k in pose.keys())
|
| 690 |
+
if max_diff < 0.001: # 阈值: 0.001 rad 或 0.001 m
|
| 691 |
+
return # 跳过发送
|
| 692 |
+
```
|
| 693 |
+
|
| 694 |
+
#### 3. 状态查询缓存 (reachy_controller.py)
|
| 695 |
+
```python
|
| 696 |
+
# 缓存 daemon 状态查询结果
|
| 697 |
+
self._cache_ttl = 0.1 # 100ms TTL
|
| 698 |
+
self._last_status_query = 0.0
|
| 699 |
+
|
| 700 |
+
def _get_cached_status(self):
|
| 701 |
+
now = time.time()
|
| 702 |
+
if now - self._last_status_query < self._cache_ttl:
|
| 703 |
+
return self._state_cache.get('status') # 使用缓存
|
| 704 |
+
# ... 查询并更新缓存
|
| 705 |
+
```
|
| 706 |
+
|
| 707 |
+
#### 4. 头部姿态查询缓存 (reachy_controller.py)
|
| 708 |
+
```python
|
| 709 |
+
# 缓存 get_current_head_pose() 和 get_current_joint_positions() 结果
|
| 710 |
+
def _get_cached_head_pose(self):
|
| 711 |
+
# 100ms 内复用缓存结果
|
| 712 |
+
```
|
| 713 |
+
|
| 714 |
+
### 修复效果
|
| 715 |
+
|
| 716 |
+
| 指标 | 修复前 | 修复后 | 改善 |
|
| 717 |
+
|------|--------|--------|------|
|
| 718 |
+
| 控制消息频率 | ~100 msg/s | ~20 msg/s | ↓ 80% |
|
| 719 |
+
| 状态查询频率 | ~50 msg/s | ~5 msg/s | ↓ 90% |
|
| 720 |
+
| 总 Zenoh 消息 | ~150 msg/s | ~25 msg/s | ↓ 83% |
|
| 721 |
+
| Daemon CPU 负载 | 持续高负载 | 正常负载 | 显著降低 |
|
| 722 |
+
| 预期稳定性 | 数小时内崩溃 | 可稳定运行数天 | 大幅提升 |
|
| 723 |
+
|
| 724 |
+
### 相关文件
|
| 725 |
+
- `DAEMON_CRASH_FIX_PLAN.md` - 详细修复方案和测试计划
|
| 726 |
+
- `movement_manager.py` - 控制循环优化
|
| 727 |
+
- `reachy_controller.py` - 状态查询缓存
|
| 728 |
+
|
| 729 |
+
### 后续优化建议
|
| 730 |
+
1. ⏳ 动态频率调整 - 运动时 50Hz,空闲时 5Hz
|
| 731 |
+
2. ⏳ 批量状态查询 - 一次性获取所有状态
|
| 732 |
+
3. ⏳ 性能监控和告警 - 实时监控 daemon 健康状态
|
| 733 |
+
|
| 734 |
+
---
|
| 735 |
+
|
| 736 |
### SDK 数据结构参考
|
| 737 |
|
| 738 |
```python
|
pyproject.toml
CHANGED
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
| 4 |
|
| 5 |
[project]
|
| 6 |
name = "reachy_mini_ha_voice"
|
| 7 |
-
version = "0.2.
|
| 8 |
description = "Home Assistant Voice Assistant for Reachy Mini"
|
| 9 |
readme = "README.md"
|
| 10 |
requires-python = ">=3.10"
|
|
|
|
| 4 |
|
| 5 |
[project]
|
| 6 |
name = "reachy_mini_ha_voice"
|
| 7 |
+
version = "0.2.1"
|
| 8 |
description = "Home Assistant Voice Assistant for Reachy Mini"
|
| 9 |
readme = "README.md"
|
| 10 |
requires-python = ">=3.10"
|
reachy_mini_ha_voice/__init__.py
CHANGED
|
@@ -11,7 +11,7 @@ Key features:
|
|
| 11 |
- Reachy Mini motion control integration
|
| 12 |
"""
|
| 13 |
|
| 14 |
-
__version__ = "0.2.
|
| 15 |
__author__ = "Desmond Dong"
|
| 16 |
|
| 17 |
# Don't import main module here to avoid runpy warning
|
|
|
|
| 11 |
- Reachy Mini motion control integration
|
| 12 |
"""
|
| 13 |
|
| 14 |
+
__version__ = "0.2.1"
|
| 15 |
__author__ = "Desmond Dong"
|
| 16 |
|
| 17 |
# Don't import main module here to avoid runpy warning
|
reachy_mini_ha_voice/motion.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
"""Reachy Mini motion control integration.
|
| 2 |
|
| 3 |
This module provides a high-level motion API that delegates to the
|
| 4 |
-
MovementManager for unified
|
| 5 |
"""
|
| 6 |
|
| 7 |
import logging
|
|
@@ -17,7 +17,7 @@ class ReachyMiniMotion:
|
|
| 17 |
"""Reachy Mini motion controller for voice assistant.
|
| 18 |
|
| 19 |
All public motion methods (on_*) are non-blocking. They send commands
|
| 20 |
-
to the MovementManager which handles them in its
|
| 21 |
"""
|
| 22 |
|
| 23 |
def __init__(self, reachy_mini=None):
|
|
|
|
| 1 |
"""Reachy Mini motion control integration.
|
| 2 |
|
| 3 |
This module provides a high-level motion API that delegates to the
|
| 4 |
+
MovementManager for unified 20Hz control.
|
| 5 |
"""
|
| 6 |
|
| 7 |
import logging
|
|
|
|
| 17 |
"""Reachy Mini motion controller for voice assistant.
|
| 18 |
|
| 19 |
All public motion methods (on_*) are non-blocking. They send commands
|
| 20 |
+
to the MovementManager which handles them in its 20Hz control loop.
|
| 21 |
"""
|
| 22 |
|
| 23 |
def __init__(self, reachy_mini=None):
|
reachy_mini_ha_voice/movement_manager.py
CHANGED
|
@@ -5,12 +5,13 @@ This module provides a centralized control system for robot movements,
|
|
| 5 |
inspired by the reachy_mini_conversation_app architecture.
|
| 6 |
|
| 7 |
Key features:
|
| 8 |
-
- Single
|
| 9 |
- Command queue pattern (thread-safe external API)
|
| 10 |
- Error throttling (prevents log explosion)
|
| 11 |
- Speech-driven head sway
|
| 12 |
- Breathing animation during idle
|
| 13 |
- Graceful shutdown
|
|
|
|
| 14 |
"""
|
| 15 |
|
| 16 |
import logging
|
|
@@ -35,7 +36,7 @@ logger = logging.getLogger(__name__)
|
|
| 35 |
# Constants (borrowed from conversation_app)
|
| 36 |
# =============================================================================
|
| 37 |
|
| 38 |
-
CONTROL_LOOP_FREQUENCY_HZ =
|
| 39 |
TARGET_PERIOD = 1.0 / CONTROL_LOOP_FREQUENCY_HZ
|
| 40 |
|
| 41 |
# Speech sway parameters (from conversation_app SwayRollRT)
|
|
@@ -288,10 +289,13 @@ class BreathingAnimation:
|
|
| 288 |
|
| 289 |
class MovementManager:
|
| 290 |
"""
|
| 291 |
-
Unified movement manager with
|
| 292 |
|
| 293 |
All external interactions go through the command queue,
|
| 294 |
ensuring thread safety and preventing race conditions.
|
|
|
|
|
|
|
|
|
|
| 295 |
"""
|
| 296 |
|
| 297 |
def __init__(self, reachy_mini: Optional["ReachyMini"] = None):
|
|
@@ -341,6 +345,10 @@ class MovementManager:
|
|
| 341 |
self._audio_loudness_db: float = -100.0
|
| 342 |
self._audio_lock = threading.Lock()
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
logger.info("MovementManager initialized")
|
| 345 |
|
| 346 |
# =========================================================================
|
|
@@ -671,6 +679,16 @@ class MovementManager:
|
|
| 671 |
if self.robot is None:
|
| 672 |
return
|
| 673 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 674 |
# Check if connection is lost and we should skip sending commands
|
| 675 |
now = self._now()
|
| 676 |
if self._connection_lost:
|
|
@@ -705,8 +723,9 @@ class MovementManager:
|
|
| 705 |
body_yaw=pose["body_yaw"],
|
| 706 |
)
|
| 707 |
|
| 708 |
-
# Command succeeded - update connection health
|
| 709 |
self._last_successful_command = now
|
|
|
|
| 710 |
if self._connection_lost:
|
| 711 |
logger.info("✓ Connection to robot restored")
|
| 712 |
self._connection_lost = False
|
|
@@ -757,7 +776,7 @@ class MovementManager:
|
|
| 757 |
# =========================================================================
|
| 758 |
|
| 759 |
def _control_loop(self) -> None:
|
| 760 |
-
"""Main
|
| 761 |
logger.info("Movement manager control loop started (%.0f Hz)", CONTROL_LOOP_FREQUENCY_HZ)
|
| 762 |
|
| 763 |
last_time = self._now()
|
|
|
|
| 5 |
inspired by the reachy_mini_conversation_app architecture.
|
| 6 |
|
| 7 |
Key features:
|
| 8 |
+
- Single 20Hz control loop (reduced from 100Hz to prevent daemon crashes)
|
| 9 |
- Command queue pattern (thread-safe external API)
|
| 10 |
- Error throttling (prevents log explosion)
|
| 11 |
- Speech-driven head sway
|
| 12 |
- Breathing animation during idle
|
| 13 |
- Graceful shutdown
|
| 14 |
+
- Pose change detection (skip sending if no significant change)
|
| 15 |
"""
|
| 16 |
|
| 17 |
import logging
|
|
|
|
| 36 |
# Constants (borrowed from conversation_app)
|
| 37 |
# =============================================================================
|
| 38 |
|
| 39 |
+
CONTROL_LOOP_FREQUENCY_HZ = 20 # 20Hz control loop (reduced from 100Hz to prevent daemon crashes)
|
| 40 |
TARGET_PERIOD = 1.0 / CONTROL_LOOP_FREQUENCY_HZ
|
| 41 |
|
| 42 |
# Speech sway parameters (from conversation_app SwayRollRT)
|
|
|
|
| 289 |
|
| 290 |
class MovementManager:
|
| 291 |
"""
|
| 292 |
+
Unified movement manager with 20Hz control loop.
|
| 293 |
|
| 294 |
All external interactions go through the command queue,
|
| 295 |
ensuring thread safety and preventing race conditions.
|
| 296 |
+
|
| 297 |
+
Note: Frequency reduced from 100Hz to 20Hz to prevent daemon crashes
|
| 298 |
+
caused by excessive Zenoh message traffic.
|
| 299 |
"""
|
| 300 |
|
| 301 |
def __init__(self, reachy_mini: Optional["ReachyMini"] = None):
|
|
|
|
| 345 |
self._audio_loudness_db: float = -100.0
|
| 346 |
self._audio_lock = threading.Lock()
|
| 347 |
|
| 348 |
+
# Pose change detection (prevent unnecessary commands)
|
| 349 |
+
self._last_sent_pose: Optional[Dict[str, float]] = None
|
| 350 |
+
self._pose_change_threshold = 0.001 # 0.001 rad or 0.001 m
|
| 351 |
+
|
| 352 |
logger.info("MovementManager initialized")
|
| 353 |
|
| 354 |
# =========================================================================
|
|
|
|
| 679 |
if self.robot is None:
|
| 680 |
return
|
| 681 |
|
| 682 |
+
# Check if pose changed significantly (prevent unnecessary commands)
|
| 683 |
+
if self._last_sent_pose is not None:
|
| 684 |
+
max_diff = max(
|
| 685 |
+
abs(pose[k] - self._last_sent_pose.get(k, 0.0))
|
| 686 |
+
for k in pose.keys()
|
| 687 |
+
)
|
| 688 |
+
if max_diff < self._pose_change_threshold:
|
| 689 |
+
# No significant change, skip sending command
|
| 690 |
+
return
|
| 691 |
+
|
| 692 |
# Check if connection is lost and we should skip sending commands
|
| 693 |
now = self._now()
|
| 694 |
if self._connection_lost:
|
|
|
|
| 723 |
body_yaw=pose["body_yaw"],
|
| 724 |
)
|
| 725 |
|
| 726 |
+
# Command succeeded - update connection health and cache
|
| 727 |
self._last_successful_command = now
|
| 728 |
+
self._last_sent_pose = pose.copy() # Cache sent pose
|
| 729 |
if self._connection_lost:
|
| 730 |
logger.info("✓ Connection to robot restored")
|
| 731 |
self._connection_lost = False
|
|
|
|
| 776 |
# =========================================================================
|
| 777 |
|
| 778 |
def _control_loop(self) -> None:
|
| 779 |
+
"""Main 20Hz control loop."""
|
| 780 |
logger.info("Movement manager control loop started (%.0f Hz)", CONTROL_LOOP_FREQUENCY_HZ)
|
| 781 |
|
| 782 |
last_time = self._now()
|
reachy_mini_ha_voice/reachy_controller.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
"""Reachy Mini controller wrapper for ESPHome entities."""
|
| 2 |
|
| 3 |
import logging
|
| 4 |
-
|
|
|
|
| 5 |
import math
|
| 6 |
import numpy as np
|
| 7 |
from scipy.spatial.transform import Rotation as R
|
|
@@ -30,6 +31,13 @@ class ReachyController:
|
|
| 30 |
"""
|
| 31 |
self.reachy = reachy_mini
|
| 32 |
self._speaker_volume = 100 # Default volume
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
@property
|
| 35 |
def is_available(self) -> bool:
|
|
@@ -38,48 +46,54 @@ class ReachyController:
|
|
| 38 |
|
| 39 |
# ========== Phase 1: Basic Status & Volume ==========
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
"""Get daemon
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
if not self.is_available:
|
| 44 |
-
return
|
|
|
|
| 45 |
try:
|
| 46 |
-
# client.get_status() returns a dict with 'state' key
|
| 47 |
status = self.reachy.client.get_status(wait=False)
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
-
logger.error(f"Error getting
|
| 51 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
def get_backend_ready(self) -> bool:
|
| 54 |
-
"""Check if backend is ready."""
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
try:
|
| 58 |
-
# Check if daemon state is 'running'
|
| 59 |
-
status = self.reachy.client.get_status(wait=False)
|
| 60 |
-
return status.get('state') == 'running'
|
| 61 |
-
except Exception as e:
|
| 62 |
-
logger.error(f"Error getting backend status: {e}")
|
| 63 |
return False
|
|
|
|
| 64 |
|
| 65 |
def get_error_message(self) -> str:
|
| 66 |
-
"""Get current error message."""
|
| 67 |
-
|
|
|
|
| 68 |
return "Robot not available"
|
| 69 |
-
|
| 70 |
-
status = self.reachy.client.get_status(wait=False)
|
| 71 |
-
return status.get('error') or ""
|
| 72 |
-
except Exception as e:
|
| 73 |
-
logger.error(f"Error getting error message: {e}")
|
| 74 |
-
return str(e)
|
| 75 |
|
| 76 |
def get_speaker_volume(self) -> float:
|
| 77 |
-
"""Get speaker volume (0-100)."""
|
| 78 |
if not self.is_available:
|
| 79 |
return self._speaker_volume
|
| 80 |
try:
|
| 81 |
-
# Get volume from daemon API
|
| 82 |
-
status = self.
|
|
|
|
|
|
|
| 83 |
wlan_ip = status.get('wlan_ip', 'localhost')
|
| 84 |
response = requests.get(f"http://{wlan_ip}:8000/api/volume/current", timeout=2)
|
| 85 |
if response.status_code == 200:
|
|
@@ -91,7 +105,7 @@ class ReachyController:
|
|
| 91 |
|
| 92 |
def set_speaker_volume(self, volume: float) -> None:
|
| 93 |
"""
|
| 94 |
-
Set speaker volume (0-100).
|
| 95 |
|
| 96 |
Args:
|
| 97 |
volume: Volume level 0-100
|
|
@@ -104,8 +118,11 @@ class ReachyController:
|
|
| 104 |
return
|
| 105 |
|
| 106 |
try:
|
| 107 |
-
# Set volume via daemon API
|
| 108 |
-
status = self.
|
|
|
|
|
|
|
|
|
|
| 109 |
wlan_ip = status.get('wlan_ip', 'localhost')
|
| 110 |
response = requests.post(
|
| 111 |
f"http://{wlan_ip}:8000/api/volume/set",
|
|
@@ -178,12 +195,11 @@ class ReachyController:
|
|
| 178 |
# ========== Phase 2: Motor Control ==========
|
| 179 |
|
| 180 |
def get_motors_enabled(self) -> bool:
|
| 181 |
-
"""Check if motors are enabled."""
|
| 182 |
-
|
|
|
|
| 183 |
return False
|
| 184 |
try:
|
| 185 |
-
# Get motor control mode from backend status
|
| 186 |
-
status = self.reachy.client.get_status(wait=False)
|
| 187 |
backend_status = status.get('backend_status')
|
| 188 |
if backend_status and isinstance(backend_status, dict):
|
| 189 |
motor_mode = backend_status.get('motor_control_mode', 'disabled')
|
|
@@ -215,12 +231,11 @@ class ReachyController:
|
|
| 215 |
logger.error(f"Error setting motor state: {e}")
|
| 216 |
|
| 217 |
def get_motor_mode(self) -> str:
|
| 218 |
-
"""Get current motor control mode."""
|
| 219 |
-
|
|
|
|
| 220 |
return "disabled"
|
| 221 |
try:
|
| 222 |
-
# Get motor control mode from backend status
|
| 223 |
-
status = self.reachy.client.get_status(wait=False)
|
| 224 |
backend_status = status.get('backend_status')
|
| 225 |
if backend_status and isinstance(backend_status, dict):
|
| 226 |
motor_mode = backend_status.get('motor_control_mode', 'disabled')
|
|
@@ -283,6 +298,42 @@ class ReachyController:
|
|
| 283 |
|
| 284 |
# ========== Phase 3: Pose Control ==========
|
| 285 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
def _extract_pose_from_matrix(self, pose_matrix: np.ndarray) -> tuple:
|
| 287 |
"""
|
| 288 |
Extract position (x, y, z) and rotation (roll, pitch, yaw) from 4x4 pose matrix.
|
|
@@ -307,11 +358,11 @@ class ReachyController:
|
|
| 307 |
return x, y, z, roll, pitch, yaw
|
| 308 |
|
| 309 |
def get_head_x(self) -> float:
|
| 310 |
-
"""Get head X position in mm."""
|
| 311 |
-
|
|
|
|
| 312 |
return 0.0
|
| 313 |
try:
|
| 314 |
-
pose = self.reachy.get_current_head_pose()
|
| 315 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 316 |
return x * 1000 # Convert m to mm
|
| 317 |
except Exception as e:
|
|
@@ -332,11 +383,11 @@ class ReachyController:
|
|
| 332 |
logger.error(f"Error setting head X: {e}")
|
| 333 |
|
| 334 |
def get_head_y(self) -> float:
|
| 335 |
-
"""Get head Y position in mm."""
|
| 336 |
-
|
|
|
|
| 337 |
return 0.0
|
| 338 |
try:
|
| 339 |
-
pose = self.reachy.get_current_head_pose()
|
| 340 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 341 |
return y * 1000
|
| 342 |
except Exception as e:
|
|
@@ -356,11 +407,11 @@ class ReachyController:
|
|
| 356 |
logger.error(f"Error setting head Y: {e}")
|
| 357 |
|
| 358 |
def get_head_z(self) -> float:
|
| 359 |
-
"""Get head Z position in mm."""
|
| 360 |
-
|
|
|
|
| 361 |
return 0.0
|
| 362 |
try:
|
| 363 |
-
pose = self.reachy.get_current_head_pose()
|
| 364 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 365 |
return z * 1000
|
| 366 |
except Exception as e:
|
|
@@ -380,11 +431,11 @@ class ReachyController:
|
|
| 380 |
logger.error(f"Error setting head Z: {e}")
|
| 381 |
|
| 382 |
def get_head_roll(self) -> float:
|
| 383 |
-
"""Get head roll angle in degrees."""
|
| 384 |
-
|
|
|
|
| 385 |
return 0.0
|
| 386 |
try:
|
| 387 |
-
pose = self.reachy.get_current_head_pose()
|
| 388 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 389 |
return math.degrees(roll)
|
| 390 |
except Exception as e:
|
|
@@ -407,11 +458,11 @@ class ReachyController:
|
|
| 407 |
logger.error(f"Error setting head roll: {e}")
|
| 408 |
|
| 409 |
def get_head_pitch(self) -> float:
|
| 410 |
-
"""Get head pitch angle in degrees."""
|
| 411 |
-
|
|
|
|
| 412 |
return 0.0
|
| 413 |
try:
|
| 414 |
-
pose = self.reachy.get_current_head_pose()
|
| 415 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 416 |
return math.degrees(pitch)
|
| 417 |
except Exception as e:
|
|
@@ -433,11 +484,11 @@ class ReachyController:
|
|
| 433 |
logger.error(f"Error setting head pitch: {e}")
|
| 434 |
|
| 435 |
def get_head_yaw(self) -> float:
|
| 436 |
-
"""Get head yaw angle in degrees."""
|
| 437 |
-
|
|
|
|
| 438 |
return 0.0
|
| 439 |
try:
|
| 440 |
-
pose = self.reachy.get_current_head_pose()
|
| 441 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 442 |
return math.degrees(yaw)
|
| 443 |
except Exception as e:
|
|
@@ -459,12 +510,12 @@ class ReachyController:
|
|
| 459 |
logger.error(f"Error setting head yaw: {e}")
|
| 460 |
|
| 461 |
def get_body_yaw(self) -> float:
|
| 462 |
-
"""Get body yaw angle in degrees."""
|
| 463 |
-
|
|
|
|
| 464 |
return 0.0
|
| 465 |
try:
|
| 466 |
-
|
| 467 |
-
head_joints, _ = self.reachy.get_current_joint_positions()
|
| 468 |
return math.degrees(head_joints[0])
|
| 469 |
except Exception as e:
|
| 470 |
logger.error(f"Error getting body yaw: {e}")
|
|
@@ -480,13 +531,12 @@ class ReachyController:
|
|
| 480 |
logger.error(f"Error setting body yaw: {e}")
|
| 481 |
|
| 482 |
def get_antenna_left(self) -> float:
|
| 483 |
-
"""Get left antenna angle in degrees."""
|
| 484 |
-
|
|
|
|
| 485 |
return 0.0
|
| 486 |
try:
|
| 487 |
-
|
| 488 |
-
# antenna_joints is [right, left]
|
| 489 |
-
_, antennas = self.reachy.get_current_joint_positions()
|
| 490 |
return math.degrees(antennas[1]) # left is index 1
|
| 491 |
except Exception as e:
|
| 492 |
logger.error(f"Error getting left antenna: {e}")
|
|
@@ -504,11 +554,12 @@ class ReachyController:
|
|
| 504 |
logger.error(f"Error setting left antenna: {e}")
|
| 505 |
|
| 506 |
def get_antenna_right(self) -> float:
|
| 507 |
-
"""Get right antenna angle in degrees."""
|
| 508 |
-
|
|
|
|
| 509 |
return 0.0
|
| 510 |
try:
|
| 511 |
-
_, antennas =
|
| 512 |
return math.degrees(antennas[0]) # right is index 0
|
| 513 |
except Exception as e:
|
| 514 |
logger.error(f"Error getting right antenna: {e}")
|
|
@@ -603,12 +654,11 @@ class ReachyController:
|
|
| 603 |
# ========== Phase 6: Diagnostic Information ==========
|
| 604 |
|
| 605 |
def get_control_loop_frequency(self) -> float:
|
| 606 |
-
"""Get control loop frequency in Hz."""
|
| 607 |
-
|
|
|
|
| 608 |
return 0.0
|
| 609 |
try:
|
| 610 |
-
# Get control loop stats from backend status
|
| 611 |
-
status = self.reachy.client.get_status(wait=False)
|
| 612 |
backend_status = status.get('backend_status')
|
| 613 |
if backend_status and isinstance(backend_status, dict):
|
| 614 |
control_loop_stats = backend_status.get('control_loop_stats', {})
|
|
@@ -619,59 +669,39 @@ class ReachyController:
|
|
| 619 |
return 0.0
|
| 620 |
|
| 621 |
def get_sdk_version(self) -> str:
|
| 622 |
-
"""Get SDK version."""
|
| 623 |
-
|
|
|
|
| 624 |
return "N/A"
|
| 625 |
-
|
| 626 |
-
status = self.reachy.client.get_status(wait=False)
|
| 627 |
-
return status.get('version') or "unknown"
|
| 628 |
-
except Exception as e:
|
| 629 |
-
logger.error(f"Error getting SDK version: {e}")
|
| 630 |
-
return "error"
|
| 631 |
|
| 632 |
def get_robot_name(self) -> str:
|
| 633 |
-
"""Get robot name."""
|
| 634 |
-
|
|
|
|
| 635 |
return "N/A"
|
| 636 |
-
|
| 637 |
-
status = self.reachy.client.get_status(wait=False)
|
| 638 |
-
return status.get('robot_name') or "unknown"
|
| 639 |
-
except Exception as e:
|
| 640 |
-
logger.error(f"Error getting robot name: {e}")
|
| 641 |
-
return "error"
|
| 642 |
|
| 643 |
def get_wireless_version(self) -> bool:
|
| 644 |
-
"""Check if this is a wireless version."""
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
try:
|
| 648 |
-
status = self.reachy.client.get_status(wait=False)
|
| 649 |
-
return status.get('wireless_version', False)
|
| 650 |
-
except Exception as e:
|
| 651 |
-
logger.error(f"Error getting wireless version: {e}")
|
| 652 |
return False
|
|
|
|
| 653 |
|
| 654 |
def get_simulation_mode(self) -> bool:
|
| 655 |
-
"""Check if simulation mode is enabled."""
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
try:
|
| 659 |
-
status = self.reachy.client.get_status(wait=False)
|
| 660 |
-
return status.get('simulation_enabled', False)
|
| 661 |
-
except Exception as e:
|
| 662 |
-
logger.error(f"Error getting simulation mode: {e}")
|
| 663 |
return False
|
|
|
|
| 664 |
|
| 665 |
def get_wlan_ip(self) -> str:
|
| 666 |
-
"""Get WLAN IP address."""
|
| 667 |
-
|
|
|
|
| 668 |
return "N/A"
|
| 669 |
-
|
| 670 |
-
status = self.reachy.client.get_status(wait=False)
|
| 671 |
-
return status.get('wlan_ip') or "N/A"
|
| 672 |
-
except Exception as e:
|
| 673 |
-
logger.error(f"Error getting WLAN IP: {e}")
|
| 674 |
-
return "error"
|
| 675 |
|
| 676 |
# ========== Phase 7: IMU Sensors (Wireless only) ==========
|
| 677 |
|
|
@@ -788,140 +818,142 @@ class ReachyController:
|
|
| 788 |
logger.debug(f"ReSpeaker not available: {e}")
|
| 789 |
return None
|
| 790 |
|
| 791 |
-
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
| 804 |
-
|
| 805 |
-
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
|
| 822 |
-
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
|
| 827 |
-
|
| 828 |
-
|
| 829 |
-
|
| 830 |
-
|
| 831 |
-
|
| 832 |
-
|
| 833 |
-
|
| 834 |
-
|
| 835 |
-
|
| 836 |
-
|
| 837 |
-
|
| 838 |
-
|
| 839 |
-
|
| 840 |
-
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
| 896 |
-
|
| 897 |
-
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
-
|
| 908 |
-
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
|
| 919 |
-
|
| 920 |
-
|
| 921 |
-
|
| 922 |
-
|
| 923 |
-
|
| 924 |
-
|
|
|
|
|
|
|
| 925 |
|
| 926 |
# ========== Phase 12: Audio Processing (via local SDK) ==========
|
| 927 |
|
|
@@ -1048,7 +1080,7 @@ class ReachyController:
|
|
| 1048 |
|
| 1049 |
def get_passive_joints_json(self) -> str:
|
| 1050 |
"""
|
| 1051 |
-
Get passive joints as JSON string.
|
| 1052 |
|
| 1053 |
Returns:
|
| 1054 |
JSON string: "[passive_1_x, passive_1_y, passive_1_z, ..., passive_7_z]"
|
|
@@ -1058,8 +1090,10 @@ class ReachyController:
|
|
| 1058 |
return "[]"
|
| 1059 |
try:
|
| 1060 |
import json
|
| 1061 |
-
# Get WLAN IP from daemon status
|
| 1062 |
-
status = self.
|
|
|
|
|
|
|
| 1063 |
wlan_ip = status.get('wlan_ip', 'localhost')
|
| 1064 |
# Call the backend API to get passive joints
|
| 1065 |
backend_url = f"http://{wlan_ip}:8000/api/state/full?with_passive_joints=true"
|
|
|
|
| 1 |
"""Reachy Mini controller wrapper for ESPHome entities."""
|
| 2 |
|
| 3 |
import logging
|
| 4 |
+
import time
|
| 5 |
+
from typing import Any, Dict, Optional, TYPE_CHECKING
|
| 6 |
import math
|
| 7 |
import numpy as np
|
| 8 |
from scipy.spatial.transform import Rotation as R
|
|
|
|
| 31 |
"""
|
| 32 |
self.reachy = reachy_mini
|
| 33 |
self._speaker_volume = 100 # Default volume
|
| 34 |
+
|
| 35 |
+
# State caching to reduce daemon load
|
| 36 |
+
self._state_cache: Dict[str, Any] = {}
|
| 37 |
+
self._cache_ttl = 0.1 # 100ms cache TTL
|
| 38 |
+
self._last_status_query = 0.0
|
| 39 |
+
self._last_pose_query = 0.0
|
| 40 |
+
self._last_joints_query = 0.0
|
| 41 |
|
| 42 |
@property
|
| 43 |
def is_available(self) -> bool:
|
|
|
|
| 46 |
|
| 47 |
# ========== Phase 1: Basic Status & Volume ==========
|
| 48 |
|
| 49 |
+
def _get_cached_status(self) -> Optional[Dict]:
|
| 50 |
+
"""Get cached daemon status to reduce query frequency."""
|
| 51 |
+
now = time.time()
|
| 52 |
+
if now - self._last_status_query < self._cache_ttl:
|
| 53 |
+
return self._state_cache.get('status')
|
| 54 |
+
|
| 55 |
if not self.is_available:
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
try:
|
|
|
|
| 59 |
status = self.reachy.client.get_status(wait=False)
|
| 60 |
+
self._state_cache['status'] = status
|
| 61 |
+
self._last_status_query = now
|
| 62 |
+
return status
|
| 63 |
except Exception as e:
|
| 64 |
+
logger.error(f"Error getting status: {e}")
|
| 65 |
+
return None
|
| 66 |
+
|
| 67 |
+
def get_daemon_state(self) -> str:
|
| 68 |
+
"""Get daemon state with caching."""
|
| 69 |
+
status = self._get_cached_status()
|
| 70 |
+
if status is None:
|
| 71 |
+
return "not_available"
|
| 72 |
+
return status.get('state', 'unknown')
|
| 73 |
|
| 74 |
def get_backend_ready(self) -> bool:
|
| 75 |
+
"""Check if backend is ready with caching."""
|
| 76 |
+
status = self._get_cached_status()
|
| 77 |
+
if status is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
return False
|
| 79 |
+
return status.get('state') == 'running'
|
| 80 |
|
| 81 |
def get_error_message(self) -> str:
|
| 82 |
+
"""Get current error message with caching."""
|
| 83 |
+
status = self._get_cached_status()
|
| 84 |
+
if status is None:
|
| 85 |
return "Robot not available"
|
| 86 |
+
return status.get('error') or ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
def get_speaker_volume(self) -> float:
|
| 89 |
+
"""Get speaker volume (0-100) with caching."""
|
| 90 |
if not self.is_available:
|
| 91 |
return self._speaker_volume
|
| 92 |
try:
|
| 93 |
+
# Get volume from daemon API (use cached status for IP)
|
| 94 |
+
status = self._get_cached_status()
|
| 95 |
+
if status is None:
|
| 96 |
+
return self._speaker_volume
|
| 97 |
wlan_ip = status.get('wlan_ip', 'localhost')
|
| 98 |
response = requests.get(f"http://{wlan_ip}:8000/api/volume/current", timeout=2)
|
| 99 |
if response.status_code == 200:
|
|
|
|
| 105 |
|
| 106 |
def set_speaker_volume(self, volume: float) -> None:
|
| 107 |
"""
|
| 108 |
+
Set speaker volume (0-100) with cached status.
|
| 109 |
|
| 110 |
Args:
|
| 111 |
volume: Volume level 0-100
|
|
|
|
| 118 |
return
|
| 119 |
|
| 120 |
try:
|
| 121 |
+
# Set volume via daemon API (use cached status for IP)
|
| 122 |
+
status = self._get_cached_status()
|
| 123 |
+
if status is None:
|
| 124 |
+
logger.error("Cannot get daemon status for volume control")
|
| 125 |
+
return
|
| 126 |
wlan_ip = status.get('wlan_ip', 'localhost')
|
| 127 |
response = requests.post(
|
| 128 |
f"http://{wlan_ip}:8000/api/volume/set",
|
|
|
|
| 195 |
# ========== Phase 2: Motor Control ==========
|
| 196 |
|
| 197 |
def get_motors_enabled(self) -> bool:
|
| 198 |
+
"""Check if motors are enabled with caching."""
|
| 199 |
+
status = self._get_cached_status()
|
| 200 |
+
if status is None:
|
| 201 |
return False
|
| 202 |
try:
|
|
|
|
|
|
|
| 203 |
backend_status = status.get('backend_status')
|
| 204 |
if backend_status and isinstance(backend_status, dict):
|
| 205 |
motor_mode = backend_status.get('motor_control_mode', 'disabled')
|
|
|
|
| 231 |
logger.error(f"Error setting motor state: {e}")
|
| 232 |
|
| 233 |
def get_motor_mode(self) -> str:
|
| 234 |
+
"""Get current motor control mode with caching."""
|
| 235 |
+
status = self._get_cached_status()
|
| 236 |
+
if status is None:
|
| 237 |
return "disabled"
|
| 238 |
try:
|
|
|
|
|
|
|
| 239 |
backend_status = status.get('backend_status')
|
| 240 |
if backend_status and isinstance(backend_status, dict):
|
| 241 |
motor_mode = backend_status.get('motor_control_mode', 'disabled')
|
|
|
|
| 298 |
|
| 299 |
# ========== Phase 3: Pose Control ==========
|
| 300 |
|
| 301 |
+
def _get_cached_head_pose(self) -> Optional[np.ndarray]:
|
| 302 |
+
"""Get cached head pose to reduce query frequency."""
|
| 303 |
+
now = time.time()
|
| 304 |
+
if now - self._last_pose_query < self._cache_ttl:
|
| 305 |
+
return self._state_cache.get('head_pose')
|
| 306 |
+
|
| 307 |
+
if not self.is_available:
|
| 308 |
+
return None
|
| 309 |
+
|
| 310 |
+
try:
|
| 311 |
+
pose = self.reachy.get_current_head_pose()
|
| 312 |
+
self._state_cache['head_pose'] = pose
|
| 313 |
+
self._last_pose_query = now
|
| 314 |
+
return pose
|
| 315 |
+
except Exception as e:
|
| 316 |
+
logger.error(f"Error getting head pose: {e}")
|
| 317 |
+
return None
|
| 318 |
+
|
| 319 |
+
def _get_cached_joint_positions(self) -> Optional[tuple]:
|
| 320 |
+
"""Get cached joint positions to reduce query frequency."""
|
| 321 |
+
now = time.time()
|
| 322 |
+
if now - self._last_joints_query < self._cache_ttl:
|
| 323 |
+
return self._state_cache.get('joint_positions')
|
| 324 |
+
|
| 325 |
+
if not self.is_available:
|
| 326 |
+
return None
|
| 327 |
+
|
| 328 |
+
try:
|
| 329 |
+
joints = self.reachy.get_current_joint_positions()
|
| 330 |
+
self._state_cache['joint_positions'] = joints
|
| 331 |
+
self._last_joints_query = now
|
| 332 |
+
return joints
|
| 333 |
+
except Exception as e:
|
| 334 |
+
logger.error(f"Error getting joint positions: {e}")
|
| 335 |
+
return None
|
| 336 |
+
|
| 337 |
def _extract_pose_from_matrix(self, pose_matrix: np.ndarray) -> tuple:
|
| 338 |
"""
|
| 339 |
Extract position (x, y, z) and rotation (roll, pitch, yaw) from 4x4 pose matrix.
|
|
|
|
| 358 |
return x, y, z, roll, pitch, yaw
|
| 359 |
|
| 360 |
def get_head_x(self) -> float:
|
| 361 |
+
"""Get head X position in mm with caching."""
|
| 362 |
+
pose = self._get_cached_head_pose()
|
| 363 |
+
if pose is None:
|
| 364 |
return 0.0
|
| 365 |
try:
|
|
|
|
| 366 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 367 |
return x * 1000 # Convert m to mm
|
| 368 |
except Exception as e:
|
|
|
|
| 383 |
logger.error(f"Error setting head X: {e}")
|
| 384 |
|
| 385 |
def get_head_y(self) -> float:
|
| 386 |
+
"""Get head Y position in mm with caching."""
|
| 387 |
+
pose = self._get_cached_head_pose()
|
| 388 |
+
if pose is None:
|
| 389 |
return 0.0
|
| 390 |
try:
|
|
|
|
| 391 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 392 |
return y * 1000
|
| 393 |
except Exception as e:
|
|
|
|
| 407 |
logger.error(f"Error setting head Y: {e}")
|
| 408 |
|
| 409 |
def get_head_z(self) -> float:
|
| 410 |
+
"""Get head Z position in mm with caching."""
|
| 411 |
+
pose = self._get_cached_head_pose()
|
| 412 |
+
if pose is None:
|
| 413 |
return 0.0
|
| 414 |
try:
|
|
|
|
| 415 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 416 |
return z * 1000
|
| 417 |
except Exception as e:
|
|
|
|
| 431 |
logger.error(f"Error setting head Z: {e}")
|
| 432 |
|
| 433 |
def get_head_roll(self) -> float:
|
| 434 |
+
"""Get head roll angle in degrees with caching."""
|
| 435 |
+
pose = self._get_cached_head_pose()
|
| 436 |
+
if pose is None:
|
| 437 |
return 0.0
|
| 438 |
try:
|
|
|
|
| 439 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 440 |
return math.degrees(roll)
|
| 441 |
except Exception as e:
|
|
|
|
| 458 |
logger.error(f"Error setting head roll: {e}")
|
| 459 |
|
| 460 |
def get_head_pitch(self) -> float:
|
| 461 |
+
"""Get head pitch angle in degrees with caching."""
|
| 462 |
+
pose = self._get_cached_head_pose()
|
| 463 |
+
if pose is None:
|
| 464 |
return 0.0
|
| 465 |
try:
|
|
|
|
| 466 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 467 |
return math.degrees(pitch)
|
| 468 |
except Exception as e:
|
|
|
|
| 484 |
logger.error(f"Error setting head pitch: {e}")
|
| 485 |
|
| 486 |
def get_head_yaw(self) -> float:
|
| 487 |
+
"""Get head yaw angle in degrees with caching."""
|
| 488 |
+
pose = self._get_cached_head_pose()
|
| 489 |
+
if pose is None:
|
| 490 |
return 0.0
|
| 491 |
try:
|
|
|
|
| 492 |
x, y, z, roll, pitch, yaw = self._extract_pose_from_matrix(pose)
|
| 493 |
return math.degrees(yaw)
|
| 494 |
except Exception as e:
|
|
|
|
| 510 |
logger.error(f"Error setting head yaw: {e}")
|
| 511 |
|
| 512 |
def get_body_yaw(self) -> float:
|
| 513 |
+
"""Get body yaw angle in degrees with caching."""
|
| 514 |
+
joints = self._get_cached_joint_positions()
|
| 515 |
+
if joints is None:
|
| 516 |
return 0.0
|
| 517 |
try:
|
| 518 |
+
head_joints, _ = joints
|
|
|
|
| 519 |
return math.degrees(head_joints[0])
|
| 520 |
except Exception as e:
|
| 521 |
logger.error(f"Error getting body yaw: {e}")
|
|
|
|
| 531 |
logger.error(f"Error setting body yaw: {e}")
|
| 532 |
|
| 533 |
def get_antenna_left(self) -> float:
|
| 534 |
+
"""Get left antenna angle in degrees with caching."""
|
| 535 |
+
joints = self._get_cached_joint_positions()
|
| 536 |
+
if joints is None:
|
| 537 |
return 0.0
|
| 538 |
try:
|
| 539 |
+
_, antennas = joints
|
|
|
|
|
|
|
| 540 |
return math.degrees(antennas[1]) # left is index 1
|
| 541 |
except Exception as e:
|
| 542 |
logger.error(f"Error getting left antenna: {e}")
|
|
|
|
| 554 |
logger.error(f"Error setting left antenna: {e}")
|
| 555 |
|
| 556 |
def get_antenna_right(self) -> float:
|
| 557 |
+
"""Get right antenna angle in degrees with caching."""
|
| 558 |
+
joints = self._get_cached_joint_positions()
|
| 559 |
+
if joints is None:
|
| 560 |
return 0.0
|
| 561 |
try:
|
| 562 |
+
_, antennas = joints
|
| 563 |
return math.degrees(antennas[0]) # right is index 0
|
| 564 |
except Exception as e:
|
| 565 |
logger.error(f"Error getting right antenna: {e}")
|
|
|
|
| 654 |
# ========== Phase 6: Diagnostic Information ==========
|
| 655 |
|
| 656 |
def get_control_loop_frequency(self) -> float:
|
| 657 |
+
"""Get control loop frequency in Hz with caching."""
|
| 658 |
+
status = self._get_cached_status()
|
| 659 |
+
if status is None:
|
| 660 |
return 0.0
|
| 661 |
try:
|
|
|
|
|
|
|
| 662 |
backend_status = status.get('backend_status')
|
| 663 |
if backend_status and isinstance(backend_status, dict):
|
| 664 |
control_loop_stats = backend_status.get('control_loop_stats', {})
|
|
|
|
| 669 |
return 0.0
|
| 670 |
|
| 671 |
def get_sdk_version(self) -> str:
|
| 672 |
+
"""Get SDK version with caching."""
|
| 673 |
+
status = self._get_cached_status()
|
| 674 |
+
if status is None:
|
| 675 |
return "N/A"
|
| 676 |
+
return status.get('version') or "unknown"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 677 |
|
| 678 |
def get_robot_name(self) -> str:
|
| 679 |
+
"""Get robot name with caching."""
|
| 680 |
+
status = self._get_cached_status()
|
| 681 |
+
if status is None:
|
| 682 |
return "N/A"
|
| 683 |
+
return status.get('robot_name') or "unknown"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 684 |
|
| 685 |
def get_wireless_version(self) -> bool:
|
| 686 |
+
"""Check if this is a wireless version with caching."""
|
| 687 |
+
status = self._get_cached_status()
|
| 688 |
+
if status is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
return False
|
| 690 |
+
return status.get('wireless_version', False)
|
| 691 |
|
| 692 |
def get_simulation_mode(self) -> bool:
|
| 693 |
+
"""Check if simulation mode is enabled with caching."""
|
| 694 |
+
status = self._get_cached_status()
|
| 695 |
+
if status is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 696 |
return False
|
| 697 |
+
return status.get('simulation_enabled', False)
|
| 698 |
|
| 699 |
def get_wlan_ip(self) -> str:
|
| 700 |
+
"""Get WLAN IP address with caching."""
|
| 701 |
+
status = self._get_cached_status()
|
| 702 |
+
if status is None:
|
| 703 |
return "N/A"
|
| 704 |
+
return status.get('wlan_ip') or "N/A"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 705 |
|
| 706 |
# ========== Phase 7: IMU Sensors (Wireless only) ==========
|
| 707 |
|
|
|
|
| 818 |
logger.debug(f"ReSpeaker not available: {e}")
|
| 819 |
return None
|
| 820 |
|
| 821 |
+
# ========== Phase 11: LED Control (DISABLED - LEDs are inside the robot and not visible) ==========
|
| 822 |
+
# According to PROJECT_PLAN.md principle 8: "LED都被隐藏在了机器人内部,所有的LED控制全部都忽略"
|
| 823 |
+
# The following LED methods are kept but commented out for reference.
|
| 824 |
+
# They are not registered as entities in entity_registry.py.
|
| 825 |
+
|
| 826 |
+
# def get_led_brightness(self) -> float:
|
| 827 |
+
# """Get LED brightness (0-100)."""
|
| 828 |
+
# respeaker = self._get_respeaker()
|
| 829 |
+
# if respeaker is None:
|
| 830 |
+
# return getattr(self, '_led_brightness', 50.0)
|
| 831 |
+
# try:
|
| 832 |
+
# result = respeaker.read("LED_BRIGHTNESS")
|
| 833 |
+
# if result is not None:
|
| 834 |
+
# self._led_brightness = (result[1] / 255.0) * 100.0
|
| 835 |
+
# return self._led_brightness
|
| 836 |
+
# except Exception as e:
|
| 837 |
+
# logger.debug(f"Error getting LED brightness: {e}")
|
| 838 |
+
# return getattr(self, '_led_brightness', 50.0)
|
| 839 |
+
|
| 840 |
+
# def set_led_brightness(self, brightness: float) -> None:
|
| 841 |
+
# """Set LED brightness (0-100)."""
|
| 842 |
+
# brightness = max(0.0, min(100.0, brightness))
|
| 843 |
+
# self._led_brightness = brightness
|
| 844 |
+
# respeaker = self._get_respeaker()
|
| 845 |
+
# if respeaker is None:
|
| 846 |
+
# return
|
| 847 |
+
# try:
|
| 848 |
+
# value = int((brightness / 100.0) * 255)
|
| 849 |
+
# respeaker.write("LED_BRIGHTNESS", [value])
|
| 850 |
+
# logger.info(f"LED brightness set to {brightness}%")
|
| 851 |
+
# except Exception as e:
|
| 852 |
+
# logger.error(f"Error setting LED brightness: {e}")
|
| 853 |
+
|
| 854 |
+
# def get_led_effect(self) -> str:
|
| 855 |
+
# """Get current LED effect."""
|
| 856 |
+
# respeaker = self._get_respeaker()
|
| 857 |
+
# if respeaker is None:
|
| 858 |
+
# return getattr(self, '_led_effect', 'off')
|
| 859 |
+
# try:
|
| 860 |
+
# result = respeaker.read("LED_EFFECT")
|
| 861 |
+
# if result is not None:
|
| 862 |
+
# effect_map = {0: 'off', 1: 'solid', 2: 'breathing', 3: 'rainbow', 4: 'doa'}
|
| 863 |
+
# self._led_effect = effect_map.get(result[1], 'off')
|
| 864 |
+
# return self._led_effect
|
| 865 |
+
# except Exception as e:
|
| 866 |
+
# logger.debug(f"Error getting LED effect: {e}")
|
| 867 |
+
# return getattr(self, '_led_effect', 'off')
|
| 868 |
+
|
| 869 |
+
# def set_led_effect(self, effect: str) -> None:
|
| 870 |
+
# """Set LED effect."""
|
| 871 |
+
# self._led_effect = effect
|
| 872 |
+
# respeaker = self._get_respeaker()
|
| 873 |
+
# if respeaker is None:
|
| 874 |
+
# return
|
| 875 |
+
# try:
|
| 876 |
+
# effect_map = {'off': 0, 'solid': 1, 'breathing': 2, 'rainbow': 3, 'doa': 4}
|
| 877 |
+
# value = effect_map.get(effect, 0)
|
| 878 |
+
# respeaker.write("LED_EFFECT", [value])
|
| 879 |
+
# logger.info(f"LED effect set to {effect}")
|
| 880 |
+
# except Exception as e:
|
| 881 |
+
# logger.error(f"Error setting LED effect: {e}")
|
| 882 |
+
|
| 883 |
+
# def get_led_color_r(self) -> float:
|
| 884 |
+
# """Get LED red color component (0-255)."""
|
| 885 |
+
# respeaker = self._get_respeaker()
|
| 886 |
+
# if respeaker is None:
|
| 887 |
+
# return getattr(self, '_led_color_r', 0.0)
|
| 888 |
+
# try:
|
| 889 |
+
# result = respeaker.read("LED_COLOR")
|
| 890 |
+
# if result is not None:
|
| 891 |
+
# color = result[1] if len(result) > 1 else 0
|
| 892 |
+
# self._led_color_r = float((color >> 16) & 0xFF)
|
| 893 |
+
# return self._led_color_r
|
| 894 |
+
# except Exception as e:
|
| 895 |
+
# logger.debug(f"Error getting LED color R: {e}")
|
| 896 |
+
# return getattr(self, '_led_color_r', 0.0)
|
| 897 |
+
|
| 898 |
+
# def set_led_color_r(self, value: float) -> None:
|
| 899 |
+
# """Set LED red color component (0-255)."""
|
| 900 |
+
# self._led_color_r = max(0.0, min(255.0, value))
|
| 901 |
+
# self._update_led_color()
|
| 902 |
+
|
| 903 |
+
# def get_led_color_g(self) -> float:
|
| 904 |
+
# """Get LED green color component (0-255)."""
|
| 905 |
+
# respeaker = self._get_respeaker()
|
| 906 |
+
# if respeaker is None:
|
| 907 |
+
# return getattr(self, '_led_color_g', 0.0)
|
| 908 |
+
# try:
|
| 909 |
+
# result = respeaker.read("LED_COLOR")
|
| 910 |
+
# if result is not None:
|
| 911 |
+
# color = result[1] if len(result) > 1 else 0
|
| 912 |
+
# self._led_color_g = float((color >> 8) & 0xFF)
|
| 913 |
+
# return self._led_color_g
|
| 914 |
+
# except Exception as e:
|
| 915 |
+
# logger.debug(f"Error getting LED color G: {e}")
|
| 916 |
+
# return getattr(self, '_led_color_g', 0.0)
|
| 917 |
+
|
| 918 |
+
# def set_led_color_g(self, value: float) -> None:
|
| 919 |
+
# """Set LED green color component (0-255)."""
|
| 920 |
+
# self._led_color_g = max(0.0, min(255.0, value))
|
| 921 |
+
# self._update_led_color()
|
| 922 |
+
|
| 923 |
+
# def get_led_color_b(self) -> float:
|
| 924 |
+
# """Get LED blue color component (0-255)."""
|
| 925 |
+
# respeaker = self._get_respeaker()
|
| 926 |
+
# if respeaker is None:
|
| 927 |
+
# return getattr(self, '_led_color_b', 0.0)
|
| 928 |
+
# try:
|
| 929 |
+
# result = respeaker.read("LED_COLOR")
|
| 930 |
+
# if result is not None:
|
| 931 |
+
# color = result[1] if len(result) > 1 else 0
|
| 932 |
+
# self._led_color_b = float(color & 0xFF)
|
| 933 |
+
# return self._led_color_b
|
| 934 |
+
# except Exception as e:
|
| 935 |
+
# logger.debug(f"Error getting LED color B: {e}")
|
| 936 |
+
# return getattr(self, '_led_color_b', 0.0)
|
| 937 |
+
|
| 938 |
+
# def set_led_color_b(self, value: float) -> None:
|
| 939 |
+
# """Set LED blue color component (0-255)."""
|
| 940 |
+
# self._led_color_b = max(0.0, min(255.0, value))
|
| 941 |
+
# self._update_led_color()
|
| 942 |
+
|
| 943 |
+
# def _update_led_color(self) -> None:
|
| 944 |
+
# """Update LED color from R, G, B components."""
|
| 945 |
+
# respeaker = self._get_respeaker()
|
| 946 |
+
# if respeaker is None:
|
| 947 |
+
# return
|
| 948 |
+
# try:
|
| 949 |
+
# r = int(getattr(self, '_led_color_r', 0))
|
| 950 |
+
# g = int(getattr(self, '_led_color_g', 0))
|
| 951 |
+
# b = int(getattr(self, '_led_color_b', 0))
|
| 952 |
+
# color = (r << 16) | (g << 8) | b
|
| 953 |
+
# respeaker.write("LED_COLOR", [color])
|
| 954 |
+
# logger.info(f"LED color set to RGB({r}, {g}, {b})")
|
| 955 |
+
# except Exception as e:
|
| 956 |
+
# logger.error(f"Error setting LED color: {e}")
|
| 957 |
|
| 958 |
# ========== Phase 12: Audio Processing (via local SDK) ==========
|
| 959 |
|
|
|
|
| 1080 |
|
| 1081 |
def get_passive_joints_json(self) -> str:
|
| 1082 |
"""
|
| 1083 |
+
Get passive joints as JSON string with cached status.
|
| 1084 |
|
| 1085 |
Returns:
|
| 1086 |
JSON string: "[passive_1_x, passive_1_y, passive_1_z, ..., passive_7_z]"
|
|
|
|
| 1090 |
return "[]"
|
| 1091 |
try:
|
| 1092 |
import json
|
| 1093 |
+
# Get WLAN IP from cached daemon status
|
| 1094 |
+
status = self._get_cached_status()
|
| 1095 |
+
if status is None:
|
| 1096 |
+
return "[]"
|
| 1097 |
wlan_ip = status.get('wlan_ip', 'localhost')
|
| 1098 |
# Call the backend API to get passive joints
|
| 1099 |
backend_url = f"http://{wlan_ip}:8000/api/state/full?with_passive_joints=true"
|
reachy_mini_ha_voice/voice_assistant.py
CHANGED
|
@@ -161,7 +161,7 @@ class VoiceAssistantService:
|
|
| 161 |
except Exception as e:
|
| 162 |
_LOGGER.warning("Failed to initialize Reachy Mini media: %s", e)
|
| 163 |
|
| 164 |
-
# Start motion controller (
|
| 165 |
if self._motion is not None:
|
| 166 |
self._motion.start()
|
| 167 |
|
|
|
|
| 161 |
except Exception as e:
|
| 162 |
_LOGGER.warning("Failed to initialize Reachy Mini media: %s", e)
|
| 163 |
|
| 164 |
+
# Start motion controller (20Hz control loop)
|
| 165 |
if self._motion is not None:
|
| 166 |
self._motion.start()
|
| 167 |
|