Mujoco 对闭链机器人建模

Mujoco 对闭链机器人建模

MuJoCo是目前机器人强化学习中最流行的仿真器,官方论坛提供了一些常见机器人的模型,但是如果其中没有自己需要的机器人模型,就只能自己建一个了。本文主要记录一下如何建立MuJoCo闭链机器人模型。

1、闭链机器人参数

参数如下:
在这里插入图片描述
在这里插入图片描述
运动情况:
蓝色:腿和驱动旋转;
绿色:腿和交点旋转;
黄色:腿和交点滑动;
红色:驱动动力;
暂时无法在文档外展示此内容

参数:

​ d= 67.5 mm;r= 15 mm;l= 1000 mm;

​ M= 10 kg;

2、Mujoco建模

MuJoCo最主要的参考资料就是官方文档了,建模部分主要参考ModelingXML ReferenceMuJoCo的模型有三种:xx.mjb(二进制文件), xx.txt, xx.xml,我们常用的还是xml格式的,便于阅读和修改。

XML主要分为以下四个部分:

  • <option>:设置求解器相关的参数

  • <worldbody><body>tag定义了所有的模拟器组件,包括灯光、地板以及你的机器人;

  • <equality>: equality constraints,构建闭环的关键;

  • <acutator>定义可以执行运动的关节。定义的顺序需要按照运动学顺序来。

    具体细节如下所示:

2.1 mujoco the top-level element

<mujoco model='xz'>

</mujoco>

2.2 设置求解器相关的参数

​ 如果未在 XML 文件中指定其值,则使用默认值。其主要包括对仿真时间步长的设计,选择算法并设置其参数,启用和禁用仿真管道的不同部分,以及调整系统级物理属性(如重力)。

    <option timestep="0.001" integrator="RK4" gravity='0 0 0'>
		<flag sensornoise="enable" contact="disable" energy="enable"/>
	</option>
  • timestep :仿真时间步长,即每一步的时间;
  • integrator : 积分器类型;
  • gravity : 重力(系统级物理属性)

2.3 建模模型的主体部分

    <!-- 定义所有模拟器组件 -->
	<worldbody>
         <!设置环境的灯光     The diffuse component of the headlight、position、direction>
        <light diffuse=".5 .5 .5" pos="0 0 3" dir="0 0 -1"/>
        <! 设置地板   类型 、大小  颜色>
		<geom type="plane" size="10 10 0.1" rgba=".7 .7 .7 1"/>
       
         <!-- 构建串联机器人 -->
         <! body设置>
        <! 主体  >
        <body name='base' pos="0 0 5" euler="0 0 0">
               <! it could contain many sub-bodies >
			<geom type="box" size="0.2 0.6 0.2" rgba="0 .9 0 1" />
			<inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
            <!-- 在这里嵌套下一个关节-->
            
              <! 右侧旋转轴>
			<body name="right_motor" pos="0 0.66 -0.1" euler="-90 0 0">
                   <!-- name: 关节名,pos: 与上一个实体的偏移量 -->
				<joint name="right_motor" type="hinge" pos="0 0 0" axis="0 0 1" />
                  <!-- 定义其几何特性。 -->
				<geom type="cylinder" size="0.05 0.05" rgba="0 0 0.9 1" />
                  <!-- 定义实体的惯性。如果不写inertial其惯性会从geom中推断出来 -->
				<inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
			</body>
             
            <! 右侧腿1>
			<body pos="0 0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_right1_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_right1" pos="0 0 0" euler="0 26.57 0">
                    <joint name="leg_right1_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>

             <! 右侧腿2>
            <body pos="0 0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_right2_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_right2" pos="0 0 0" euler="0 -26.57 0">
                    <joint name="leg_right2_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>

             <! 左侧旋转轴>
			<body name="left_motor" pos="0 -0.66 -0.1" euler="-90 0 0">
				<joint name="left_motor" type="hinge" pos="0 0 0" axis="0 0 1" />
				<geom type="cylinder" size="0.05 0.05" rgba="0 0 0.9 1" />
				<inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
			</body>

            <! 左侧腿1>
            <body pos="0 -0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_left1_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_left1" pos="0 0 0" euler="0 26.57 0">
                    <joint name="leg_left1_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>

            <! 左侧腿2>
            <body pos="0 -0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_left2_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_left2" pos="0 0 0" euler="0 -26.57 0">
                    <joint name="leg_left2_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>
        </body>
	</worldbody>

  • euler: 围绕三个坐标轴的旋转角度。 这些旋转所围绕的轴的顺序由编译器的 eulerseq 属性确定,并且对于整个模型都是相同的。

  • diaginertia:对角惯性矩阵,表示物体相对于惯性系的惯性。 如果省略此属性,则需要下一个属性。

  • joint type: free, ball, slide, hinge

  • 如果不需要引用的话, body, geom, site的name可以不用指定。 如果在后续需要使用,必须要设置name。

  • 可以设置多个相同层次的body

  • 一个body内可以设置多个joint,甚至可以在同约束方向设置两个joint,两个是独立的

  • 其余属性名称及含义见MuJoCo官方给出的XML API

    MuJoCo官方XML API https://mujoco.readthedocs.io/en/latest/modeling.html#CComposite

2.4 Equality constraints(构建闭环的关键)

