Skip to content

Light-weight-MPC/MPC-simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MPC-simulator: C++, Python, Webassembly

This is a lightweight software base for automating MPC simulations on Finite Step-Response Models. All data files, including the model description and controller definition, are defined using JSON format making the software easily integratable. Having defined the input files, one can simulate the controller from the command line, calling make.sh using CMake and GNU compiler. In order to visualize the simulation data, plotting functionality is implemented in Python. Additionally, in order to interface the software to web-applications, the software is compiled to wasm format calling emcc.sh using the Emscripten compiler.

Operating system: Linux, MacOS

Modules

  • data: System, scenario and simulation files.
  • MPC: Solving the MPC problems.
  • model: Generating a Finite Step-Response Model.
  • IO: Parsing and serializing respectively input and output data.
  • vis: Visualizing simulations in jupyter notebook

OsqpEigen is a folder holding the osqp-eigen software.

Dependancies:

This software is developed using a environment and package manager conda, builded using CMake and compiled using GCC.

Other C/C++ compilers needed: Linux:

sudo apt-get install g++
sudo apt-get install emscripten //For Webassembly

Other libraries used:

Run MPC-core:

From project root:

  • Use conda in order to create environment access the environments in conda folder:
conda env create -f env.yml

or make a new environment and install conda packages:

conda create --name new_environment
conda install -n env -c anaconda cmake
conda install -n env -c conda-forge osqp-eigen
conda install -n env -c conda-forge nlohmann_json
conda install -n env -c conda-forge cli11
conda env export > env/env.yml
  • Build and run program, NB! lightweight.sh calls binary ./mpc_simulator Arguments:
  • [-T int] mpc_horizon: Simulate scenario for the given mpc_horizon
  • [-s string] scenario_name: Scenario file to be simulated
  • [-r string] reference vector
  • [-n bool] new simulation
chmod +x lightweight.sh                       // Set execute permission
sh lightweight.sh -T mpc_horizon -s sce -r [ref] -n

Webassembly

Light-weight MPC uses the Emscripten compiler in order to compile the C++-code to Webassembly which can be reached from a website using JavaScript. In order to interface web, binding files are requred the implementation of the interface functionality can be found in the wasm folder.

Install the Webassembly compiler locally, online documentation:

git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
git pull
./emsdk install latest
./emsdk activate latest

Or for MacOS, download via Brew

brew install emscripten

Before building and running the compiler, the enscripten compiler needs to be sourced to terminal. Afterwards, emcc.sh can be run. The output, mpc_simulator.mjs is stored in the Front-End's src-folder. emcc.sh takes an argument [-d dest] with is the filepath to the webassembly file

source ./emsdk_env.sh
sh emcc.sh -d ../frontend/src

Visualization

MPC-core has an secondary code base providing plot functionality. This software is developed in Python, and can be installed using conda. The visualization tool is found in the folder named vis

conda install -n env -c anaconda jupyter
conda install -n env -c anaconda numpy
conda install -n env -c conda-forge matplotlib

One can choose to either open Jupyter-Notebook and run vis.ipynb. Alternatively, can the plots be produced in by a terminal command from root:

python3 vis/plot.py -s "Simulation"

Open loop simulation

When facing a new model to try to fit tuning parameters too. It might be useful to assess the open loop response. The simulator can produce an open loop simulation by calling another build script:

Arguments:

  • [-T int] mpc_horizon: Simulate scenario for the given mpc_horizon
  • [-s string] scenario_name: Scenario file to be simulated
  • [-r string] Actuation vector
  • [-a string] Step vector
chmod +x openloop.sh                       // Set execute permission
sh openloop.sh -T mpc_horizon -s sce -r [ref] -a [step]

In an open loop simulation the actuation is determined without the use of a controller. Here the actuation vector determines the maximum value the actuation will reach. The corresponding step vector determines how the actuation increases each simulation step. By passing an actuation and a step vector equal to 1 a step response is applied to the system.