From 13b6d413fec00bb09d704ce494e8b2e381d72382 Mon Sep 17 00:00:00 2001 From: lijinghan12 <15894570343@163.com> Date: Sat, 20 Dec 2025 13:05:02 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=94=A8=E6=8C=89=E9=94=AE=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E4=B8=89=E6=8C=87=E6=9C=BA=E6=A2=B0=E8=87=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mechanical_arm/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mechanical_arm/main.py b/src/mechanical_arm/main.py index 8750bf5f82..6199a351e7 100644 --- a/src/mechanical_arm/main.py +++ b/src/mechanical_arm/main.py @@ -435,4 +435,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main()#结束过程 \ No newline at end of file From fdf3af658294bab3aff2f383efbbe5fb8a452b8c Mon Sep 17 00:00:00 2001 From: lijinghan12 <15894570343@163.com> Date: Sat, 20 Dec 2025 20:43:15 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=94=A8mujoco=E8=BF=9B=E8=A1=8C=E6=9C=BA?= =?UTF-8?q?=E6=A2=B0=E8=87=82=E7=9A=84=E5=88=9D=E6=AD=A5=E4=BB=BF=E7=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mechanical_arm/model_test.py | 64 ++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/mechanical_arm/model_test.py diff --git a/src/mechanical_arm/model_test.py b/src/mechanical_arm/model_test.py new file mode 100644 index 0000000000..8f463605cc --- /dev/null +++ b/src/mechanical_arm/model_test.py @@ -0,0 +1,64 @@ +import mujoco +import time +import numpy as np +import os + +try: + # 尝试使用较新版本的mujoco.viewer + import mujoco.viewer + + use_new_viewer = True +except ImportError: + # 如果没有mujoco.viewer,则使用glfw_viewer(如果可用) + try: + from mujoco import glfw_viewer + + use_new_viewer = False + except ImportError: + # 如果都不可用,给出提示 + print("未找到可用的可视化模块,请确认安装了正确的MuJoCo版本") + exit(1) + +# 读取模型文件内容 +model_path = os.path.join(os.path.dirname(__file__), "机械臂.xml") +with open(model_path, 'r', encoding='utf-8') as f: + model_xml = f.read() + +# 从字符串加载模型 +model = mujoco.MjModel.from_xml_string(model_xml) +data = mujoco.MjData(model) + +# 设置到home位置 +data.qpos[:] = [0, 0.785398, 0, -1.5708, 0, 0, 0] +mujoco.mj_forward(model, data) + +if use_new_viewer: + # 使用新版本的viewer + with mujoco.viewer.launch_passive(model, data) as viewer: + print("按ESC键退出") + while viewer.is_running(): + step_start = time.time() + + mujoco.mj_step(model, data) + viewer.sync() + + # 控制步频 + time_until_next_step = model.opt.timestep - (time.time() - step_start) + if time_until_next_step > 0: + time.sleep(time_until_next_step) +else: + # 使用glfw_viewer + viewer = glfw_viewer.GlfwViewer(model, data) + print("按ESC键退出") + while viewer.is_running(): + step_start = time.time() + + mujoco.mj_step(model, data) + viewer.render() + + # 控制步频 + time_until_next_step = model.opt.timestep - (time.time() - step_start) + if time_until_next_step > 0: + time.sleep(time_until_next_step) + + viewer.close() \ No newline at end of file From c5e0debddcf5563fdf93088e3b9d8904e21327ae Mon Sep 17 00:00:00 2001 From: lijinghan12 <15894570343@163.com> Date: Sun, 21 Dec 2025 10:35:01 +0800 Subject: [PATCH 3/3] =?UTF-8?q?mujoco=E4=BB=BF=E7=9C=9F=E6=9C=BA=E6=A2=B0?= =?UTF-8?q?=E8=87=82=E8=BF=9B=E8=A1=8C=E8=A7=84=E5=BE=8B=E8=BF=90=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mechanical_arm/motion_test.py | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/mechanical_arm/motion_test.py diff --git a/src/mechanical_arm/motion_test.py b/src/mechanical_arm/motion_test.py new file mode 100644 index 0000000000..554b2d1225 --- /dev/null +++ b/src/mechanical_arm/motion_test.py @@ -0,0 +1,82 @@ +import mujoco +import time +import numpy as np +import os + +try: + # 尝试使用较新版本的mujoco.viewer + import mujoco.viewer + + use_new_viewer = True +except ImportError: + # 如果没有mujoco.viewer,则使用glfw_viewer(如果可用) + try: + from mujoco import glfw_viewer + + use_new_viewer = False + except ImportError: + # 如果都不可用,给出提示 + print("未找到可用的可视化模块,请确认安装了正确的MuJoCo版本") + exit(1) + +# 读取模型文件内容 +model_path = os.path.join(os.path.dirname(__file__), "arm.xml") +with open(model_path, 'r', encoding='utf-8') as f: + model_xml = f.read() + +# 从字符串加载模型 +model = mujoco.MjModel.from_xml_string(model_xml) +data = mujoco.MjData(model) + +# 设置到home位置 +data.qpos[:] = [0, 0.785398, 0, -1.5708, 0, 0, 0] +mujoco.mj_forward(model, data) + + +def move_arm_in_wave(data, t): + """生成波动运动轨迹""" + # 基础正弦波运动 + for i in range(7): + data.ctrl[i] = np.sin(t + i * np.pi / 4) * 0.5 + + +if use_new_viewer: + # 使用新版本的viewer + with mujoco.viewer.launch_passive(model, data) as viewer: + print("按ESC键退出") + t = 0 + while viewer.is_running(): + step_start = time.time() + + # 更新控制信号,使机械臂运动 + move_arm_in_wave(data, t) + t += 0.01 + + mujoco.mj_step(model, data) + viewer.sync() + + # 控制步频 + time_until_next_step = model.opt.timestep - (time.time() - step_start) + if time_until_next_step > 0: + time.sleep(time_until_next_step) +else: + # 使用glfw_viewer + viewer = glfw_viewer.GlfwViewer(model, data) + print("按ESC键退出") + t = 0 + while viewer.is_running(): + step_start = time.time() + + # 更新控制信号,使机械臂运动 + move_arm_in_wave(data, t) + t += 0.01 + + mujoco.mj_step(model, data) + viewer.render() + + # 控制步频 + time_until_next_step = model.opt.timestep - (time.time() - step_start) + if time_until_next_step > 0: + time.sleep(time_until_next_step) + + viewer.close() \ No newline at end of file