Mujoco搭建自己的仿真模型(xacro/urdf转xml)
·
目录
1、xacro转urdf
因为ros的更新换代,许多机械臂的功能包不再提供urdf文件,而给的是xacro文件,因此第一步是将xacro转为urdf。
xacro转urdf时,xacro文件robot标签下添加
<mujoco>
<compiler
meshdir="../meshes_mujoco/"
balanceinertia="true"
discardvisual="false" />
</mujoco>
其中../meshes_mujoco改为自己的mesh绝对文件夹路径,mesh文件夹需要和要转的xacro在同一个文件夹下
运行命令:
rosrun xacro xacro ~/catkin_ws/src/universal_robot/ur_description/urdf/ur5_robot.urdf.xacro > ~/catkin_ws/src/mujoco/ur5_robot.urdf
2、urdf转xml
修改urdf文件
将dae文件后缀改为stl(不支持dae文件格式)
<mesh filename="file:///home/lyx/catkin_ws/src/mujoco/ur5/mesh/visual/base.stl"/>
转换后的urdf文件会多一行 --便于mujoco定位(mesh位置) -- stl模型全部都需要在该文件下
<?xml version="1.0" ?>
<!-- =================================================================================== -->
<!-- | This document was autogenerated by xacro from /home/lyx/catkin_ws/src/universal_robot/ur_description/urdf/ur5_robot.urdf.xacro | -->
<!-- | EDITING THIS FILE BY HAND IS NOT RECOMMENDED | -->
<!-- =================================================================================== -->
<robot name="ur5">
<mujoco>
<compiler balanceinertia="true" discardvisual="false" meshdir="/home/lyx/catkin_ws/src/mujoco/ur5/mesh/"/>
</mujoco>
...
</robot>
利用mujoco模块将urdf转为xml
import mujoco
model = mujoco.MjModel.from_xml_path("/home/lyx/catkin_ws/src/mujoco/ur5/urdf/ur5_robot.urdf")
mujoco.mj_saveLastXML("/home/lyx/catkin_ws/src/mujoco/ur5_robot.xml",model)
3、简单测试
import math
import time
import mujoco
import mujoco.viewer
import os
# 模型路径
model_path = '/home/lyx/catkin_ws/src/mujoco/ur5/ur5_robot.xml' # 更新为你的模型文件路径
# 加载模型
model = mujoco.MjModel.from_xml_path(model_path)
# 创建仿真数据
data = mujoco.MjData(model)
if __name__ == "__main__":
# 创建仿真和可视化器
with mujoco.viewer.launch_passive(model, data) as viewer:
duration = 5
start_time = time.time()
current_time = 0
while current_time <= duration:
mujoco.mj_step(model, data) # 仿真步进
viewer.sync() # 图像同步
time.sleep(0.01)
current_time = time.time() - start_time
4、将关节由自由关节转换为非自由关节 (添加执行器)
转换后的xml文件执行,出现机械臂随意摆动,因为此时的关节为自由关节
xml添加执行器
<option integrator="implicitfast"/>
<default>
<default class="large_actuator">
<position kp="2000" kv="100" forcerange="-105 105"/>
</default>
<default class="small_actuator">
<position kp="500" kv="50" forcerange="-52 52"/>
</default>
<site size="0.001" rgba="0.5 0.5 0.5 0.3" group="4"/>
</default>
<asset>
...
</asset>
<worldbody>
...
</worldbody>
<actuator>
<position class="large_actuator" name="shoulder_pan_joint" joint="shoulder_pan_joint"/>
<position class="large_actuator" name="shoulder_lift_joint" joint="shoulder_lift_joint" ctrlrange="-2.2497294058206907 2.2497294058206907"/>
<position class="large_actuator" name="elbow_joint" joint="elbow_joint"/>
<position class="large_actuator" name="wrist_1_joint" joint="wrist_1_joint" ctrlrange="-2.5795966344476193 2.5795966344476193"/>
<position class="small_actuator" name="wrist_2_joint" joint="wrist_2_joint"/>
<position class="small_actuator" name="wrist_3_joint" joint="wrist_3_joint" ctrlrange="-2.0996310901491784 2.0996310901491784"/>
</actuator>
<keyframe>
<key name="home" qpos="0 0.26179939 3.14159265 -2.26892803 0 1.57079633"
ctrl="0 0.26179939 3.14159265 -2.26892803 0 1.57079633"/>
<key name="retract" qpos="0 -0.34906585 3.14159265 -2.54818071 0 1.57079633"
ctrl="0 -0.34906585 3.14159265 -2.54818071 0 1.57079633"/>
</keyframe>
简单搭建自己的仿真模型成功
更多推荐

所有评论(0)