Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# ==================================
# === 開發與除錯設定 (Dev & Debug Settings)
# ==================================
# 如果設為 true,硬體模式將不會連接真實的USB序列埠,
# 而是啟動一個與MuJoCo模擬器連結的「虛擬Teensy」。
# 這允許在沒有實體硬體的情況下,完整地測試和除錯 HardwareController。
use_virtual_teensy: false # 預設為 false,要測試時手動改為 true

# ==================================
# === 模擬與模型設定 (Simulation & Model Settings)
# ==================================
Expand Down
8 changes: 5 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ def soft_reset():
t_since_update = time.time() - hw_controller.hw_state_data.last_update_time
conn_status = f"Data Delay: {t_since_update:.2f}s" if t_since_update < 1.0 else "Data Timeout!"
state.hardware_status_text = f"Connection Status: {conn_status}\n"
state.hardware_status_text += f"LinVel: {np.array2string(hw_controller.hw_state_data.lin_vel_local, precision=2)}\n"
state.hardware_status_text += f"Gyro: {np.array2string(hw_controller.hw_state_data.imu_gyro_radps, precision=2)}"
# 顯示角速度(Gyro)資訊
state.hardware_status_text += f"AngVel: {np.array2string(hw_controller.hw_state_data.angular_velocity_radps, precision=2)}\n"
# 顯示加速度資訊
state.hardware_status_text += f"Accel: {np.array2string(hw_controller.hw_state_data.accelerometer_ms2, precision=2)}"
else:
state.hardware_status_text = "Hardware controller not running."

Expand Down Expand Up @@ -210,4 +212,4 @@ def soft_reset():
print("\n程式已安全退出。")

if __name__ == "__main__":
main()
main()
9 changes: 8 additions & 1 deletion main_nicegui.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from src.controllers.simulation_controller import SimulationController
from src.input_handlers.keyboard_input_handler import KeyboardInputHandler
from src.core.logger import log
from src.utils.gamepad_presence_guard import start_gamepad_presence_guard
# 【v4.3.2 新增】 導入新的 ObservationManager
from src.simulation.observation_manager import ObservationManager

Expand Down Expand Up @@ -88,6 +89,10 @@ def main() -> None:
try:
config = load_config()
state = SimulationState(config)
if config.use_virtual_teensy:
# 🔌 虛擬Teensy模式:不需要實體序列埠也能進入硬體模式
state.serial_is_connected = True
log.info("虛擬Teensy模式啟用,跳過序列埠連線檢查。")
except Exception as exc:
sys.exit(f"failed to initialise: {exc}")

Expand All @@ -108,6 +113,8 @@ def main() -> None:

xbox_handler = XboxInputHandler(state)
state.xbox_handler_ref = xbox_handler
# 啟動搖桿存在守門員,只用於 UI 顯示,不影響 handler
start_gamepad_presence_guard(state)

# 【v4.3.2 修改】 將 observation_manager 傳入 PolicyManager
policy_manager = PolicyManager(config, observation_manager, None) # 在 NiceGUI 模式下,overlay 設為 None
Expand Down Expand Up @@ -150,4 +157,4 @@ def cleanup_resources() -> None:


if __name__ in {"__main__", "__mp_main__"}:
main()
main()
68 changes: 1 addition & 67 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,4 @@ pip install pyserial
pip install Pillow
pip install scipy //運動學 hardware 需用

pip install numpy onnxruntime PyYAML nicegui pygame pyserial Pillow scipy

### 專案目錄結構與模組化原則
```
simulation_test_recoil/
├── assets/ # 靜態資源 (Static Assets)
├── models/ # AI 模型文件 (AI Model Files)
├── pdf/ # 參考文檔 (Reference Documents)
├── src/ # 核心原始碼 (Core Source Code)
│ ├── core/ # 核心通用模組 (Core Common Modules)
│ ├── controllers/ # 主要控制器與邏輯協調 (Main Controllers & Logic Orchestration)
│ ├── simulation/ # 模擬環境相關 (Simulation Environment Specific)
│ ├── hardware/ # 硬體交互與底層AI推理 (Hardware Interaction & Low-level AI Inference)
│ ├── input_handlers/ # 用戶輸入處理 (User Input Handling)
│ ├── utils/ # 通用工具函式 (General Utility Functions)
│ └── mock/ # 模擬/測試用模組 (Mock/Test Modules)
├── test/ # 測試與輔助腳本 (Tests & Auxiliary Scripts)
├── output/ # 生成的輸出文件 (Generated Output Files)
├── .gitignore # Git 忽略文件 (Git Ignore File)
├── config.yaml # 應用程式主配置 (Main Application Configuration)
├── project_overview_config.yaml # 專案概覽工具配置 (Project Overview Tool Configuration)
├── main.py # CLI 主入口 (CLI Main Entry Point)
├── main_nicegui.py # NiceGUI UI 主入口 (NiceGUI UI Main Entry Point)
├── readme.md # 專案說明 (Project Readme)
└── tennsy.md # Teensy 韌體文檔 (Teensy Firmware Documentation)
```

