mj_inverse() calculates the inverse dynamics of the dynamics system, similar to
inverseDynamics() function in Matlab Robotics Toolbox.
Input to the function need to be pre-assigned to the
sim.data struct (
mjData.mocap_quat) and the result can be obtained from
qfrc_inverse is the sum of all applied forces, namely
Jx' * xfrc_applied are user defined external forces or forces from spring damper mechanisms. If no additional force is exerted on either joint or body, joints has no friction, the first two terms become 0 and
qfrc_inverse = qfrc_acturator.
The contact forces are treated as internal forces of the system and can be handled implicitly by the inverse dynamics solver.
As for actuation, we have
in simple cases, but MuJoCo can also model more elaborate actuators.
mj_inverse() does not currently attempt to infer actuator forces. It just computes
qfrc_inverse which is the sum of all external and actuator forces. On the bright side, the constraint forces are the output of the computation and not the input. So all you need to provide is the current position, velocity and acceleration (
It may seem magic, but the reason it is doable is because the constraint model is soft, so the kinematics uniquely determine both the applied force and the constraint force.2
The top-level function mj_inverse invokes the following sequence of computations.
- Compute the forward kinematics.
- Compute the body inertias and joint axes.
- Compute the tendon lengths and moment arms.
- Compute the actuator lengths and moment arms.
- Compute the composite rigid body inertias and form the joint-space inertia matrix $M(\qb)$.
- Compute the sparse factorization of the joint-space inertia matrix.
- Construct the list of active contacts (
- Construct the constraint Jacobian and compute the constraint residuals.
- Compute sensor data that only depends on position, and the potential energy if enabled (
- Compute the tendon and actuator velocities (
- Compute the body velocities (
sim.data.body_xvelr) and joint axes rates of change.
- Compute sensor data that depends on velocity, and the kinetic energy if enabled (
- Compute all passive forces (
- Compute the reference constraint acceleration.
- Compute the constraint force. This is done analytically, without using a numerical solver.
- Compute the inverse dynamics for the unconstrained system.
- Compute sensor data that depends on force and acceleration if enabled (
- Compute the vector
mjData.qfrc_inverseby combining all results. This is the main output of inverse dynamics. It equals the sum of external and actuation forces.