Skip to content
Closed
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
Binary file modified .gitignore
Binary file not shown.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ python-markdown-math
imageio
numpy
matplotlib
tensorflow
tensorflow
mujoco>=2.3.0
112 changes: 112 additions & 0 deletions src/drone_simulation/main.py
Original file line number Diff line number Diff line change
@@ -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 = """
<mujoco>
<visual>
<global azimuth="45" elevation="-30"/>
</visual>

<worldbody>
<!-- 明亮的背景 -->
<light name="top" pos="0 0 10" dir="0 0 -1" directional="true" diffuse="1 1 1"/>

<!-- 彩色地面 -->
<geom name="ground" type="plane" pos="0 0 0" size="5 5 0.1" rgba="0.6 0.8 0.6 1"/>

<!-- 大号彩色无人机 -->
<body name="drone" pos="0 0 1">
<freejoint/>

<!-- 中心大立方体 -->
<geom name="body" type="box" size="0.25 0.25 0.05" rgba="1 0.5 0 1" mass="1.0"/>

<!-- 四个大旋翼(更容易看到) -->
<geom name="rotor1" type="cylinder" pos="0.5 0.5 0" size="0.2 0.02" rgba="1 0 0 1" mass="0.2"/>
<geom name="rotor2" type="cylinder" pos="0.5 -0.5 0" size="0.2 0.02" rgba="0 1 0 1" mass="0.2"/>
<geom name="rotor3" type="cylinder" pos="-0.5 -0.5 0" size="0.2 0.02" rgba="0 0 1 1" mass="0.2"/>
<geom name="rotor4" type="cylinder" pos="-0.5 0.5 0" size="0.2 0.02" rgba="1 1 0 1" mass="0.2"/>

<!-- 连接臂 -->
<geom name="arm1" type="capsule" fromto="0 0 0 0.5 0.5 0" size="0.03" rgba="0 0 0 1"/>
<geom name="arm2" type="capsule" fromto="0 0 0 0.5 -0.5 0" size="0.03" rgba="0 0 0 1"/>
<geom name="arm3" type="capsule" fromto="0 0 0 -0.5 -0.5 0" size="0.03" rgba="0 0 0 1"/>
<geom name="arm4" type="capsule" fromto="0 0 0 -0.5 0.5 0" size="0.03" rgba="0 0 0 1"/>
</body>
</worldbody>
</mujoco>
"""


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()
164 changes: 164 additions & 0 deletions src/drone_simulation/model.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?xml version="1.0" ?>
<mujoco model="quadrotor">

<!-- 仿真选项 -->
<option timestep="0.005" iterations="50" tolerance="1e-10" cone="elliptic">
<flag contact="enable" energy="enable"/>
</option>

<!-- 物理参数 -->
<size nconmax="100" njmax="200"/>

<!-- 资产定义 -->
<asset>
<!-- 纹理 -->
<texture name="skybox" type="skybox" builtin="gradient" rgb1="0.6 0.8 1.0" rgb2="0.2 0.3 0.6"/>
<texture name="ground" type="2d" builtin="checker" rgb1="0.8 0.9 0.8" rgb2="0.6 0.8 0.6" width="512" height="512"/>
<texture name="metal" type="2d" builtin="flat" rgb1="0.5 0.5 0.5" rgb2="0.1 0.1 0.1"/>
<texture name="propeller" type="2d" builtin="flat" rgb1="0.9 0.2 0.2" rgb2="0.5 0.1 0.1"/>

<!-- 材质 -->
<material name="sky" texture="skybox" texuniform="true"/>
<material name="ground_mat" texture="ground" texuniform="true" texrepeat="10 10"/>
<material name="metal_mat" texture="metal" shininess="0.5" specular="0.5"/>
<material name="propeller_mat" texture="propeller" shininess="0.3" specular="0.2"/>
<material name="red" rgba="0.8 0.2 0.2 1"/>
<material name="green" rgba="0.2 0.8 0.2 1"/>
<material name="blue" rgba="0.2 0.2 0.8 1"/>
<material name="black" rgba="0.1 0.1 0.1 1"/>
<material name="white" rgba="1 1 1 1"/>

