Giter Site home page Giter Site logo

light-weight-mpc / mpc-simulator Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 745 KB

C++ Model Predictive Control simulator on Finite Step-Response Models

License: BSD 3-Clause "New" or "Revised" License

CMake 1.13% Shell 1.48% C++ 84.00% C 4.59% Python 7.52% Jupyter Notebook 1.28%
convex-optimization eigen3 model-predictive-control mpc-control osqp-eigen quadratic-programming conda gnu cmake webassembly

mpc-simulator's Introduction

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.

mpc-simulator's People

Contributors

gotvinne avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.