How leggedrobotics/ocs2 works
Project type and purpose
• This is an open‐source C++ toolbox (OCS2) for optimal control of switched systems, offering multiple solver algorithms (DDP, iLQR, SQP, SLQ, PISOC), automatic differentiation, URDF-based model setup, ROS interfaces, Python bindings, documentation, and robotic examples.
High-level architectural layers
A. Third-party libraries
• cppad, cppadcg, ipopt, Eigen, BLASFEO, HPIPM, Pinocchio, Raisim, etc.
• Footprint: ocs2_thirdparty, blasfeo_catkin, hpipm_catkin
B. Core computation layer (ocs2_core)
• Numeric traits, integrators, differentiators, constraints, costs, dynamics, loop-shaping, penalties, thread support
• Auto-diff engine and solver pre‐computation
C. Solver modules
• Differential dynamic programming: ocs2_ddp
• Interior point multiple-shooting: ocs2_ipm
• Sequential linear quadratic (SLQ): ocs2_slp
• Sequential quadratic programming (SQP): ocs2_sqp/ocs2_sqp
• Classical multiple-shooting: ocs2_oc
• Aggregator/upper‐level control: ocs2_ocs2
• Gradient‐based solvers: ocs2_frank_wolfe
D. Interfaces and bindings
• ROS messages & topics: ocs2_msgs
• ROS wrapper & visualization: ocs2_ros_interfaces
• Python bindings: ocs2_python_interface, ocs2_mpcnet_core + per-robot MPCNet packages
E. Robotics examples and deployment
• Robot-specific modules: ocs2_robotic_examples/* (ballbot, cartpole, legged, mobile manipulator, anymal, quadrotor)
• Physics-based rollout: ocs2_raisim (Raisim core, visualization, ROS converters)
F. Documentation and CI
• Sphinx and Doxygen: ocs2_doc
• GitHub Actions, Jenkins pipelines, Dockerfile
Data and control flow
1) User supplies URDF & configuration →
2) Model generator (in core or examples) produces system dynamics, cost, constraints →
3) Core pre‐computation & auto-diff →
4) Solver module (DDP/SPQ/SLQ/IPM/etc.) computes optimal control →
5) Rollout & performance indices →
6) Interfaces publish results via ROS topics (ocs2_msgs) →
7) Visualization in RViz or execution on hardware
8) Python layer (MPCNet or Python API) can drive training or call C++ library
Architectural patterns and principles
• Layered modular design: clear separation of core math, solver algorithms, interfaces, and applications
• Plugin-style solver modules share common core interfaces
• Use of CMake for multi-package build, ROS catkin style for message and interface packages
• Python bindings via Pybind11 layered on top of C++ core
• Automatic differentiation isolates derivative code from high-level logic