#### Defining Sensors Sensors are defined in the `<sensor>` section of the [[XML Files|XML]] model. ```xml <sensor> <type name="position" objtype="body" objname="block" reftype="body" refname="goal"/> <type name="orientation" objtype="body" objname="block" reftype="body" refname="goal"/> </sensor> ``` Each sensor has: - **`name`**: Identifier used to access sensor ID. - **`objtype` / `objname`**: The object being measured (e.g. a body). - **`reftype` / `refname`** _(optional)_: Frame to express the reading in (e.g., a relative frame like a goal). Additionally, we can define the sensor as one of the following types ... |Sensor Type|Tag|Output Dim|Purpose| |---|---|---|---| |`framepos`|3|Position (x, y, z) of `obj` in `ref` frame|| |`framequat`|4|Orientation as quaternion (w, x, y, z)|| |`velocimeter`|3|Linear velocity of a body|| |`gyro`|3|Angular velocity|| |`jointpos`|1 per joint|Joint positions|| |`jointvel`|1 per joint|Joint velocities|| |`force`|3 or 6|Contact or constraint forces|| |`accelerometer`|3|Acceleration (usually noisy, good for IMU sim)|| --- #### Accessing Sensors ```python title='Print Sensor Information' import mujoco mj_model = mujoco.MjModel.from_xml_path("model.xml") mj_data = mujoco.MjData(mj_model) # Inspect sensor metadata print(mj_model.sensor_names) # All sensor names print(mj_model.sensor_adr) # Start index in sensordata print(mj_model.sensor_dim) # Output size per sensor ``` ```python title='Access Sensor Data' def get_sensor_data(model, data, sensor_name): sensor_id = mujoco.mj_name2id(model, mujoco.mjtObj.mjOBJ_SENSOR, sensor_name) adr = model.sensor_adr[sensor_id] dim = model.sensor_dim[sensor_id] return data.sensordata[adr:adr + dim] ```