diff --git a/agent_context/topics/atomic-actions/atomic-actions.md b/agent_context/topics/atomic-actions/atomic-actions.md index 8201877ea7..6138199ac1 100644 --- a/agent_context/topics/atomic-actions/atomic-actions.md +++ b/agent_context/topics/atomic-actions/atomic-actions.md @@ -105,6 +105,35 @@ repeated-pick/open-drawer environment components carry the same Newton-only overlays, while their declared Default backend continues to consume only the portable fields. +For UR5 with `dexuni`, the paper-cup `move_held_object.py` tutorial opts into +VBD contact history with `latest` collision matching, sliding friction 4 on the +gripper alone, and a 0.0115 m candidate close target. Transport orientation +is not yet repeatably aligned. The native contact alpha and the cup +and ground materials are unchanged. `create_tutorial_simulation` accepts +`newton_solver_options`, and `add_tutorial_robot` accepts +`newton_gripper_friction` and optional `arm_damping`; these are authored before +model construction. This +calibration also applies to the UR5-only `pour.py` tutorial with its 0.013 m +cube close target. The UR5 plastic-tray `coordinated_pickment.py` tutorial uses +the same contact settings with a 0.023 m close target; the dual-arm helper +`add_dual_tutorial_robot` also accepts `newton_gripper_friction`. +The UR5 vertical-can `hand_over.py` tutorial uses those contact settings with +left/right close targets of 0.007/0.0055 m and retains its original arm drives. +Its replay callback holds the current targets for one second immediately before +the compiled `receive_release` segment so the receiving arm can settle before +opening. The wait uses segment metadata, not a fixed waypoint index, and is +scoped to that same profile. This scene-specific calibration does not promise +identical final poses or stability across initial-state changes. Horizontal +objects and other robot families retain their prior configuration. +The UR5 DexUni `place.py` profile uses a 0.013 m close target, gripper friction 4, +contact history with `latest` matching, contact alpha 0.5, and arm damping 1000. +The lower arm damping reduces tracking lag when the fingers open; finger +drives and authored release waypoints remain unchanged. Complete cached +replays preserve the grasp to release, but landing orientation still varies; +this is a scoped improvement, not a claim of full solver equivalence. +These explicit scene choices do not change world defaults +or establish a generic object-width-to-gripper-target mapping. + ## Semantic integration boundary `semantics` contains: diff --git a/agent_context/topics/sensor-system/lifecycle.md b/agent_context/topics/sensor-system/lifecycle.md index 8a83ede7d3..fce171f43a 100644 --- a/agent_context/topics/sensor-system/lifecycle.md +++ b/agent_context/topics/sensor-system/lifecycle.md @@ -40,6 +40,13 @@ support. Do not infer force support from sensor construction alone. Inspect backend/solver/device matrix: Newton AutoSolver may change the answer during preparation, so the manager checks again afterwards. +DexUni's `pure_mujoco` path reports rigid-contact geometry, normal impulse and +tangential impulse through the MuJoCo contact buffer, except when MuJoCo uses +its CPU implementation. Other DexUni paths expose available rigid-contact +geometry with `impulse=False` and `friction=False`; zero-valued output columns +there are placeholders, not measured zero forces. Particle-shape contacts do +not enter this rigid ContactQuery. + The sensor retains backend-emitted rows rather than reducing an actor pair to one contact. Force-reporting paths apply the query's positive-impulse filtering; geometry-only paths may provide valid rows with zero impulse. Solver settings diff --git a/agent_context/topics/simulation-system/articulation-adapters.md b/agent_context/topics/simulation-system/articulation-adapters.md index 2d449a0c08..88bcdd95f6 100644 --- a/agent_context/topics/simulation-system/articulation-adapters.md +++ b/agent_context/topics/simulation-system/articulation-adapters.md @@ -21,12 +21,21 @@ the native articulation before Direct GPU initialization and initial reset. Iteration counts are configured together because the native setter is atomic. Applying them only during facade binding is too late for captured GPU settings. +`root_props.newton_gravity_compensation` is Newton creation intent applied in +both asset physics modes; omission preserves the source/default policy. It +compensates articulation link weight without removing payload gravity or +contact reactions. DexSim checks the resolved solver's MuJoCo support. + `joint_drive_props` is the sole joint-property entry point; sparse fields retain source values. `drive_type` describes the response and `target_mode` the active command components. Lowering masks inactive gains: none/effort clear both, velocity clears position gain. Newton rejects active acceleration drives; non-MuJoCo position mode is gain-based emulation with zero velocity target. Exact field options and defaults remain in `cfg/articulation.py`. +Newton-specific `joint_drive_props.passive_damping` lowers to passive DOF +damping, separately from the drive's velocity gain. It retains the same +exact-name/regex/control-part matching and remains active for passive or effort +target modes. Applied Newton-only settings are rejected on Default. ## State order and batched writes @@ -122,3 +131,5 @@ FREE-joint synchronization, mesh export and no-op root writes; `tests/sim/objects/test_articulation.py` for topology/FK. Source configuration and root timing are covered by `tests/sim/spawn/test_descriptors.py` and `tests/sim/test_sim_manager.py`. +`tests/sim/test_dexuni_two_way.py` compares contact-limited dynamic fingers +against the same one-way commands, including CUDA Graph and reset. diff --git a/agent_context/topics/simulation-system/physics-configuration.md b/agent_context/topics/simulation-system/physics-configuration.md index 2d243afe7f..c09260466e 100644 --- a/agent_context/topics/simulation-system/physics-configuration.md +++ b/agent_context/topics/simulation-system/physics-configuration.md @@ -6,7 +6,7 @@ Return to the [simulation overview](simulation-system.md) for lifecycle entry. ## Backend, solver and device resolution `SimulationManagerCfg.physics_cfg` selects the backend by config type. -`cfg/simulation.py` owns `PhysicsBackendCfg`, `DefaultPhysicsCfg` and +`cfg/physics.py` owns `PhysicsBackendCfg`, `DefaultPhysicsCfg` and `NewtonPhysicsCfg`; do not add another backend string to the manager config. Gym's file-owned selector and composition rules belong to [environment configuration](../env-framework/configuration.md). @@ -26,7 +26,7 @@ The concrete solver is resolved from the complete Spawn scene, then exposed through `NewtonPhysicsBackend.solver_type`. Do not guess a solver-specific robot preset while it is unresolved. Gradient mode requires an explicit `semi_implicit` solver. Read exact solver options/defaults in -`cfg/simulation.py` and the installed DexSim config types; do not substitute a +`cfg/physics.py` and the installed DexSim config types; do not substitute a hardcoded fallback when an expected DexSim API is absent. `collision_cfg` owns the external Newton contact pipeline. Its @@ -38,10 +38,19 @@ external-pipeline tuning does not configure internal contact generation. The manual differentiable trajectory requires the external pipeline; see [differentiable environment](../differentiable-env/differentiable-env.md). +DexUni preserves its one-way contact policy unless `coupling="two_way"` is +explicit. Two-way feedback requires dynamically integrated MuJoCo joints and +rejects full-surface rigid-soft contacts. Prescribed kinematic trajectories +cannot respond to contact reactions. Forward solver expert options through +`NewtonPhysicsCfg.solver_cfg`; DexSim owns their validation and dispatch. +Articulation gravity compensation and passive damping are separate creation +settings; see [articulation adapters](articulation-adapters.md). + Capability checks remain explicit: Default owns native rigid constraints and Newton deformables are separate. Contact support is rechecked after Newton -solver resolution; DexUni and MuJoCo-Warp on CPU do not expose the supported -ContactQuery path. See `physics/` and [sensors](../sensor-system/sensor-system.md). +solver resolution; capabilities depend on the concrete DexUni contact path +and whether MuJoCo runs on CUDA. See `physics/` and the +[contact capability contract](../sensor-system/lifecycle.md#contact-queries). ## Sparse source physics diff --git a/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst b/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst index 8ad718a951..f9ba36283a 100644 --- a/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst +++ b/docs/source/api_reference/embodichain/embodichain.lab.sim.cfg.rst @@ -135,6 +135,13 @@ For manager-owned Newton steps, ``sync_to_renderer=None`` or ``False`` publishes only for visual consumers; ``True`` additionally requests publication on steps without consumers. +DexUni accepts ``coupling="two_way"`` with ``joint_mode="dynamic"`` to feed +contact reactions back to articulations. Keep ``collision_cfg=None`` when +DexUni owns collision detection. ``coupling_options`` configures the native +feedback policy; full-surface rigid-soft contacts are unsupported in this mode. +Articulation gravity compensation and passive joint damping are configured +separately below. + .. currentmodule:: embodichain.lab.sim.cfg.physics .. autosummary:: @@ -148,6 +155,41 @@ without consumers. physics_backend_from_cfg validate_physics_cfg +Articulation configuration module +--------------------------------- + +Articulation types live in ``embodichain.lab.sim.cfg.articulation`` and remain +available from the ``embodichain.lab.sim.cfg`` facade. Root creation settings +are independent of sparse link and joint physics overlays. +``root_props.newton_gravity_compensation`` compensates articulation link +weight on MuJoCo-backed Newton solvers while retaining payload gravity and +contact reactions. Omission preserves source intent. + +``NewtonJointDrivePropertiesCfg.passive_damping`` selects passive DOF damping +through the same exact-name, regex and control-part rules as the drive gains. +It is independent of the drive velocity gain ``damping`` and stays active for +passive and effort target modes. In dictionaries, select this subtype with +``joint_drive_props.backend="newton"``. Both settings require Newton. + +.. currentmodule:: embodichain.lab.sim.cfg.articulation + +.. autosummary:: + + ArticulationRootPropertiesCfg + LinkPhysicsOverrideCfg + link_attrs_from_dict + JointDrivePropertiesCfg + NewtonJointDrivePropertiesCfg + ArticulationCfg + +.. autoclass:: ArticulationRootPropertiesCfg + :members: + :no-index: + +.. autoclass:: NewtonJointDrivePropertiesCfg + :members: + :no-index: + Compatibility facade -------------------- diff --git a/embodichain/lab/sim/cfg/articulation.py b/embodichain/lab/sim/cfg/articulation.py index 9f5f34f657..c63ce6622e 100644 --- a/embodichain/lab/sim/cfg/articulation.py +++ b/embodichain/lab/sim/cfg/articulation.py @@ -34,6 +34,15 @@ _rigid_body_physics_from_dict, ) +__all__ = [ + "ArticulationRootPropertiesCfg", + "LinkPhysicsOverrideCfg", + "link_attrs_from_dict", + "JointDrivePropertiesCfg", + "NewtonJointDrivePropertiesCfg", + "ArticulationCfg", +] + def _normalize_joint_target_mode(value: object) -> int: """Normalize a portable joint target mode to its backend integer value.""" @@ -94,6 +103,16 @@ class ArticulationRootPropertiesCfg: min_velocity_iters: int | None = None """Default-only minimum root velocity-solver iterations (0 to 255).""" + newton_gravity_compensation: float | None = None + """MuJoCo link-weight compensation fraction in [0, 1], on Newton only. + + ``None`` preserves the source/default policy; zero retains gravity and one + compensates every link in this articulation. Payload gravity and contact + reactions remain active. This creation setting applies in both asset + physics modes, independently of DexUni's coupling policy. The finalized + Newton solver must support MuJoCo gravity compensation. + """ + def __post_init__(self) -> None: """Require the two values consumed by the atomic Default setter.""" if (self.min_position_iters is None) != (self.min_velocity_iters is None): @@ -376,12 +395,23 @@ def to_dict(self) -> dict[str, Any]: @configclass class NewtonJointDrivePropertiesCfg(JointDrivePropertiesCfg): - """Compatibility subtype for serialized Newton joint-drive configs. + """Joint-drive configuration with Newton-specific passive dynamics. ``target_mode`` is now portable and lives on :class:`JointDrivePropertiesCfg`. The subtype remains so existing - ``backend="newton"`` dictionaries and round trips retain their type; new - robot definitions should use the common class. + ``backend="newton"`` dictionaries and round trips retain their type. Use + the common class when no Newton-specific dynamics are required. + """ + + passive_damping: dict[str, float] | float | None = None + """Nonnegative passive joint damping, independent of drive damping. + + Uses the same joint-name, regex and robot control-part rules as the drive + gains. Apply it with ``asset_physics_mode='overlay'``; ``None`` preserves + source damping. Unlike :attr:`damping`, this + lowers to Newton's ``JointDofConfig.damping`` and remains active when the + drive target mode is ``none`` or ``effort``. It requires Newton and is + consumed by MuJoCo-backed solvers, including dynamic DexUni. """ diff --git a/embodichain/lab/sim/cfg/physics.py b/embodichain/lab/sim/cfg/physics.py index 314eca508a..7734507670 100644 --- a/embodichain/lab/sim/cfg/physics.py +++ b/embodichain/lab/sim/cfg/physics.py @@ -349,6 +349,14 @@ class NewtonPhysicsCfg(PhysicsBackendCfg): parameters accepted by that DexSim solver config. If omitted, EmbodiChain preserves DexSim's scene-aware ``AutoSolverCfg`` default. A DexSim build exporting ``AutoSolverCfg`` is required; no concrete-solver fallback is used. + For ``solver_type='dexuni'``, the current DexSim contract keeps expert + options under ``vbd_options``, ``mujoco_options`` and + ``collision_options``; use ``joint_mode`` and ``contact_mode`` for the + coupled dispatch policy. ``coupling='two_way'`` enables contact reactions + on dynamically integrated articulations. Optional ``coupling_options`` + accepts ``mass_scale``, ``proxy_relaxation`` and ``iterations``. Two-way + coupling requires ``joint_mode='dynamic'`` and does not support full-surface + rigid-soft contacts; DexSim validates these combinations. """ collision_cfg: NewtonCollisionPipelineCfg | Mapping[str, Any] | None = field( diff --git a/embodichain/lab/sim/cfg/robot.py b/embodichain/lab/sim/cfg/robot.py index c26dac1325..b25e371825 100644 --- a/embodichain/lab/sim/cfg/robot.py +++ b/embodichain/lab/sim/cfg/robot.py @@ -357,6 +357,8 @@ def resolve( solver_candidates = [] if solver_type != "auto": solver_candidates.append(f"newton_{solver_type}") + if solver_type == "dexuni": + solver_candidates.extend(("newton_mujoco_warp", "newton_mjwarp")) if solver_type == "mujoco_warp": solver_candidates.append("newton_mjwarp") candidates = (*solver_candidates, "newton", "default") diff --git a/embodichain/lab/sim/objects/articulation.py b/embodichain/lab/sim/objects/articulation.py index ccafd9dcfb..2da484a755 100644 --- a/embodichain/lab/sim/objects/articulation.py +++ b/embodichain/lab/sim/objects/articulation.py @@ -1045,7 +1045,7 @@ def root_link_name(self) -> str: Returns: str: The name of the root link. """ - return self.entities[0].get_root_link_name() + return self._entities[0].get_root_link_name() @cached_property def joint_names(self) -> List[str]: diff --git a/embodichain/lab/sim/objects/backends/collision.py b/embodichain/lab/sim/objects/backends/collision.py index a39a35775a..1f2889669e 100644 --- a/embodichain/lab/sim/objects/backends/collision.py +++ b/embodichain/lab/sim/objects/backends/collision.py @@ -184,9 +184,9 @@ def _from_descriptor(entity: object) -> list[CollisionShapeDesc] | None: dtype=torch.float32, ).reshape(3) shapes: list[CollisionShapeDesc] = [] + # Match native shape queries: disabling contact response keeps geometry + # available to planners, including moving obstacles that exert no impulses. for shape_idx, collision in enumerate(collisions): - if getattr(collision, "enable_collision", True) is False: - continue geometry_type = getattr(collision, "geometry_type", None) geometry_type = getattr(geometry_type, "value", geometry_type) local_pose = torch.as_tensor( diff --git a/embodichain/lab/sim/objects/backends/newton.py b/embodichain/lab/sim/objects/backends/newton.py index 33b1644044..1763ea5287 100644 --- a/embodichain/lab/sim/objects/backends/newton.py +++ b/embodichain/lab/sim/objects/backends/newton.py @@ -204,11 +204,14 @@ def _configure_newton_mimic_compliance( backend = get_newton_backend(result.world) if ( backend is None - or backend.solver_type != "mujoco_warp" + or backend.solver_type not in {"mujoco_warp", "dexuni"} or backend.cfg.requires_grad or (backend.model is not None and backend.model.requires_grad) ): return False + solver = backend.mujoco_solver + if solver is None: + return False relation_names = [ (state_joint_names[child_id], state_joint_names[parent_id]) @@ -284,13 +287,21 @@ def _configure_newton_mimic_compliance( f"Newton model has no mimic constraint for joint pair {error.args[0]}." ) from error - solver = runtime.solver mapping = getattr(solver, "mjc_eq_to_newton_mimic", None) mjw_model = getattr(solver, "mjw_model", None) if mapping is None or mjw_model is None: raise RuntimeError("MuJoCo-Warp did not materialize Newton mimic rows.") mapping_values = np.asarray(mapping.numpy()) + if backend.solver_type == "dexuni": + from newton import Model + + scene_rows = backend.get_mujoco_index_map( + Model.AttributeFrequency.CONSTRAINT_MIMIC + ).numpy() + mapping_values = mapping_values.copy() + valid = mapping_values >= 0 + mapping_values[valid] = scene_rows[mapping_values[valid]] selected = np.isin(mapping_values, constraint_rows) if int(selected.sum()) != len(constraint_rows): raise RuntimeError( diff --git a/embodichain/lab/sim/physics/newton.py b/embodichain/lab/sim/physics/newton.py index cbb37d11bf..5d7f8e6c4e 100644 --- a/embodichain/lab/sim/physics/newton.py +++ b/embodichain/lab/sim/physics/newton.py @@ -201,19 +201,33 @@ def supports_rigid_object_group(self) -> bool: @property def supports_contact_sensor(self) -> bool: # ContactSensor consumes the backend-neutral Scene ContactQuery API, - # but not every Newton solver/device pair currently publishes that - # query. Keep this runtime-sensitive so ``solver_type='auto'`` can be - # rechecked after Spawn finalization resolves the concrete solver. + # but CPU MuJoCo cannot publish its force report. Keep this runtime- + # sensitive so ``auto`` and DexUni's scene-selected solver path are + # rechecked after Spawn finalization. solver_type = self.solver_type if solver_type is None: return True solver_type = solver_type.lower() - if solver_type.endswith("dexuni"): - return False + if solver_type == "dexuni": + world = getattr(self._manager, "_world", None) + if world is None: + return True + from dexsim.engine.newton_physics.backend_registry import ( + get_newton_backend, + ) + + backend = get_newton_backend(world) + solver = None if backend is None else backend.solver + features = None if solver is None else getattr(solver, "features", None) + if getattr(features, "backend", None) == "pure_mujoco": + mujoco_solver = backend.mujoco_solver + return mujoco_solver is not None and not mujoco_solver.use_mujoco_cpu + # Other DexUni paths expose rigid-contact geometry through + # ContactQuery, which marks impulse and friction unavailable. + return True runtime_device = self._runtime_device if runtime_device is None: runtime_device = str(getattr(self._manager, "device", "")) return not ( - solver_type.endswith("mujoco_warp") - and runtime_device.lower().startswith("cpu") + solver_type == "mujoco_warp" and runtime_device.lower().startswith("cpu") ) diff --git a/embodichain/lab/sim/sensors/contact_sensor.py b/embodichain/lab/sim/sensors/contact_sensor.py index 08ce036eab..c443b22df0 100644 --- a/embodichain/lab/sim/sensors/contact_sensor.py +++ b/embodichain/lab/sim/sensors/contact_sensor.py @@ -111,7 +111,9 @@ class ContactSensor(BaseSensor): The sensor preserves every backend-emitted contact row that passes the backend's positive-impulse filter. Geometry-only Newton solvers retain all - candidate contact rows. + candidate rigid-contact rows. Inspect :attr:`contact_capabilities` before + interpreting ``impulse`` or ``friction``: DexUni paths other than + ``pure_mujoco`` do not report either quantity. """ SUPPORTED_DATA_TYPES = [ diff --git a/embodichain/lab/sim/sim_manager.py b/embodichain/lab/sim/sim_manager.py index 3eaae5abcb..a75c2f2bfe 100644 --- a/embodichain/lab/sim/sim_manager.py +++ b/embodichain/lab/sim/sim_manager.py @@ -4558,6 +4558,7 @@ def reset_objects_state( None, "auto", "mujoco_warp", + "dexuni", } if ( use_coordinated_newton_clear @@ -4662,6 +4663,9 @@ def export_usd(self, fpath: str) -> bool: bool: True if export is successful, False otherwise. """ try: + # The optional USD kit registers the export methods on DexSim Env. + import dexsim.kit.usd + self._env.export_to_usd_file(fpath) logger.log_info(f"Simulation scene exported to USD file: {fpath}") return True @@ -4778,6 +4782,14 @@ def _deferred_destroy(self) -> None: "_robots", ): for asset in getattr(self, registry_name, {}).values(): + # Parsed material wrappers must die before their native renderer. + for material_attr in ( + "_visual_material", + "_original_visual_material", + "_original_visual_material_inst", + ): + if hasattr(asset, material_attr): + setattr(asset, material_attr, []) if hasattr(asset, "_data"): asset._data = None if hasattr(asset, "_spawn_result"): diff --git a/embodichain/lab/sim/spawn/descriptors.py b/embodichain/lab/sim/spawn/descriptors.py index bbcbe2af0e..8152b347b8 100644 --- a/embodichain/lab/sim/spawn/descriptors.py +++ b/embodichain/lab/sim/spawn/descriptors.py @@ -71,6 +71,7 @@ DefaultRigidBodyPropertiesCfg, MassPropertiesCfg, NewtonCollisionPropertiesCfg, + NewtonJointDrivePropertiesCfg, NewtonRigidBodyMaterialCfg, RigidBodyMaterialCfg, RigidBodyPhysicsCfg, @@ -621,6 +622,10 @@ def articulation_desc_from_cfg( newton_solver_type=newton_solver_type, ) fixed_base, self_collision_enabled = _articulation_root_values(cfg) + compensation = _articulation_gravity_compensation(cfg, newton_solver_type) + newton_options = ( + {} if compensation is None else {"newton_gravity_compensation": compensation} + ) return ArticulationDesc( name=_articulation_uid(cfg.uid, str(path)), pose=_pose_from_cfg(cfg), @@ -635,9 +640,32 @@ def articulation_desc_from_cfg( urdf_read_inertia=True, per_env=per_env, body_scale=_vector3(cfg.body_scale, field_name="body_scale"), + **newton_options, ) +def _articulation_gravity_compensation( + cfg: ArticulationCfg, newton_solver_type: str | None +) -> float | None: + """Validate explicit Newton articulation creation intent.""" + value = cfg.root_props.newton_gravity_compensation + if value is None: + return None + if ( + not isinstance(value, numbers.Real) + or not np.isfinite(value) + or not 0 <= value <= 1 + ): + raise ValueError("newton_gravity_compensation must be finite and in [0, 1].") + if newton_solver_type is None: + raise ValueError("newton_gravity_compensation requires the Newton backend.") + if not hasattr(ArticulationDesc, "newton_gravity_compensation"): + raise RuntimeError( + "newton_gravity_compensation requires DexSim dev f32785317 or newer." + ) + return float(value) + + def _validate_articulation_rigid_physics( cfg: ArticulationCfg, *, @@ -734,6 +762,9 @@ def configure_articulation_desc( f"Articulation source {desc.name!r} must be resolved before " "configuration." ) + compensation = _articulation_gravity_compensation(cfg, newton_solver_type) + if compensation is not None: + desc.newton_gravity_compensation = compensation preserve_source_physics = cfg.resolve_asset_physics_mode() == "preserve" setattr(desc, "_embodichain_preserve_source_physics", preserve_source_physics) for link in desc.links: @@ -895,6 +926,14 @@ def configure_articulation_desc( newton=newton_desc, newton_target_mode=joint_target_modes.get(joint_name), ) + if ( + newton_desc.damping is not None + and desc.get_joint_desc(joint_name).newton.damping != newton_desc.damping + ): + raise RuntimeError( + "DexSim did not retain passive_damping; install DexSim dev " + "f32785317 or newer." + ) _apply_articulation_newton_gap_default(desc, newton_solver_type) return desc @@ -1151,6 +1190,27 @@ def _compile_joint_properties( setattr(default_desc, default_field, scalar) setattr(newton_desc, newton_field, scalar) + if ( + isinstance(cfg.joint_drive_props, NewtonJointDrivePropertiesCfg) + and cfg.joint_drive_props.passive_damping is not None + ): + if newton_solver_type is None: + raise ValueError("passive_damping requires the Newton backend.") + matches = _joint_property_matches( + cfg.joint_drive_props.passive_damping, + joint_names, + property_name="passive_damping", + control_parts=control_parts, + ) + for joint_name, value in matches: + if ( + not isinstance(value, numbers.Real) + or not np.isfinite(value) + or value < 0 + ): + raise ValueError("passive_damping must be finite and nonnegative.") + joint_properties[joint_name][1].damping = float(value) + # Solvers that ignore Newton's target-mode enum still consume drive gains. # Masking inactive components makes NONE, EFFORT, and VELOCITY deterministic # across the currently supported solver set. @@ -1170,9 +1230,13 @@ def _compile_joint_properties( if newton_solver_type is None else newton_solver_type.replace("-", "_").lower() ) - if normalized_solver not in {None, "auto", "mujoco_warp", "mjwarp"} and any( - mode == 1 for mode in joint_target_modes.values() - ): + if normalized_solver not in { + None, + "auto", + "mujoco_warp", + "mjwarp", + "dexuni", + } and any(mode == 1 for mode in joint_target_modes.values()): warnings.warn( f"Newton solver {newton_solver_type!r} does not consume " "joint_target_mode. POSITION is emulated with its configured " diff --git a/embodichain/lab/sim/spawn/usd.py b/embodichain/lab/sim/spawn/usd.py index 77aa4632f3..3302733044 100644 --- a/embodichain/lab/sim/spawn/usd.py +++ b/embodichain/lab/sim/spawn/usd.py @@ -39,6 +39,7 @@ _compile_rigid_physics, _compile_visual_material, _articulation_root_values, + _articulation_gravity_compensation, _pose_from_cfg, _required_uid, _resolve_rigid_physics, @@ -178,6 +179,7 @@ def articulation_desc_from_usd( newton_solver_type: str | None = None, ) -> tuple[ArticulationDesc, dict[str, MaterialDesc]]: """Select the sole articulation in a USD stage.""" + compensation = _articulation_gravity_compensation(cfg, newton_solver_type) preserve_asset_physics = cfg.resolve_asset_physics_mode() == "preserve" if not preserve_asset_physics: _validate_articulation_rigid_physics( @@ -206,6 +208,8 @@ def articulation_desc_from_usd( fixed_base_default=bool(desc.fixed_base), self_collision_default=desc.enable_self_collision, ) + if compensation is not None: + desc.newton_gravity_compensation = compensation return desc, materials diff --git a/embodichain_tasks/configs/components/execution_policies/trajectory_open_loop_slow.yaml b/embodichain_tasks/configs/components/execution_policies/trajectory_open_loop_slow.yaml index 0554f31877..52b97666c0 100644 --- a/embodichain_tasks/configs/components/execution_policies/trajectory_open_loop_slow.yaml +++ b/embodichain_tasks/configs/components/execution_policies/trajectory_open_loop_slow.yaml @@ -5,6 +5,8 @@ requires: embodiment_contract: single_arm_parallel_gripper motion: + strategy: ik_interp + velocity_targets: auto # At the pour environment's 100 Hz cadence, arm moves keep their 4.8 s # duration while position targets advance smoothly at every physics step. sample_count: 480 diff --git a/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml b/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml index aa9876d1b5..2f07a06686 100644 --- a/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml +++ b/embodichain_tasks/configs/tasks/manipulation/open_drawer/envs/newton.yaml @@ -13,15 +13,15 @@ physics_config: num_substeps: 20 collision_cfg: null solver_cfg: - solver_type: mujoco_warp - solver: newton - integrator: implicitfast - iterations: 20 - ls_iterations: 100 - cone: elliptic - impratio: 1000.0 - use_mujoco_contacts: true - enable_multiccd: true + solver_type: dexuni + mujoco_options: + solver: newton + integrator: implicitfast + iterations: 20 + ls_iterations: 100 + cone: elliptic + impratio: 1000.0 + enable_multiccd: true simulation: light: diff --git a/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml b/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml index 003e2cbec2..5ff40344ae 100644 --- a/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml +++ b/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/envs/newton.yaml @@ -15,15 +15,15 @@ physics_config: num_substeps: 20 collision_cfg: null solver_cfg: - solver_type: mujoco_warp - solver: newton - integrator: implicitfast - iterations: 20 - ls_iterations: 100 - cone: elliptic - impratio: 1000.0 - use_mujoco_contacts: true - enable_multiccd: true + solver_type: dexuni + mujoco_options: + solver: newton + integrator: implicitfast + iterations: 20 + ls_iterations: 100 + cone: elliptic + impratio: 1000.0 + enable_multiccd: true simulation: light: diff --git a/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml b/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml index fdd5fab264..c2f332d971 100644 --- a/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml +++ b/embodichain_tasks/configs/tasks/manipulation/repeated_pick_place/task.ur5.objective.yaml @@ -9,7 +9,7 @@ objective: task_program: program: task_program/program.yaml integration: task_program/integration.yaml - execution_policy: ../../../components/execution_policies/trajectory_open_loop.yaml + execution_policy: ../../../components/execution_policies/trajectory_open_loop_slow.yaml embodiment: component: ../../../components/embodiments/ur5_dh_pgi_140_80.yaml diff --git a/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml b/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml index baa1a285a2..202e1ddf78 100644 --- a/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml +++ b/embodichain_tasks/configs/tasks/manipulation/rubiks_cube_pick_place/task.ur5.yaml @@ -6,7 +6,7 @@ environment: task_program: program: task_program/program.yaml integration: task_program/integration.yaml - execution_policy: ../../../components/execution_policies/trajectory_open_loop.yaml + execution_policy: ../../../components/execution_policies/trajectory_open_loop_slow.yaml embodiment: component: ../../../components/embodiments/ur5_dh_pgi_140_80.yaml diff --git a/examples/sim/demo/fold_tshirt.py b/examples/sim/demo/fold_tshirt.py index 50f0f975ba..c0424a795e 100644 --- a/examples/sim/demo/fold_tshirt.py +++ b/examples/sim/demo/fold_tshirt.py @@ -354,19 +354,22 @@ def initialize_simulation( use_cuda_graph=use_cuda_graph, solver_cfg={ "solver_type": "dexuni", - "iterations": SOLVER_ITERATIONS, - "particle_enable_self_contact": True, - "particle_self_contact_radius": 0.002, - "particle_self_contact_margin": 0.002, - "particle_topological_contact_filter_threshold": 1, - "particle_rest_shape_contact_exclusion_radius": 0.005, - "particle_vertex_contact_buffer_size": 96, - "particle_edge_contact_buffer_size": 128, - "particle_collision_detection_interval": -1, - "self_contact_bvh_rebuild_interval_frames": 15, - "rigid_contact_max": 0, - "step_rigid_bodies": False, - "soft_contact_margin": 0.008, + "joint_mode": "kinematic", + "contact_mode": "soft", + "vbd_options": { + "iterations": SOLVER_ITERATIONS, + "particle_enable_self_contact": True, + "particle_self_contact_radius": 0.002, + "particle_self_contact_margin": 0.002, + "particle_topological_contact_filter_threshold": 1, + "particle_rest_shape_contact_exclusion_radius": 0.005, + "particle_vertex_contact_buffer_size": 96, + "particle_edge_contact_buffer_size": 128, + "particle_collision_detection_interval": -1, + }, + "collision_options": { + "soft_contact_margin": 0.008, + }, "soft_contact_ke": 3.0e5, "soft_contact_kd": 5.0e-2, "soft_contact_mu": 0.5, diff --git a/examples/sim/demo/grasp_cup_to_caffe.py b/examples/sim/demo/grasp_cup_to_caffe.py index aa3fff4636..a7aed78525 100644 --- a/examples/sim/demo/grasp_cup_to_caffe.py +++ b/examples/sim/demo/grasp_cup_to_caffe.py @@ -92,13 +92,9 @@ def initialize_simulation(args) -> SimulationManager: """ physics_cfg = physics_cfg_for_backend(args.physics) if args.physics == "newton": - # This contact-heavy URDF scene needs Newton's collision pipeline; - # MuJoCo's native contact path is not reliable for these convex meshes. physics_cfg.solver_cfg = { - "solver_type": "mujoco_warp", - "use_mujoco_contacts": False, - "nconmax": 16384, - "njmax": 65536, + "solver_type": "dexuni", + "mujoco_options": {"nconmax": 16384, "njmax": 65536}, } config = SimulationManagerCfg( @@ -217,7 +213,8 @@ def create_table(sim: SimulationManager) -> RigidObject: attrs=RigidBodyPhysicsCfg( mass_props=MassPropertiesCfg(mass=0.5), ), - body_type="kinematic", + # This fixture never moves; static shapes also enter DexUni's MuJoCo view. + body_type="static", init_pos=[1.1, -0.5, 0.08], init_rot=[0.0, 0.0, 0.0], ) diff --git a/examples/sim/demo/pick_up_cloth.py b/examples/sim/demo/pick_up_cloth.py index 1b4839e06c..e9360db754 100644 --- a/examples/sim/demo/pick_up_cloth.py +++ b/examples/sim/demo/pick_up_cloth.py @@ -437,29 +437,28 @@ def main(args: argparse.Namespace | None = None) -> None: num_substeps=12, solver_cfg={ "solver_type": "dexuni", - "iterations": 24, - "particle_enable_self_contact": True, - "particle_self_contact_radius": 0.002, - "particle_self_contact_margin": 0.002, - "particle_topological_contact_filter_threshold": 1, - "particle_rest_shape_contact_exclusion_radius": 0.005, - "particle_vertex_contact_buffer_size": 96, - "particle_edge_contact_buffer_size": 128, - "particle_collision_detection_interval": -1, - "particle_enable_tile_solve": True, - "soft_contact_margin": 0.008, + "joint_mode": "kinematic", + "contact_mode": "soft", + "vbd_options": { + "iterations": 24, + "particle_enable_self_contact": True, + "particle_self_contact_radius": 0.002, + "particle_self_contact_margin": 0.002, + "particle_topological_contact_filter_threshold": 1, + "particle_rest_shape_contact_exclusion_radius": 0.005, + "particle_vertex_contact_buffer_size": 96, + "particle_edge_contact_buffer_size": 128, + "particle_collision_detection_interval": -1, + "particle_enable_tile_solve": True, + "rigid_contact_k_start": CLOTH_RIGID_CONTACT_KE, + "rigid_body_particle_contact_buffer_size": 512, + }, + "collision_options": { + "soft_contact_margin": 0.008, + }, "soft_contact_ke": CLOTH_RIGID_CONTACT_KE, "soft_contact_kd": CLOTH_RIGID_CONTACT_KD, "soft_contact_mu": CLOTH_RIGID_CONTACT_MU, - # Use the mixed material stiffness immediately instead of - # ramping new contacts from the low DexUni default. - "rigid_contact_k_start": CLOTH_RIGID_CONTACT_KE, - "rigid_body_particle_contact_buffer_size": 512, - "rigid_contact_max": 0, - # The registered runtime control advances and interpolates the - # robot kinematically at every Newton substep. - "step_rigid_bodies": False, - "self_contact_bvh_rebuild_interval_frames": 1, }, # DexUni owns its particle-shape contacts and collision detection. collision_cfg=None, diff --git a/examples/sim/demo/press_softbody.py b/examples/sim/demo/press_softbody.py index e5b7209677..0405fee8b4 100644 --- a/examples/sim/demo/press_softbody.py +++ b/examples/sim/demo/press_softbody.py @@ -48,7 +48,7 @@ def parse_arguments(argv: list[str] | None = None) -> argparse.Namespace: import numpy as np import torch -from dexsim.utility.path import get_resources_data_path +from embodichain.data import get_data_path from embodichain.lab.sim import SimulationManager, SimulationManagerCfg from embodichain.lab.sim.cfg import ( NewtonPhysicsCfg, @@ -98,22 +98,26 @@ def initialize_simulation(args: argparse.Namespace) -> SimulationManager: num_substeps=NUM_SUBSTEPS, solver_cfg={ "solver_type": "dexuni", - "iterations": SOLVER_ITERATIONS, - "particle_enable_self_contact": False, - "particle_self_contact_radius": 0.005, - "particle_self_contact_margin": 0.005, - "particle_topological_contact_filter_threshold": 3, - "particle_enable_tile_solve": True, - "rigid_body_particle_contact_buffer_size": 512, - "rigid_contact_k_start": SOFT_CONTACT_KE, - "rigid_contact_max": 0, - "soft_contact_margin": SOFT_CONTACT_MARGIN, + # DexUni keeps solver-specific expert options in namespaced + # mappings. The kinematic trajectory owns the robot joints. + "joint_mode": "kinematic", + "contact_mode": "soft", + "vbd_options": { + "iterations": SOLVER_ITERATIONS, + "particle_enable_self_contact": False, + "particle_self_contact_radius": 0.005, + "particle_self_contact_margin": 0.005, + "particle_topological_contact_filter_threshold": 3, + "particle_enable_tile_solve": True, + "rigid_body_particle_contact_buffer_size": 512, + "rigid_contact_k_start": SOFT_CONTACT_KE, + }, + "collision_options": { + "soft_contact_margin": SOFT_CONTACT_MARGIN, + }, "soft_contact_ke": SOFT_CONTACT_KE, "soft_contact_kd": SOFT_CONTACT_KD, "soft_contact_mu": SOFT_CONTACT_MU, - # The registered trajectory updates the robot kinematically at - # every Newton substep; DexUni only needs to solve the soft body. - "step_rigid_bodies": False, }, # DexUni owns its particle-shape contacts and collision detection. collision_cfg=None, diff --git a/examples/sim/demo/scoop_ice.py b/examples/sim/demo/scoop_ice.py index 8b05f43a0e..a9a1ee313c 100644 --- a/examples/sim/demo/scoop_ice.py +++ b/examples/sim/demo/scoop_ice.py @@ -79,9 +79,12 @@ def initialize_simulation(args): """ physics_cfg = physics_cfg_for_backend(args.physics) if args.physics == "newton": - # Hundreds of free bodies make dense Newton Hessian factorization - # expensive. CG avoids that factorization while retaining contacts. - physics_cfg.solver_cfg = {"solver_type": "mujoco_warp", "solver": "cg"} + physics_cfg.solver_cfg = { + "solver_type": "dexuni", + "mujoco_options": {"solver": "cg"}, + # Dense ice contacts exceed VBD's default per-body capacity of 64. + "vbd_options": {"rigid_body_contact_buffer_size": 256}, + } config = SimulationManagerCfg( headless=True, device=args.device, diff --git a/examples/sim/motion/planners/curobo_planner.py b/examples/sim/motion/planners/curobo_planner.py index 11b7e3f373..a983deb940 100644 --- a/examples/sim/motion/planners/curobo_planner.py +++ b/examples/sim/motion/planners/curobo_planner.py @@ -502,9 +502,9 @@ def _build_scene( init_rot=(0.0, 0.0, 0.0), ) ) + sim.prepare() finally: demo_block_mesh_path.unlink(missing_ok=True) - sim.prepare() if robot_type == "w1": # Keep the W1-specific IK diagnostic batched so it remains useful when @@ -861,7 +861,10 @@ def main(args: argparse.Namespace | None = None) -> None: binding, motion_policy, ), - ) + ), + context=engine.initial_context( + control_dt=sim.sim_config.physics_dt * args.step_repeat, + ), ) success = compiled.plan_success trajectory = compiled.trajectory.positions @@ -899,7 +902,10 @@ def main(args: argparse.Namespace | None = None) -> None: binding, motion_policy, ), - ) + ), + context=engine.initial_context( + control_dt=sim.sim_config.physics_dt * args.step_repeat, + ), ) success = compiled.plan_success trajectory = compiled.trajectory.positions diff --git a/examples/sim/motion/trajectory_velocity_tracking.py b/examples/sim/motion/trajectory_velocity_tracking.py index 9d1145ad12..8772c98f67 100644 --- a/examples/sim/motion/trajectory_velocity_tracking.py +++ b/examples/sim/motion/trajectory_velocity_tracking.py @@ -396,8 +396,7 @@ def main(args: argparse.Namespace | None = None) -> None: robot: Robot = sim.add_robot( FrankaPandaCfg.from_dict({"uid": "tracking_franka"}) ) - if sim.is_use_gpu_physics: - sim.init_gpu_physics() + sim.prepare() if not args.headless: sim.open_window() common_initial_qpos = robot.get_qpos().clone() diff --git a/pyproject.toml b/pyproject.toml index bef5415c44..0a74511358 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,7 +25,7 @@ dynamic = ["version"] # Core install dependencies (kept from requirements.txt). dependencies = [ - "dexsim_engine==0.5.1rc1", + "dexsim_engine>=0.5.1rc1,<0.6", "setuptools>=78.1.1", "gymnasium>=0.29.1", "huggingface_hub>=0.35.3,<1.0", diff --git a/scripts/tutorials/atomic_action/axis_align.py b/scripts/tutorials/atomic_action/axis_align.py index 5f9906b7a4..3b231f1fca 100644 --- a/scripts/tutorials/atomic_action/axis_align.py +++ b/scripts/tutorials/atomic_action/axis_align.py @@ -42,6 +42,8 @@ from embodichain.lab.sim.shapes import CubeCfg from embodichain.utils import logger from scripts.tutorials.atomic_action.tutorial_utils import ( + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, add_ur5_gripper_robot, clone_local_pose_from_first_env, create_affordance_sampling_context, @@ -152,7 +154,14 @@ def main() -> None: sim = create_tutorial_simulation(args) robot = add_ur5_gripper_robot(sim, tcp_z=0.15) obj = create_align_object(sim) - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS + if args.robot == "ur5" and sim.physics.solver_type == "dexuni" + else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) initialize_pre_pick_robot_pose(robot, obj, hand_open) motion_gen = create_toppra_motion_generator( robot, diff --git a/scripts/tutorials/atomic_action/coordinated_pickment.py b/scripts/tutorials/atomic_action/coordinated_pickment.py index 33c62d5e03..c3a3df1b42 100644 --- a/scripts/tutorials/atomic_action/coordinated_pickment.py +++ b/scripts/tutorials/atomic_action/coordinated_pickment.py @@ -161,6 +161,8 @@ class PickmentObjectPreset: PICKMENT_HAND_INTERP_STEPS = 10 PICKMENT_HOLD_STEPS = 4 ROBOTIQ_2F_140_CLOSE_QPOS = 0.7 +# Calibrated for the plastic tray and UR5/DH PGI one-way contact. +DEXUNI_TRAY_CLOSE_QPOS = 0.023 TRAJECTORY_SIM_STEPS = 4 @@ -189,6 +191,8 @@ def parse_arguments() -> argparse.Namespace: def create_dual_robot( sim: SimulationManager, robot_type: TutorialRobot, + *, + newton_gripper_friction: float | None = None, ) -> Robot: """Create the selected dual-arm robot with its matching grippers.""" return add_dual_tutorial_robot( @@ -198,6 +202,7 @@ def create_dual_robot( urdf_name=f"dual_{robot_type}_coordinated_pickment", tcp_z=GRIPPER_TCP_Z, solver="pytorch", + newton_gripper_friction=newton_gripper_friction, ) @@ -395,6 +400,12 @@ def run_coordinated_pickment_demo( hand_close_qpos = ( ROBOTIQ_2F_140_CLOSE_QPOS if args.robot == "ur10" else preset.hand_close_qpos ) + if ( + args.robot == "ur5" + and args.object == "plastic_tray" + and sim.physics.solver_type == "dexuni" + ): + hand_close_qpos = DEXUNI_TRAY_CLOSE_QPOS left_open, left_close = get_hand_open_close_qpos( robot, hand_control_part="left_hand", @@ -527,8 +538,27 @@ def main() -> None: sim = create_tutorial_simulation( args, arena_space=3.0, + newton_solver_options=( + { + "vbd_options": {"rigid_contact_history": True}, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" + and args.robot == "ur5" + and args.object == "plastic_tray" + else None + ), + ) + use_vbd_grasp = ( + args.robot == "ur5" + and args.object == "plastic_tray" + and sim.physics.solver_type == "dexuni" + ) + robot = create_dual_robot( + sim, + args.robot, + newton_gripper_friction=4.0 if use_vbd_grasp else None, ) - robot = create_dual_robot(sim, args.robot) run_coordinated_pickment_demo(args, sim, robot) diff --git a/scripts/tutorials/atomic_action/coordinated_placement.py b/scripts/tutorials/atomic_action/coordinated_placement.py index 6bd7c08e3f..b8049f5ca4 100644 --- a/scripts/tutorials/atomic_action/coordinated_placement.py +++ b/scripts/tutorials/atomic_action/coordinated_placement.py @@ -235,7 +235,8 @@ def create_table(sim: SimulationManager) -> RigidObject: static_friction=0.95, restitution=0.01, ), - body_type="kinematic", + # This fixture never moves; static shapes also enter DexUni's MuJoCo view. + body_type="static", init_pos=list(TABLE_INIT_POS), init_rot=list(TABLE_INIT_ROT), ) diff --git a/scripts/tutorials/atomic_action/hand_over.py b/scripts/tutorials/atomic_action/hand_over.py index a292730703..62541bd60f 100644 --- a/scripts/tutorials/atomic_action/hand_over.py +++ b/scripts/tutorials/atomic_action/hand_over.py @@ -19,6 +19,7 @@ from __future__ import annotations import argparse +import math import sys from pathlib import Path @@ -91,6 +92,9 @@ # --------------------------------------------------------------------------- HAND_CLOSE_QPOS = 0.04 +DEXUNI_CAN_LEFT_CLOSE_QPOS = 0.007 +DEXUNI_CAN_RIGHT_CLOSE_QPOS = 0.0055 +DEXUNI_RELEASE_SETTLE_DURATION = 1.0 HANDOVER_SAMPLE_INTERVAL = 220 HANDOVER_HAND_INTERP_STEPS = 10 HANDOVER_PRE_GRASP_DISTANCE = 0.08 @@ -127,6 +131,8 @@ def parse_arguments() -> argparse.Namespace: def create_dual_robot( sim: SimulationManager, robot_type: TutorialRobot, + *, + newton_gripper_friction: float | None = None, ) -> Robot: """Create the selected dual-arm robot with its matching grippers.""" return add_dual_tutorial_robot( @@ -139,6 +145,7 @@ def create_dual_robot( hand_stiffness=1e3, hand_damping=1e2, hand_max_effort=1e4, + newton_gripper_friction=newton_gripper_friction, ) @@ -212,11 +219,20 @@ def run_handover_demo( planner=getattr(args, "planner", "trapezoidal"), ) + use_vbd_grasp = ( + args.robot == "ur5" + and not args.is_horizontal + and sim.physics.solver_type == "dexuni" + ) left_open, left_close = get_hand_open_close_qpos( - robot, hand_control_part="left_hand", close_qpos=HAND_CLOSE_QPOS + robot, + hand_control_part="left_hand", + close_qpos=DEXUNI_CAN_LEFT_CLOSE_QPOS if use_vbd_grasp else HAND_CLOSE_QPOS, ) right_open, right_close = get_hand_open_close_qpos( - robot, hand_control_part="right_hand", close_qpos=HAND_CLOSE_QPOS + robot, + hand_control_part="right_hand", + close_qpos=DEXUNI_CAN_RIGHT_CLOSE_QPOS if use_vbd_grasp else HAND_CLOSE_QPOS, ) final_pose = torch.eye(4, dtype=torch.float32) @@ -297,6 +313,17 @@ def run_handover_demo( if wait_for_user: input("Press Enter to execute the handover...") + release_start = compiled.segment(0, "receive_release").start + + def settle_before_release(step_index: int, _total_steps: int) -> None: + if step_index == release_start - 1: + # Let the receiving arm stop before opening the gripper. + sim.update( + step=math.ceil( + DEXUNI_RELEASE_SETTLE_DURATION / sim.sim_config.physics_dt + ) + ) + replay_trajectory( sim, robot, @@ -305,6 +332,7 @@ def run_handover_demo( video_prefix="handover_auto_play", hold_steps=0, trajectory_sim_steps=TRAJECTORY_SIM_STEPS, + on_trajectory_step=settle_before_release if use_vbd_grasp else None, look_at=HANDOVER_RECORD_LOOK_AT, ) if wait_for_user: @@ -317,8 +345,27 @@ def main() -> None: sim = create_tutorial_simulation( args, arena_space=3.0, + newton_solver_options=( + { + "vbd_options": {"rigid_contact_history": True}, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" + and args.robot == "ur5" + and not args.is_horizontal + else None + ), + ) + use_vbd_grasp = ( + args.robot == "ur5" + and not args.is_horizontal + and sim.physics.solver_type == "dexuni" + ) + robot = create_dual_robot( + sim, + args.robot, + newton_gripper_friction=4.0 if use_vbd_grasp else None, ) - robot = create_dual_robot(sim, args.robot) run_handover_demo(args, sim, robot) serve_tutorial_scene(sim, args) diff --git a/scripts/tutorials/atomic_action/move_held_object.py b/scripts/tutorials/atomic_action/move_held_object.py index 8b98d1a19b..e8658decf0 100644 --- a/scripts/tutorials/atomic_action/move_held_object.py +++ b/scripts/tutorials/atomic_action/move_held_object.py @@ -27,6 +27,7 @@ sys.path.insert(0, str(_REPO_ROOT)) import torch +import trimesh from embodichain.data import get_data_path from embodichain.lab.sim.atomic_actions import ( @@ -43,6 +44,7 @@ from embodichain.lab.sim.shapes import MeshCfg, MeshCollisionCfg from embodichain.utils import logger from scripts.tutorials.atomic_action.tutorial_utils import ( + DEFAULT_GRIPPER_CLOSE_QPOS, add_tutorial_robot, broadcast_pose_batch, clone_local_pose_from_first_env, @@ -74,6 +76,8 @@ MOVE_HELD_OBJECT_SAMPLE_INTERVAL = 120 HAND_INTERP_STEPS = 12 POST_TRAJECTORY_STEPS = 240 +# Candidate for this cup and UR5/DH PGI; transport orientation remains unaligned. +DEXUNI_CUP_CLOSE_QPOS = 0.0115 def parse_arguments() -> argparse.Namespace: @@ -87,11 +91,16 @@ def parse_arguments() -> argparse.Namespace: def create_pick_object(sim) -> RigidObject: """Create and settle the paper cup used by the tutorial.""" + mesh = trimesh.load_mesh(get_data_path(OBJECT_MESH_PATH), process=False) + # Author cup proportions in the mesh; physical body scale must be uniform. + mesh.apply_scale((0.75, 0.75, 1.0)) obj = sim.add_rigid_object( cfg=RigidObjectCfg( uid="paper_cup", shape=MeshCfg( - fpath=get_data_path(OBJECT_MESH_PATH), + vertices=mesh.vertices, + triangles=mesh.faces, + normals=mesh.vertex_normals, collision=MeshCollisionCfg( approximation="convex_decomposition", max_hulls=16, @@ -104,7 +113,6 @@ def create_pick_object(sim) -> RigidObject: newton_contact=sim.is_newton_backend, ), init_pos=[*OBJECT_XY, OBJECT_INITIAL_Z], - body_scale=(0.75, 0.75, 1.0), ) ) sim.prepare() @@ -129,15 +137,35 @@ def make_object_target_pose(device: torch.device) -> torch.Tensor: def main() -> None: """Plan MoveEndEffector -> PickUp -> MoveHeldObject.""" args = parse_arguments() - sim = create_tutorial_simulation(args) - robot = add_tutorial_robot(sim, args.robot) + sim = create_tutorial_simulation( + args, + newton_solver_options=( + { + "vbd_options": {"rigid_contact_history": True}, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" and args.robot == "ur5" + else None + ), + ) + use_vbd_grasp = args.robot == "ur5" and sim.physics.solver_type == "dexuni" + robot = add_tutorial_robot( + sim, + args.robot, + newton_gripper_friction=4.0 if use_vbd_grasp else None, + ) obj = create_pick_object(sim) sim.prepare() motion_gen = create_curobo_motion_generator( robot, planner=getattr(args, "planner", "trapezoidal"), ) - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUP_CLOSE_QPOS if use_vbd_grasp else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) engine = create_simulation_atomic_action_engine( motion_generator=motion_gen, diff --git a/scripts/tutorials/atomic_action/pickup.py b/scripts/tutorials/atomic_action/pickup.py index b1c015168e..fd22880c67 100644 --- a/scripts/tutorials/atomic_action/pickup.py +++ b/scripts/tutorials/atomic_action/pickup.py @@ -40,6 +40,8 @@ from embodichain.lab.sim.shapes import CubeCfg from embodichain.utils import logger from scripts.tutorials.atomic_action.tutorial_utils import ( + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, add_tutorial_robot, clone_local_pose_from_first_env, create_affordance_sampling_context, @@ -138,7 +140,14 @@ def main() -> None: robot = add_tutorial_robot(sim, args.robot, tcp_z=0.15) obj = create_pick_object(sim) sim.prepare() - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS + if args.robot == "ur5" and sim.physics.solver_type == "dexuni" + else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) initialize_pre_pick_robot_pose(robot, obj, hand_open) motion_gen = create_curobo_motion_generator( robot, diff --git a/scripts/tutorials/atomic_action/place.py b/scripts/tutorials/atomic_action/place.py index a60a3efbc4..d476e9a4fc 100644 --- a/scripts/tutorials/atomic_action/place.py +++ b/scripts/tutorials/atomic_action/place.py @@ -42,6 +42,8 @@ from embodichain.lab.sim.shapes import CubeCfg from embodichain.utils import logger from scripts.tutorials.atomic_action.tutorial_utils import ( + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, add_tutorial_robot, broadcast_pose_batch, broadcast_waypoint_pose_batch, @@ -91,6 +93,8 @@ def parse_arguments() -> argparse.Namespace: def create_pick_object(sim) -> RigidObject: """Create a settled cube for the PickUp and Place sequence.""" + # Start outside the default ground's 1 mm VBD contact margin. + ground_clearance = 0.001 if sim.physics.solver_type == "dexuni" else 0.0 obj = sim.add_rigid_object( cfg=RigidObjectCfg( uid="cube", @@ -103,7 +107,7 @@ def create_pick_object(sim) -> RigidObject: angular_damping=0.2, newton_contact=sim.is_newton_backend, ), - init_pos=[*OBJECT_XY, 0.5 * OBJECT_SIZE[2]], + init_pos=[*OBJECT_XY, 0.5 * OBJECT_SIZE[2] + ground_clearance], ) ) sim.prepare() @@ -135,15 +139,41 @@ def make_place_eef_poses(device: torch.device) -> torch.Tensor: def main() -> None: """Plan and replay PickUp followed by a multi-waypoint Place.""" args = parse_arguments() - sim = create_tutorial_simulation(args) - robot = add_tutorial_robot(sim, args.robot, tcp_z=0.15) + sim = create_tutorial_simulation( + args, + newton_solver_options=( + { + "vbd_options": { + "rigid_contact_history": True, + "rigid_avbd_contact_alpha": 0.5, + }, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" and args.robot == "ur5" + else None + ), + ) + use_vbd_grasp = sim.physics.solver_type == "dexuni" and args.robot == "ur5" + robot = add_tutorial_robot( + sim, + args.robot, + tcp_z=0.15, + newton_gripper_friction=4.0 if use_vbd_grasp else None, + # Reach the release waypoint before the fingers finish opening. + arm_damping=1000.0 if use_vbd_grasp else None, + ) obj = create_pick_object(sim) sim.prepare() motion_gen = create_curobo_motion_generator( robot, planner=getattr(args, "planner", "trapezoidal"), ) - hand_open, hand_close = get_hand_open_close_qpos(robot) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS if use_vbd_grasp else DEFAULT_GRIPPER_CLOSE_QPOS + ), + ) initialize_pre_pick_robot_pose(robot, obj, hand_open) engine = create_simulation_atomic_action_engine( diff --git a/scripts/tutorials/atomic_action/pour.py b/scripts/tutorials/atomic_action/pour.py index 34c2ef0e4c..5b3ce79c02 100644 --- a/scripts/tutorials/atomic_action/pour.py +++ b/scripts/tutorials/atomic_action/pour.py @@ -39,17 +39,19 @@ PourOptions, ) from embodichain.lab.sim.motion.planners import ( - TrapezoidalPlanOptions, ToppraPlanOptions, TrajectorySampleMethod, ) from embodichain.utils import logger from scripts.tutorials.atomic_action.axis_align import ( + OBJECT_SIZE, create_align_object, create_axis_align_semantics, ) from scripts.tutorials.atomic_action.tutorial_utils import ( - add_ur5_gripper_robot, + DEFAULT_GRIPPER_CLOSE_QPOS, + DEXUNI_CUBE_CLOSE_QPOS, + add_tutorial_robot, create_parallel_jaw_grasp_pose_generator, create_toppra_motion_generator, create_tutorial_argument_parser, @@ -71,7 +73,8 @@ HAND_INTERP_STEPS = 12 POST_TRAJECTORY_STEPS = 240 PICK_MOTION_SAMPLE_COUNT = PICK_SAMPLE_INTERVAL - HAND_INTERP_STEPS -OBJ_POSITION = (-0.5, 0.0, 0.0) +# Start above the ground so penetration recovery does not move the grasp target. +OBJ_POSITION = (-0.5, 0.0, 0.5 * OBJECT_SIZE[2]) RECORD_LOOK_AT = ( (-1.5, 0.2, 1.2), (-0.4, 0.0, 0.4), @@ -103,14 +106,9 @@ def _create_pick_motion_policy( sample_method=TrajectorySampleMethod.QUANTITY, sample_interval=PICK_MOTION_SAMPLE_COUNT, ) - elif planner == "trapezoidal": - plan_opts = TrapezoidalPlanOptions( - sample_method=TrajectorySampleMethod.QUANTITY, - sample_interval=PICK_MOTION_SAMPLE_COUNT, - ) - elif planner == "curobo": - # cuRobo owns its native sampling and does not use the joint-space - # sample-count options above. + elif planner in ("trapezoidal", "curobo"): + # Preserve native waypoints before PickUp resamples its two motion + # segments. A fixed quantity can be smaller than the Cartesian IK path. plan_opts = None else: raise ValueError(f"Unsupported tutorial planner {planner!r}.") @@ -124,13 +122,38 @@ def _create_pick_motion_policy( def main() -> None: """Plan and replay PickUp followed by Pour.""" args = parse_arguments() - sim = create_tutorial_simulation(args) - robot = add_ur5_gripper_robot(sim, tcp_z=0.15) + sim = create_tutorial_simulation( + args, + newton_solver_options=( + { + "vbd_options": {"rigid_contact_history": True}, + "collision_options": {"contact_matching": "latest"}, + } + if args.physics == "newton" + else None + ), + ) + use_vbd_grasp = sim.physics.solver_type == "dexuni" + robot = add_tutorial_robot( + sim, + "ur5", + tcp_z=0.15, + newton_gripper_friction=4.0 if use_vbd_grasp else None, + ) + obj_position = list(OBJ_POSITION) + if use_vbd_grasp: + # Start outside the default ground's 1 mm VBD contact margin. + obj_position[2] += 0.001 obj = create_align_object( sim, - obj_position=OBJ_POSITION, + obj_position=obj_position, + ) + hand_open, hand_close = get_hand_open_close_qpos( + robot, + close_qpos=( + DEXUNI_CUBE_CLOSE_QPOS if use_vbd_grasp else DEFAULT_GRIPPER_CLOSE_QPOS + ), ) - hand_open, hand_close = get_hand_open_close_qpos(robot) initialize_pre_pick_robot_pose(robot, obj, hand_open) planner = getattr(args, "planner", "trapezoidal") motion_gen = create_toppra_motion_generator(robot, planner=planner) diff --git a/scripts/tutorials/atomic_action/scenario_utils.py b/scripts/tutorials/atomic_action/scenario_utils.py index d52e6bd996..10cf327425 100644 --- a/scripts/tutorials/atomic_action/scenario_utils.py +++ b/scripts/tutorials/atomic_action/scenario_utils.py @@ -234,6 +234,7 @@ def add_dual_tutorial_robot( hand_stiffness: float = 1e3, hand_damping: float = 1e2, hand_max_effort: float = 1e4, + newton_gripper_friction: float | None = None, ) -> Robot: """Add a supported dual-arm tutorial robot to a simulation. @@ -254,6 +255,7 @@ def add_dual_tutorial_robot( hand_stiffness: Hand joint drive stiffness. hand_damping: Hand joint drive damping. hand_max_effort: Hand joint maximum effort. + newton_gripper_friction: Optional Newton gripper sliding friction. Returns: The added dual-arm robot instance. @@ -274,7 +276,7 @@ def add_dual_tutorial_robot( hand_damping=hand_damping, hand_max_effort=hand_max_effort, ) - configure_newton_gripper_contacts(sim, robot_cfg) + configure_newton_gripper_contacts(sim, robot_cfg, friction=newton_gripper_friction) return sim.add_robot(cfg=robot_cfg) diff --git a/scripts/tutorials/atomic_action/slide.py b/scripts/tutorials/atomic_action/slide.py index e0c4f6dff6..00de32564a 100644 --- a/scripts/tutorials/atomic_action/slide.py +++ b/scripts/tutorials/atomic_action/slide.py @@ -98,10 +98,16 @@ def create_drawer( init_pos=DRAWER_POSITION, init_rot=DRAWER_ORIENTATION, init_qpos=(0.0,), - joint_drive_props=JointDrivePropertiesCfg(drive_type="none"), + joint_drive_props=JointDrivePropertiesCfg( + drive_type="force", + target_mode="velocity", + stiffness=0.0, + damping=5.0, + ), attrs=create_tutorial_rigid_body_physics( static_friction=1.0, dynamic_friction=1.0, + linear_damping=0.0, ), ) configure_newton_link_contacts( diff --git a/scripts/tutorials/atomic_action/tutorial_utils.py b/scripts/tutorials/atomic_action/tutorial_utils.py index 090855bbce..b88cf90ebb 100644 --- a/scripts/tutorials/atomic_action/tutorial_utils.py +++ b/scripts/tutorials/atomic_action/tutorial_utils.py @@ -114,6 +114,9 @@ palm_depth=0.096, ) DEFAULT_GRIPPER_CLOSE_QPOS = 0.036 +# Calibrated for the UR5's DH-PGI gripper holding the tutorial's 5 cm cube. +# DexUni's one-way contacts cannot stop the fingers at an oversized close target. +DEXUNI_CUBE_CLOSE_QPOS = 0.013 NEWTON_GRASP_CONTACT_STIFFNESS = 4.0e4 NEWTON_GRASP_CONTACT_DAMPING = 4.0e2 # MuJoCo-Warp's default contact dimension (3) has no torsional friction. A @@ -1156,10 +1159,8 @@ def _tutorial_physics_cfg( ) -> PhysicsBackendCfg: """Build the shared physics configuration for atomic-action tutorials. - Newton tutorials intentionally use MuJoCo Warp's native collision path. - It generates contacts inside every solver substep, so an external Newton - collision pipeline would be unused and would only allocate unnecessary - contact buffers. + DexUni owns collision detection and retains MuJoCo articulation drives. + Free rigid bodies use VBD with the solver's native one-way coupling. """ physics_cfg = physics_cfg_for_backend(backend) if isinstance(physics_cfg, NewtonPhysicsCfg): @@ -1168,20 +1169,21 @@ def _tutorial_physics_cfg( # contacts and fast-changing manipulator loads. DexSim sizes # contact/constraint buffers from the finalized scene, including the # contact dimensions authored on gripper and object surfaces. - # MultiCCD retains up to four contacts per gripper-mesh/object pair, - # which prevents a marginal two-finger grasp from sliding away. + # Retain the existing MuJoCo articulation tuning. Free-body contacts + # remain VBD-owned and do not inherit these MuJoCo contact options. physics_cfg.num_substeps = 20 physics_cfg.collision_cfg = None physics_cfg.solver_cfg = { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } return physics_cfg @@ -1191,6 +1193,7 @@ def create_tutorial_simulation( *, arena_space: float = 2.5, sun_direction: Sequence[float] = DEFAULT_TUTORIAL_SUN_DIRECTION, + newton_solver_options: Mapping[str, object] | None = None, ) -> SimulationManager: """Create the shared simulation setup used by atomic-action tutorials. @@ -1200,10 +1203,17 @@ def create_tutorial_simulation( arena_space: Spacing between parallel simulation arenas in meters. sun_direction: Direction of the single global sun light. The vector points from the light toward the scene. + newton_solver_options: Scene-specific overrides for the Newton solver + configuration, such as VBD contact history and collision matching. Returns: A simulation manager with the tutorial key light configured. """ + physics_cfg = _tutorial_physics_cfg(getattr(args, "physics", "default")) + if newton_solver_options is not None: + if not isinstance(physics_cfg, NewtonPhysicsCfg): + raise ValueError("Newton solver options require the Newton backend.") + physics_cfg.solver_cfg = {**physics_cfg.solver_cfg, **newton_solver_options} sim = SimulationManager( SimulationManagerCfg( width=VIEWER_WIDTH, @@ -1211,7 +1221,7 @@ def create_tutorial_simulation( headless=True, num_envs=args.num_envs, device=args.device, - physics_cfg=_tutorial_physics_cfg(getattr(args, "physics", "default")), + physics_cfg=physics_cfg, render_cfg=RenderCfg(renderer=args.renderer), physics_dt=1.0 / 100.0, arena_space=arena_space, @@ -1296,6 +1306,9 @@ def add_tutorial_robot( robot_type: TutorialRobot, init_pos: Sequence[float] = (0.0, 0.0, 0.0), init_qpos: Sequence[float] | None = None, + *, + newton_gripper_friction: float | None = None, + arm_damping: float | None = None, **kwargs, ) -> Robot: """Add a selected tutorial robot with the shared PGI gripper. @@ -1305,6 +1318,9 @@ def add_tutorial_robot( robot_type: Tutorial robot family to construct. init_pos: Root position of the robot in its arena. init_qpos: Optional full robot joint configuration. + newton_gripper_friction: Optional sliding-friction coefficient for the + Newton gripper collision links; object materials are unaffected. + arm_damping: Optional drive damping for the arm control part. Returns: The added robot instance. @@ -1318,7 +1334,9 @@ def add_tutorial_robot( init_qpos=init_qpos, **kwargs, ) - configure_newton_gripper_contacts(sim, robot_cfg) + if arm_damping is not None: + robot_cfg.joint_drive_props.damping["arm"] = arm_damping + configure_newton_gripper_contacts(sim, robot_cfg, friction=newton_gripper_friction) return sim.add_robot(cfg=robot_cfg) @@ -1507,9 +1525,19 @@ def configure_newton_link_contacts( def configure_newton_gripper_contacts( sim: SimulationManager, robot_cfg: RobotCfg, + *, + friction: float | None = None, ) -> None: - """Configure Newton gripper contacts and recompute their source inertia.""" + """Configure Newton gripper contacts and recompute their source inertia. + + Args: + sim: Simulation manager that owns the robot. + robot_cfg: Robot configuration to update before spawning. + friction: Optional static and dynamic sliding-friction coefficient. + """ if not sim.is_newton_backend: + if friction is not None: + raise ValueError("Newton gripper friction requires the Newton backend.") return configure_newton_link_contacts( @@ -1518,9 +1546,11 @@ def configure_newton_gripper_contacts( group_name="newton_gripper_contacts", link_names_expr=[_GRIPPER_CONTACT_LINK_PATTERN], ) - robot_cfg.link_attrs["newton_gripper_contacts"].attrs.mass_props = ( - MassPropertiesCfg(recompute_inertia=True) - ) + attrs = robot_cfg.link_attrs["newton_gripper_contacts"].attrs + attrs.mass_props = MassPropertiesCfg(recompute_inertia=True) + if friction is not None: + attrs.material_props.static_friction = friction + attrs.material_props.dynamic_friction = friction def create_trapezoidal_motion_generator( @@ -2442,6 +2472,7 @@ def create_tutorial_robot_cfg( "DEFAULT_AXIS_LEN", "DEFAULT_AXIS_SIZE", "DEFAULT_GRIPPER_CLOSE_QPOS", + "DEXUNI_CUBE_CLOSE_QPOS", "DEFAULT_TUTORIAL_SUN_DIRECTION", "DEFAULT_TUTORIAL_SUN_INTENSITY", "GRIPPER_HAND_JOINT_PATTERN", diff --git a/scripts/tutorials/gym/modular_env.py b/scripts/tutorials/gym/modular_env.py index f7d33c5d75..e33bdc33c4 100644 --- a/scripts/tutorials/gym/modular_env.py +++ b/scripts/tutorials/gym/modular_env.py @@ -211,7 +211,7 @@ class ExampleCfg(EmbodiedEnvCfg): shape=MeshCfg( fpath=get_data_path("TableWare/tableware/fork/standard_fork_scale.ply"), ), - body_scale=(0.75, 0.75, 1.0), + body_scale=(0.75, 0.75, 0.75), init_pos=(0.8, 0, 1.0), ), ] diff --git a/scripts/tutorials/sim/open_drawer.py b/scripts/tutorials/sim/open_drawer.py index 58f893e342..ecd40c17b9 100644 --- a/scripts/tutorials/sim/open_drawer.py +++ b/scripts/tutorials/sim/open_drawer.py @@ -530,22 +530,21 @@ def _tutorial_physics_cfg( """Build the physics configuration used by this tutorial.""" physics_cfg = physics_cfg_for_backend(backend) if isinstance(physics_cfg, NewtonPhysicsCfg): - # Keep the same MuJoCo-Warp solver/contact profile as the atomic-action - # tutorial and packaged Newton task. The finer drawer substep cadence - # is intentional; per-world capacities remain auto-sized by DexSim - # from the finalized scene. + # Share the DexUni articulation profile with the atomic-action tutorials. + # Keep the finer drawer substep cadence and native contact ownership. physics_cfg.num_substeps = 20 physics_cfg.collision_cfg = None physics_cfg.solver_cfg = { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } return physics_cfg diff --git a/tests/conftest.py b/tests/conftest.py index 44b48cae8c..ab4c6550b0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,6 +26,15 @@ os.environ.setdefault("EMBODICHAIN_SIM_EXIT_PROCESS", "0") +@pytest.fixture +def requires_dexuni_two_way() -> None: + """Qualify dev-only contracts without excluding released-engine coverage.""" + from dexsim.engine.newton_physics import DexUniSolverCfg + + if "coupling" not in inspect.signature(DexUniSolverCfg).parameters: + pytest.skip("DexUni two-way coupling requires DexSim dev f32785317 or newer.") + + @pytest.fixture(scope="session") def _discover_task_packages(): """Discover all installed task packages once per test session. diff --git a/tests/gym/envs/task_program/test_task_vertical_slices.py b/tests/gym/envs/task_program/test_task_vertical_slices.py index 5fcf845f47..c8a5e50265 100644 --- a/tests/gym/envs/task_program/test_task_vertical_slices.py +++ b/tests/gym/envs/task_program/test_task_vertical_slices.py @@ -402,15 +402,16 @@ def test_newton_contact_overlays_are_scoped_in_packaged_manipulation_configs() - assert cube_physics.num_substeps == 20 assert cube_physics.collision_cfg is None assert cube_physics.solver_cfg == { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1000.0, + "enable_multiccd": True, + }, } cube_collision = cube_cfg.rigid_object[0].attrs.collision_props assert isinstance(cube_collision, NewtonCollisionPropertiesCfg) diff --git a/tests/sim/atomic_actions/test_newton_collision_pipeline_schedule.py b/tests/sim/atomic_actions/test_newton_collision_pipeline_schedule.py index 2ea7c730b5..9f977d3de1 100644 --- a/tests/sim/atomic_actions/test_newton_collision_pipeline_schedule.py +++ b/tests/sim/atomic_actions/test_newton_collision_pipeline_schedule.py @@ -34,4 +34,14 @@ def test_atomic_action_tutorial_disables_external_collision_pipeline() -> None: assert physics_cfg.collision_cfg is None dexsim_cfg = physics_cfg.to_dexsim_cfg(gpu_id=0) assert dexsim_cfg.collision_pipeline_cfg is None - assert dexsim_cfg.solver_cfg.use_mujoco_contacts is True + assert dexsim_cfg.solver_cfg.solver_type == "dexuni" + assert dexsim_cfg.solver_cfg.mujoco_options == { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + } + assert dexsim_cfg.solver_cfg.vbd_options == {} diff --git a/tests/sim/atomic_actions/test_tutorial_utils.py b/tests/sim/atomic_actions/test_tutorial_utils.py index 987b2c1aa9..037a2851b5 100644 --- a/tests/sim/atomic_actions/test_tutorial_utils.py +++ b/tests/sim/atomic_actions/test_tutorial_utils.py @@ -268,7 +268,7 @@ def _run_obstacle_animation(*, pace_wall_time: bool) -> tuple[MagicMock, MagicMo return obstacle, adapter -def test_atomic_action_tutorial_uses_native_mujoco_contact_settings() -> None: +def test_atomic_action_tutorial_uses_native_dexuni_settings() -> None: module = importlib.import_module("scripts.tutorials.atomic_action.tutorial_utils") default_cfg = module._tutorial_physics_cfg("default") @@ -279,29 +279,24 @@ def test_atomic_action_tutorial_uses_native_mujoco_contact_settings() -> None: assert newton_cfg.num_substeps == 20 assert newton_cfg.collision_cfg is None assert newton_cfg.solver_cfg == { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } dexsim_cfg = newton_cfg.to_dexsim_cfg(gpu_id=0) - assert dexsim_cfg.solver_cfg.solver_type == "mujoco_warp" - assert dexsim_cfg.solver_cfg.solver == "newton" - assert dexsim_cfg.solver_cfg.integrator == "implicitfast" - assert dexsim_cfg.solver_cfg.iterations == 20 - assert dexsim_cfg.solver_cfg.ls_iterations == 100 - assert dexsim_cfg.solver_cfg.nconmax is None - assert dexsim_cfg.solver_cfg.njmax is None - assert dexsim_cfg.solver_cfg.cone == "elliptic" - assert dexsim_cfg.solver_cfg.impratio == pytest.approx(1_000.0) - assert dexsim_cfg.solver_cfg.use_mujoco_contacts is True - assert dexsim_cfg.solver_cfg.enable_multiccd is True + assert dexsim_cfg.solver_cfg.solver_type == "dexuni" + assert ( + dexsim_cfg.solver_cfg.mujoco_options == newton_cfg.solver_cfg["mujoco_options"] + ) + assert dexsim_cfg.solver_cfg.vbd_options == {} assert dexsim_cfg.collision_pipeline_cfg is None @@ -1023,6 +1018,7 @@ def test_place_tutorial_registers_pick_object_with_simulation_engine_factory() - n_sample=1, no_vis_eef_axis=True, robot="ur5", + physics="default", ) sim = MagicMock() sim.device = torch.device("cpu") @@ -1316,9 +1312,13 @@ def test_pour_tutorial_uses_configured_pickup_and_local_rotation_axis() -> None: assert module.POUR_INTERNAL_AXIS == (1.0, 0.0, 0.0) pick_policy = module._create_pick_motion_policy() assert pick_policy.sample_count == module.PICK_SAMPLE_INTERVAL - assert isinstance(pick_policy.plan_opts, module.TrapezoidalPlanOptions) - assert pick_policy.plan_opts.sample_method is module.TrajectorySampleMethod.QUANTITY - assert pick_policy.plan_opts.sample_interval == module.PICK_MOTION_SAMPLE_COUNT + assert pick_policy.plan_opts is None + toppra_policy = module._create_pick_motion_policy("toppra") + assert isinstance(toppra_policy.plan_opts, module.ToppraPlanOptions) + assert ( + toppra_policy.plan_opts.sample_method is module.TrajectorySampleMethod.QUANTITY + ) + assert toppra_policy.plan_opts.sample_interval == module.PICK_MOTION_SAMPLE_COUNT def test_replay_timed_trajectory_uses_arrival_intervals() -> None: diff --git a/tests/sim/sensors/test_contact_newton_e2e.py b/tests/sim/sensors/test_contact_newton_e2e.py index 94b1816d33..c523fd4024 100644 --- a/tests/sim/sensors/test_contact_newton_e2e.py +++ b/tests/sim/sensors/test_contact_newton_e2e.py @@ -17,7 +17,10 @@ from __future__ import annotations +import math + import pytest +import torch import warp as wp from embodichain.lab.sim.cfg import ( @@ -26,9 +29,13 @@ RigidBodyPhysicsCfg, RigidObjectCfg, ) -from embodichain.lab.sim.sensors import ContactSensorCfg +from embodichain.lab.sim.sensors import ( + ArticulationContactFilterCfg, + ContactSensorCfg, +) from embodichain.lab.sim.shapes import CubeCfg from embodichain.lab.sim.sim_manager import SimulationManager, SimulationManagerCfg +from embodichain.lab.sim.robots import URRobotCfg pytestmark = [pytest.mark.requires_sim, pytest.mark.gpu] @@ -103,3 +110,179 @@ def test_newton_contact_sensor_reports_each_arena() -> None: from dexsim.engine.newton_physics import teardown_newton_physics teardown_newton_physics() + + +def test_dexuni_pure_mujoco_contact_sensor_reports_impulse() -> None: + """Pure DexUni/MuJoCo exposes geometry, normal and friction impulses.""" + wp.init() + if not wp.is_cuda_available(): + pytest.skip("CUDA is required for the DexUni contact contract.") + + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + device="cuda:0", + physics_cfg=NewtonPhysicsCfg( + device="cuda:0", + num_substeps=1, + use_cuda_graph=False, + solver_cfg={ + "solver_type": "dexuni", + "joint_mode": "dynamic", + "contact_mode": "auto", + }, + ), + ) + ) + try: + sim.add_rigid_object( + RigidObjectCfg( + uid="ground", + shape=CubeCfg(size=[3.0, 3.0, 0.1]), + attrs=RigidBodyPhysicsCfg(), + body_type="static", + init_pos=(0.0, 0.0, -0.05), + ) + ) + sim.add_robot( + URRobotCfg.from_dict( + { + "robot_type": "ur10", + "uid": "robot", + "init_pos": (0.0, 0.0, 0.0), + } + ) + ) + sensor = sim.add_sensor( + ContactSensorCfg( + uid="contacts", + rigid_uid_list=["ground"], + articulation_cfg_list=[ + ArticulationContactFilterCfg(articulation_uid="robot") + ], + filter_need_both_actor=False, + max_contacts_per_env=64, + ) + ) + + sim.update(step=10) + sensor.update() + + assert sensor.contact_capabilities.geometry + assert sensor.contact_capabilities.impulse + assert sensor.contact_capabilities.friction + data = sensor.get_data() + valid = data["is_valid"] + assert valid.any() + assert torch.isfinite(data["position"][valid]).all() + assert torch.isfinite(data["normal"][valid]).all() + assert torch.isfinite(data["friction"][valid]).all() + assert torch.isfinite(data["impulse"][valid]).all() + assert torch.isfinite(data["distance"][valid]).all() + normal = data["normal"][valid] + normal_norm = torch.linalg.vector_norm(normal, dim=-1) + assert torch.allclose(normal_norm, torch.ones_like(normal_norm), atol=1.0e-4) + tangent_residual = (data["friction"][valid] * normal).sum(dim=-1).abs() + assert tangent_residual.max() < 1.0e-4 + assert (data["user_ids"][valid] >= 0).all() + assert (data["impulse"][valid] > 1.0e-7).any() + + # Cross-check the adapter against DexUni's native force report. The + # sensor's normal impulse is the normal component of that force over + # the solver substep, so this catches axis/sign/unit regressions that + # finite-value assertions cannot detect. + from dexsim.engine.newton_physics.backend_registry import get_newton_backend + from newton import Contacts + + backend = get_newton_backend(sim._world) + assert backend.solver.features.backend == "pure_mujoco" + report = Contacts( + backend.mujoco_solver.get_max_contact_count(), + 0, + device=backend.model.device, + requested_attributes={"force"}, + ) + backend.mujoco_solver.update_contacts(report, backend.state_0) + raw_count = int(report.rigid_contact_count.numpy()[0]) + raw_forces = report.force.numpy()[:raw_count, :3] + raw_normals = report.rigid_contact_normal.numpy()[:raw_count] + substep_dt = float(backend.cfg.dt) / int(backend.cfg.num_substeps) + raw_normal_impulse = abs((raw_forces * raw_normals).sum(axis=1)) * substep_dt + raw_normal_impulse = raw_normal_impulse[raw_normal_impulse > 1.0e-7] + sensor_total = float(data["impulse"][valid].sum().item()) + raw_total = float(raw_normal_impulse.sum()) + assert raw_total > 0.0 + assert math.isclose(sensor_total, raw_total, rel_tol=2.0e-3, abs_tol=2.0e-4) + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + from dexsim.engine.newton_physics import teardown_newton_physics + + teardown_newton_physics() + + +def test_dexuni_vbd_contact_sensor_exposes_geometry_only() -> None: + """VBD-owned rigid contacts keep geometry but disable force fields.""" + wp.init() + if not wp.is_cuda_available(): + pytest.skip("CUDA is required for the DexUni contact contract.") + + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + device="cuda:0", + physics_cfg=NewtonPhysicsCfg( + device="cuda:0", + num_substeps=1, + use_cuda_graph=False, + solver_cfg={"solver_type": "dexuni"}, + ), + ) + ) + try: + sim.add_rigid_object( + RigidObjectCfg( + uid="ground", + shape=CubeCfg(size=[1.0, 1.0, 0.1]), + attrs=RigidBodyPhysicsCfg(), + body_type="static", + ) + ) + sim.add_rigid_object( + RigidObjectCfg( + uid="cube", + shape=CubeCfg(size=[0.2, 0.2, 0.2]), + attrs=RigidBodyPhysicsCfg(mass_props=MassPropertiesCfg(mass=1.0)), + body_type="dynamic", + init_pos=(0.0, 0.0, 0.4), + ) + ) + sensor = sim.add_sensor( + ContactSensorCfg( + uid="contacts", + rigid_uid_list=["cube"], + filter_need_both_actor=False, + max_contacts_per_env=16, + ) + ) + + sim.update(step=120) + sensor.update() + + assert sensor.contact_capabilities.geometry + assert not sensor.contact_capabilities.impulse + assert not sensor.contact_capabilities.friction + data = sensor.get_data() + valid = data["is_valid"] + assert valid.any() + assert torch.isfinite(data["position"][valid]).all() + assert torch.isfinite(data["normal"][valid]).all() + assert torch.isfinite(data["distance"][valid]).all() + assert torch.count_nonzero(data["impulse"][valid]) == 0 + assert torch.count_nonzero(data["friction"][valid]) == 0 + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + from dexsim.engine.newton_physics import teardown_newton_physics + + teardown_newton_physics() diff --git a/tests/sim/spawn/test_descriptors.py b/tests/sim/spawn/test_descriptors.py index 11b57a2a08..a4edf0e669 100644 --- a/tests/sim/spawn/test_descriptors.py +++ b/tests/sim/spawn/test_descriptors.py @@ -1138,6 +1138,143 @@ def test_articulation_constructor_defers_newton_properties_until_configure() -> assert contact.ke is None +@pytest.mark.parametrize("mode", ["preserve", "overlay"]) +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_articulation_newton_gravity_compensation_is_creation_intent(mode) -> None: + cfg = ArticulationCfg.from_dict( + { + "uid": "robot", + "fpath": "robot.urdf", + "asset_physics_mode": mode, + "root_props": {"newton_gravity_compensation": 1.0}, + } + ) + descriptor = articulation_desc_from_cfg(cfg, newton_solver_type="dexuni") + assert descriptor.newton_gravity_compensation == 1.0 + + resolved = _resolved_articulation_desc() + configure_articulation_desc(resolved, cfg, newton_solver_type="dexuni") + assert resolved.newton_gravity_compensation == 1.0 + + +def test_articulation_unconfigured_gravity_compensation_preserves_source() -> None: + descriptor = _resolved_articulation_desc() + descriptor.newton_gravity_compensation = 0.5 + configure_articulation_desc( + descriptor, ArticulationCfg(), newton_solver_type="dexuni" + ) + assert descriptor.newton_gravity_compensation == 0.5 + + +@pytest.mark.parametrize("mode", ["preserve", "overlay"]) +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_usd_articulation_forwards_gravity_compensation(monkeypatch, mode) -> None: + source = _resolved_articulation_desc() + monkeypatch.setattr( + "embodichain.lab.sim.spawn.usd._parse_singleton", + lambda *_args: (SimpleNamespace(materials={}), source), + ) + cfg = ArticulationCfg.from_dict( + { + "uid": "robot", + "fpath": "robot.usd", + "asset_physics_mode": mode, + "root_props": {"newton_gravity_compensation": 0.5}, + } + ) + descriptor, _ = articulation_desc_from_usd(cfg, newton_solver_type="dexuni") + assert descriptor.newton_gravity_compensation == 0.5 + + +@pytest.mark.parametrize("value", [-0.1, 1.1, float("nan"), float("inf")]) +def test_articulation_gravity_compensation_rejects_invalid_values(value) -> None: + cfg = ArticulationCfg() + cfg.root_props.newton_gravity_compensation = value + with pytest.raises(ValueError, match="newton_gravity_compensation"): + articulation_desc_from_cfg( + cfg, source_path="robot.urdf", newton_solver_type="dexuni" + ) + + +def test_articulation_gravity_compensation_rejects_default_backend() -> None: + cfg = ArticulationCfg() + cfg.root_props.newton_gravity_compensation = 1.0 + with pytest.raises(ValueError, match="Newton backend"): + articulation_desc_from_cfg(cfg, source_path="robot.urdf") + + +def test_gravity_compensation_requires_native_descriptor_support(monkeypatch) -> None: + monkeypatch.delattr(ArticulationDesc, "newton_gravity_compensation", raising=False) + cfg = ArticulationCfg() + cfg.root_props.newton_gravity_compensation = 1.0 + with pytest.raises(RuntimeError, match="requires DexSim dev"): + articulation_desc_from_cfg( + cfg, source_path="robot.urdf", newton_solver_type="dexuni" + ) + + +@pytest.mark.parametrize("target_mode", ["position_velocity", "none"]) +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_newton_passive_damping_is_independent_of_drive_gains(target_mode) -> None: + cfg = ArticulationCfg.from_dict( + { + "asset_physics_mode": "overlay", + "joint_drive_props": { + "backend": "newton", + "target_mode": target_mode, + "stiffness": 12.0, + "damping": 4.0, + "passive_damping": {"arm_.*": 50.0}, + }, + } + ) + descriptor = _resolved_articulation_desc() + configure_articulation_desc(descriptor, cfg, newton_solver_type="dexuni") + joint = descriptor.get_joint_desc("arm_joint") + assert joint.newton.damping == 50.0 + expected_kd = 0.0 if target_mode == "none" else 4.0 + assert joint.newton.target_kd == expected_kd + assert joint.dexsim.damping == expected_kd + round_trip = JointDrivePropertiesCfg.from_dict(cfg.joint_drive_props.to_dict()) + assert round_trip.passive_damping == {"arm_.*": 50.0} + + +@pytest.mark.parametrize("value", [-1.0, float("nan"), float("inf")]) +def test_newton_passive_damping_rejects_invalid_values(value) -> None: + cfg = ArticulationCfg(asset_physics_mode="overlay") + cfg.joint_drive_props = NewtonJointDrivePropertiesCfg() + cfg.joint_drive_props.passive_damping = value + with pytest.raises(ValueError, match="passive_damping"): + configure_articulation_desc( + _resolved_articulation_desc(), cfg, newton_solver_type="dexuni" + ) + + +def test_newton_passive_damping_rejects_default_backend() -> None: + cfg = ArticulationCfg(asset_physics_mode="overlay") + cfg.joint_drive_props = NewtonJointDrivePropertiesCfg() + cfg.joint_drive_props.passive_damping = 50.0 + with pytest.raises(ValueError, match="Newton backend"): + configure_articulation_desc(_resolved_articulation_desc(), cfg) + + +def test_passive_damping_rejects_native_merges_that_discard_it(monkeypatch) -> None: + cfg = ArticulationCfg( + asset_physics_mode="overlay", + joint_drive_props=NewtonJointDrivePropertiesCfg(passive_damping=50.0), + ) + descriptor = _resolved_articulation_desc() + set_properties = descriptor.set_joint_properties + + def discard_damping(*args, **kwargs): + set_properties(*args, **kwargs) + descriptor.get_joint_desc("arm_joint").newton.damping = None + + monkeypatch.setattr(descriptor, "set_joint_properties", discard_damping) + with pytest.raises(RuntimeError, match="did not retain passive_damping"): + configure_articulation_desc(descriptor, cfg, newton_solver_type="dexuni") + + @pytest.mark.parametrize("solver_type", [None, "auto", "mujoco_warp"]) @pytest.mark.parametrize("mode", ["preserve", "overlay"]) @pytest.mark.parametrize("source_gap", [None, 0.0, 0.025]) diff --git a/tests/sim/test_backend_parity.py b/tests/sim/test_backend_parity.py index c5fabe1ce2..9b4f156806 100644 --- a/tests/sim/test_backend_parity.py +++ b/tests/sim/test_backend_parity.py @@ -123,7 +123,7 @@ def test_contact_sensor_guard_uses_backend_capability() -> None: [ ("mujoco_warp", "cuda:0", True), ("mujoco_warp", "cpu", False), - ("dexuni", "cuda:0", False), + ("dexuni", "cuda:0", True), ("xpbd", "cpu", True), ], ) @@ -139,6 +139,37 @@ def test_newton_contact_sensor_capability_depends_on_runtime( assert backend.supports_contact_sensor is expected +@pytest.mark.parametrize( + ("solver_path", "use_mujoco_cpu", "expected"), + [ + ("pure_mujoco", False, True), + ("pure_mujoco", True, False), + ("pure_vbd", False, True), + ("coupled", False, True), + ("dexuni_kinematic_soft", False, True), + ], +) +def test_dexuni_contact_sensor_capability_follows_resolved_path( + monkeypatch: pytest.MonkeyPatch, + solver_path: str, + use_mujoco_cpu: bool, + expected: bool, +) -> None: + world = object() + native = SimpleNamespace( + solver_type="dexuni", + solver=SimpleNamespace(features=SimpleNamespace(backend=solver_path)), + mujoco_solver=SimpleNamespace(use_mujoco_cpu=use_mujoco_cpu), + ) + monkeypatch.setattr( + "dexsim.engine.newton_physics.backend_registry.get_newton_backend", + lambda candidate: native if candidate is world else None, + ) + backend = NewtonPhysicsBackend(SimpleNamespace(_world=world)) + + assert backend.supports_contact_sensor is expected + + def test_contact_sensor_capability_is_rechecked_after_prepare( monkeypatch: pytest.MonkeyPatch, ) -> None: diff --git a/tests/sim/test_cfg.py b/tests/sim/test_cfg.py index ab5f9e2286..c53d24754f 100644 --- a/tests/sim/test_cfg.py +++ b/tests/sim/test_cfg.py @@ -1004,6 +1004,7 @@ def test_backend_joint_and_articulation_configs_round_trip() -> None: "sleep_threshold": None, "min_position_iters": None, "min_velocity_iters": None, + "newton_gravity_compensation": None, } assert type(restored_root) is ArticulationRootPropertiesCfg @@ -1078,6 +1079,7 @@ def test_newton_physics_inherits_common_gravity_and_collision_config( contact_distance: float, ) -> None: cfg = NewtonPhysicsCfg( + solver_cfg=None, gravity=[0.0, 0.0, -1.5], collision_cfg=NewtonCollisionPipelineCfg( broad_phase="sap", diff --git a/tests/sim/test_dexuni_two_way.py b/tests/sim/test_dexuni_two_way.py new file mode 100644 index 0000000000..a1b2a218be --- /dev/null +++ b/tests/sim/test_dexuni_two_way.py @@ -0,0 +1,178 @@ +# ---------------------------------------------------------------------------- +# Copyright (c) 2021-2026 DexForce Technology Co., Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ---------------------------------------------------------------------------- + +"""Physical acceptance of DexUni contact feedback through EmbodiChain.""" + +from __future__ import annotations + +from pathlib import Path + +import pytest +import torch + +from embodichain.lab.sim.cfg import ( + ArticulationCfg, + ArticulationRootPropertiesCfg, + MassPropertiesCfg, + NewtonCollisionPropertiesCfg, + NewtonJointDrivePropertiesCfg, + NewtonPhysicsCfg, + NewtonRigidBodyMaterialCfg, + RenderCfg, + RigidBodyPhysicsCfg, + RigidObjectCfg, +) +from embodichain.lab.sim.shapes import CubeCfg +from embodichain.lab.sim.sim_manager import SimulationManager, SimulationManagerCfg + +pytestmark = [ + pytest.mark.requires_sim, + pytest.mark.gpu, + pytest.mark.usefixtures("requires_dexuni_two_way"), +] + +FINGER_START = 0.08 +FINGER_HALF = 0.01 +BOX_HALF = 0.025 +TARGET = FINGER_START - FINGER_HALF +CONTACT_TRAVEL = FINGER_START - FINGER_HALF - BOX_HALF + + +def _gripper_urdf(path: Path) -> None: + """Write two light, effort-limited fingers without downloadable assets.""" + fingers = [] + for name, sign in (("left", -1.0), ("right", 1.0)): + fingers.append(f""" + + + + + + + + + """) + path.write_text( + '' + "".join(fingers) + "", + encoding="utf-8", + ) + + +def _grip(path: Path, coupling: str, use_cuda_graph: bool) -> torch.Tensor: + material = RigidBodyPhysicsCfg( + collision_props=NewtonCollisionPropertiesCfg(margin=0.0, gap=0.0), + material_props=NewtonRigidBodyMaterialCfg( + ke=2.0e4, kd=100.0, dynamic_friction=1.0 + ), + ) + sim = SimulationManager( + SimulationManagerCfg( + headless=True, + render_cfg=RenderCfg(renderer="no-render"), + physics_cfg=NewtonPhysicsCfg( + device="cuda:0", + physics_dt=1.0 / 60.0, + num_substeps=10, + use_cuda_graph=use_cuda_graph, + collision_cfg=None, + solver_cfg={ + "solver_type": "dexuni", + "joint_mode": "dynamic", + "coupling": coupling, + "vbd_options": {"iterations": 10}, + "collision_options": {"broad_phase": "nxn"}, + }, + ), + ) + ) + try: + articulation = sim.add_articulation( + ArticulationCfg( + uid="gripper", + fpath=str(path), + asset_physics_mode="overlay", + build_pk_chain=False, + attrs=material, + root_props=ArticulationRootPropertiesCfg( + newton_gravity_compensation=1.0 + ), + joint_drive_props=NewtonJointDrivePropertiesCfg( + target_mode="position_velocity", + stiffness=1000.0, + damping=20.0, + passive_damping=50.0, + armature=0.3, + max_effort=10.0, + ), + ) + ) + sim.add_rigid_object( + RigidObjectCfg( + uid="floor", + shape=CubeCfg(size=(1.0, 1.0, 0.1)), + body_type="static", + init_pos=(0.0, 0.0, -0.05), + attrs=material, + ) + ) + box_material = material.copy() + box_material.mass_props = MassPropertiesCfg(mass=0.0625) + box = sim.add_rigid_object( + RigidObjectCfg( + uid="box", + shape=CubeCfg(size=(2 * BOX_HALF,) * 3), + init_pos=(0.0, 0.0, BOX_HALF), + attrs=box_material, + ) + ) + sim.prepare() + articulation.set_qpos( + torch.full((1, 2), TARGET, device=sim.device), target=True + ) + sim.update(step=120) + qpos = articulation.get_qpos().detach().cpu() + assert torch.isfinite(qpos).all() + assert torch.isfinite(box.get_local_pose()).all() + if use_cuda_graph: + assert sim.physics.cuda_graph_status == "captured" + + sim.reset_objects_state() + torch.testing.assert_close( + articulation.get_qpos(), torch.zeros((1, 2), device=sim.device) + ) + return qpos + finally: + sim.destroy(exit_process=False) + SimulationManager.flush_cleanup_queue() + + +@pytest.mark.parametrize("use_cuda_graph", [False, True]) +def test_dexuni_two_way_contact_blocks_dynamic_finger_targets( + tmp_path: Path, use_cuda_graph: bool +) -> None: + if not torch.cuda.is_available(): + pytest.skip("CUDA is required for the DexUni two-way contract.") + path = tmp_path / "gripper.urdf" + _gripper_urdf(path) + one_way = _grip(path, "one_way", use_cuda_graph) + two_way = _grip(path, "two_way", use_cuda_graph) + + # Without feedback, the same effort-limited fingers reach the commanded + # overlapping pose. Feedback stops them at the box's physical surface. + assert torch.all(one_way > TARGET - 0.005), one_way + assert torch.all(two_way < CONTACT_TRAVEL + 0.01), two_way + assert torch.all(two_way > CONTACT_TRAVEL - 0.01), two_way diff --git a/tests/sim/test_open_drawer_tutorial.py b/tests/sim/test_open_drawer_tutorial.py index 35e2c2fe72..a761e9d357 100644 --- a/tests/sim/test_open_drawer_tutorial.py +++ b/tests/sim/test_open_drawer_tutorial.py @@ -115,7 +115,7 @@ def add_articulation(self, cfg): assert drawer_cfg.link_attrs is None -def test_tutorial_newton_physics_cfg_uses_shared_mujoco_warp_profile() -> None: +def test_tutorial_newton_physics_cfg_uses_shared_dexuni_profile() -> None: tutorial = _load_tutorial_module() cfg = tutorial._tutorial_physics_cfg("newton") @@ -123,15 +123,16 @@ def test_tutorial_newton_physics_cfg_uses_shared_mujoco_warp_profile() -> None: assert cfg.num_substeps == 20 assert cfg.collision_cfg is None assert cfg.solver_cfg == { - "solver_type": "mujoco_warp", - "solver": "newton", - "integrator": "implicitfast", - "iterations": 20, - "ls_iterations": 100, - "cone": "elliptic", - "impratio": 1_000.0, - "use_mujoco_contacts": True, - "enable_multiccd": True, + "solver_type": "dexuni", + "mujoco_options": { + "solver": "newton", + "integrator": "implicitfast", + "iterations": 20, + "ls_iterations": 100, + "cone": "elliptic", + "impratio": 1_000.0, + "enable_multiccd": True, + }, } diff --git a/tests/sim/test_scoop_ice_demo.py b/tests/sim/test_scoop_ice_demo.py index 74a28daa8e..12495f7702 100644 --- a/tests/sim/test_scoop_ice_demo.py +++ b/tests/sim/test_scoop_ice_demo.py @@ -196,4 +196,11 @@ def manager(cfg): monkeypatch.setattr(demo, "SimulationManager", manager) demo.initialize_simulation(demo.build_parser().parse_args(["--physics", "newton"])) - assert configs[0].physics_cfg.solver_cfg["solver"] == "cg" + assert configs[0].physics_cfg.solver_cfg["solver_type"] == "dexuni" + assert configs[0].physics_cfg.solver_cfg["mujoco_options"]["solver"] == "cg" + assert ( + configs[0].physics_cfg.solver_cfg["vbd_options"][ + "rigid_body_contact_buffer_size" + ] + == 256 + ) diff --git a/tests/sim/test_sim_manager_cfg.py b/tests/sim/test_sim_manager_cfg.py index 6adf8c2945..fef7fd2529 100644 --- a/tests/sim/test_sim_manager_cfg.py +++ b/tests/sim/test_sim_manager_cfg.py @@ -174,7 +174,7 @@ def test_newton_physics_cfg_requires_dexsim_auto_solver_api( @pytest.mark.no_sim def test_newton_gradient_mode_rejects_auto_solver() -> None: - cfg = NewtonPhysicsCfg(requires_grad=True) + cfg = NewtonPhysicsCfg(solver_cfg=None, requires_grad=True) with pytest.raises(RuntimeError, match="explicit.*semi_implicit"): cfg.to_dexsim_cfg(gpu_id=0) @@ -193,7 +193,9 @@ def test_newton_physics_cfg_passes_warp_log_suppression() -> None: def test_newton_physics_cfg_forwards_collision_pipeline_update_interval( update_interval: int | None, ) -> None: - cfg = NewtonPhysicsCfg(collision_cfg={"update_interval": update_interval}) + cfg = NewtonPhysicsCfg( + solver_cfg=None, collision_cfg={"update_interval": update_interval} + ) dexsim_cfg = cfg.to_dexsim_cfg(gpu_id=0) @@ -300,7 +302,7 @@ def test_newton_backend_reports_scene_resolved_auto_solver( manager = SimpleNamespace(_world=world) backend = NewtonPhysicsBackend(manager) world_config = SimpleNamespace(newton_cfg=None) - sim_config = SimulationManagerCfg(physics_cfg=NewtonPhysicsCfg()) + sim_config = SimulationManagerCfg(physics_cfg=NewtonPhysicsCfg(solver_cfg=None)) monkeypatch.setattr( "dexsim.engine.newton_physics.backend_registry.get_newton_backend", lambda candidate: native_backend if candidate is world else None, @@ -474,8 +476,8 @@ def test_newton_physics_cfg_accepts_dexuni_solver_mapping( cfg = NewtonPhysicsCfg( solver_cfg={ config_key: solver_name, - "iterations": 12, - "step_rigid_bodies": False, + "joint_mode": "kinematic", + "vbd_options": {"iterations": 12}, } ) @@ -483,8 +485,8 @@ def test_newton_physics_cfg_accepts_dexuni_solver_mapping( assert isinstance(dexsim_cfg.solver_cfg, DexUniSolverCfg) assert dexsim_cfg.solver_cfg.solver_type == "dexuni" - assert dexsim_cfg.solver_cfg.iterations == 12 - assert dexsim_cfg.solver_cfg.step_rigid_bodies is False + assert dexsim_cfg.solver_cfg.joint_mode == "kinematic" + assert dexsim_cfg.solver_cfg.vbd_options["iterations"] == 12 @pytest.mark.no_sim @@ -499,3 +501,54 @@ def test_newton_physics_cfg_directly_accepts_dexsim_solver_cfg_object() -> None: assert isinstance(dexsim_cfg.solver_cfg, XPBDSolverCfg) assert dexsim_cfg.solver_cfg.iterations == 8 assert dexsim_cfg.solver_cfg.enable_restitution is True + + +@pytest.mark.no_sim +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_newton_physics_cfg_forwards_two_way_coupling() -> None: + cfg = NewtonPhysicsCfg( + collision_cfg=None, + solver_cfg={ + "solver_type": "dexuni", + "joint_mode": "dynamic", + "coupling": "two_way", + "coupling_options": { + "mass_scale": 4.0, + "proxy_relaxation": 0.5, + "iterations": 2, + }, + "mujoco_articulation_paths": ("env_0/robot",), + "vbd_options": {"iterations": 10}, + }, + ) + native = cfg.to_dexsim_cfg(gpu_id=0) + assert native.collision_pipeline_cfg is None + assert native.solver_cfg.joint_mode == "dynamic" + assert native.solver_cfg.coupling == "two_way" + assert native.solver_cfg.coupling_options == cfg.solver_cfg["coupling_options"] + assert native.solver_cfg.mujoco_articulation_paths == ("env_0/robot",) + assert native.solver_cfg.vbd_options == {"iterations": 10} + + +@pytest.mark.no_sim +@pytest.mark.parametrize( + ("options", "error"), + [ + ({"joint_mode": "kinematic"}, "requires joint_mode='dynamic'"), + ({"coupling_options": {"mass_scale": 0.0}}, "mass_scale"), + ({"coupling_options": {"proxy_relaxation": 1.1}}, "proxy_relaxation"), + ({"coupling_options": {"iterations": 0}}, "iterations"), + ({"coupling_options": {"unknown": 1.0}}, "Unsupported"), + ( + {"collision_options": {"enable_rigid_soft_full_surface_contact": True}}, + "full-surface", + ), + ], +) +@pytest.mark.usefixtures("requires_dexuni_two_way") +def test_newton_physics_cfg_preserves_native_two_way_validation(options, error) -> None: + cfg = NewtonPhysicsCfg( + solver_cfg={"solver_type": "dexuni", "coupling": "two_way", **options} + ) + with pytest.raises(ValueError, match=error): + cfg.to_dexsim_cfg(gpu_id=0) diff --git a/tests/test_task_program_package_data.py b/tests/test_task_program_package_data.py index 83ca7f26ab..76dde820c8 100644 --- a/tests/test_task_program_package_data.py +++ b/tests/test_task_program_package_data.py @@ -109,8 +109,8 @@ Path("tasks/manipulation/tableware/stack_cups/README.md"), Path("components/execution_policies/motion_gen_verified.yaml"), Path("components/execution_policies/trajectory_open_loop.yaml"), - Path("components/execution_policies/trajectory_open_loop_dense.yaml"), Path("components/execution_policies/trajectory_open_loop_slow.yaml"), + Path("components/execution_policies/trajectory_open_loop_dense.yaml"), Path("components/expansion_policies/task_program_episode.yaml"), Path("components/randomization_profiles/cube_initial_pose.yaml"), Path("components/randomization_profiles/rgb_visual.yaml"),