From ba41358428988a28880d35241ae8702f7698b4e9 Mon Sep 17 00:00:00 2001 From: ZKX34 <3442977470@qq.com> Date: Wed, 24 Dec 2025 21:43:10 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E6=97=A0=E4=BA=BA=E6=9C=BA?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drone_simulation/main.py | 112 +++++++++++++++++++++ src/drone_simulation/mjmodel.xml | 52 ++++++++++ src/drone_simulation/model.xml | 164 +++++++++++++++++++++++++++++++ 3 files changed, 328 insertions(+) create mode 100644 src/drone_simulation/main.py create mode 100644 src/drone_simulation/mjmodel.xml create mode 100644 src/drone_simulation/model.xml diff --git a/src/drone_simulation/main.py b/src/drone_simulation/main.py new file mode 100644 index 0000000000..78f334b53d --- /dev/null +++ b/src/drone_simulation/main.py @@ -0,0 +1,112 @@ +# simple_visible_drone.py +import mujoco +import mujoco.viewer +import numpy as np +import time +import os +import glob + +# 定义日志文件路径(可根据实际情况修改) +LOG_FILE_PATHS = [ + "mujoco.log", + "drone_simulation.log", + "./logs/*.log" +] + + +def delete_log_files(): + """删除指定的日志文件(无控制台输出)""" + deleted_count = 0 + for path in LOG_FILE_PATHS: + if "*" in path: + for file in glob.glob(path): + try: + os.remove(file) + deleted_count += 1 + except (FileNotFoundError, PermissionError): + continue + else: + if os.path.exists(path): + try: + os.remove(path) + deleted_count += 1 + except (FileNotFoundError, PermissionError): + continue + + +# 最小化但可见的模型 +MJCF_MODEL = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +""" + + +def main(): + # 删除日志文件(无输出) + delete_log_files() + + # 加载模型和数据 + model = mujoco.MjModel.from_xml_string(MJCF_MODEL) + data = mujoco.MjData(model) + + # 等待3秒(无提示) + time.sleep(3) + + with mujoco.viewer.launch_passive(model, data) as viewer: + # 设置相机视角 + viewer.cam.lookat[:] = [0, 0, 1] + viewer.cam.distance = 8.0 + viewer.cam.azimuth = 45 + viewer.cam.elevation = -30 + + t = 0 + while viewer.is_running() and t < 20: + # 简单的上下浮动 + force_z = 20 * np.sin(t * 2) + 50 + + # 应用力 + data.qfrc_applied[2] = force_z + + # 缓慢旋转 + data.qfrc_applied[5] = 5 * np.sin(t * 0.5) + + mujoco.mj_step(model, data) + viewer.sync() + + t += 0.01 + time.sleep(0.01) + + +if __name__ == "__main__": + main() diff --git a/src/drone_simulation/mjmodel.xml b/src/drone_simulation/mjmodel.xml new file mode 100644 index 0000000000..b86af68bb9 --- /dev/null +++ b/src/drone_simulation/mjmodel.xml @@ -0,0 +1,52 @@ + + + + \ No newline at end of file diff --git a/src/drone_simulation/model.xml b/src/drone_simulation/model.xml new file mode 100644 index 0000000000..717a287e45 --- /dev/null +++ b/src/drone_simulation/model.xml @@ -0,0 +1,164 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file