Convex and Analytically-Invertible Dynamics With Contacts and Constraints Theory and Implementation in MuJoCo
Convex and Analytically-Invertible Dynamics With Contacts and Constraints Theory and Implementation in MuJoCo
We have not divided by dt because fE , f are impulses. by modifying the inertia as M = M + Bh. Thus we imple-
The computation is carried out in two phases. Phase I ment implicit damping by defining D ≡ armature+Bh, and
corresponds to smooth dynamics and preparation for impulse adding −Bv to the passive force p (q, v). We require D to
dynamics, and relies on standard methods [9]. Phase II cor- be diagonal and non-negative, so as to preserve the sparsity
responds to impulse dynamics; it is the more the challenging and positive-definiteness of M .
part and is also where the novelty of our approach lies.
C. Constraint dynamics
In Phase I we compute M, D, c, p, JE , J. In our implemen-
tation in the MuJoCo physics engine, M (q)−D is computed The dynamics of equality-constrained systems can be
with the Composite Rigid Body (CRB) algorithm, then D obtained from the Gauss principle [11], which we now recall.
is added and the resulting M is LDL-factorized taking Suppose we have unconstrained continuous-time dynamics
advantage of branch-induced sparsity. c (q, v) is computed M a = τ subject to acceleration constraints JE a = a∗E .
with the Recursive Newton-Euler (RNE) algorithm – which Then, given M, τ, JE , a∗E , the constrained acceleration is the
is actually an algorithm for inverse dynamics: solution to the convex optimization problem
T
RNE (q, v, v̇) = (M (q) − D) v̇ + c (q, v) (2) min (M a − τ ) M −1 (M a − τ ) (4)
a : JE a=a∗
E
Here we use it to compute RNE (q, v, 0) = c (q, v). Note Since we aim to invert the dynamics later, and hard con-
that CRB and RNE do not take into account the extra inertia straints are non-invertible, we must soften the constraints
D, which is why we have to add it to the output of these somehow. We propose to do this by softening the Gauss prin-
algorithms to obtain the total inertia M . The contact Jacobian ciple, i.e. replacing the hard constraint with a soft penalty.
Proposition. The acceleration of the constrained system is and v+ ≡ Jv 0 is the next-step velocity after the contact
defined as the solution to the convex optimization problem impulse, all expressed in contact space.
T To complete the forward dynamics computation we have
min (M a − τ ) M −1 (M a − τ ) + (5) to solve (8) for (f, v+ ) given (A, v− ). This involves twice
a
(JE a − a∗E )
T
R−1 ∗ as many unknowns as the number of equations in (8), thus
E (JE a − aE )
we need additional information – which comes in the from
where the regularizer RE is a diagonal positive matrix. of inequality constraints reflecting the laws of contact and
In the limit RE → 0 the solution to problem (5) converges friction. We now introduce these inequalities, and at the same
to the solution to problem (4), which is non-invertible. How- time explain what exactly is included in the contact space.
ever for any RE 0 the resulting dynamics are invertible, In our current implementation in MuJoCo the contact
as shown by construction later. solver can handle three types of objects: friction loss in the
We now return to discrete time and impose a soft con- joints, limits on joint angles and distances, and frictional
straint on velocity rather than acceleration: JE v 0 = v∗E (q, v). contacts. The elements fi of the vector f satisfy different
Here v∗E is the desired next-step velocity in constraint space, inequality constraints depending on the type of object they
computed by any suitable constraint stabilization mechanism. represent, as follows:
Our specific choice will be described later.
friction: η (i) − fi ≥ 0
Theorem 1. The dynamics under the proposed soft Gauss
principle are η (i) + fi ≥ 0
v 0 = vb + M
c−1 J T f (6) limit: fi ≥ 0
(9)
where M
c and vb are defined as
contact: fi ≥ 0
d(i) 2
c ≡ M + JET R−1 JE
M (7) X fi+j
E
fi2 − ≥0
−1 T −1 ∗ µj (i)
vb ≡ v + M
c (p + u − c) h + JE RE (vE − JE v) j=1
Proof. Apply the soft Gauss principle with Assembling the left hand sides of the above inequalities in
the vector φ (f ), we can write (9) as φ (f ) ≥ 0.
v0 − v ∗ v∗ − JE v JT f In (9) subscripts denote vector elements as usual, while
a= , aE = E , τ =p+u−c+
h h h i in brackets denotes parameters of the object whose data
Solving (5) analytically yields (6, 7). starts at position i in the vector f . In the case of friction
We can interpret M
c as the apparent inertia that takes into loss, η (i) is the (load-independent) joint torque that is lost
account the constraints, and vb as the next-step velocity that to friction before the joint starts accelerating. Limits can
takes into account all forces except for the contact impulse. be defined for revolute, prismatic and ball joints (the latter
For hard constraints the dynamics remain in the general limits are cylinders in the angle-axis representation of the
form (6), but the definitions (7) are replaced with joint quaternion), as well as for tendon lengths (tendons
are strings whose spatial path is defined by via points and
c−1 ≡ M −1 − M −1 JET A−1 JE M −1
M E wrapping objects), and distances between geometric shapes
vb ≡ v + M (p + u − c) h + M −1 JET A−1 (v∗E − JE v)
c−1
E (i.e. frictionless contacts).
In the case of frictional contacts, d (i) ∈ {2, 3, 5} is
where AE ≡ JE M −1 JET is the inverse inertia in constraint the dimensionality of the friction space and µj (i) are the
space. This can be shown by solving (4), or by taking the friction coefficients in the d (i) dimensions. The contact
limit RE → 0 and using the matrix inversion lemma. contributes 1 + d (i) elements to the vector f with the
Equations (6, 7) represent modified dynamics which im- following semantics. Consider a 3D frame whose first axis
plicitly take the constraints into account. We did not compute is aligned with the contact normal. Impulses (fi , fi+1 , fi+2 )
the impulse fE explicitly because it can only be computed cause relative translation along the contact frame axes, while
after v 0 is known, i.e. in the context of inverse dynamics. (fi+3 , fi+4 , fi+5 ) cause relative rotation around the axes.
D. Contact dynamics Thus fi is the normal impulse, (fi+1 , fi+2 ) is the tangential
friction impulse, fi+3 is the torsional friction impulse, and
We now have everything in place for the contact com- (fi+4 , fi+5 ) is the rolling friction impulse. The contact
putation stage. First we project (6) in contact space via inequalities in (9) specify that the normal impulse must be
multiplication by the contact Jacobian J: non-negative, and that the impulse vector must lie within the
Jv 0 = J vb + J M
c−1 J T f elliptical friction cone. In the special case when d (i) = 2
and µ1 (i) = µ2 (i) = µ (i), this reduces to the more familiar
Then we write the contact-space dynamics as definition of a friction cone:
q
Af + v− = v+ (8) 2
µ (i) fi ≥ fi+1 2
+ fi+2
c−1 T
where A ≡ J M J is the inverse of the apparent inertia, We now return to the computation of the contact impulse
v− ≡ J vb is the next-step velocity before the contact impulse, f and next-step velocity v+ . Conditions (8, 9) are still
insufficient to determine a unique solution. There are two Proof. Combining (6) and (7) yields
paths forward: complementarity-based and complementarity-
M + JET R−1
0
free. The former approach introduces additional constraints E JE (v − v) =
Contact impulse
Time (2 sec) Time (2 sec)
Fig. 1. The two MuJoCo models used in the simulations. Left: single-joint Fig. 2. Comparison of the friction impulse and contact impulse computed
mechanism with dry friction. The arrow shows the contact impulse. Right: by the forward (black) and inverse (orange) dynamics, while the single-
27-dof humanoid. The red cylinders show the active contacts. Joint limits joint system was being perturbed randomly. The two curves in each plot
prevent more contacts. are within 1E-5 of each other, thus the difference is not visible.
set B = B (1 + ) , K = K (1 + ) for some B, K. Assuming To summarize, the user specifies the impulse regularization
for the moment that A is invertible, (25) becomes scaling and the error reduction time constant κ, and then the
solver parameters R, RE , v∗ , v∗E are computed automatically
ẍ + B ẋ + Kx = a (26) using (27, 28, 24) with the above modifications.
1+
Instead of using a diagonal approximation, we can obtain
Therefore in the limit → ∞ we have a spring-damper
a stabilization mechanism that is closer to critical damping
driven by a. In the limit → 0 the spring-damper becomes
as follows. Define a diagonal R as above. Now solve the
autonomous and removes constraint violations regardless of
following equations for B and K:
a. The latter limit corresponds to hard constraints and con-
AR−1 B = 2κ−1 I + AR−1
tacts. In practice we use small . Since our solver implements
the above dynamics implicitly, we can get very close to the AR−1 K = κ−2 I + AR−1
→ 0 limit without instabilities, numerical errors, or need
for small timesteps. If A is invertible these equations can be solved exactly,
While (26) is illuminating, it does not apply in general resulting in exact critical damping. If not, then a pseudo-
because A can be singular; for example, two frictional inverse can be used. The resulting B and K are no longer
contacts on the same body make A singular. Furthermore diagonal, but that does not complicate the computation.
we cannot set R = A because we want R to be diagonal,
VI. S IMULATIONS
so that the dynamics can be inverted analytically.
In our implementation we set R to a diagonal matrix: A. Correctness
We demonstrate numerically the correctness of the in-
Rii = max (Aii , rmin ) , (27)
version using a single-joint mechanism (Figure 1 left) with
Similarly the stiffness and damping are diagonal: two impulses: dry friction in the joint and contact with the
ground. Figure 2 compares these impulses as computed by
Bii = 2 (1 + ) κ−1 , Kii = (1 + ) κ−2 (28) the forward and inverse dynamics, while the mechanism is
We can now carry the analysis of (25) further by approx- perturbed with random control forces.
imating A with its diagonal, resulting in Recall that the impulses in the forward dynamics are
computed with our GPGS iterative solver which may not
discover the optimal solution within the number of iterations
ẍ + 2κ−1 ẋ + κ−2 x ≈ a
1+ we allow in runtime. Indeed for more complex simulations
Note that the choice of stiffness and damping coefficients in the forward and inverse do not agree so closely, making
(28) made the autonomous part of the dynamics critically- the inverse dynamics approach more appealing for optimal
damped, where κ−1 is the natural frequency and so κ is the control and estimation applications.
time constant of the x-space dynamics. It is also informative
B. Speed of computation
to look at penetrations. If we consider a free-floating object
resting on the ground in the presence of gravity g, we have Figure 3 illustrates the computational efficiency of our
a = g, and so the penetration is algorithms and their implementation in MuJoCo. The tests
were done on an Intel i7-3930K processor, Windows 7,
κ2 g single-threaded computation taking advantage of AVX in-
x≈
1+ structions (with custom BLAS-like routines). We dragged
independent of the mass of the object. the humanoid model around in the virtual environment
There are two final caveats. First, the Rii corresponding using a 3D mouse. This generated many different contact
to the friction dimensions of each contact must be further configurations with different Jacobian size. We run both the
adjusted so as to satisfy (22). Second, in friction dimensions forward and inverse dynamics and timed them using high-
we only care about velocity, thus we set the corresponding resolution timers. The timing data and size of the Jacobian
Kii = 0. In that case the velocity decays to 0 at rate 2κ−1 , at each simulation step were saved in a file. Afterwards, we
and κ again has the meaning of a time constant. found all simulation steps in which the Jacobian had a given
500 20
CPU time per step (microsec) the input, but the actual computation is not affected since it
400 Forward(50) / Inverse always relies on the same analytical procedure.
15
Our softness parameters , κ are generally related to the
Speedup factor
300 constraint-force mixing (CFM) and error reduction parameter
Forward(50)
10 (ERP) introduced in the Open Dynamics Engine (ODE),
200
although the specifics are different. CFM regularization in
5 ODE is applied to the full system while we only apply
100 Forward(5) Forward(5) / Inverse
Inverse
regularization in the impulse space, and furthermore the
0 0 problem solved by ODE does not become convex even after
0 20 40 60 80 100 0 20 40 60 80 100
Size of contact Jacobian Size of contact Jacobian
regularization. ERP implements a first-order constraint stabi-
lization mechanism while we use a second-order mechanism.
Fig. 3. Comparison of the speed of the forward and inverse dynamics Our results have many potential applications in data anal-
computations for the humanoid. Left: CPU time per simulation step is shown ysis, simulation, estimation and control. We have already
for different sizes of the contact Jacobian. The PGS iterative solver in the
forward dynamics was run for 5 or 50 iterations. Right: the speedup factor
leveraged the new analytical inverse dynamics in the context
is the ratio of forward dynamics CPU time to inverse dynamics CPU time. of state estimation, using it to enforce a physics consis-
tency prior [13]. Future applications to optimal control are
size (plotted on the x-axis in Figure 3), and computed the particularly exciting. We were recently able to synthesize
median of the corresponding CPU times. We performed the complex full-body movements with direct trajectory opti-
test twice, with 5 and 50 iterations of the GPGS solver in the mization automatically, without relying on motion capture
forward dynamics. To generate larger Jacobians we enabled or manual scripting [14]. While this was done offline, we
all contact friction directions. For 10 contacts with sliding estimate that in model-predictive control (MPC) mode our
friction only, the Jacobian size would be 30. existing optimizer will be an order of magnitude slower than
We find the results in Figure 3 quite remarkable. On a real time. The analytical inverse dynamics developed here
single core of a desktop processor, we can evaluate the may provide the missing order of magnitude speedup. If we
inverse dynamics of a 27-dof humanoid subject to 100 could replicate [14] in MPC mode, it is likely to transform
impulses in 30 microseconds. In the absence of impulses the robotic control as well as interactive games.
time goes down to 10 microseconds. Since the simulation
timestep is 10 milliseconds, the inverse dynamics are being R EFERENCES
computed between 300 and 1000 times faster than real time [1] D. Stewart and J. Trinkle, “An implicit time-stepping scheme for
rigid-body dynamics with inelastic collisions and coulomb friction,”
depending on the number of impulses. The forward dynamics International Journal Numerical Methods Engineering, vol. 39, pp.
are slower – by an order of magnitude – but still much faster 2673–2691, 1996.
than real time. Note that increasing the number of GPGS [2] M. Anitescu, F. Potra, and D. Stewart, “Time-stepping for three-
dimensional rigid body dynamics,” Computer Methods in Applied
iterations from 5 to 50 is expensive with large number of Mechanics and Engineering, vol. 177, pp. 183–197, 1999.
impulses, but the inverse dynamics are faster regardless of [3] D. Kaufman, S. Sueda, D. James, and D. Pai, “Staggered projections
the number of GPGS iterations. This is because computing for frictional contact in multibody systems,” ACM Transactions on
Graphics, vol. 164, pp. 1–11, 2008.
the full A matrix is avoided. [4] E. Todorov, “A convex, smooth and invertible contact model for
trajectory optimization,” ICRA, 2011.
VII. C ONCLUSIONS AND FUTURE WORK [5] E. Drumwright and Shell, “Modeling contact friction and joint fric-
tion in dynamic robotic simulation using the principle of maximum
We described a full-featured simulation pipeline for multi- dissipation,” International Workshop on the Algorithmic Foundations
joint dynamics subject to constraints and contacts. Forward of Robotics, 2010.
simulation requires an iterative solver, while inverse dynam- [6] E. Drumwright and E. Shell, “An evaluation of methods for modeling
contact in multibody simulation,” ICRA, 2011.
ics are computed analytically and faster. [7] Y. Tassa, T. Erez, and E. Todorov, “Synthesis and stabilization of
The inversion is possible because our formulation allows a complex behaviors through online trajectory optimization,” IROS,
certain amount of softness. One can think of the impulses as 2012.
[8] A. Chatterjee and A. Ruina, “A new algebraic rigid body collision
being generated by smart spring-dampers, which are aware of law based on impulse space considerations,” Journal of Applied
each other and furthermore scale their stiffness and damping Mechanics, 1998.
automatically with inertia. The forward formulation consid- [9] R. Featherstone, Rigid Body Dynamics Algorithms. Springer, 2008.
[10] ——, “Efficient factorization of the joint-space inertia matrix for
ers the interactions among all impulses, involving the dense branched kinematic trees,” International Journal of Robotics Research,
matrix A and the diagonal regularizer R. The mathematical 2005.
relation between the forward and inverse is such that the A [11] F. Udwadia and R. Kalaba, “A new perspective on constrained motion,”
Proceedings of the Royal Society, 1992.
matrix is no longer needed in the inverse, and we are left only [12] J. Baumgarte, “Stabilization of constraints and integrals of motion in
with the R matrix allowing us to decompose the problem. dynamical systems,” Computer Methods In Applied Mechanics And
Softness is controlled by the two parameters , κ whose Engineering, 1972.
[13] K. Lowrey, Y. Tassa, T. Erez, S. Kolev, and E. Todorov, “Physically-
effects we analyzed. Small values of these parameters make consistent sensor fusion for contact-rich behaviors,” manuscript under
the forward dynamics harder to simulate (requiring smaller review, 2014.
timesteps or larger number of solver iterations). They also [14] I. Mordatch, E. Todorov, and Z. Popovic, “Discovery of complex
behaviors through contact-invariant optimization,” SIGGRAPH, 2012.
make the output of the inverse dynamics more sensitive to