Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Google deepmind Mujoco Engine Sleep

From Leeroopedia
Knowledge Sources
Domains Physics Simulation, Performance Optimization, Sleep Management
Last Updated 2026-02-15 04:00 GMT

Overview

Manages the sleep/wake state of kinematic trees to skip computations for bodies that are effectively at rest.

Description

engine_sleep.c implements MuJoCo's sleep system, which deactivates kinematic trees whose velocities fall below a configurable tolerance for a sustained period. The system operates on a per-tree basis using the tree_asleep counter: negative values indicate awake trees, and positive values count sleep cycles. Trees are put to sleep when their weighted velocity norm remains below tolerance, and woken by various events: collisions between sleeping and awake bodies, tendon connections to awake trees, equality constraints linking to awake trees, or explicit user waking. The module also determines the sleep state of tendons, actuators, equality constraints, and sensors based on the trees they reference, and maintains optimized index arrays (dof_awake_ind, body_awake_ind) for skipping sleeping elements in subsequent computations.

Usage

Called during the forward dynamics pipeline when sleep is enabled (mjENBL_SLEEP). mj_wake() checks for wake conditions, mj_sleep() checks for sleep conditions, and mj_updateSleep() propagates the current sleep state to all derived arrays. Wake events are also triggered by collision detection, tendon, and equality constraint processing.

Code Reference

Source Location

Key Functions

// compute sleep arrays from tree_asleep (main update)
void mj_updateSleep(const mjModel* m, mjData* d);

// compute sleeping arrays with configurable static-body handling
void mj_updateSleepInit(const mjModel* m, mjData* d, int flg_staticawake);

// wake trees based on velocity, collision, tendon, equality events
int mj_wake(const mjModel* m, mjData* d);
int mj_wakeCollision(const mjModel* m, mjData* d);
int mj_wakeTendon(const mjModel* m, mjData* d);
int mj_wakeEquality(const mjModel* m, mjData* d);

// put trees to sleep if velocity below tolerance
int mj_sleep(const mjModel* m, mjData* d);

// wake a specific tree
int mj_wakeTree(int* tree_asleep, int ntree, int i, int wakeval);

// get the sleep cycle count for a tree
int mj_sleepCycle(const int* tree_asleep, int ntree, int i);

// determine sleep state of model elements
static mjtSleepState mj_tendonSleepState(const mjModel* m, const mjData* d, int i);
static mjtSleepState mj_actuatorSleepState(const mjModel* m, const mjData* d, int i);
static mjtSleepState mj_equalitySleepState(const mjModel* m, const mjData* d, int i);
static mjtSleepState mj_sensorSleepState(const mjModel* m, const mjData* d, int i);

Import

#include "engine/engine_sleep.h"

I/O Contract

Inputs

Name Type Required Description
m const mjModel* Yes Physics model with sleep tolerance parameters
d mjData* Yes Simulation state with velocities and tree_asleep counters
flg_staticawake int Yes (updateSleepInit) Whether to treat static bodies as awake
i int Yes (wakeTree) Tree index to wake
wakeval int Yes (wakeTree) Wake counter value to set

Outputs

Name Type Description
d->tree_asleep int* Sleep counter per tree (negative = awake, positive = sleep cycles)
d->tree_awake int* Binary awake flag per tree
d->body_awake int* Awake/asleep/static state per body
d->dof_awake_ind int* Indices of awake DOFs
d->body_awake_ind int* Indices of awake bodies
d->nv_awake int Number of awake DOFs
d->nbody_awake int Number of awake bodies
d->ntree_awake int Number of awake trees
return (wake/sleep) int Whether any state change occurred

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment