A (hopefully) easy-to-follow Iterative MPC tracking controller built with CVXPY and paired with MuJoCo. Designed to help anyone looking to transition from basic control to real-time convex optimization.
MuJoCo simulation with the mushr car modelThis mainly uses CVXPY to maintain a strict quadratic programming framework rather than relying on a non-linear solver like CasADi (which I love btw!). But, implementing an iMPC, we can still bridge the gap between convex optimization and real-world vehicle physics, allowing you to handle non-linear kinematics through iterative linearization.
Note: I've also preserved my original notebooks on Model Predictive Control for path-following problems here for historical context, a bit outdated.
This repo builds upon code and ideas from other open-source projects; please check them out in the Special Thanks section!
You can run the demo with MuJoCo and the mushr car or just python with no physics simulation:
Here is a brief overview of the codebase:
mpc_python
├── config/
│ ├── mpc.yaml # the configuration of the MPC. Start here!
│ └── simulation.yaml # common configuration of the demos
└── mpc_python/
├── cvxpy_mpc/
│ ├── cvxpy_mpc.py # main mpc implementation
│ └── utils.py
├── models/
│ └── mushr/ # model from prl-mushr/mushr_mujoco_ros
├── mpc_demo_mujoco.py # mujoco demo entry point
└── mpc_demo_nosim.py # no physics demo entry point
There are a few ways to run this:
A Nix flake is provided for a reproducible development shell.
Just run the 2 examples below:
nix run --impure .#mujoco-demo
nix run .#nosim-demoOtherwise, enter the development shell: this is heavier but also includes jupyter lab to experiment with the notebooks.
nix develop --impureGUI demos require nixGL (auto-detects Intel/AMD/NVIDIA GPU):
nixGL python mpc_python/mpc_demo_mujoco.pyHeadless demos run without it:
python mpc_python/mpc_demo_nosim.pyconda env create -f env.yml
conda activate simulationThen run the scripts:
python3 mpc_demo_mujoco.py
python3 mpc_demo_nosim.py-
Model derivation — analytical and numerical derivation (1.0, parametrized curves, differential, motion model)
-
MPC — implementation and testing of various tweaks (2.0, 2.1, 2.2, 2.3)
-
Obstacle Avoidance — halfplane constraints to avoid track collisions (3.0, 3.1) — Still work in progress!
- Prof. Borrelli - mpc papers and material
- AtsushiSakai - pythonrobotics
- alexliniger - mpcc and his paper
- arex18 - rocket-lander
- prl-mushr - mushr for the vehicle used






