mjlab.sensor#
Sensor implementations.
Classes:
Wrapper over MuJoCo builtin sensors. |
|
BuiltinSensorCfg(name: 'str', sensor_type: 'SensorType', obj: 'ObjRef | None' = None, ref: 'ObjRef | None' = None, cutoff: 'float' = 0.0) |
|
Reference to a MuJoCo object (body, joint, site, etc.). |
|
Contact sensor output (only requested fields are populated). |
|
Specifies what to match on one side of a contact. |
|
Tracks contacts with automatic pattern expansion to multiple MuJoCo sensors. |
|
Tracks contacts between primary and secondary patterns. |
|
Base sensor interface with typed data. |
|
Base configuration for a sensor. |
- class mjlab.sensor.BuiltinSensor[source]#
Bases:
Sensor[Tensor]Wrapper over MuJoCo builtin sensors.
Can add a new sensor to the spec, or wrap an existing sensor from entity XML. Returns raw MuJoCo sensordata as torch.Tensor with shape depending on sensor type (e.g., accelerometer: (N, 3), framequat: (N, 4)).
Methods:
__init__([cfg, name])from_existing(name)Wrap an existing sensor already defined in entity XML.
edit_spec(scene_spec, entities)Edit the scene spec to add this sensor.
initialize(mj_model, model, data, device)Initialize the sensor after model compilation.
Attributes:
Get the current sensor data.
- classmethod from_existing(name: str) BuiltinSensor[source]#
Wrap an existing sensor already defined in entity XML.
- edit_spec(scene_spec: MjSpec, entities: dict[str, Entity]) None[source]#
Edit the scene spec to add this sensor.
This is called during scene construction to add sensor elements to the MjSpec.
- Parameters:
scene_spec – The scene MjSpec to edit.
entities – Dictionary of entities in the scene, keyed by name.
- initialize(mj_model: MjModel, model: mujoco_warp.Model, data: mujoco_warp.Data, device: str) None[source]#
Initialize the sensor after model compilation.
This is called after the MjSpec is compiled into an MjModel and the simulation is ready to run. Use this to cache sensor indices, allocate buffers, etc.
- Parameters:
mj_model – The compiled MuJoCo model.
model – The mjwarp model wrapper.
data – The mjwarp data arrays.
device – Device for tensor operations (e.g., “cuda”, “cpu”).
- property data: Tensor#
Get the current sensor data.
This property returns the sensor’s current data in its specific type. The data type is specified by the type parameter T.
- Returns:
The sensor data in the format specified by type parameter T.
- class mjlab.sensor.BuiltinSensorCfg[source]#
Bases:
SensorCfgBuiltinSensorCfg(name: ‘str’, sensor_type: ‘SensorType’, obj: ‘ObjRef | None’ = None, ref: ‘ObjRef | None’ = None, cutoff: ‘float’ = 0.0)
Attributes:
Which builtin sensor to use.
The type and name of the object the sensor is attached to.
The type and name of object to which the frame-of-reference is attached to.
When this value is positive, it limits the absolute value of the sensor output.
Methods:
- sensor_type: Literal['accelerometer', 'velocimeter', 'gyro', 'force', 'torque', 'magnetometer', 'rangefinder', 'jointpos', 'jointvel', 'jointlimitpos', 'jointlimitvel', 'jointlimitfrc', 'jointactuatorfrc', 'tendonpos', 'tendonvel', 'tendonactuatorfrc', 'actuatorpos', 'actuatorvel', 'actuatorfrc', 'framepos', 'framequat', 'framexaxis', 'frameyaxis', 'framezaxis', 'framelinvel', 'frameangvel', 'framelinacc', 'frameangacc', 'subtreecom', 'subtreelinvel', 'subtreeangmom', 'e_potential', 'e_kinetic', 'clock']#
Which builtin sensor to use.
- ref: ObjRef | None = None#
The type and name of object to which the frame-of-reference is attached to.
- cutoff: float = 0.0#
When this value is positive, it limits the absolute value of the sensor output.
- build() BuiltinSensor[source]#
Build sensor instance from this config.
- __init__(name: str, sensor_type: Literal['accelerometer', 'velocimeter', 'gyro', 'force', 'torque', 'magnetometer', 'rangefinder', 'jointpos', 'jointvel', 'jointlimitpos', 'jointlimitvel', 'jointlimitfrc', 'jointactuatorfrc', 'tendonpos', 'tendonvel', 'tendonactuatorfrc', 'actuatorpos', 'actuatorvel', 'actuatorfrc', 'framepos', 'framequat', 'framexaxis', 'frameyaxis', 'framezaxis', 'framelinvel', 'frameangvel', 'framelinacc', 'frameangacc', 'subtreecom', 'subtreelinvel', 'subtreeangmom', 'e_potential', 'e_kinetic', 'clock'], obj: ObjRef | None = None, ref: ObjRef | None = None, cutoff: float = 0.0) None#
- class mjlab.sensor.ObjRef[source]#
Bases:
objectReference to a MuJoCo object (body, joint, site, etc.).
Used to specify which object a sensor is attached to and its frame of reference. The entity field allows scoping objects to specific entity namespaces.
Attributes:
Type of the object.
Name of the object.
Optional entity prefix for the object name.
Methods:
Get the full name with entity prefix if applicable.
__init__(type, name[, entity])
- class mjlab.sensor.ContactData[source]#
Bases:
objectContact sensor output (only requested fields are populated).
Attributes:
[B, N] 0=no contact, >0=match count
[B, N, 3] contact frame (global if reduce="netforce" or global_frame=True)
[B, N, 3] contact frame (global if reduce="netforce" or global_frame=True)
[B, N] penetration depth
[B, N, 3] global frame
[B, N, 3] global frame, primary→secondary
[B, N, 3] global frame
[B, N] time in air (if track_air_time=True)
[B, N] duration of last air phase (if track_air_time=True)
[B, N] time in contact (if track_air_time=True)
[B, N] duration of last contact phase (if track_air_time=True)
Methods:
__init__([found, force, torque, dist, pos, ...])- force: Tensor | None = None#
[B, N, 3] contact frame (global if reduce=”netforce” or global_frame=True)
- torque: Tensor | None = None#
[B, N, 3] contact frame (global if reduce=”netforce” or global_frame=True)
- last_contact_time: Tensor | None = None#
[B, N] duration of last contact phase (if track_air_time=True)
- __init__(found: Tensor | None = None, force: Tensor | None = None, torque: Tensor | None = None, dist: Tensor | None = None, pos: Tensor | None = None, normal: Tensor | None = None, tangent: Tensor | None = None, current_air_time: Tensor | None = None, last_air_time: Tensor | None = None, current_contact_time: Tensor | None = None, last_contact_time: Tensor | None = None) None#
- class mjlab.sensor.ContactMatch[source]#
Bases:
objectSpecifies what to match on one side of a contact.
mode: “geom”, “body”, or “subtree” pattern: Regex or tuple of regexes (expands within entity if specified) entity: Entity name to search within (None = treat pattern as literal MuJoCo name) exclude: Filter out matches using these regex patterns or exact names.
Attributes:
Methods:
__init__(mode, pattern[, entity, exclude])
- class mjlab.sensor.ContactSensor[source]#
Bases:
Sensor[ContactData]Tracks contacts with automatic pattern expansion to multiple MuJoCo sensors.
Methods:
__init__(cfg)edit_spec(scene_spec, entities)Expand patterns and add MuJoCo sensors (one per primary x field pair).
initialize(mj_model, model, data, device)Map sensors to sensordata buffer and allocate air time state.
reset([env_ids])Reset sensor state for specified environments.
update(dt)Update sensor state after a simulation step.
compute_first_contact(dt[, abs_tol])Returns [B, N] bool: True for contacts established within last dt seconds.
compute_first_air(dt[, abs_tol])Returns [B, N] bool: True for contacts broken within last dt seconds.
Attributes:
Get the current sensor data.
- __init__(cfg: ContactSensorCfg) None[source]#
- edit_spec(scene_spec: MjSpec, entities: dict[str, Entity]) None[source]#
Expand patterns and add MuJoCo sensors (one per primary x field pair).
- initialize(mj_model: MjModel, model: mujoco_warp.Model, data: mujoco_warp.Data, device: str) None[source]#
Map sensors to sensordata buffer and allocate air time state.
- property data: ContactData#
Get the current sensor data.
This property returns the sensor’s current data in its specific type. The data type is specified by the type parameter T.
- Returns:
The sensor data in the format specified by type parameter T.
- reset(env_ids: Tensor | slice | None = None) None[source]#
Reset sensor state for specified environments.
Base implementation does nothing. Override in subclasses that maintain internal state.
- Parameters:
env_ids – Environment indices to reset. If None, reset all environments.
- update(dt: float) None[source]#
Update sensor state after a simulation step.
Base implementation does nothing. Override in subclasses that need per-step updates.
- Parameters:
dt – Time step in seconds.
- class mjlab.sensor.ContactSensorCfg[source]#
Bases:
SensorCfgTracks contacts between primary and secondary patterns.
Output shape: [B, N * num_slots] or [B, N * num_slots, 3] where N = # of primaries
- Fields (choose subset):
found: 0=no contact, >0=match count before reduction
force, torque: 3D vectors in contact frame (or global if reduce=”netforce”)
dist: penetration depth
pos, normal, tangent: 3D vectors in global frame (normal: primary→secondary)
- Reduction modes (selects top num_slots from all matches):
“none”: fast, non-deterministic
“mindist”, “maxforce”: closest/strongest contacts
“netforce”: sum all forces (global frame)
- Policies:
secondary_policy: “first”, “any”, or “error” when secondary matches multiple
track_air_time: enables landing/takeoff detection
global_frame: rotates force/torque to global (requires normal+tangent fields)
Attributes:
Methods:
- primary: ContactMatch#
- secondary: ContactMatch | None = None#
- build() ContactSensor[source]#
Build sensor instance from this config.
- __init__(name: str, primary: ContactMatch, secondary: ContactMatch | None = None, fields: tuple[str, ...] = ('found', 'force'), reduce: Literal['none', 'mindist', 'maxforce', 'netforce'] = 'maxforce', num_slots: int = 1, secondary_policy: Literal['first', 'any', 'error'] = 'first', track_air_time: bool = False, global_frame: bool = False, debug: bool = False) None#
- class mjlab.sensor.Sensor[source]#
-
Base sensor interface with typed data.
Type parameter T specifies the type of data returned by the sensor. For example: - Sensor[torch.Tensor] for sensors returning raw tensors - Sensor[ContactData] for sensors returning structured contact data
Methods:
edit_spec(scene_spec, entities)Edit the scene spec to add this sensor.
initialize(mj_model, model, data, device)Initialize the sensor after model compilation.
reset([env_ids])Reset sensor state for specified environments.
update(dt)Update sensor state after a simulation step.
Attributes:
Get the current sensor data.
- abstractmethod edit_spec(scene_spec: mujoco.MjSpec, entities: dict[str, Entity]) None[source]#
Edit the scene spec to add this sensor.
This is called during scene construction to add sensor elements to the MjSpec.
- Parameters:
scene_spec – The scene MjSpec to edit.
entities – Dictionary of entities in the scene, keyed by name.
- abstractmethod initialize(mj_model: MjModel, model: mujoco_warp.Model, data: mujoco_warp.Data, device: str) None[source]#
Initialize the sensor after model compilation.
This is called after the MjSpec is compiled into an MjModel and the simulation is ready to run. Use this to cache sensor indices, allocate buffers, etc.
- Parameters:
mj_model – The compiled MuJoCo model.
model – The mjwarp model wrapper.
data – The mjwarp data arrays.
device – Device for tensor operations (e.g., “cuda”, “cpu”).
- abstract property data: T#
Get the current sensor data.
This property returns the sensor’s current data in its specific type. The data type is specified by the type parameter T.
- Returns:
The sensor data in the format specified by type parameter T.