<!-- 网格 -->
<mesh name="quad_body" file="quad_body.stl" scale="0.01 0.01 0.01"/>
<mesh name="propeller_blade" file="propeller_blade.stl" scale="0.01 0.01 0.01"/>
</asset>

<!-- 世界定义 -->
<worldbody>
<!-- 光源 -->
<light name="top_light" pos="0 0 10" dir="0 0 -1" directional="true" diffuse="0.8 0.8 0.8"/>
<light name="front_light" pos="5 0 5" dir="-1 0 -1" directional="true" diffuse="0.5 0.5 0.5"/>

<!-- 天空和地面 -->
<geom name="sky" type="plane" pos="0 0 0" size="0 0 0" material="sky"/>
<geom name="ground" type="plane" pos="0 0 0" size="20 20 0.1" material="ground_mat" condim="3" friction="1 0.005 0.0001"/>

<!-- 参考坐标系 -->
<geom name="origin_x" type="cylinder" fromto="0 0 0.1 1 0 0.1" size="0.01" rgba="1 0 0 1"/>
<geom name="origin_y" type="cylinder" fromto="0 0 0.1 0 1 0.1" size="0.01" rgba="0 1 0 1"/>
<geom name="origin_z" type="cylinder" fromto="0 0 0.1 0 0 1.1" size="0.01" rgba="0 0 1 1"/>

<!-- 四旋翼无人机主体 -->
<body name="quadrotor" pos="0 0 1.5" euler="0 0 0">
<!-- 自由关节 (6自由度) -->
<freejoint name="quad_free_joint"/>

<!-- 主体框架 -->
<geom name="center_body" type="cylinder" size="0.1 0.02" rgba="0.3 0.3 0.3 1" mass="0.3"/>
<geom name="arm_front_right" type="capsule" fromto="0 0 0 0.25 0.25 0" size="0.008" rgba="0.1 0.1 0.1 1" mass="0.05"/>
<geom name="arm_front_left" type="capsule" fromto="0 0 0 0.25 -0.25 0" size="0.008" rgba="0.1 0.1 0.1 1" mass="0.05"/>
<geom name="arm_back_left" type="capsule" fromto="0 0 0 -0.25 -0.25 0" size="0.008" rgba="0.1 0.1 0.1 1" mass="0.05"/>
<geom name="arm_back_right" type="capsule" fromto="0 0 0 -0.25 0.25 0" size="0.008" rgba="0.1 0.1 0.1 1" mass="0.05"/>

<!-- 电机和旋翼 (前右) -->
<body name="motor_front_right" pos="0.25 0.25 0">
<geom name="motor_housing_front_right" type="cylinder" size="0.025 0.03" rgba="0.2 0.2 0.2 1" mass="0.05"/>

<body name="rotor_front_right" pos="0 0 0.05">
<joint name="rotor_front_right_joint" type="hinge" axis="0 0 1" limited="false"/>
<geom name="propeller_front_right" type="cylinder" size="0.12 0.005" rgba="0.8 0.2 0.2 0.8" mass="0.02"/>
</body>
</body>

<!-- 电机和旋翼 (前左) -->
<body name="motor_front_left" pos="0.25 -0.25 0">
<geom name="motor_housing_front_left" type="cylinder" size="0.025 0.03" rgba="0.2 0.2 0.2 1" mass="0.05"/>

<body name="rotor_front_left" pos="0 0 0.05">
<joint name="rotor_front_left_joint" type="hinge" axis="0 0 1" limited="false"/>
<geom name="propeller_front_left" type="cylinder" size="0.12 0.005" rgba="0.2 0.8 0.2 0.8" mass="0.02"/>
</body>
</body>

<!-- 电机和旋翼 (后左) -->
<body name="motor_back_left" pos="-0.25 -0.25 0">
<geom name="motor_housing_back_left" type="cylinder" size="0.025 0.03" rgba="0.2 0.2 0.2 1" mass="0.05"/>

