From 201e3eab05d0230e7bc97603c2011bbea4aee51e Mon Sep 17 00:00:00 2001 From: HuangJinjie2887 Date: Sun, 21 Dec 2025 13:33:10 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E5=89=8D=E6=9C=9F?= =?UTF-8?q?=E5=8F=A4=E6=97=A9=E7=AE=80=E5=8C=96=E6=A8=A1=E5=9E=8B=E5=8F=8A?= =?UTF-8?q?=E5=85=B6=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Embodied_Human_Arm_Modeling/arm_model.xml | 43 ------------------- .../run_arm_model.py | 0 2 files changed, 43 deletions(-) delete mode 100644 src/Embodied_Human_Arm_Modeling/arm_model.xml delete mode 100644 src/Embodied_Human_Arm_Modeling/run_arm_model.py diff --git a/src/Embodied_Human_Arm_Modeling/arm_model.xml b/src/Embodied_Human_Arm_Modeling/arm_model.xml deleted file mode 100644 index 3fa35a4a42..0000000000 --- a/src/Embodied_Human_Arm_Modeling/arm_model.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - \ No newline at end of file diff --git a/src/Embodied_Human_Arm_Modeling/run_arm_model.py b/src/Embodied_Human_Arm_Modeling/run_arm_model.py deleted file mode 100644 index e69de29bb2..0000000000 From 104ba7da98d1cd4e32cd6049652cd7080991dc2a Mon Sep 17 00:00:00 2001 From: HuangJinjie2887 Date: Sun, 21 Dec 2025 18:20:49 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E5=8A=A8?= =?UTF-8?q?=E7=94=BB=E5=8F=82=E6=95=B0=EF=BC=8C=E8=AE=A9=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E5=8A=A8=E4=BD=9C=E6=9B=B4=E7=B2=BE=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hand_animation_controller.py | 291 +++++++++++++----- 1 file changed, 220 insertions(+), 71 deletions(-) diff --git a/src/Embodied_Human_Arm_Modeling/hand_animation_controller.py b/src/Embodied_Human_Arm_Modeling/hand_animation_controller.py index be9249941d..ae6c96070e 100644 --- a/src/Embodied_Human_Arm_Modeling/hand_animation_controller.py +++ b/src/Embodied_Human_Arm_Modeling/hand_animation_controller.py @@ -4,6 +4,7 @@ import numpy as np import time import sys +import threading class HandDemoMujoco3: @@ -22,11 +23,6 @@ def __init__(self, model_path='left_hand.xml'): print(f"📊 仿真时间步: {self.model.opt.timestep:.4f}秒") print("=" * 60) - # 在 MuJoCo 3.x 中获取执行器名称的替代方法 - print("📋 执行器信息:") - # 注意:MuJoCo 3.x 中获取执行器名称的方式不同 - # 这里我们只显示数量,不尝试获取名称 - # 创建预设姿态 self._create_preset_poses() @@ -38,6 +34,23 @@ def __init__(self, model_path='left_hand.xml'): self.start_values = None self.target_values = None + # 添加暂停相关变量 + self.paused = False + self.pause_start_time = 0 + self.total_pause_time = 0 + self.pause_lock = threading.Lock() + + # 添加用户控制变量 + self.manual_mode = False + self.should_exit = False + + # 控制变量 + self.should_toggle_pause = False + self.should_next_pose = False + self.should_prev_pose = False + self.should_toggle_mode = False + self.should_restart = False + print(f"🎭 创建了 {len(self.poses)} 种预设姿态") print("=" * 60) @@ -63,13 +76,6 @@ def _create_preset_poses(self): 'description': '所有手指完全握紧' } - # 捏取 - self.poses['捏取'] = { - 'values': self._create_pose_pinch(), - 'emoji': '🤏', - 'description': '拇指和食指对捏' - } - # 圆柱体抓握 self.poses['圆柱体抓握'] = { 'values': self._create_pose_cylinder(), @@ -119,11 +125,11 @@ def _create_pose_fist(self): # 假设前20个执行器是:手腕(2) + 拇指(5) + 4个手指*3 + 小指额外(1) values[:20] = [ 0.0, 0.0, # 手腕 - 0.0, 1.57, 0.0, 0.5, 0.3, # 拇指 - 1.57, 1.57, 1.57, # 食指 - 1.57, 1.57, 1.57, # 中指 - 1.57, 1.57, 1.57, # 无名指 - 0.0, 1.57, 1.57, 1.57 # 小指 + 0.199, 0.354, 0.209, 0.698, 0.3, # 拇指 + 5, 5, 5, # 食指 + 5, 5, 5, # 中指 + 5, 5, 5, # 无名指 + 0.0, 5, 5, 5, # 小指 ] elif self.model.nu >= 10: # 简化的握拳姿态 @@ -139,36 +145,6 @@ def _create_pose_fist(self): return values - def _create_pose_pinch(self): - """创建捏取姿态""" - values = np.zeros(self.model.nu) - - if self.model.nu >= 20: - values[:20] = [ - 0.0, 0.0, # 手腕 - 0.5, 0.6, 0.0, 0.5, 0.8, # 拇指 - 0.2, 0.7, 0.7, # 食指 - 0.0, 0.2, 0.2, # 中指 - 0.0, 0.1, 0.1, # 无名指 - 0.0, 0.1, 0.1, 0.1 # 小指 - ] - elif self.model.nu >= 5: - # 简化的捏取:假设前5个执行器中,第2个是拇指,第3个是食指 - for i in range(self.model.nu): - if i == 2: # 拇指 - values[i] = 0.5 - elif i == 3: # 食指 - values[i] = 0.7 - elif i >= 4: # 其他手指 - values[i] = 0.2 - else: # 手腕 - values[i] = 0.0 - else: - # 最小配置 - for i in range(self.model.nu): - values[i] = 0.5 if i == 2 else 0.0 - - return values def _create_pose_cylinder(self): """创建圆柱体抓握姿态""" @@ -203,12 +179,12 @@ def _create_pose_scissors(self): if self.model.nu >= 20: values[:20] = [ - 0.0, 0.0, # 手腕 + 0.0, 10, # 手腕 0.2, 0.4, 0.0, 0.3, 0.2, # 拇指 0.0, 0.0, 0.0, # 食指 0.0, 0.0, 0.0, # 中指 - 0.7, 1.57, 1.57, # 无名指 - 0.0, 1.57, 1.57, 1.57 # 小指 + 0.7, 5, 5, # 无名指 + 0.0, 1, 5, 5 # 小指 ] elif self.model.nu >= 7: # 简化的剪刀手:假设第3-4个是食指和中指,其他手指弯曲 @@ -233,11 +209,11 @@ def _create_pose_ok(self): if self.model.nu >= 20: values[:20] = [ 0.0, 0.0, # 手腕 - 0.4, 0.6, 0.0, 0.5, 0.7, # 拇指 - 0.3, 0.7, 0.9, # 食指 - 0.0, 0.1, 0.2, # 中指 - 0.0, 0.1, 0.2, # 无名指 - 0.0, 0.1, 0.2, 0.2 # 小指 + -0.178, 1.22, 0.134, 0.698, 0.361, # 拇指 + 0.2, 1.57, 1.3, # 食指 + 0.0, 2, 0.2, # 中指 + 0.0, 1, 0.2, # 无名指 + 0.0, -0.5, 0.2, 0.2 # 小指 ] elif self.model.nu >= 5: # 简化的OK手势 @@ -266,9 +242,9 @@ def _create_pose_pointing(self): 0.0, 0.0, # 手腕 0.2, 0.3, 0.0, 0.2, 0.3, # 拇指 0.0, 0.0, 0.0, # 食指 - 1.57, 1.57, 1.57, # 中指 - 1.57, 1.57, 1.57, # 无名指 - 0.0, 1.57, 1.57, 1.57 # 小指 + 5, 5, 5, # 中指 + 5, 5, 5, # 无名指 + 0.0, 5, 5, 5, # 小指 ] elif self.model.nu >= 4: # 简化的指点:假设第3个是食指 @@ -332,6 +308,114 @@ def update_animation(self): return False + def toggle_pause(self): + """切换暂停状态""" + with self.pause_lock: + if self.paused: + # 从暂停状态恢复 + self.paused = False + pause_duration = time.time() - self.pause_start_time + self.total_pause_time += pause_duration + print(f"\n▶️ 演示继续 (暂停了 {pause_duration:.1f} 秒)") + else: + # 进入暂停状态 + self.paused = True + self.pause_start_time = time.time() + print(f"\n⏸️ 演示暂停") + + # 显示当前状态信息 + current_pose = self.demo_sequence[self.current_pose_idx] + pose_info = self.poses[current_pose] + progress = (self.current_pose_idx + 1) / len(self.demo_sequence) * 100 + print(f" 当前姿态: {current_pose} - {pose_info['description']}") + print(f" 进度: {progress:.1f}%") + + def next_pose(self): + """切换到下一个姿态""" + if not self.paused and not self.manual_mode: + # 只有在自动模式下才能手动切换 + return False + + with self.pause_lock: + if self.paused: + # 从暂停状态恢复但切换到下一个姿态 + self.paused = False + pause_duration = time.time() - self.pause_start_time + self.total_pause_time += pause_duration + + self.current_pose_idx = (self.current_pose_idx + 1) % len(self.demo_sequence) + next_pose = self.demo_sequence[self.current_pose_idx] + self.start_animation(next_pose) + + print(f"\n⏭️ 切换到下一个姿态: {next_pose}") + return True + + def previous_pose(self): + """切换到上一个姿态""" + if not self.paused and not self.manual_mode: + # 只有在自动模式下才能手动切换 + return False + + with self.pause_lock: + if self.paused: + # 从暂停状态恢复但切换到上一个姿态 + self.paused = False + pause_duration = time.time() - self.pause_start_time + self.total_pause_time += pause_duration + + self.current_pose_idx = (self.current_pose_idx - 1) % len(self.demo_sequence) + previous_pose = self.demo_sequence[self.current_pose_idx] + self.start_animation(previous_pose) + + print(f"\n⏮️ 切换到上一个姿态: {previous_pose}") + return True + + def toggle_manual_mode(self): + """切换手动模式""" + self.manual_mode = not self.manual_mode + if self.manual_mode: + print(f"\n🎮 切换到手动模式") + print(" 使用控制台命令控制") + else: + print(f"\n🤖 切换到自动模式") + return self.manual_mode + + def print_controls(self): + """打印控制说明""" + print("\n" + "=" * 60) + print("🎮 控制说明 (在终端中输入命令):") + print(" pause: 暂停/继续演示") + print(" next: 下一个姿态") + print(" prev: 上一个姿态") + print(" mode: 切换手动/自动模式") + print(" restart: 重新开始演示") + print(" help: 显示控制说明") + print(" quit: 退出演示") + print("=" * 60) + + def process_command(self, command): + """处理控制台命令""" + command = command.strip().lower() + + if command == 'pause': + self.should_toggle_pause = True + elif command == 'next': + self.should_next_pose = True + elif command == 'prev': + self.should_prev_pose = True + elif command == 'mode': + self.should_toggle_mode = True + elif command == 'restart': + self.should_restart = True + elif command == 'help': + self.print_controls() + elif command == 'quit': + self.should_exit = True + print("\n👋 正在退出演示...") + elif command: + print(f"❌ 未知命令: {command}") + print("输入 'help' 查看可用命令") + def run_demo(self): """运行演示""" print("\n" + "=" * 60) @@ -340,7 +424,9 @@ def run_demo(self): print(f"🎬 演示序列: {len(self.demo_sequence)} 个姿态") print(f"⏱️ 每个姿态保持: 3.0秒") print(f"🎥 动画过渡: {self.animation_duration}秒") - print("按 Ctrl+C 退出演示") + self.print_controls() + print("=" * 60) + print("\n💡 提示: 在终端中输入命令控制演示") print("=" * 60) # 设置初始姿态 @@ -348,7 +434,25 @@ def run_demo(self): self.data.ctrl[:] = self.poses[initial_pose]['values'] last_change = time.time() - hold_duration = 3.0 # 每个姿态保持3秒 + hold_duration = 5 # 每个姿态保持3秒 + + # 启动一个线程来处理用户输入 + import threading + + def input_thread(): + """处理用户输入的线程""" + while not self.should_exit: + try: + command = input("\n> ").strip() + if command: + self.process_command(command) + except (EOFError, KeyboardInterrupt): + self.should_exit = True + break + + # 启动输入线程 + input_handler = threading.Thread(target=input_thread, daemon=True) + input_handler.start() try: with mujoco.viewer.launch_passive(self.model, self.data) as viewer: @@ -366,19 +470,62 @@ def run_demo(self): sys.stdout.write(f"进度: {0.0:5.1f}% - {pose_info['description']}") sys.stdout.flush() - while viewer.is_running(): + while viewer.is_running() and not self.should_exit: current_time = time.time() - # 更新动画 - self.update_animation() - - # 检查是否需要切换到下一个姿态 - if not self.animating and (current_time - last_change > hold_duration): - self.current_pose_idx = (self.current_pose_idx + 1) % len(self.demo_sequence) - next_pose = self.demo_sequence[self.current_pose_idx] - - if self.start_animation(next_pose): - last_change = current_time + # 处理命令 + if self.should_toggle_pause: + self.toggle_pause() + self.should_toggle_pause = False + + if self.should_next_pose: + self.next_pose() + if not self.manual_mode: + self.toggle_manual_mode() + last_change = current_time + self.should_next_pose = False + + if self.should_prev_pose: + self.previous_pose() + if not self.manual_mode: + self.toggle_manual_mode() + last_change = current_time + self.should_prev_pose = False + + if self.should_toggle_mode: + self.toggle_manual_mode() + self.should_toggle_mode = False + + if self.should_restart: + self.current_pose_idx = 0 + self.start_animation(self.demo_sequence[0]) + self.paused = False + self.total_pause_time = 0 + last_change = current_time + print(f"\n🔄 重新开始演示") + self.should_restart = False + + # 如果暂停,跳过更新 + if self.paused: + viewer.sync() + time.sleep(0.01) # 减少CPU使用率 + continue + + # 在手动模式下,不自动切换姿态 + if not self.manual_mode: + # 更新动画 + self.update_animation() + + # 检查是否需要切换到下一个姿态 + if not self.animating and (current_time - last_change > hold_duration): + self.current_pose_idx = (self.current_pose_idx + 1) % len(self.demo_sequence) + next_pose = self.demo_sequence[self.current_pose_idx] + + if self.start_animation(next_pose): + last_change = current_time + else: + # 手动模式下,只更新动画 + self.update_animation() # 运行仿真 mujoco.mj_step(self.model, self.data) @@ -396,8 +543,10 @@ def run_demo(self): import traceback traceback.print_exc() finally: + self.should_exit = True print("\n" + "=" * 60) print("🎉 演示结束") + print(f"⏱️ 总暂停时间: {self.total_pause_time:.1f}秒") print("=" * 60) From b3179ba895839bee621f3e7b8a2984209bd59423 Mon Sep 17 00:00:00 2001 From: HuangJinjie2887 Date: Sun, 21 Dec 2025 21:32:45 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=8C=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=97=A7=E5=B1=95=E7=A4=BA=E8=84=9A=E6=9C=AC=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B8=A6=E6=8E=A7=E5=88=B6=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E7=89=88=E8=84=9A=E6=9C=AC=EF=BC=8C=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E9=80=9A=E8=BF=87=E7=BB=88=E7=AB=AF=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...oller.py => interactive_hand_simulator.py} | 10 +--- .../simulate_arm.py | 59 ------------------- 2 files changed, 3 insertions(+), 66 deletions(-) rename src/Embodied_Human_Arm_Modeling/{hand_animation_controller.py => interactive_hand_simulator.py} (98%) delete mode 100644 src/Embodied_Human_Arm_Modeling/simulate_arm.py diff --git a/src/Embodied_Human_Arm_Modeling/hand_animation_controller.py b/src/Embodied_Human_Arm_Modeling/interactive_hand_simulator.py similarity index 98% rename from src/Embodied_Human_Arm_Modeling/hand_animation_controller.py rename to src/Embodied_Human_Arm_Modeling/interactive_hand_simulator.py index ae6c96070e..66d144d1df 100644 --- a/src/Embodied_Human_Arm_Modeling/hand_animation_controller.py +++ b/src/Embodied_Human_Arm_Modeling/interactive_hand_simulator.py @@ -1,4 +1,3 @@ -# 保存为 hand_demo_mujoco3.py import mujoco import mujoco.viewer import numpy as np @@ -104,11 +103,10 @@ def _create_preset_poses(self): 'description': '食指伸直,其他手指握起' } - # 演示序列 + # 演示序列 (去掉了捏取动作) self.demo_sequence = [ '张开手', '握拳', - '捏取', '圆柱体抓握', '剪刀手', 'OK手势', @@ -422,7 +420,7 @@ def run_demo(self): print("🤖 手部抓握姿态全自动演示 (MuJoCo 3.x 兼容版)") print("=" * 60) print(f"🎬 演示序列: {len(self.demo_sequence)} 个姿态") - print(f"⏱️ 每个姿态保持: 3.0秒") + print(f"⏱️ 每个姿态保持: 5.0秒") print(f"🎥 动画过渡: {self.animation_duration}秒") self.print_controls() print("=" * 60) @@ -434,11 +432,9 @@ def run_demo(self): self.data.ctrl[:] = self.poses[initial_pose]['values'] last_change = time.time() - hold_duration = 5 # 每个姿态保持3秒 + hold_duration = 5.0 # 每个姿态保持5秒 # 启动一个线程来处理用户输入 - import threading - def input_thread(): """处理用户输入的线程""" while not self.should_exit: diff --git a/src/Embodied_Human_Arm_Modeling/simulate_arm.py b/src/Embodied_Human_Arm_Modeling/simulate_arm.py deleted file mode 100644 index b907272946..0000000000 --- a/src/Embodied_Human_Arm_Modeling/simulate_arm.py +++ /dev/null @@ -1,59 +0,0 @@ -import os -import sys -os.environ['MUJOCO_GL'] = 'egl' -# 关键设置:获取当前脚本文件的绝对目录,并设置为工作目录 -SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) -os.chdir(SCRIPT_DIR) -# simulate_arm.py -import mujoco -import mujoco.viewer -import numpy as np -import time - -def main(): - # 1. 加载模型 - model = mujoco.MjModel.from_xml_path('arm_model.xml') - data = mujoco.MjData(model) - - # 2. 设置初始关节位置(例如,让肘部稍微弯曲) - data.qpos[model.joint('elbow').id] = -np.radians(45) # -45度 - - # 3. 使用 viewer 进行交互式仿真 - print("正在启动仿真查看器...") - print("提示:") - print(" - 在查看器窗口按 [Space] 暂停/继续物理仿真。") - print(" - 在左侧‘关节’面板,可以拖拽滑块实时驱动电机,观察手臂运动。") - - with mujoco.viewer.launch(model, data) as viewer: - # 设置一个简单的摆动目标 - target_positions = np.array([0.3, 0.1, -0.5, 0.0]) # 对应四个电机的目标位置 - time_per_cycle = 5.0 # 摆动周期(秒) - start_time = time.time() - - # 保持查看器窗口开启 - while viewer.is_running(): - step_start = time.time() - - # 计算基于时间的周期性目标 - t = time.time() - start_time - phase = 2 * np.pi * t / time_per_cycle - current_target = target_positions * np.sin(phase) - - # 将目标位置设置为电机的控制信号(简单的位置控制) - data.ctrl[:] = current_target - - # 执行一步物理仿真 - mujoco.mj_step(model, data) - - # 同步查看器 - viewer.sync() - - # 粗略的时间同步,避免占用过多CPU - time_until_next_step = model.opt.timestep - (time.time() - step_start) - if time_until_next_step > 0: - time.sleep(min(time_until_next_step, model.opt.timestep)) - - print("仿真结束。") - -if __name__ == "__main__": - main() \ No newline at end of file From 9c5563725b5515a84203884fc8bb5f2d45f6478a Mon Sep 17 00:00:00 2001 From: HuangJinjie2887 Date: Mon, 22 Dec 2025 19:11:34 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E6=8C=89=E7=85=A7=E6=9C=80=E7=BB=88?= =?UTF-8?q?=E7=9A=84=E9=A1=B9=E7=9B=AE=E5=86=85=E5=AE=B9=E6=94=B9=E5=86=99?= =?UTF-8?q?readme=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Embodied_Human_Arm_Modeling/README.md | 269 +++++++++++++++++----- 1 file changed, 209 insertions(+), 60 deletions(-) diff --git a/src/Embodied_Human_Arm_Modeling/README.md b/src/Embodied_Human_Arm_Modeling/README.md index 1c875b1653..92d89c64b8 100644 --- a/src/Embodied_Human_Arm_Modeling/README.md +++ b/src/Embodied_Human_Arm_Modeling/README.md @@ -1,96 +1,245 @@ -Shadow Hand 手部动画演示 -项目概述 -这是一个基于 MuJoCo 物理引擎的 Shadow Hand 手部模型动画演示程序。该脚本加载 Shadow Hand 的 MJCF 模型文件,并通过简单的控制指令循环展示三种不同的手部姿态。 - -功能特性 -模型加载:加载 Shadow Hand 的 MJCF 模型文件(left_hand.xml) - -姿态控制:预定义了三种手部姿态: - -张开手:所有关节设置为零位置 - -握拳:模拟握拳动作的关节角度 - -捏取:模拟捏取物体的手部姿态 - -实时动画:每3秒自动切换一种姿态 - -可视化界面:使用 MuJoCo 的被动查看器实时显示手部动作 +Shadow Hand 手部动画演示项目 +📋 项目概述 +本项目基于 Shadow Robot Company 开源的 Shadow Hand E3M5 左手模型(Apache 2.0 License),使用 MuJoCo 物理引擎开发了一个交互式的手部动画演示系统。该系统可以展示多种预设手部姿态,并支持用户通过命令行进行实时控制。 +🚀 快速开始 系统要求 Python 3.8+ -MuJoCo 2.3.0+ +MuJoCo 3.x NumPy -安装与运行 -1. 安装依赖 +安装依赖 bash pip install mujoco numpy -2. 准备模型文件 -确保 left_hand.xml 模型文件与脚本在同一目录下,或修改脚本中的路径指向正确的模型文件。 - -3. 运行程序 +运行演示 bash -python simple_hand_animation.py +python interactive_hand_simulator.py +确保 left_hand.xml 模型文件和 assets/ 目录(包含 OBJ 网格文件)与脚本在同一目录下。 + +🎮 功能特性 +1. 预设手部姿态库 +系统包含 6 种精心设计的手部姿态: + +张开手 🤚 - 所有手指完全展开 -动画循环: +握拳 ✊ - 所有手指完全握紧 -每3秒切换一个预定义姿态 +圆柱体抓握 🫱 - 环绕抓握柱状物体 -实时更新控制指令并同步查看器 +剪刀手 ✌️ - 食指和中指张开呈V形 -使用方法 -运行程序后,MuJoCo 查看器将自动打开 +OK手势 👌 - 拇指和食指形成圆圈 -手部模型将循环展示三种姿态: +指点 👉 - 食指伸直,其他手指握起 -张开手 → 等待3秒 → 握拳 → 等[hand_animation_controller.py](../../../mujoco_menagerie/shadow_hand/hand_animation_controller.py)待3秒 → 捏取 → 等待3秒 → 重复 +2. 交互控制模式 +支持两种控制模式: -按 Ctrl+C 可中断程序并关闭查看器 +自动模式 🤖 - 自动循环播放所有姿态(每姿态保持5秒) -输出信息[hand_animation_controller.py](../../../mujoco_menagerie/shadow_hand/hand_animation_controller.py) -程序运行时会输出以下信息: +手动模式 🎮 - 用户通过命令行控制姿态切换 -✅ 模型加载成功 +3. 动画系统 +平滑过渡动画(1.5秒过渡时间) -📊 关节数: [关节数量] +缓动函数实现自然的运动曲线 -📊 执行器数: [执行器数量] +实时姿态进度显示 -🎭 切换到: [当前姿态名称] +4. 控制命令 +在程序运行期间,可在终端中输入以下命令: -文件结构 +text +pause: 暂停/继续演示 +next: 切换到下一个姿态 +prev: 切换到上一个姿态 +mode: 切换手动/自动模式 +restart:重新开始演示 +help: 显示控制说明 +quit: 退出演示 +📁 文件说明 +核心文件 text . -├── simple_hand_animation.py # 主程序文件 -├── left_hand.xml # Shadow Hand MJCF 模型文件 -└── README.md # 说明文档 -注意事项 -确保 MuJoCo 许可证正确配置 +├── left_hand.xml # Shadow Hand 左手MJCF模型文件 +├── interactive_hand_simulator.py # 主程序 - 交互式手部演示 +└── assets/ # 模型网格文件目录(包含所有.obj文件) +模型结构说明 +left_hand.xml 文件定义了完整的 Shadow Hand 左手模型: + +主要组件: +前臂(lh_forearm) - 包含惯性参数和碰撞几何体 + +手腕(lh_wrist) - 包含两个旋转关节(WRJ1, WRJ2) + +手掌(lh_palm) - 手部基础结构 + +五个手指 - 分别对应索引、中指、无名指、小指和拇指 + +关节配置: +手腕关节:2个自由度(俯仰和横滚) + +拇指关节:5个自由度(THJ1-THJ5) + +其他手指:每个3-4个自由度(包括肌腱耦合) + +执行器系统: +20个位置控制执行器 + +包含肌腱耦合(例如FFJ0同时控制FFJ2和FFJ1) + +💻 代码架构 +HandDemoMujoco3 类 +主程序类,包含以下主要方法: + +初始化方法: +python +__init__(model_path='left_hand.xml') # 加载模型并初始化状态 +姿态创建方法: +python +_create_preset_poses() # 创建6种预设手部姿态 +_create_pose_fist() # 创建握拳姿态 +_create_pose_cylinder() # 创建圆柱体抓握姿态 +_create_pose_scissors() # 创建剪刀手姿态 +_create_pose_ok() # 创建OK手势 +_create_pose_pointing() # 创建指点姿态 +动画控制方法: +python +start_animation(pose_name) # 开始动画到指定姿态 +update_animation() # 更新动画状态 +toggle_pause() # 切换暂停状态 +next_pose() # 切换到下一个姿态 +previous_pose() # 切换到上一个姿态 +toggle_manual_mode() # 切换手动/自动模式 +用户交互方法: +python +process_command(command) # 处理控制台命令 +print_controls() # 打印控制说明 +run_demo() # 运行主演示循环 +多线程设计 +主线程:处理物理仿真和图形渲染 + +输入线程:监听用户命令行输入 + +线程安全:使用锁保护共享状态变量 + +🔧 技术实现细节 +动画插值系统 +python +# 使用缓动函数(ease in-out)实现平滑过渡 +if t < 0.5: + t_eased = 2 * t * t +else: + t_eased = -1 + (4 - 2 * t) * t + +# 线性插值计算 +current_values = start_values + (target_values - start_values) * t_eased +姿态数据适配 +代码根据实际执行器数量自动调整姿态参数: + +支持完整20执行器配置 + +支持简化配置(最少3个执行器) + +容错性设计,避免索引错误 + +📊 运行输出示例 +text +============================================================ +✅ 手部模型加载成功 +📊 执行器数量: 20 +📊 关节数量: 24 +📊 仿真时间步: 0.0020秒 +============================================================ +🎭 创建了 6 种预设姿态 +============================================================ + +🤖 手部抓握姿态全自动演示 (MuJoCo 3.x 兼容版) +============================================================ +🎬 演示序列: 7 个姿态 +⏱️ 每个姿态保持: 5.0秒 +🎥 动画过渡: 1.5秒 + +🎮 控制说明 (在终端中输入命令): + pause: 暂停/继续演示 + next: 下一个姿态 + prev: 上一个姿态 + mode: 切换手动/自动模式 + restart: 重新开始演示 + help: 显示控制说明 + quit: 退出演示 +============================================================ + +演示开始... + +🤚 [张开手 ] 进度: 0.0% - 所有手指完全展开 +🛠️ 故障排除 +常见问题 +找不到模型文件 + +text +❌ 找不到模型文件 'left_hand.xml' +确保 left_hand.xml 和 assets/ 目录与脚本在同一目录下。 + +缺少网格文件 + +text +MuJoCo错误: 无法加载网格文件 +检查 assets/ 目录是否包含所有引用的 .obj 文件。 + +MuJoCo版本不兼容 +确保安装的是 MuJoCo 3.x 版本。 + +📝 扩展开发 +添加新姿态 +要添加新的手部姿态,可在 _create_preset_poses() 方法中添加: + +python +self.poses['新手势'] = { + 'values': self._create_custom_pose(), + 'emoji': '🎯', + 'description': '手势描述' +} +修改动画参数 +可以调整以下参数: + +self.animation_duration - 动画过渡时间 + +hold_duration - 姿态保持时间 + +self.model.opt.timestep - 仿真时间步长 + +📄 许可证说明 +模型文件 +来源:Shadow Robot Company + +许可证:Apache 2.0 License + +原始仓库:https://github.com/shadow-robot/sr_common -模型文件需与脚本在同一目录或指定正确路径 +动画脚本 +作者:项目开发者 -姿态控制值基于原始模型配置,可能需要根据具体模型调整 +用途:学术演示和教育用途 -扩展设想 -基于当前代码,未来可以: +依赖:MuJoCo, NumPy -增加更多预定义手部姿态 +🙏 致谢 +Shadow Robot Company - 提供高质量的开源手部模型 -添加手动控制模式(键盘或GUI) +DeepMind - 开发 MuJoCo 物理引擎 -实现姿态间的平滑过渡动画 +MuJoCo 社区 - 提供技术支持和文档 -记录和回放手部动作序列 +📧 反馈与贡献 +如发现任何问题或有改进建议,请: -添加物理交互对象(如抓取小球) +检查代码注释和错误信息 -致谢 -Shadow Robot Company 提供开源手部模型 +参考 MuJoCo 官方文档 -DeepMind 开发 MuJoCo 物理引擎 +在课程作业仓库中提交问题报告 -许可证 -遵循原始模型的 Apache 2.0 许可证。 \ No newline at end of file +注意:本项目为学术用途,遵循 Apache 2.0 开源许可证。模型版权归 Shadow Robot Company 所有,动画代码为课程作业原创实现。 \ No newline at end of file