Skip to content
This repository has been archived by the owner on May 23, 2022. It is now read-only.

An example showing how to use CppADCodeGenEigenPy to automatically differentiate rigid body dynamics.

Notifications You must be signed in to change notification settings

adamheins/CppADCodeGenEigenPy-dynamics-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This repo has been archived because it has been included directly into the CppADCodeGenEigenPy project (in the examples/ directory).

CppADCodeGenEigenPy Dynamics Example

This repo shows an example of using CppADCodeGenEigenPy to differentiate functions related to rigid body dynamics.

The DynamicsModel shows how to differentiate basic forward dynamics computed using the Newton-Euler equations of motion. The RolloutCostModel shows how to differentiate a scalar cost function of the system state and input forward simulated over a time horizon, similar to what may be used for model predictive control.

There are Python scripts that:

  1. show how to use the auto-differentiated models from Python, and
  2. compare the performance (both startup and runtime) with JAX, a popular Python autodiff library.

Installation and usage

First, follow the instructions to install CppADCodeGenEigenPy.

Next, get a copy of this repo:

git clone https://github.com/adamheins/CppADCodeGenEigenPy-dynamics-example.git
cd CppADCodeGenEigenPy-dynamics-example

Compile the auto-differentiated models:

# this compiles the program to generate the model
make compiler

# use the program compiled by the above line to actually produce the model
# (which is shared object .so file)
make model

If you get an error about not being able to find an Eigen header, you may have to change the path to the Eigen include directory in the Makefile.

Install required Python dependencies (note that Python 3 is expected):

pip install -r requirements.txt

Test out the models using the provided scripts:

python scripts/test_dynamics_model.py
python scripts/test_rollout_model.py

These scripts print information about the execution time for the compiled model and the equivalent JAX model, and assert that both models produce equivalent results.

On my system, loading the C++ RolloutCostModel takes under 1 millisecond, whereas the equivalent JAX model takes about 5 seconds, since it has to JIT compile each time the script is run. After the initial compilation, evaluating the Jacobians is also about an order of magnitude faster using the C++ model.

About

An example showing how to use CppADCodeGenEigenPy to automatically differentiate rigid body dynamics.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages