Skip to content

Examples of model predictive control using the CasADi C++ and Python APIs

License

Notifications You must be signed in to change notification settings

taskbjorn/mpc-playground

Repository files navigation

Overview

This repository holds a collection of examples of model predictive control using the CasADi C++ and Python APIs.

Examples

Differential drive robot

This example shows planning with a differential drive robot. The OCP is solved using single shooting.

Codes are available here:

Differential drive robot example trajectory animation

Differential drive robot example states plot

Differential drive robot example controls plot

Ackermann-steered robot

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:

Ackermann robot example trajectory animation

Ackermann robot example states plot

Ackermann robot example controls plot

Getting started

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.

Using the Python API examples

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

Using the C++ API examples

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.

Installing dependencies

Install IPOPT using APT:

sudo apt install -y coinor-libipopt-dev

Building CasADi from sources

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))

(Optional) Building additional HSL linear solvers from sources

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

(Optional) Building the Matplot++ library from sources

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 to Release
  • Set BUILD_EXAMPLES to OFF

Run the build and install processes:

sudo make install -j$((`nproc`+1))

About

Examples of model predictive control using the CasADi C++ and Python APIs

Topics

Resources

License

Stars

Watchers

Forks