ami-iit / jaxsim

A differentiable physics engine and multibody dynamics library for control and robot learning.
https://jaxsim.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
57 stars 9 forks source link

Investigate AD (automatic differentiation) support #4

Open diegoferigo opened 2 years ago

diegoferigo commented 2 years ago

JAXsim currently focuses only on sampling performance, exploiting jax.jit and jax.vmap. Being written in JAX, the forward step of the simulation should be differentiable (also considering contact dynamics, since it is continuous), but it has not yet been investigated.

Interesting resources:

diegoferigo commented 3 months ago

I had preliminary results of differentiating against the hardware parameters introduced in #101 (and refined in #120).

I created a simple toy problem of a box falling for 1 second over a compliant flat terrain. I designed an quick optimization pipeline that simulates two of such boxes: a nominal one with default parameters, and a training one with wrong parameters. Once I collect trajectories from both, I compute the RMS error between the two trajectory, get the gradient w.r.t. a set of hardware parameters, and with it update the parameters of the training model.

As first attempt, this seems promising. Of course, when dealing with quantities not belonging to $\mathbb{R}^n$, the optimization should be properly adapted. For $\text{SE}(3)$, I believe we can exploit jaxlie. For the inertia tensor, probably we need to find a proper parameterization for symmetric positive-definite matrices that also meet triangular inequality. I would start looking at a principal axes decomposition together with the enforcement of positive principal moments of inertia (maybe storing them as logarithms).

cc @flferretti @CarlottaSartore @traversaro @S-Dafarra @DanielePucci

DanielePucci commented 3 months ago

Super, important for many in @ami-iit/alpha-delta-tau

diegoferigo commented 3 weeks ago

A good example that compares a derivative computed analytically w.r.t. the same quantity obtained by AD is:

diegoferigo commented 3 weeks ago

Another example of using AD is the validation of $\dot{M}(\mathbf{q}) = C(\mathbf{q}, \boldsymbol{\nu}) + C^\top(\mathbf{q}, \boldsymbol{\nu})$ performed in: