diff --git a/src/embodied_robot/README.md b/src/embodied_robot/README.md index 2d82baa853..3a750c11d9 100644 --- a/src/embodied_robot/README.md +++ b/src/embodied_robot/README.md @@ -1,16 +1,17 @@ -具身机器人: +Embodied Robot: -1. 行走: - # 核心库(必须安装) - mujoco==3.3.6 - numpy==2.2.6 +Walking: +Core Libraries (Mandatory Installation) +mujoco==3.3.6 +numpy==2.2.6 +python==3.12 -# 间接依赖(通常自动安装,若缺失则补充) - absl-py==2.3.1 - glfw==2.10.0 - PyOpenGL==3.1.10 - etils==1.13.0 - importlib_resources==6.5.2 - typing_extensions==4.15.0 - zipp==3.23.0 - fsspec==2025.9.0 +Indirect Dependencies (Typically Installed Automatically; Install Manually if Missing) +absl-py==2.3.1 +glfw==2.10.0 +PyOpenGL==3.1.10 +etils==1.13.0 +importlib_resources==6.5.2 +typing_extensions==4.15.0 +zipp==3.23.0 +fsspec==2025.9.0 diff --git a/src/embodied_robot/main.py b/src/embodied_robot/main.py index 6a798d96e8..759bb6b642 100644 --- a/src/embodied_robot/main.py +++ b/src/embodied_robot/main.py @@ -1,84 +1,210 @@ -import mujoco -import time -from mujoco import viewer +# -*- coding: utf-8 -*- # 强制声明文件编码为UTF-8 +# 或简化版: +# coding=utf-8 +import os +import sys +import subprocess +import platform +from pathlib import Path -def main(): - # 定义简单的双轮机器人模型 - robot_xml = """ - - +def setup_environment(): + """ + Initialize runtime environment - adapt to directory structure: + main.py is at the same level as robot_walk, scripts/models in robot_walk subdirectory + """ + # Get project root (directory of main.py) + project_root = Path(__file__).resolve().parent + print(f"📁 Project root directory: {project_root}") + + # Define paths + robot_walk_dir = project_root / "robot_walk" + script_file = robot_walk_dir / "move_straight.py" + model_file = robot_walk_dir / "Robot_move_straight.xml" + + # Check subdirectory existence + if not robot_walk_dir.exists(): + print(f"\n❌ Missing subdirectory: {robot_walk_dir}") + print("📋 Required directory structure:") + print(" embodied_robot/") + print(" ├── main.py") + print(" └── robot_walk/") + print(" ├── move_straight.py") + print(" └── Robot_move_straight.xml") + sys.exit(1) + print(f"✅ Found subdirectory: {robot_walk_dir}") + + # Check file existence + files_to_check = [ + ("Robot control script", script_file), + ("Mujoco model file", model_file) + ] + + missing_files = [] + for file_desc, file_path in files_to_check: + if not file_path.exists(): + missing_files.append(f"{file_desc}: {file_path}") + else: + print(f"✅ {file_desc} found: {file_path}") + + # Handle missing files + if missing_files: + print("\n❌ Missing required files:") + for missing in missing_files: + print(f" - {missing}") + print("\n📋 Ensure robot_walk directory contains:") + print(" 1. move_straight.py (Robot control script)") + print(" 2. Robot_move_straight.xml (Mujoco model file)") + sys.exit(1) + + return project_root, robot_walk_dir, script_file, model_file + + +def get_python_executable(): + """ + Get correct Python interpreter path (priority to virtual environment) + """ + python_exe = sys.executable + print(f"\n🐍 Using Python interpreter: {python_exe}") + + # Verify Python version + try: + version_result = subprocess.run( + [python_exe, "--version"], + capture_output=True, + text=True, + check=True + ) + python_version = version_result.stdout.strip() + print(f"🔍 Python version: {python_version}") + + # Check minimum version (3.8+) + version_parts = python_version.split()[1].split('.') + major = int(version_parts[0]) + minor = int(version_parts[1]) + if major < 3 or (major == 3 and minor < 8): + print("⚠️ Warning: Mujoco recommends Python 3.8+, compatibility issues may occur") + except Exception as e: + print(f"⚠️ Failed to detect Python version: {e}") + + return python_exe + + +def check_dependencies(): + """ + Check required packages installation + """ + required_packages = [ + "mujoco", + "numpy" + ] + + missing_packages = [] + for pkg in required_packages: + try: + __import__(pkg) + print(f"✅ Package {pkg} is installed") + except ImportError: + missing_packages.append(pkg) + + if missing_packages: + print("\n❌ Missing required packages:") + for pkg in missing_packages: + print(f" - {pkg}") + print("\n📦 Install missing packages with:") + print(f" {sys.executable} -m pip install {' '.join(missing_packages)}") + + # Ask for auto-install + if input("\n📥 Auto-install missing packages? (y/n): ").lower() == 'y': + try: + subprocess.run( + [sys.executable, "-m", "pip", "install"] + missing_packages, + check=True + ) + print("✅ Packages installed successfully") + except subprocess.CalledProcessError as e: + print(f"❌ Package installation failed: {e}") + sys.exit(1) + + +def run_robot_simulation(python_exe, robot_walk_dir, script_file): """ + Launch robot simulation script (run in robot_walk directory for correct path resolution) + """ + print("\n🚀 Starting robot patrol simulation...") + print("=" * 50) + + try: + # Set environment variables (no logs) + env = os.environ.copy() + env['MUJOCO_QUIET'] = '1' + env['PYTHONPATH'] = str(Path(__file__).resolve().parent) + os.pathsep + env.get('PYTHONPATH', '') + + # Run script in robot_walk directory + result = subprocess.run( + [python_exe, str(script_file)], + cwd=str(robot_walk_dir), + env=env, + stdout=sys.stdout, + stderr=sys.stderr, + check=True + ) + + print("=" * 50) + print("🏁 Simulation completed successfully") + return result.returncode + + except subprocess.CalledProcessError as e: + print(f"\n❌ Simulation error, return code: {e.returncode}") + return e.returncode + except KeyboardInterrupt: + print("\n🛑 Simulation interrupted by user") + return 0 + except Exception as e: + print(f"\n❌ Unexpected error: {e}") + import traceback + traceback.print_exc() + return 1 - # 加载模型 - model = mujoco.MjModel.from_xml_string(robot_xml) - data = mujoco.MjData(model) - # 初始化可视化器 - viewer_instance = viewer.launch_passive(model, data) +def main(): + """ + Main launcher function + """ - # 设置直行速度(左右轮相同速度) - forward_speed = 1.0 - data.ctrl[0] = forward_speed # 左轮速度 - data.ctrl[1] = forward_speed # 右轮速度 + print("=" * 50) + print("🤖 DeepMind Humanoid Robot Simulation Launcher") + print("📌 Multi-target patrol + Dynamic obstacle avoidance") + print("=" * 50) - # 运行仿真10秒 - total_time = 10.0 - steps = int(total_time / model.opt.timestep) + # 1. Setup environment + try: + project_root, robot_walk_dir, script_file, model_file = setup_environment() + except Exception as e: + print(f"\n❌ Environment initialization failed: {e}") + sys.exit(1) - start_time = time.time() - for _ in range(steps): - # 执行仿真步 - mujoco.mj_step(model, data) + # 2. Get Python executable + python_exe = get_python_executable() - # 更新可视化 - viewer_instance.sync() + # 3. Check dependencies + print("\n🔍 Checking dependencies...") + check_dependencies() - # 维持实时速率 - elapsed = time.time() - start_time - expected = _ * model.opt.timestep - if elapsed < expected: - time.sleep(expected - elapsed) + # 4. Run simulation + exit_code = run_robot_simulation(python_exe, robot_walk_dir, script_file) - print(f"仿真完成,机器人最终位置: {data.body('base').xpos}") - viewer_instance.close() + # 5. Exit + sys.exit(exit_code) if __name__ == "__main__": - main() - - - + # Fix Windows encoding issues + if platform.system() == "Windows": + try: + os.system("chcp 65001 > nul") + except: + pass + + # Launch main program + main() \ No newline at end of file diff --git a/src/embodied_robot/robot_walk/Robot_move_straight.xml b/src/embodied_robot/robot_walk/Robot_move_straight.xml index aa65f0e3a4..585472c4b8 100644 --- a/src/embodied_robot/robot_walk/Robot_move_straight.xml +++ b/src/embodied_robot/robot_walk/Robot_move_straight.xml @@ -1,333 +1,324 @@ - - - -