This repository holds a collection of examples of model predictive control using the CasADi C++ and Python APIs.
This example shows planning with a differential drive robot. The OCP is solved using single shooting.
Codes are available here:
This example shows an Ackermann-steered robot using a 3 DOF vehicle model and a linear tire. The OCP is solved using multiple shooting.
Codes are available here:
Clone the repository to a folder of your choice:
git clone https://github.com/taskbjorn/mpc-playground ~/mpc-playground
Then, follow the instructions for the API of your choice.
To use the CasADi Python API, you will need a working installation of Python 3 with package manager Pip. CasADi has a package on PyPI including all required dependencies.
You may install the package using the provided requirements.txt
file:
pip install -r requirements.txt
You may then run any example by issuing from the root of the repository:
python src/{example-filename}.py
To use the CasADi C++ API, you may be required to build CasADi from sources. Detailed and up-to-date instructions are available here, below we provide minimal steps to get a working installation under Ubuntu 22.04 with IPOPT and HSL linear solvers.
Install IPOPT using APT:
sudo apt install -y coinor-libipopt-dev
Clone the CasADi repository in a new location of your choice (the home directory for the current user ~/
in these instructions)
git clone https://github.com/casadi/casadi.git ~/casadi
mkdir ~/casadi/build
cd ~/casadi/build
cmake -DWITH_IPTOPT=ON ..
sudo make install -j$((`nproc`+1))
Some examples make use of faster sparse linear solvers such as MA27. In order to use these solvers, they must be built from sources.
Clone the coin-or-tool/ThirdParty-HSL in a new location of your choice (the home directory for the current user ~/
in these instructions):
git clone https://github.com/coin-or-tools/ThirdParty-HSL ~/ThirdParty-HSL
Obtain a personal or academic license for the HSL solvers at the this link. Unpack the source code archive and move the contents under the ThirdParty-HSL repository folder:
tar -xvzf coinhsl-x-y-z.tar.gz --directory ~/ThirdParty-HSL
Build the ThirdParty-HSL library:
cd ~/ThirdParty
./configure
sudo make install -j$((`nproc`+1))
CasADi looks for the HSL solvers under a legacy name. You may be required to create a symbolic link for the HSL library:
cd /usr/lib/local
ln -S libcoinhsl.so libhsl.so
To generate plots at the end of the execution of the C++ examples, the Matplot++ library is required. Below are instructions to build the Matplot++ library from sources on Ubuntu 22.04.
Install the required dependencies:
sudo apt install -y cmake-curses-gui libfftw3-dev
Clone the alandefreitas/matplotplusplus in a folder of your choice (the home directory for the current user ~/
in these instructions):
git clone https://github.com/alandefreitas/matplotplusplus ~/matplotplusplus
Create a build folder and configure the build using CMake:
mkdir -p ~/matplotplusplus/build/
cd ~/matplotplusplus/build/
ccmake ../
Press C
to configure your project. After configuring the project:
- Set
CMAKE_BUILD_TYPE
toRelease
- Set
BUILD_EXAMPLES
toOFF
Run the build and install processes:
sudo make install -j$((`nproc`+1))