Spaces:
Sleeping
Sleeping
File size: 10,481 Bytes
5f38e95 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | r"""MuJoCo adapter.
Builds an MJCF model programmatically from a :class:`Robot` definition:
* every base anchor becomes a worldbody ``<site>``,
* the platform is a single free-body with mass and inertia taken from
:class:`PlatformInertia`,
* every platform attachment becomes a body-local ``<site>``,
* each cable is a ``<tendon><spatial>`` connecting the two sites --- the
tendon is visualised and its length is accessible from
:meth:`MuJoCoAdapter.read_cable_lengths`, but it carries no actuator
force; cable forces are applied externally as a free-body wrench via
:meth:`MuJoCoAdapter.apply_wrench`. This split keeps tension
computation in the cdpr core (where the QP-based tension distribution
lives) and uses MuJoCo purely as a rigid-body integrator + renderer.
Quaternion convention note: MuJoCo stores quaternions as ``(w, x, y, z)``;
the framework's :class:`Pose` uses SciPy's ``(x, y, z, w)``. Conversions
happen inside :meth:`set_pose` and :meth:`read_state`.
Usage::
from cdpr.adapters import make_backend
with make_backend("mujoco", robot=ipanema_class()) as backend:
backend.set_pose(initial_pose)
for k in range(n_steps):
backend.apply_wrench(wrench) # F + tau on platform body origin
backend.step(dt)
state = backend.read_state()
"""
from __future__ import annotations
from typing import TYPE_CHECKING
import numpy as np
from numpy.typing import NDArray
from cdpr.adapters.base import AdapterCapability, PhysicsBackend
from cdpr.core.exceptions import MissingAdapterDependency
if TYPE_CHECKING: # pragma: no cover
from cdpr.core.frames import Pose, Wrench
from cdpr.dynamics.rigid_body import PlatformState
from cdpr.geometry.robot import Robot
try:
import mujoco
IS_AVAILABLE = True
except ImportError:
mujoco = None # type: ignore[assignment]
IS_AVAILABLE = False
def _require() -> None:
if not IS_AVAILABLE:
raise MissingAdapterDependency(
backend="mujoco",
install_hint="pip install 'cdpr[adapters-mujoco]' (or pip install mujoco)",
)
# ---------------------------------------------------------------------------
# MJCF generation
# ---------------------------------------------------------------------------
def build_mjcf(robot: "Robot", *,
gravity: tuple[float, float, float] = (0.0, 0.0, -9.81),
timestep: float = 1e-3) -> str:
"""Return the MJCF XML for a CDPR :class:`Robot`."""
inertia = robot.require_inertia()
a = robot.anchors
b = robot.attachments
g_str = " ".join(f"{v:.6g}" for v in gravity)
com_str = " ".join(f"{v:.6g}" for v in inertia.com)
# MuJoCo's fullinertia order: M11 M22 M33 M12 M13 M23
I = inertia.inertia
fullinertia = f"{I[0,0]:.6g} {I[1,1]:.6g} {I[2,2]:.6g} {I[0,1]:.6g} {I[0,2]:.6g} {I[1,2]:.6g}"
# Crude bounding box of the platform attachments for the visual geom.
bbox = np.max(np.abs(b), axis=0)
if (bbox < 1e-6).all(): # point-mass robot
bbox = np.array([0.02, 0.02, 0.02])
half = bbox * 0.5
geom_size_str = " ".join(f"{max(v, 0.01):.6g}" for v in half)
anchor_sites = "\n ".join(
f'<site name="anchor_{i}" pos="{a[i,0]:.6g} {a[i,1]:.6g} {a[i,2]:.6g}" '
f'size="0.02" rgba="0.2 0.2 0.2 1"/>' for i in range(robot.n_cables)
)
attach_sites = "\n ".join(
f'<site name="attach_{i}" pos="{b[i,0]:.6g} {b[i,1]:.6g} {b[i,2]:.6g}" '
f'size="0.015" rgba="0.85 0.32 0.15 1"/>' for i in range(robot.n_cables)
)
tendons = "\n ".join(
f'<spatial name="cable_{i}" width="0.004" rgba="0.1 0.45 0.7 0.9">'
f'<site site="attach_{i}"/><site site="anchor_{i}"/></spatial>'
for i in range(robot.n_cables)
)
xml = f"""<mujoco model="cdpr_{robot.name}">
<option timestep="{timestep:.6g}" integrator="RK4" gravity="{g_str}"/>
<visual>
<quality shadowsize="2048"/>
<map zfar="100"/>
</visual>
<worldbody>
{anchor_sites}
<body name="platform" pos="0 0 0">
<freejoint name="platform_free"/>
<inertial pos="{com_str}" mass="{inertia.mass:.6g}" fullinertia="{fullinertia}"/>
{attach_sites}
<geom name="platform_geom" type="box" size="{geom_size_str}"
rgba="0.30 0.55 0.85 0.35" contype="0" conaffinity="0"/>
</body>
</worldbody>
<tendon>
{tendons}
</tendon>
</mujoco>
"""
return xml
# ---------------------------------------------------------------------------
# Quaternion conversions
# ---------------------------------------------------------------------------
def _quat_xyzw_to_wxyz(q: NDArray[np.float64]) -> NDArray[np.float64]:
"""SciPy (x, y, z, w) -> MuJoCo (w, x, y, z)."""
return np.array([q[3], q[0], q[1], q[2]])
def _quat_wxyz_to_xyzw(q: NDArray[np.float64]) -> NDArray[np.float64]:
"""MuJoCo (w, x, y, z) -> SciPy (x, y, z, w)."""
return np.array([q[1], q[2], q[3], q[0]])
# ---------------------------------------------------------------------------
# Adapter
# ---------------------------------------------------------------------------
class MuJoCoAdapter(PhysicsBackend):
"""MuJoCo rigid-body backend with cdpr-supplied cable wrench."""
name = "mujoco"
@property
def capabilities(self) -> AdapterCapability:
return (
AdapterCapability.LOAD_ROBOT
| AdapterCapability.SET_POSE
| AdapterCapability.READ_STATE
| AdapterCapability.STEP_PHYSICS
| AdapterCapability.APPLY_WRENCH
| AdapterCapability.READ_CABLE_LENGTHS
)
def __init__(
self,
robot: "Robot",
*,
gravity: tuple[float, float, float] = (0.0, 0.0, -9.81),
timestep: float = 1e-3,
xml_override: str | None = None,
) -> None:
_require()
self._gravity = gravity
self._timestep = timestep
self._xml_override = xml_override
self.mjModel = None
self.mjData = None
self._platform_body_id: int | None = None
self._cable_tendon_ids: list[int] = []
self._dof_index_lin: slice | None = None
super().__init__(robot) # invokes load_robot
# --- lifecycle ---------------------------------------------------
def load_robot(self, robot: "Robot") -> None:
xml = self._xml_override or build_mjcf(
robot, gravity=self._gravity, timestep=self._timestep,
)
self.xml = xml
self.mjModel = mujoco.MjModel.from_xml_string(xml)
self.mjData = mujoco.MjData(self.mjModel)
self.robot = robot
# Cache the platform body id and cable tendon ids by name.
self._platform_body_id = int(mujoco.mj_name2id(
self.mjModel, mujoco.mjtObj.mjOBJ_BODY, "platform",
))
if self._platform_body_id < 0:
raise RuntimeError("mujoco model has no 'platform' body --- MJCF build failed.")
self._cable_tendon_ids = [
int(mujoco.mj_name2id(self.mjModel, mujoco.mjtObj.mjOBJ_TENDON, f"cable_{i}"))
for i in range(robot.n_cables)
]
def close(self) -> None:
# MuJoCo objects own native buffers; drop our references and the
# garbage collector frees them.
self.mjModel = None
self.mjData = None
# --- pose ---------------------------------------------------------
def set_pose(self, pose: "Pose") -> None:
if self.mjData is None:
raise RuntimeError("mujoco adapter not initialised")
# Free joint qpos layout: x y z qw qx qy qz
self.mjData.qpos[:3] = pose.position
self.mjData.qpos[3:7] = _quat_xyzw_to_wxyz(pose.quaternion_xyzw)
self.mjData.qvel[:6] = 0.0
mujoco.mj_forward(self.mjModel, self.mjData)
def read_state(self) -> "PlatformState":
if self.mjData is None:
raise RuntimeError("mujoco adapter not initialised")
from scipy.spatial.transform import Rotation
from cdpr.core.frames import Pose, Twist
from cdpr.dynamics.rigid_body import PlatformState
pos = self.mjData.qpos[:3].copy()
quat_xyzw = _quat_wxyz_to_xyzw(self.mjData.qpos[3:7].copy())
# qvel for a free joint: first 3 are linear velocity in WORLD frame,
# next 3 are angular velocity in WORLD frame (MuJoCo docs:
# https://mujoco.readthedocs.io/en/stable/computation/index.html#cdof).
vel_lin = self.mjData.qvel[:3].copy()
vel_ang = self.mjData.qvel[3:6].copy()
return PlatformState(
pose=Pose(position=pos, rotation=Rotation.from_quat(quat_xyzw)),
velocity=Twist.from_parts(vel_lin, vel_ang),
)
# --- forces -------------------------------------------------------
def apply_wrench(self, wrench: "Wrench") -> None:
if self.mjData is None or self._platform_body_id is None:
raise RuntimeError("mujoco adapter not initialised")
# xfrc_applied is shape (nbody, 6) in (Fx Fy Fz Tx Ty Tz), in WORLD frame.
self.mjData.xfrc_applied[self._platform_body_id, :3] = wrench.force
self.mjData.xfrc_applied[self._platform_body_id, 3:] = wrench.torque
# --- step ---------------------------------------------------------
def step(self, dt: float) -> None:
if self.mjData is None or self.mjModel is None:
raise RuntimeError("mujoco adapter not initialised")
# Honour the caller's dt: temporarily override the model timestep,
# then take however many sub-steps the configured timestep needs.
substeps = max(int(round(dt / self.mjModel.opt.timestep)), 1)
for _ in range(substeps):
mujoco.mj_step(self.mjModel, self.mjData)
# --- cable lengths -------------------------------------------------
def read_cable_lengths(self) -> NDArray[np.float64]:
if self.mjData is None:
raise RuntimeError("mujoco adapter not initialised")
# ten_length is shape (ntendon,) in metres.
lengths = np.empty(len(self._cable_tendon_ids), dtype=np.float64)
for k, tid in enumerate(self._cable_tendon_ids):
lengths[k] = float(self.mjData.ten_length[tid])
return lengths
# The name the registry / factory expects.
Adapter = MuJoCoAdapter
|