Equality constraints可以施加超出运动学树结构和其中定义的关节/自由度已经施加的额外约束。 它们可用于创建环形接头,或一般模型机械耦合。 执行这些约束的内力与所有其他约束力一起计算。 可用的equality constraints类型有: 在一个点连接两个物体(在运动树之外创建一个球窝接头); 将两个主体焊接在一起; 使两个表面相互滑动; 固定关节或肌腱的位置; 通过三次多项式耦合两个关节或两个肌腱的位置。

<equality>
		<connect body1='right_motor'  body2='leg_right1'        anchor='-0.1 0 0'/>
    	<connect body1='right_motor'  body2='leg_right2'        anchor='0.1 0 0'/>
        <connect body1='left_motor'  body2='leg_left1'        anchor='-0.1 0 0'/>
    	<connect body1='left_motor'  body2='leg_left2'        anchor='0.1 0 0'/>
</equality>
  • connect: 此元素创建一个等式约束,将两个实体连接到一个点。
  • body1: 参与约束的第一个实体的名称。
  • body2: 参与约束的第二个实体的名称。
  • anchor: Coordinates of the 3-D anchor point where the two bodies are connected.

2.5 设置驱动

Mujocoauctor有4种模式:motor模式 、 位置模式 、速度模式以及肌肉模式

​ 此处使用的是motor模式。

	  <!-- 定义关节上的执行器 -->
    <actuator>
        <motor name="torque1" joint="right_motor" gear="1" ctrlrange="-1 1" ctrllimited="true"/>
        <motor name="torque2" joint="left_motor" gear="1" ctrlrange="-1 1" ctrllimited="true"/>
    </actuator>
  • gear: This attribute scales the length (and consequently moment arms, velocity and force) of the actuator, for all transmission types. It is different from the gain in the force generation mechanism, because the gain only scales the force output and does not affect the length, moment arms and velocity. For actuators with scalar transmission, only the first element of this vector is used.
  • ctrlrange: Range for clamping the control input. The compiler expects the first value to be smaller than the second value.
  • ctrllimited: If true, the control input to this actuator is automatically clamped to ctrlrange at runtime. If false, control input clamping is disabled. Note that control input clamping can also be globally disabled with the clampctrl attribute of option/ flag.

2.6 仿真结果

在这里插入图片描述

3、完整mjfc文件

<mujoco model='xz'>
    <option timestep="0.001" integrator="RK4" gravity='0 0 0'>
		<flag sensornoise="enable" contact="disable" energy="enable"/>
	</option>
    <worldbody>
    <light diffuse=".5 .5 .5" pos="0 0 3" dir="0 0 -1"/>
		<geom type="plane" size="10 10 0.1" rgba=".7 .7 .7 1"/>

        <body name='base' pos="0 0 5" euler="0 0 0">
			<geom type="box" size="0.2 0.6 0.2" rgba="0 .9 0 1" />
			<inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>

			<body name="right_motor" pos="0 0.66 -0.1" euler="-90 0 0">
				<joint name="right_motor" type="hinge" pos="0 0 0" axis="0 0 1" />
				<geom type="cylinder" size="0.05 0.05" rgba="0 0 0.9 1" />
				<inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
			</body>

			<body pos="0 0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_right1_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_right1" pos="0 0 0" euler="0 26.57 0">
                    <joint name="leg_right1_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>

            <body pos="0 0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_right2_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_right2" pos="0 0 0" euler="0 -26.57 0">
                    <joint name="leg_right2_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>

			<body name="left_motor" pos="0 -0.66 -0.1" euler="-90 0 0">
				<joint name="left_motor" type="hinge" pos="0 0 0" axis="0 0 1" />
				<geom type="cylinder" size="0.05 0.05" rgba="0 0 0.9 1" />
				<inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
			</body>

            <body pos="0 -0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_left1_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_left1" pos="0 0 0" euler="0 26.57 0">
                    <joint name="leg_left1_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>

            <body pos="0 -0.75 0.1" euler="0 0 0">
				<inertial mass="0.1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                <joint name="leg_left2_y" type="hinge" pos="0 0 0" axis="0 1 0"/>
                <body name="leg_left2" pos="0 0 0" euler="0 -26.57 0">
                    <joint name="leg_left2_z" type="slide" pos="0 0 0" axis="0 0 1" />
                    <geom type="cylinder" pos="0 0 -1" size="0.03 1.3" rgba="0 0 0.9 1" />
                    <inertial mass="1" pos="0 0 0" diaginertia="0.1 0.1 0.1"/>
                </body>
            </body>

        </body>
	</worldbody>

	<equality>
		<connect body1='right_motor'  body2='leg_right1'        anchor='-0.1 0 0'/>
    	<connect body1='right_motor'  body2='leg_right2'        anchor='0.1 0 0'/>

        <connect body1='left_motor'  body2='leg_left1'        anchor='-0.1 0 0'/>
    	<connect body1='left_motor'  body2='leg_left2'        anchor='0.1 0 0'/>
	</equality>

	<actuator>
        <motor name="torque1" joint="right_motor" gear="1" ctrlrange="-1 1" ctrllimited="true"/>
        <motor name="torque2" joint="left_motor" gear="1" ctrlrange="-1 1" ctrllimited="true"/>
    </actuator>
</mujoco>

Mujoco入门 https://mujoco.readthedocs.io/en/latest/overview.html#

Mujoco基本教程 https://blog.csdn.net/upr_rom/article/details/121969467?spm=1001.2101.3001.6650.13&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-13-121969467-blog-123098910.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-13-121969467-blog-123098910.pc_relevant_default&utm_relevant_index=14

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值