Device layer#

DeviceLayer is the simulation and hardware abstraction boundary in REAF. In reaf-sim, DeviceLayer manages a collection of Devices, coordinates the underlying Simulator, and orchestrates the physics substepping loop.

Architecture#

In reaf-sim, the TaskLayer interacts with the DeviceLayer exclusively via standardized commands and measurements:

  • Command ingestion: Maps commands (e.g. "arm/ctrl") into actuator inputs, mocap targets, or applied forces.

  • Physics advancement: Steps the underlying Simulator through intermediate physics substeps.

  • Measurement extraction: Extracts physical states (joint positions, velocities, contact forces, camera pixels) into batched PyTorch tensors for the task layer.

Device lifecycle#

Each Device follows a standardized lifecycle:

  1. Spec configuration (configure_spec): At scene composition time, before compilation, devices attach required bodies, joints, actuators, sensors, sites, or cameras to the uncompiled mujoco.MjSpec.

  2. Initialization (initialize): After the Simulator compiles the model, devices cache indices or allocate runtime buffers.

  3. Substep commands (set_substep_commands): Before each physics substep, devices write control inputs into the simulation.

  4. Substep update (on_substep): After each physics substep, devices advance per-substep state (e.g. history buffers or captures at a given substep).

  5. Step update (on_step): On the final physics substep, right after on_substep and before get_measurements and substep processors, devices advance per-control-step state (holding the prior step’s value across earlier substeps).

  6. Measurements (get_measurements): Returns the current measurements in a dictionary of tensors. By convention, it does not advance any state, since DeviceLayer may read a device more than once at any point in the device layer lifecycle.

  7. Reset (reset): Clears per-world buffers and sets the initial simulation state of the specified environments (via env_mask).

  8. Begin stepping (on_begin_stepping): Records the reset state of the reset environments (e.g. fills history buffers with the reset reading). on_step does not run after resets, so devices that latch per-step state set it for the reset environments here.

Note

on_begin_stepping is separate from reset because reset runs before the environment resetter, sim.set_const(), and sim.forward(), so sim.data (e.g. sensordata or xpos) does not hold the reset state yet. on_begin_stepping runs after them and before the reset state is read, which lets stateful devices seed their buffers for the reset environments in a hook rather than in get_measurements.

Only measurements of the reset environments are refreshed after a partial reset; the other environments keep the measurements of their last control step. Measurements are committed into persistent contiguous output buffers, so returned tensors are isolated from subsequent sim.data updates and keep stable memory addresses across steps and resets.

Substep loop (simulation)#

DeviceLayer runs n_substeps = round(control_timestep / simulation_timestep) physics steps per control step. Commands and measurements are processed at the physics frequency:

  • SubstepCommandsProcessor (e.g. StochasticSubstepCommands): Transforms commands before they reach devices (actuation latency, dropout, jitter).

  • SubstepMeasurementsProcessor (e.g. StochasticSubstepMeasurements): Post-processes raw device measurements before they reach the task layer (sensor noise, latency, bias). Like devices, processors should advance state in on_substep and on_begin_stepping and, by convention, not in process_measurements. Substep measurements are zero-copy views of simulator or device state, so processors must not modify them in place, and must copy them into their own buffers to keep them (e.g. as a history).

Built-in devices#

reaf.simulation.torch provides built-in devices organized by functionality:

  • Actuation:

    • ActuatorDevice: Controls existing MJCF joint, tendon, or site actuators and exposes their state and force measurements.

    • ArticulatedDevice: Manages articulated robot mechanisms, exposing joint positions, velocities, effort limits, and home pose resets.

  • Bodies:

    • FreeDevice: Manages 6-DoF free-floating bodies (positions, orientations, and linear/angular velocities).

    • MocapDevice: Controls kinematic motion targets and mocap bodies for Cartesian end-effector tracking.

  • Sites & sensors:

    • SiteDevice: Measures global Cartesian positions, orientations, and coordinate transform matrices of MuJoCo sites.

    • InsideSiteDevice: Performs spatial containment queries to test whether points or bodies reside inside volumetric site bounds.

    • ContactSensorDevice: Captures contact forces, normal pressures, and collision impulses between bodies or geom pairs.

    • TactileDevice: Attaches a mjSENS_TACTILE sensor with optional post-processing.

    • MeasurementDevice: Evaluates custom callable or Term functions over (model, data) for simulation-only state measurements.

  • Cameras & visual rendering:

    • CameraDevice: Batched GPU camera rendering RGB images and metric depth maps via MuJoCo Warp (backend="warp" default BVH or backend="optix" NVIDIA RTX GPU hardware accelerated ray tracing).

    • MujocoCameraDevice: Headless CPU camera rendering RGB, depth, and segmentation masks via mujoco.Renderer.

    • BatchedMujocoCameraDevice: Multi-camera CPU device rendering RGB, depth, and segmentation masks across multiple cameras via mujoco.Renderer.

    Both mujoco.Renderer devices also offer render_now(...) for single-world Impl.CPP simulations, which renders the current state (optionally a subset of cameras and render modes) without touching cached measurements. Pass on_demand_only=True (with render_rate_hz=None) for cameras that are only read through render_now; such devices publish no measurements and skip the render after each reset.

  • Heterogeneous:

    • MultiAssetDevice: Assigns and resamples different MultiAsset geoms and meshes for a target body across parallel worlds.

Adding a device#

Custom devices subclass Device and implement its abstract methods.

For full signature details of all methods, see reaf.simulation.torch.DeviceLayer and reaf.simulation.torch.Device in the API reference.

Deprecated methods#

Device.get_substep_measurements and SubstepMeasurementsProcessor.process_substep_measurements are deprecated in favor of the hooks above. Components that override them (or that override none of the replacement methods and declare measurements) keep their previous per-substep and post-reset behavior; DeviceLayer emits a DeprecationWarning for legacy processors, and raises a TypeError if a component overrides both a deprecated method and one of its replacements.