Add sleep related data structures

PiperOrigin-RevId: 829055431
Change-Id: I1ccbd77a57044a754ae7db611b4c2c0010fbda53
This commit is contained in:
Yuval Tassa
2025-11-06 12:09:59 -08:00
committed by Copybara-Service
parent 3080e3424f
commit 252a0d73df
37 changed files with 1907 additions and 186 deletions
+82 -55
View File
@@ -24,42 +24,42 @@
//---------------------------------- primitive types (mjt) -----------------------------------------
typedef enum mjtState_ { // state elements
mjSTATE_TIME = 1<<0, // time
mjSTATE_QPOS = 1<<1, // position
mjSTATE_QVEL = 1<<2, // velocity
mjSTATE_ACT = 1<<3, // actuator activation
mjSTATE_WARMSTART = 1<<4, // acceleration used for warmstart
mjSTATE_CTRL = 1<<5, // control
mjSTATE_QFRC_APPLIED = 1<<6, // applied generalized force
mjSTATE_XFRC_APPLIED = 1<<7, // applied Cartesian force/torque
mjSTATE_EQ_ACTIVE = 1<<8, // enable/disable constraints
mjSTATE_MOCAP_POS = 1<<9, // positions of mocap bodies
mjSTATE_MOCAP_QUAT = 1<<10, // orientations of mocap bodies
mjSTATE_USERDATA = 1<<11, // user data
mjSTATE_PLUGIN = 1<<12, // plugin state
typedef enum mjtState_ { // state elements
mjSTATE_TIME = 1<<0, // time
mjSTATE_QPOS = 1<<1, // position
mjSTATE_QVEL = 1<<2, // velocity
mjSTATE_ACT = 1<<3, // actuator activation
mjSTATE_WARMSTART = 1<<4, // acceleration used for warmstart
mjSTATE_CTRL = 1<<5, // control
mjSTATE_QFRC_APPLIED = 1<<6, // applied generalized force
mjSTATE_XFRC_APPLIED = 1<<7, // applied Cartesian force/torque
mjSTATE_EQ_ACTIVE = 1<<8, // enable/disable constraints
mjSTATE_MOCAP_POS = 1<<9, // positions of mocap bodies
mjSTATE_MOCAP_QUAT = 1<<10, // orientations of mocap bodies
mjSTATE_USERDATA = 1<<11, // user data
mjSTATE_PLUGIN = 1<<12, // plugin state
mjNSTATE = 13, // number of state elements
mjNSTATE = 13, // number of state elements
// convenience values for commonly used state specifications
mjSTATE_PHYSICS = mjSTATE_QPOS | mjSTATE_QVEL | mjSTATE_ACT,
mjSTATE_FULLPHYSICS = mjSTATE_TIME | mjSTATE_PHYSICS | mjSTATE_PLUGIN,
mjSTATE_USER = mjSTATE_CTRL | mjSTATE_QFRC_APPLIED | mjSTATE_XFRC_APPLIED |
mjSTATE_PHYSICS = mjSTATE_QPOS | mjSTATE_QVEL | mjSTATE_ACT,
mjSTATE_FULLPHYSICS = mjSTATE_TIME | mjSTATE_PHYSICS | mjSTATE_PLUGIN,
mjSTATE_USER = mjSTATE_CTRL | mjSTATE_QFRC_APPLIED | mjSTATE_XFRC_APPLIED |
mjSTATE_EQ_ACTIVE | mjSTATE_MOCAP_POS | mjSTATE_MOCAP_QUAT |
mjSTATE_USERDATA,
mjSTATE_INTEGRATION = mjSTATE_FULLPHYSICS | mjSTATE_USER | mjSTATE_WARMSTART
mjSTATE_INTEGRATION = mjSTATE_FULLPHYSICS | mjSTATE_USER | mjSTATE_WARMSTART
} mjtState;
typedef enum mjtConstraint_ { // type of constraint
mjCNSTR_EQUALITY = 0, // equality constraint
mjCNSTR_FRICTION_DOF, // dof friction
mjCNSTR_FRICTION_TENDON, // tendon friction
mjCNSTR_LIMIT_JOINT, // joint limit
mjCNSTR_LIMIT_TENDON, // tendon limit
mjCNSTR_CONTACT_FRICTIONLESS, // frictionless contact
mjCNSTR_CONTACT_PYRAMIDAL, // frictional contact, pyramidal friction cone
mjCNSTR_CONTACT_ELLIPTIC // frictional contact, elliptic friction cone
typedef enum mjtConstraint_ { // type of constraint
mjCNSTR_EQUALITY = 0, // equality constraint
mjCNSTR_FRICTION_DOF, // dof friction
mjCNSTR_FRICTION_TENDON, // tendon friction
mjCNSTR_LIMIT_JOINT, // joint limit
mjCNSTR_LIMIT_TENDON, // tendon limit
mjCNSTR_CONTACT_FRICTIONLESS, // frictionless contact
mjCNSTR_CONTACT_PYRAMIDAL, // frictional contact, pyramidal friction cone
mjCNSTR_CONTACT_ELLIPTIC // frictional contact, elliptic friction cone
} mjtConstraint;
@@ -72,48 +72,55 @@ typedef enum mjtConstraintState_ { // constraint state
} mjtConstraintState;
typedef enum mjtWarning_ { // warning types
mjWARN_INERTIA = 0, // (near) singular inertia matrix
mjWARN_CONTACTFULL, // too many contacts in contact list
mjWARN_CNSTRFULL, // too many constraints
mjWARN_VGEOMFULL, // too many visual geoms
mjWARN_BADQPOS, // bad number in qpos
mjWARN_BADQVEL, // bad number in qvel
mjWARN_BADQACC, // bad number in qacc
mjWARN_BADCTRL, // bad number in ctrl
typedef enum mjtWarning_ { // warning types
mjWARN_INERTIA = 0, // (near) singular inertia matrix
mjWARN_CONTACTFULL, // too many contacts in contact list
mjWARN_CNSTRFULL, // too many constraints
mjWARN_VGEOMFULL, // too many visual geoms
mjWARN_BADQPOS, // bad number in qpos
mjWARN_BADQVEL, // bad number in qvel
mjWARN_BADQACC, // bad number in qacc
mjWARN_BADCTRL, // bad number in ctrl
mjNWARNING // number of warnings
mjNWARNING // number of warnings
} mjtWarning;
typedef enum mjtTimer_ { // internal timers
typedef enum mjtTimer_ { // internal timers
// main api
mjTIMER_STEP = 0, // step
mjTIMER_FORWARD, // forward
mjTIMER_INVERSE, // inverse
mjTIMER_STEP = 0, // step
mjTIMER_FORWARD, // forward
mjTIMER_INVERSE, // inverse
// breakdown of step/forward
mjTIMER_POSITION, // fwdPosition
mjTIMER_VELOCITY, // fwdVelocity
mjTIMER_ACTUATION, // fwdActuation
mjTIMER_CONSTRAINT, // fwdConstraint
mjTIMER_ADVANCE, // mj_Euler, mj_implicit
mjTIMER_POSITION, // fwdPosition
mjTIMER_VELOCITY, // fwdVelocity
mjTIMER_ACTUATION, // fwdActuation
mjTIMER_CONSTRAINT, // fwdConstraint
mjTIMER_ADVANCE, // mj_Euler, mj_implicit
// breakdown of fwdPosition
mjTIMER_POS_KINEMATICS, // kinematics, com, tendon, transmission
mjTIMER_POS_INERTIA, // inertia computations
mjTIMER_POS_COLLISION, // collision detection
mjTIMER_POS_MAKE, // make constraints
mjTIMER_POS_PROJECT, // project constraints
mjTIMER_POS_KINEMATICS, // kinematics, com, tendon, transmission
mjTIMER_POS_INERTIA, // inertia computations
mjTIMER_POS_COLLISION, // collision detection
mjTIMER_POS_MAKE, // make constraints
mjTIMER_POS_PROJECT, // project constraints
// breakdown of mj_collision
mjTIMER_COL_BROAD, // broadphase
mjTIMER_COL_NARROW, // narrowphase
mjTIMER_COL_BROAD, // broadphase
mjTIMER_COL_NARROW, // narrowphase
mjNTIMER // number of timers
mjNTIMER // number of timers
} mjtTimer;
typedef enum mjtSleepState_ { // sleep state of an object
mjS_STATIC = -1, // object is static
mjS_ASLEEP = 0, // object is asleep
mjS_AWAKE = 1 // object is awake
} mjtSleepState;
//---------------------------------- mjContact -----------------------------------------------------
struct mjContact_ { // result of collision detection functions
@@ -221,6 +228,10 @@ struct mjData_ {
int nA; // number of non-zeros in constraint inverse inertia matrix
int nisland; // number of detected constraint islands
int nidof; // number of dofs in all islands
int ntree_awake; // number of awake trees
int nbody_awake; // number of awake dynamic and static bodies
int nparent_awake; // number of bodies with awake parents
int nv_awake; // number of awake dofs
// global properties
mjtNum time; // simulation time
@@ -261,6 +272,9 @@ struct mjData_ {
// sensors
mjtNum* sensordata; // sensor data array (nsensordata x 1)
// sleep state
int* tree_asleep; // <0: awake; >=0: index cycle of sleeping trees (ntree x 1)
// plugins
int* plugin; // copy of m->plugin, required for deletion (nplugin x 1)
uintptr_t* plugin_data; // pointer to plugin-managed data structure (nplugin x 1)
@@ -329,6 +343,13 @@ struct mjData_ {
// computed by mj_collision/mj_collideTree
mjtByte* bvh_active; // was bounding volume checked for collision (nbvh x 1)
// computed by mj_updateSleep
int* tree_awake; // is tree awake; 0: asleep; 1: awake (ntree x 1)
int* body_awake; // body sleep state (mjtSleepState) (nbody x 1)
int* body_awake_ind; // indices of awake and static bodies (nbody x 1)
int* parent_awake_ind; // indices of bodies with awake or static parents (nbody x 1)
int* dof_awake_ind; // indices of awake dofs (nv x 1)
//-------------------- POSITION, VELOCITY dependent
// computed by mj_fwdVelocity
@@ -408,6 +429,12 @@ struct mjData_ {
mjtNum* efc_R; // inverse constraint mass (nefc x 1)
int* tendon_efcadr; // first efc address involving tendon; -1: none (ntendon x 1)
// computed by mj_island (island tree structure)
int* tree_island; // island id of this tree; -1: none (ntree x 1)
int* island_ntree; // number of trees in this island (nisland x 1)
int* island_itreeadr; // island start address in itree vector (nisland x 1)
int* map_itree2tree; // map from itree to tree (ntree x 1)
// computed by mj_island (island dof structure)
int* dof_island; // island id of this dof; -1: none (nv x 1)
int* island_nv; // number of dofs in this island (nisland x 1)
+30 -5
View File
@@ -30,6 +30,7 @@
#define mjMAXCONPAIR 50 // maximum number of contacts per geom pair
#define mjMAXTREEDEPTH 50 // maximum bounding volume hierarchy depth
#define mjMAXFLEXNODES 27 // maximum number of flex nodes
#define mjMINAWAKE 10 // minimum number of timesteps before sleeping
//---------------------------------- sizes ---------------------------------------------------------
@@ -80,8 +81,9 @@ typedef enum mjtEnableBit_ { // enable optional feature bitflags
mjENBL_INVDISCRETE = 1<<3, // discrete-time inverse dynamics
// experimental features:
mjENBL_MULTICCD = 1<<4, // multi-point convex collision detection
mjENBL_SLEEP = 1<<5, // sleeping
mjNENABLE = 5 // number of enable flags
mjNENABLE = 6 // number of enable flags
} mjtEnableBit;
@@ -132,7 +134,7 @@ typedef enum mjtCamLight_ { // tracking mode for camera and light
typedef enum mjtLightType_ { // type of light
mjLIGHT_SPOT = 0, // spot
mjLIGHT_SPOT = 0, // spot
mjLIGHT_DIRECTIONAL, // directional
mjLIGHT_POINT, // point
mjLIGHT_IMAGE, // image-based
@@ -406,8 +408,18 @@ typedef enum mjtSameFrame_ { // frame alignment of bodies with their childr
} mjtSameFrame;
typedef enum mjtSleepPolicy_ { // per-tree sleep policy
mjSLEEP_AUTO = 0, // compiler chooses sleep policy
mjSLEEP_AUTO_NEVER, // compiler sleep policy: never
mjSLEEP_AUTO_ALLOWED, // compiler sleep policy: allowed
mjSLEEP_NEVER, // user sleep policy: never
mjSLEEP_ALLOWED, // user sleep policy: allowed
mjSLEEP_INIT, // user sleep policy: initialized asleep
} mjtSleepPolicy;
typedef enum mjtLRMode_ { // mode for actuator length range computation
mjLRMODE_NONE = 0, // do not process any actuators
mjLRMODE_NONE = 0, // do not process any actuators
mjLRMODE_MUSCLE, // process muscle actuators
mjLRMODE_MUSCLEUSER, // process muscle and user actuators
mjLRMODE_ALL // process all actuators
@@ -415,7 +427,7 @@ typedef enum mjtLRMode_ { // mode for actuator length range computation
typedef enum mjtFlexSelf_ { // mode for flex selfcollide
mjFLEXSELF_NONE = 0, // no self-collisions
mjFLEXSELF_NONE = 0, // no self-collisions
mjFLEXSELF_NARROW, // skip midphase, go directly to narrowphase
mjFLEXSELF_BVH, // use BVH in midphase (if midphase enabled)
mjFLEXSELF_SAP, // use SAP in midphase
@@ -424,7 +436,7 @@ typedef enum mjtFlexSelf_ { // mode for flex selfcollide
typedef enum mjtSDFType_ { // signed distance function (SDF) type
mjSDFTYPE_SINGLE = 0, // single SDF
mjSDFTYPE_SINGLE = 0, // single SDF
mjSDFTYPE_INTERSECTION, // max(A, B)
mjSDFTYPE_MIDSURFACE, // A - B
mjSDFTYPE_COLLISION, // A + B + abs(max(A, B))
@@ -477,6 +489,9 @@ struct mjOption_ { // physics options
mjtNum noslip_tolerance; // noslip solver tolerance
mjtNum ccd_tolerance; // convex collision solver tolerance
// sleep settings
mjtNum sleep_tolerance; // sleep velocity tolerance
// physical constants
mjtNum gravity[3]; // gravitational acceleration
mjtNum wind[3]; // wind (for lift, drag and viscosity)
@@ -810,6 +825,14 @@ struct mjModel_ {
mjtNum* dof_damping; // damping coefficient (nv x 1)
mjtNum* dof_invweight0; // diag. inverse inertia in qpos0 (nv x 1)
mjtNum* dof_M0; // diag. inertia in qpos0 (nv x 1)
mjtNum* dof_length; // linear: 1; angular: approx. length scale (nv x 1)
// trees
int* tree_bodyadr; // start addr of bodies (ntree x 1)
int* tree_bodynum; // number of bodies in tree (ntree x 1)
int* tree_dofadr; // start addr of dofs (ntree x 1)
int* tree_dofnum; // number of dofs in tree (ntree x 1)
int* tree_sleep_policy; // sleep policy (mjtSleepPolicy) (ntree x 1)
// geoms
int* geom_type; // geometric type (mjtGeom) (ngeom x 1)
@@ -1077,6 +1100,8 @@ struct mjModel_ {
int* tendon_num; // number of objects in tendon's path (ntendon x 1)
int* tendon_matid; // material id for rendering (ntendon x 1)
int* tendon_group; // group for visibility (ntendon x 1)
int* tendon_treenum; // number of trees along tendon's path (ntendon x 1)
int* tendon_treeid; // first two trees along tendon's path (ntendon x 2)
mjtByte* tendon_limited; // does tendon have length limits (ntendon x 1)
mjtByte* tendon_actfrclimited; // does tendon have actuator force limits (ntendon x 1)
mjtNum* tendon_width; // width for rendering (ntendon x 1)
+4
View File
@@ -70,6 +70,7 @@ typedef enum mjtMeshInertia_ { // type of mesh inertia
mjMESH_INERTIA_SHELL // shell mesh inertia
} mjtMeshInertia;
typedef enum mjtMeshBuiltin_ { // type of built-in procedural mesh
mjMESH_BUILTIN_NONE = 0, // no built-in mesh
mjMESH_BUILTIN_SPHERE, // sphere
@@ -81,6 +82,7 @@ typedef enum mjtMeshBuiltin_ { // type of built-in procedural mesh
mjMESH_BUILTIN_PLATE, // plate
} mjtMeshBuiltin;
typedef enum mjtBuiltin_ { // type of built-in procedural texture
mjBUILTIN_NONE = 0, // no built-in texture
mjBUILTIN_GRADIENT, // gradient: rgb1->rgb2
@@ -103,6 +105,7 @@ typedef enum mjtLimited_ { // type of limit specification
mjLIMITED_AUTO, // limited inferred from presence of range
} mjtLimited;
typedef enum mjtAlignFree_ { // whether to align free joints with the inertial frame
mjALIGNFREE_FALSE = 0, // don't align
mjALIGNFREE_TRUE, // align
@@ -233,6 +236,7 @@ typedef struct mjsBody_ { // body specification
// other
mjtByte mocap; // is this a mocap body
double gravcomp; // gravity compensation
mjtSleepPolicy sleep; // sleep policy
mjDoubleVec* userdata; // user data
mjtByte explicitinertial; // whether to save the body with explicit inertial clause
mjsPlugin plugin; // passive force plugin
+27 -1
View File
@@ -26,6 +26,7 @@
X( mjtNum, ls_tolerance ) \
X( mjtNum, noslip_tolerance ) \
X( mjtNum, ccd_tolerance ) \
X( mjtNum, sleep_tolerance ) \
X( mjtNum, density ) \
X( mjtNum, viscosity ) \
X( mjtNum, o_margin )
@@ -243,7 +244,15 @@
X ( mjtNum, dof_armature, nv, 1 ) \
X ( mjtNum, dof_damping, nv, 1 ) \
X ( mjtNum, dof_invweight0, nv, 1 ) \
X ( mjtNum, dof_M0, nv, 1 )
X ( mjtNum, dof_M0, nv, 1 ) \
X ( mjtNum, dof_length, nv, 1 )
#define MJMODEL_POINTERS_TREE \
X ( int, tree_bodyadr, ntree, 1 ) \
X ( int, tree_bodynum, ntree, 1 ) \
X ( int, tree_dofadr, ntree, 1 ) \
X ( int, tree_dofnum, ntree, 1 ) \
X ( int, tree_sleep_policy, ntree, 1 )
#define MJMODEL_POINTERS_GEOM \
X ( int, geom_type, ngeom, 1 ) \
@@ -509,6 +518,8 @@
X ( int, tendon_num, ntendon, 1 ) \
X ( int, tendon_matid, ntendon, 1 ) \
X ( int, tendon_group, ntendon, 1 ) \
X ( int, tendon_treenum, ntendon, 1 ) \
X ( int, tendon_treeid, ntendon, 2 ) \
X ( mjtByte, tendon_limited, ntendon, 1 ) \
X ( mjtByte, tendon_actfrclimited, ntendon, 1 ) \
X ( mjtNum, tendon_width, ntendon, 1 ) \
@@ -586,6 +597,7 @@
X ( mjtNum, oct_coeff, noct, 8 ) \
MJMODEL_POINTERS_JOINT \
MJMODEL_POINTERS_DOF \
MJMODEL_POINTERS_TREE \
MJMODEL_POINTERS_GEOM \
MJMODEL_POINTERS_SITE \
MJMODEL_POINTERS_CAMERA \
@@ -694,6 +706,7 @@
X ( mjtNum, act_dot, na, 1 ) \
X ( mjtNum, userdata, nuserdata, 1 ) \
X ( mjtNum, sensordata, nsensordata, 1 ) \
X ( int, tree_asleep, ntree, 1 ) \
X ( int, plugin, nplugin, 1 ) \
X ( uintptr_t, plugin_data, nplugin, 1 ) \
X ( mjtNum, xpos, nbody, 3 ) \
@@ -742,6 +755,11 @@
XNV ( mjtNum, qLD, nC, 1 ) \
X ( mjtNum, qLDiagInv, nv, 1 ) \
X ( mjtByte, bvh_active, nbvh, 1 ) \
X ( int, tree_awake, ntree, 1 ) \
X ( int, body_awake, nbody, 1 ) \
X ( int, body_awake_ind, nbody, 1 ) \
X ( int, parent_awake_ind, nbody, 1 ) \
X ( int, dof_awake_ind, nv, 1 ) \
X ( mjtNum, flexedge_velocity, nflexedge, 1 ) \
X ( mjtNum, ten_velocity, ntendon, 1 ) \
X ( mjtNum, actuator_velocity, nu, 1 ) \
@@ -810,6 +828,10 @@
// array fields of mjData that are used for constraint islands
#define MJDATA_ARENA_POINTERS_ISLAND \
X ( int, tree_island, MJ_M(ntree), 1 ) \
X ( int, island_ntree, MJ_D(nisland), 1 ) \
X ( int, island_itreeadr, MJ_D(nisland), 1 ) \
X ( int, map_itree2tree, MJ_M(ntree), 1 ) \
X ( int, dof_island, MJ_M(nv), 1 ) \
X ( int, island_nv, MJ_D(nisland), 1 ) \
X ( int, island_idofadr, MJ_D(nisland), 1 ) \
@@ -876,6 +898,10 @@
X( int, nA ) \
X( int, nisland ) \
X( int, nidof ) \
X( int, ntree_awake ) \
X( int, nbody_awake ) \
X( int, nparent_awake ) \
X( int, nv_awake ) \
X( mjtNum, time ) \
X( uintptr_t, threadpool )