Equality Constraints¶
Abstract
An equality constraint ties two parts of a model together, such as a weld that holds one body to another. Any of them can start switched off and be switched on and off during a run, which is how you model a grab and a release, a latch or a clamp.
A Constraint That Starts Off¶
This model has a ball above a hook, which is a site fixed in the world. The weld joins a site on the ball, ball_site, to the hook, hook_site:
| Python | |
|---|---|
EqualityWeldSite welds two sites: it pulls their frames together wherever their bodies are, so the ball is held with its site on the hook. The sites' names come from the objects themselves, so there is no string to retype. active=False makes the constraint exist but do nothing until the run switches it on. Put it in an Equality, and append that to mjcf.equalities.
Switching It On and Off¶
In the runtime function, enable, disable and is_active act on the running simulation. They take the state because the switch is mjData.eq_active, which lives in the state and not in the model. set_active does the same with a bool. Here the weld goes on as the ball passes the hook, holds it for half a second, and goes off:
| Python | |
|---|---|
The ball falls freely until the weld is enabled, stays on the hook while it is on, and falls again once it is disabled. Every kind of equality constraint has the same methods, and the kinds are listed under Equality Types.
Tip: Switch On When the Parts Are Close
A weld is soft, so it pulls the two sites together however far apart they are, and solref and solimp set how stiff it is. Switched on across a large gap, it pulls hard and the parts can jump. Switch it on once the parts are already close, as the example does.
Equality Types¶
Each kind goes in the matching list of an Equality: connects, welds, joints, tendons, flexes or flexstrains. All of them are soft, with solref and solimp setting how stiff, and all of them have active and the methods above.
Available Equality Types
-
EqualityConnectBody
Pins one body to another at an anchor point, which you give in the first body's frame. It works like a ball joint outside the kinematic tree, and the bodies must start with the point together. Leave out the second body to pin the first to the world.
Holds a position only, so the bodies can still rotate about the point.
EqualityWeldBodyholds the rotation too. -
EqualityConnectSite
Pulls a site on one body onto a site on another, so the two meet at a point. It is the ball joint of
EqualityConnectBody, but set by sites. The sites do not have to start together, and moving a site's position at runtime moves the constraint.Holds a position only.
EqualityWeldSiteholds the rotation too. -
EqualityWeldBody
Holds two bodies at the relative pose they have in the model, removing all relative motion between them, softly like every constraint in MuJoCo. The bodies do not need to be close together. Leave out the second body to weld the first to the world, and switching that weld on and off fixes the body in place for a while.
Think of it as gluing two bodies in place. If they never need to come apart, make one a child of the other instead.
torquescalesets how much the weld cares about rotation compared with position, and 0 makes it behave like a connect. -
EqualityWeldSite
Aligns the frames of two sites, position and orientation, wherever their bodies are. It is the body weld set by sites, so the sites do not have to start together, and the constraint pulls them together when it is on.
The one in the example above, and a good fit for a grab or a clamp.
torquescaleworks as it does for the body weld, and 0 makes it behave likeEqualityConnectSite. -
EqualityJoint
Makes one joint follow another, so moving one moves the other. Slide and hinge joints only. Leave out the second joint and the first is held in place.
Good for a gear ratio, or for two fingers of a gripper that move together. Holding a joint in place makes one you can lock and unlock during a run.
-
EqualityTendon
Makes the length of one tendon follow another's. Leave out the second tendon and the first is held at its length.
Couples two tendons the way
EqualityJointcouples two joints, or holds one tendon at a length you can lock and unlock during a run. -
EqualityFlex
Keeps every edge of a flex at its starting length, which maintains the shape of the deformable object. It adds one constraint per edge, where every other kind adds a fixed number.
The cost grows with the flex, since every edge is a constraint. Use it when a flex should keep its shape.
-
EqualityFlexStrain
Holds the strain of a flex to its starting values, so a soft body keeps its volume and resists shear. Only for 3D flexes with trilinear or quadratic interpolation, and
cellpicks the grid cell it applies to.The cost per element is high: 26 constraints for a trilinear element and 162 for a quadratic one.
All Code
Success
You can now define an equality constraint that starts off, and switch it on and off while a run is going.