Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Initialize a flexcomp object in PyMJCF

未关闭
#445 4 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@quagla 已经在做这个了。

开始于 2024年1月8日。

评估

这个 Issue 还没有评估数据。

描述

Mujoco 3.0 introduced flexcomp object for modelling deformable obejcts.

However trying to create a Prop using

class Cloth(prop.Prop):
    """Simple cloth prop that consists of a MuJoco flexcomp."""

    def _build(self, *args, **kwargs):
        del args, kwargs
        mjcf_root = mjcf.RootElement()

        mjcf_root.extension.add('plugin', plugin="mujoco.elasticity.shell")

        # Props need to contain a body called prop_root
        mjcf_root.worldbody.add('body', name='prop_root')

        cloth_object = mjcf.from_file('cloth.xml')
        mjcf_root.attach(cloth_object)

        super()._build('cloth', mjcf_root)

Errors out as flexcomp is not recoginzed as part of schema.

I modified schema.xml(which is part of mjcf under dm_control) and after adding all the tags and attibutes that I am using in an xml such as below

<mujoco model="cloth">
    <extension>
        <plugin plugin="mujoco.elasticity.shell"/>
    </extension>

    <worldbody>
        <body name="cloth">
            <flexcomp type="grid" count="10 5 1" spacing=".025 .025 .025" mass="0.1"
                      name="cloth" radius="0.001">
                <contact condim="6" solref="0.001"/>
                <edge equality="true" damping="0.1"/>
                <plugin plugin="mujoco.elasticity.shell">
                    <config key="poisson" value="0"/>
                    <config key="thickness" value="10e-3"/>
                    <!--Units are in Pa (SI)-->
                    <config key="young" value="3e4"/>
                </plugin>
            </flexcomp>
        </body>
    </worldbody>
</mujoco>

I can get the xml to be parsed and get a Prop object. I looked at composite as an example and copied over the spec as described in mujoco XML reference.

However I cannot get the simulation to run and it fails at this line self._qp_mapper = _CartesianVelocityMapper(qp_params) in dm_robotics/moma/effectors/cartesian_6d_velocity_effector.py. The error is Process finished with exit code 139 (interrupted by signal 11:SIGSEGV) which just sounds like it crashed

I cannot understand why. The whole file which I am running is

"""Minimal working example of the dm_robotics Panda model."""
import dm_env
import numpy as np
from dm_env import specs

from dm_robotics.panda import environment
from dm_robotics.panda import parameters as params
from dm_robotics.panda import run_loop, utils

from dm_robotics.panda import arm_constants

import math
from dm_control import mjcf
from dm_robotics.moma import entity_initializer, prop
from dm_control.composer.variation import distributions, rotations


class Ball(prop.Prop):
    """Simple ball prop that consists of a MuJoco sphere geom."""

    def _build(self, *args, **kwargs):
        del args, kwargs
        mjcf_root = mjcf.RootElement()
        # Props need to contain a body called prop_root
        body = mjcf_root.worldbody.add('body', name='prop_root')
        body.add('geom',
                 type='sphere',
                 size=[0.04],
                 solref=[0.01, 0.5],
                 mass=1,
                 rgba=(1, 0, 0, 1))
        super()._build('ball', mjcf_root)


class Cloth(prop.Prop):
    """Simple cloth prop that consists of a MuJoco flexcomp."""

    def _build(self, *args, **kwargs):
        del args, kwargs
        mjcf_root = mjcf.RootElement()

        mjcf_root.extension.add('plugin', plugin="mujoco.elasticity.shell")

        # Props need to contain a body called prop_root
        mjcf_root.worldbody.add('body', name='prop_root')

        cloth_object = mjcf.from_file('cloth.xml')
        mjcf_root.attach(cloth_object)

        super()._build('cloth', mjcf_root)


class Agent:
    """The agent produces a trajectory tracing the path of an eight
    in the x/y control frame of the robot using end-effector velocities.
    """

    def __init__(self, spec: specs.BoundedArray) -> None:
        self._spec = spec

    def step(self, timestep: dm_env.TimeStep) -> np.ndarray:
        """Computes velocities in the x/y plane parameterized in time."""
        time = timestep.observation['time'][0]
        r = 0.1
        vel_x = r * math.cos(time)  # Derivative of x = sin(t)
        vel_y = r * ((math.cos(time) * math.cos(time)) -
                     (math.sin(time) * math.sin(time)))
        action = np.zeros(shape=self._spec.shape, dtype=self._spec.dtype)
        # The action space of the Cartesian 6D effector corresponds
        # to the linear and angular velocities in x, y and z directions
        # respectively
        action[0] = vel_x
        action[1] = vel_y
        return action


if __name__ == '__main__':
    # We initialize the default configuration for logging
    # and argument parsing. These steps are optional.
    utils.init_logging()
    parser = utils.default_arg_parser()
    args = parser.parse_args()

    # Use RobotParams to customize Panda robots added to the environment.
    robot_params = params.RobotParams(robot_ip=args.robot_ip, actuation=arm_constants.Actuation.CARTESIAN_VELOCITY)
    panda_env = environment.PandaEnvironment(robot_params)

    ball = Ball()
    cloth = Cloth()
    props = [ball, cloth]

    panda_env.add_props(props)
    initialize_props = entity_initializer.prop_initializer.PropPlacer(
        props,
        position=distributions.Uniform(-.5, .5),
        quaternion=rotations.UniformQuaternion())

    panda_env.add_entity_initializers([initialize_props])

    with panda_env.build_task_environment() as env:
        # Print the full action, observation and reward specification
        utils.full_spec(env)
        # Initialize the agent
        agent = Agent(env.action_spec())
        # Run the environment and agent either in headless mode or inside the GUI.
        if args.gui:
            app = utils.ApplicationWithPlot()
            app.launch(env, policy=agent.step)
        else:
            run_loop.run(env, agent, [], max_steps=1000, real_time=True)

Which is just a modification of the basic example in [dm_robotics_panda] (https://github.com/JeanElsner/dm_robotics_panda)

Commenting out creating of Cloth and adding to props list works fine.

主要语言
Python
星标
4.7k
派生
765
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google-deepmind/dm_control 的其他 Issue

查看 google-deepmind/dm_control 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。