<body name="rotor_back_left" pos="0 0 0.05">
<joint name="rotor_back_left_joint" type="hinge" axis="0 0 1" limited="false"/>
<geom name="propeller_back_left" type="cylinder" size="0.12 0.005" rgba="0.8 0.2 0.2 0.8" mass="0.02"/>
</body>
</body>

<!-- 电机和旋翼 (后右) -->
<body name="motor_back_right" pos="-0.25 0.25 0">
<geom name="motor_housing_back_right" type="cylinder" size="0.025 0.03" rgba="0.2 0.2 0.2 1" mass="0.05"/>

<body name="rotor_back_right" pos="0 0 0.05">
<joint name="rotor_back_right_joint" type="hinge" axis="0 0 1" limited="false"/>
<geom name="propeller_back_right" type="cylinder" size="0.12 0.005" rgba="0.2 0.8 0.2 0.8" mass="0.02"/>
</body>
</body>

<!-- 起落架 -->
<geom name="landing_gear_front" type="cylinder" pos="0.15 0 0" size="0.005 0.05" rgba="0.5 0.5 0.5 1" mass="0.01"/>
<geom name="landing_gear_back" type="cylinder" pos="-0.15 0 0" size="0.005 0.05" rgba="0.5 0.5 0.5 1" mass="0.01"/>

<!-- 视觉标记 (用于方向识别) -->
<geom name="front_marker" type="sphere" pos="0.15 0 0.02" size="0.015" rgba="1 1 0 1"/>
<geom name="rear_marker" type="sphere" pos="-0.15 0 0.02" size="0.015" rgba="0 1 1 1"/>
</body>

<!-- 障碍物 (可选) -->
<body name="obstacle1" pos="2 0 1">
<geom name="obs1" type="cylinder" size="0.3 1" rgba="0.7 0.7 0.9 0.7"/>
</body>

<body name="obstacle2" pos="-2 0 1">
<geom name="obs2" type="box" size="0.5 0.5 1" rgba="0.9 0.7 0.7 0.7"/>
</body>

<!-- 目标点 -->
<body name="target" pos="0 3 2">
<geom name="target_sphere" type="sphere" size="0.1" rgba="1 0 0 0.5" contype="0" conaffinity="0"/>
</body>

</worldbody>

<!-- 执行器定义 -->
<actuator>
<!-- 电机控制 -->
<motor name="motor_front_right" joint="rotor_front_right_joint" gear="50" ctrllimited="true" ctrlrange="0 1000"/>
<motor name="motor_front_left" joint="rotor_front_left_joint" gear="50" ctrllimited="true" ctrlrange="0 1000"/>
<motor name="motor_back_left" joint="rotor_back_left_joint" gear="50" ctrllimited="true" ctrlrange="0 1000"/>
<motor name="motor_back_right" joint="rotor_back_right_joint" gear="50" ctrllimited="true" ctrlrange="0 1000"/>
</actuator>

<!-- 传感器定义 -->
<sensor>
<!-- 位置传感器 -->
<framepos name="quad_position" objtype="body" objname="quadrotor"/>

<!-- 姿态传感器 (四元数) -->
<framequat name="quad_orientation" objtype="body" objname="quadrotor"/>

<!-- 线速度传感器 -->
<framelinvel name="quad_linear_velocity" objtype="body" objname="quadrotor"/>

<!-- 角速度传感器 -->
<frameangvel name="quad_angular_velocity" objtype="body" objname="quadrotor"/>

<!-- 线加速度传感器 -->
<framelinacc name="quad_linear_acceleration" objtype="body" objname="quadrotor"/>

<!-- 旋翼转速传感器 -->
<jointpos name="rotor_speed_front_right" joint="rotor_front_right_joint"/>
<jointpos name="rotor_speed_front_left" joint="rotor_front_left_joint"/>
<jointpos name="rotor_speed_back_left" joint="rotor_back_left_joint"/>
<jointpos name="rotor_speed_back_right" joint="rotor_back_right_joint"/>

<!-- 接触传感器 -->
<touch name="touch_sensor" site="quadrotor_center"/>
</sensor>

</mujoco>
Loading