**目錄說明與分類原則:**

* **`assets/`**:
* **原則**: 存放模擬器使用的所有靜態資源文件,例如 MuJoCo 模型所需的 3D 網格 (`.stl`)、紋理圖檔 (`.png`) 和場景定義文件 (`.xml`)。這些文件通常在運行時被載入,但不包含任何可執行邏輯。
* **`models/`**:
* **原則**: 專門用於存放預訓練的 AI 模型文件,例如 ONNX 格式的策略模型 (`.onnx`, `.ort`)。這些是應用程式的數據資產,而非程式碼。
* **`pdf/`**:
* **原則**: 存放專案相關的參考文檔,如 MuJoCo 的官方文檔等。
* **`src/`**:
* **原則**: 本專案所有核心 Python 原始碼的根目錄。所有可執行邏輯的模組都應置於此,並在此目錄內進一步細分。這確保了源碼與其他類型的文件清晰分離。
* **`src/core/`**:
* **原則**: 存放應用程式最底層、最通用、被多個高層模組共同依賴且變動頻率較低的模組。它們是整個系統的基石。
* **內容**: 全局配置 (`config.py`)、中央狀態管理 (`state.py`)、異步事件匯流排 (`event_system.py`)、日誌系統 (`logger.py`)。
* **`src/controllers/`**:
* **原則**: 存放協調應用程式高層邏輯和業務流程的「指揮官」模組。它們負責監聽事件、更新中央狀態、調用低層服務,並確保各子系統的同步運行。
* **內容**: 模擬主控制器 (`simulation_controller.py`)、硬體主控制器 (`hardware_controller.py`)、UI 邏輯控制器 (`ui_controller.py`)。
* **`src/simulation/`**:
* **原則**: 存放與 MuJoCo 物理模擬環境直接相關的模組。這些模組通常需要直接訪問 MuJoCo 的 API 或模型數據。
* **內容**: MuJoCo 模擬器接口 (`simulation.py`)、3D 渲染與除錯疊層 (`rendering.py`)、地形管理 (`terrain_manager.py`)、懸浮控制器 (`floating_controller.py`)、以及將被新版取代的舊觀察器 (`observation.py`)。
* **`src/hardware/`**:
* **原則**: 存放與外部硬體設備(如 Teensy、Xbox 搖桿)進行低層通信和 AI 推理邏輯的模組。它們負責將物理世界或感測器的數據轉換為程式碼可處理的格式,或將程式碼指令轉換為硬體可執行的命令。
* **內容**: AI 策略管理 (`policy.py`)、序列埠通信 (`serial_communicator.py`)、Xbox 搖桿接口 (`xbox_controller.py`)。
* **`src/input_handlers/`**:
* **原則**: 專門處理用戶輸入(鍵盤、搖桿)的模組。它們的職責是將原始輸入事件翻譯為應用程式內部的標準命令或請求事件。
* **內容**: 鍵盤輸入處理 (`keyboard_input_handler.py`)、Xbox 搖桿輸入處理 (`xbox_input_handler.py`)。
* **`src/utils/`**:
* **原則**: 存放跨多個模塊使用的通用輔助函數或小型工具類。這些模塊通常不包含複雜的業務邏輯,而是提供可重用的功能。
* **內容**: 序列埠選擇工具 (`serial_utils.py`)。
* **`src/mock/`**:
* **原則**: 存放用於測試或無頭模式下使用的「模擬」或「假」的模組實現。它們提供與真實模組相同的接口,但內部邏輯是簡化或模擬的。
* **內容**: 模擬 MuJoCo 環境和各類控制器 (`mock_simulation.py`)。
* **`test/`**:
* **原則**: 存放所有自動化測試腳本 (`pytest` 測試) 和獨立的輔助工具/演示腳本。這些文件不屬於應用程式的核心運行邏輯,但對開發和驗證至關重要。
* **內容**: 專案概覽生成工具 (`project_overview.py`)、序列埠控制台工具 (`test_pyserial_console.py`)、以及各種測試用例。
* **`output/`**:
* **原則**: 專門存放腳本在運行時生成的各種輸出文件。這些文件通常不應被版本控制,因此被添加到 `.gitignore` 中。
* **內容**: 專案概覽報告 (`project_overview_*.md/.txt`)、地形快照 (`terrain_snapshot_*.png`)。
* **根目錄文件**:
* **原則**: 存放專案的頂層配置、主要入口點和通用文檔。力求保持根目錄的簡潔和高層次概覽。
* **內容**: Git 忽略文件 (`.gitignore`)、應用程式主要配置 (`config.yaml`)、專案概覽工具配置 (`project_overview_config.yaml`)、主入口腳本 (`main.py`, `main_nicegui.py`)、專案總說明 (`readme.md`)、Teensy 韌體文檔 (`tennsy.md`)。
pip install numpy onnxruntime PyYAML nicegui pygame pyserial Pillow scipy
Loading