2019-11-27 20:30:11 +08:00
# mpc_python
2020-03-04 20:32:29 +08:00
2023-10-24 04:17:38 +08:00
I keep here my (old) notebooks on Model Predictive Control for path-following problems. Includes a Pybullet simulation to demo the controller.
2022-07-21 21:14:14 +08:00
This mainly uses ** [CVXPY ](https://www.cvxpy.org/ )** as a framework. This repo contains code from other projecs, check them out in the special thanks section.
2020-03-04 20:32:29 +08:00
2021-07-07 20:07:45 +08:00
## Contents
2021-04-14 22:17:16 +08:00
2022-07-21 21:14:14 +08:00
### Usage
2021-04-14 22:17:16 +08:00
2022-07-21 21:14:14 +08:00
* To run the pybullet demo:
2021-07-07 20:07:45 +08:00
```bash
2024-10-22 17:12:23 +08:00
python3 ./mpc_pybullet_demo/mpc_demo_pybullet.py
2021-07-07 20:07:45 +08:00
```
2022-07-21 21:14:14 +08:00
* To run the simulation-less demo (simpler demo that does not use pybullet, useful for debugging):
2021-07-07 20:07:45 +08:00
```bash
2024-10-22 17:12:23 +08:00
python3 ./mpc_pybullet_demo/mpc_demo_nosim.py
2021-07-07 20:07:45 +08:00
```
2022-07-21 21:14:14 +08:00
In both cases the script will promt the user for `enter` before starting the demo.
The settings for tuning the MPC controller are in the ** [mpc_config ](./mpc_pybullet_demo/mpcpy/mpc_config.py )** class.
2021-07-07 20:07:45 +08:00
### Jupyter Notebooks
1. State space model derivation -> analytical and numerical derivaion of the model
2. MPC -> implementation and testing of various tweaks/improvements
2022-07-21 21:14:14 +08:00
3. Obstacle Avoidance -> Using halfplane constrains to avaoid track collisions -> Sill **work in progress** !
2021-04-14 22:17:16 +08:00
<!-- nobody cares about this
2020-03-04 20:32:29 +08:00
## About
2020-07-06 23:54:22 +08:00
The MPC is a model predictive path following controller which does follow a predefined reference by solving an optimization problem. The resulting optimization problem is shown in the following equation:
2020-03-04 20:32:29 +08:00
2020-10-30 01:40:16 +08:00
![](img/quicklatex_equation.png)
2020-03-04 20:32:29 +08:00
2020-10-30 01:40:16 +08:00
The terns of the cost function are the sum of the **reference tracking error** , **heading effort** and **actuaction rate of change** .
2020-07-06 23:54:22 +08:00
2020-10-30 01:40:16 +08:00
Where R,P,Q are the cost matrices used to tune the response.
2020-07-06 23:54:22 +08:00
The vehicle model is described by the bicycle kinematics model using the state space matrices A and B:
2020-03-04 20:32:29 +08:00
2020-04-08 00:09:13 +08:00
![](img/quicklatex2.png)
2020-03-04 20:32:29 +08:00
2020-07-06 23:54:22 +08:00
The state variables ** (x)** of the model are:
2020-03-04 20:32:29 +08:00
2020-04-08 00:09:13 +08:00
* **x** coordinate of the robot
* **y** coordinate of the robot
2020-07-06 23:54:22 +08:00
* **v** velocuty of the robot
2020-04-08 00:09:13 +08:00
* **theta** heading of the robot
2020-03-04 20:32:29 +08:00
2020-07-06 23:54:22 +08:00
The inputs ** (u)** of the model are:
2020-03-04 20:32:29 +08:00
2020-07-06 23:54:22 +08:00
* **a** linear acceleration of the robot
* **delta** steering angle of the robot
2021-04-14 22:17:16 +08:00
-->
2020-03-04 20:32:29 +08:00
2022-07-21 21:14:14 +08:00
### Results
2020-03-04 20:32:29 +08:00
2021-04-14 22:17:16 +08:00
Racing car model is from: *https://github.com/erwincoumans/pybullet_robots* .
2020-04-08 18:43:38 +08:00
2020-07-06 23:54:22 +08:00
![](img/f10.png)
2020-04-08 18:43:38 +08:00
Results:
2020-11-02 22:44:31 +08:00
![](img/demo_bullet.gif)
2020-03-04 20:32:29 +08:00
![](img/demo.gif)
2020-04-08 18:43:38 +08:00
2022-07-21 21:14:14 +08:00
### Requirements
2020-03-04 20:32:29 +08:00
2022-07-21 21:14:14 +08:00
The environment can be repoduced via [conda ](https://www.anaconda.com/products/distribution ):
2020-03-04 20:32:29 +08:00
```bash
2022-07-21 21:14:14 +08:00
conda env create -f env.yml
conda activate simulation
2020-03-04 20:32:29 +08:00
```
2022-07-21 21:14:14 +08:00
The dependencies for just the python scripts can also be installed using `pip` :
2020-03-04 20:32:29 +08:00
```bash
pip3 install --user --requirement requirements.txt
```
2020-10-30 01:40:16 +08:00
2021-07-07 20:07:45 +08:00
## References & Special Thanks :star: :
2021-04-14 22:17:16 +08:00
* [Prof. Borrelli - mpc papers and material ](https://borrelli.me.berkeley.edu/pdfpub/IV_KinematicMPC_jason.pdf )
* [AtsushiSakai - pythonrobotics ](https://github.com/AtsushiSakai/PythonRobotics/ )
* [erwincoumans - pybullet ](https://pybullet.org/wordpress/ )
* [alexliniger - mpcc ](https://github.com/alexliniger/MPCC ) and his [paper ](https://onlinelibrary.wiley.com/doi/abs/10.1002/oca.2123 )
* [arex18 - rocket-lander ](https://github.com/arex18/rocket-lander )