wbc_kit API reference¶
Use Crocoddyl's DDP solver from Python with cppyy. To put a custom action model in
the DDP loop without Python calls or a separate CMake build, write it in inline C++.
In the unicycle benchmark, the C++ model ran about 21 times faster than the
Python-derived model and reached the same cost.
wbc_kit.bringup_crocoddyl() returns the C++ crocoddyl namespace.
Create a separate Pixi project with the https://prefix.dev/awesomebytes and
conda-forge channels, then install pixi add wbc-kit. Run scripts with
pixi run python your_script.py. Crocoddyl and ROS packages require incompatible
Boost versions in the current package set; use separate environments.
See Getting Started
and the kit overview.
Commands using pixi run -e wbc require this repository checkout.
Use this kit when¶
- A custom Crocoddyl action or cost model written in Python is too slow in the DDP loop.
- You want to prototype a model in Python and compile its C++ implementation in the same script.
- Do not use it to instantiate pinocchio models with new scalar types. This is blocked in the current environment. Crocoddyl and tsid cross-inheritance are described in the report.
Bringup¶
import wbc_kit
cr = wbc_kit.bringup_crocoddyl() # returns cppyy.gbl.crocoddyl; use its API verbatim
m = cr.ActionModelUnicycle() # built-in models, ShootingProblem, SolverFDDP, ...
The helper loads pinocchio's libpinocchio_default.so, includes pinocchio/fwd.hpp
before Crocoddyl headers, and loads the required shared libraries. It is idempotent.
Set with_solvers=False to skip JIT-compiling the shooting and solver headers.
Define a custom C++ action model¶
wbc_kit.bringup_crocoddyl()
wbc_kit.safe_cppdef(r'''
namespace mywbc {
using crocoddyl::ActionModelAbstract; using crocoddyl::ActionDataAbstract;
struct Model : ActionModelAbstract {
Model() : ActionModelAbstract(std::make_shared<crocoddyl::StateVector>(3), 2, 5) {}
void calc(const std::shared_ptr<ActionDataAbstract>& d,
const Eigen::Ref<const Eigen::VectorXd>& x,
const Eigen::Ref<const Eigen::VectorXd>& u) override { /* xnext,r,cost */ }
void calc(const std::shared_ptr<ActionDataAbstract>& d,
const Eigen::Ref<const Eigen::VectorXd>& x) override { /* terminal */ }
void calcDiff(const std::shared_ptr<ActionDataAbstract>& d,
const Eigen::Ref<const Eigen::VectorXd>& x,
const Eigen::Ref<const Eigen::VectorXd>& u) override { /* Fx,Fu,Lx.. */ }
void calcDiff(const std::shared_ptr<ActionDataAbstract>& d,
const Eigen::Ref<const Eigen::VectorXd>& x) override { /* terminal */ }
''' + wbc_kit.ACTION_MODEL_CLONES.format(cls="Model") + r'''
};
std::shared_ptr<ActionModelAbstract> make(){ return std::make_shared<Model>(); }
}''')
import cppyy
model = cppyy.gbl.mywbc.make() # native C++ model; hand to a C++-built solve
See the complete model and FDDP driver.
From this repository checkout, run the demo and benchmark with
pixi run -e wbc demo-wbc-lower.
Notes¶
- Use
safe_cppdefinstead of callingcppyy.cppdefdirectly. An incorrect override or missing clone method can make Cling crash without a Python traceback. The helper probes the code in a subprocess and raisesCppyyKitErroron failure. ACTION_MODEL_CLONESis required. Crocoddyl 3.2'sCROCODDYL_BASE_CASTmacro adds the pure virtual methodscloneAsDoubleandcloneAsFloat.- The
calcandcalcDiffsignatures must match the base class. For the double scalar, useconst Eigen::Ref<const Eigen::VectorXd>&, equivalent toconst Eigen::Ref<const VectorXs>&. - Build the running-model vector,
ShootingProblem, andSolverFDDPin C++. A C++ model created by cppyy cannot be passed to Crocoddyl's boost::pythonShootingProblem. You can use both bindings in one script, but keep their objects separate. - The
wbcenvironment is standalone. It cannot share a process with the ROS packages because they require different Boost versions.