diff --git a/src/humanoid_3d_animation/humanoid_simulation.py b/src/humanoid_3d_animation/humanoid_simulation.py
index 92f4605c1f..c29ef757f3 100644
--- a/src/humanoid_3d_animation/humanoid_simulation.py
+++ b/src/humanoid_3d_animation/humanoid_simulation.py
@@ -3,45 +3,82 @@
import os
import time
import math
-import threading # 新增:用于监听控制台输入(实现重置指令)
-import numpy as np
-
-def create_humanoid_xml(file_path):
- """
- 自动创建humanoid.xml文件并写入模型代码
- 优化点:XML内容格式化,增加注释,提升可读性
- """
- xml_content = """
-
+import threading
+import signal
+import sys
+from dataclasses import dataclass # 用于配置类
+
+# ====================== 配置抽离(核心优化点)======================
+@dataclass
+class SimConfig:
+ """仿真配置类:集中管理所有可配置参数"""
+ # 文件路径配置
+ xml_filename: str = "humanoid.xml"
+ # 仿真参数
+ timestep: float = 0.005 # 与XML中的timestep保持一致
+ sim_frequency: float = 2.0 # 关节运动频率(Hz)
+ state_print_interval: float = 1.0 # 状态打印间隔(秒)
+ # 相机参数
+ cam_distance: float = 2.0
+ cam_azimuth: float = 45.0
+ cam_elevation: float = -20.0
+ # 关节运动幅度配置
+ joint_amplitudes = {
+ "left_shoulder": 1.0, "right_shoulder": 1.0,
+ "left_elbow": 0.5, "right_elbow": 0.5,
+ "left_hip": 0.8, "right_hip": 0.8,
+ "left_knee": 0.6, "right_knee": 0.6
+ }
+ # 控制模式:sin(正弦运动)、random(随机运动)、stop(静止)
+ default_mode: str = "sin"
+
+# 全局变量:用于优雅退出
+sim_running = True
+
+def signal_handler(sig, frame):
+ """处理Ctrl+C中断信号,实现优雅退出"""
+ global sim_running
+ sim_running = False
+ print("\n⚠️ 收到中断信号,正在退出仿真...")
+ sys.exit(0)
+
+# 注册信号处理
+signal.signal(signal.SIGINT, signal_handler)
+
+# ====================== 核心功能类 ======================
+class HumanoidSimulator:
+ def __init__(self, config: SimConfig):
+ self.config = config
+ self.model = None
+ self.data = None
+ self.joint_names = list(config.joint_amplitudes.keys())
+ # 预存关节ID和控制ID(避免每次循环重复计算,性能优化)
+ self.joint_ctrl_ids = {}
+ self.joint_qpos_indices = {}
+ # 运动模式和控制信号缓存(用于平滑控制)
+ self.current_mode = config.default_mode
+ self.last_ctrl_signals = {} # 存储上一帧的控制信号
+
+ def create_xml_file(self, file_path):
+ """创建人形机器人XML文件"""
+ xml_content = f"""
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
@@ -50,8 +87,7 @@ def create_humanoid_xml(file_path):
-
-
+
@@ -60,8 +96,7 @@ def create_humanoid_xml(file_path):
-
-
+
@@ -70,8 +105,7 @@ def create_humanoid_xml(file_path):
@@ -83,10 +117,8 @@ def create_humanoid_xml(file_path):