2020-04-07 23:45:11 +08:00
# mpc_python
2020-10-30 01:40:16 +08:00
Python implementation of a mpc controller for path tracking using ** [CVXPY ](https://www.cvxpy.org/ )**.
2020-04-07 23:45:11 +08:00
## About
2020-10-30 01:40:16 +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-04-07 23:45:11 +08:00
2020-10-30 01:40:16 +08:00

2020-04-07 23:45:11 +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** .
Where R,P,Q are the cost matrices used to tune the response.
The vehicle model is described by the bicycle kinematics model using the state space matrices A and B:
2020-04-07 23:45:11 +08:00
2020-04-08 00:09:13 +08:00

2020-04-07 23:45:11 +08:00
2020-10-30 01:40:16 +08:00
The state variables ** (x)** of the model are:
2020-04-07 23:45:11 +08:00
2020-04-08 00:09:13 +08:00
* **x** coordinate of the robot
* **y** coordinate of the robot
2020-10-30 01:40:16 +08:00
* **v** velocuty of the robot
2020-04-08 00:09:13 +08:00
* **theta** heading of the robot
2020-04-07 23:45:11 +08:00
2020-10-30 01:40:16 +08:00
The inputs ** (u)** of the model are:
2020-04-07 23:45:11 +08:00
2020-10-30 01:40:16 +08:00
* **a** linear acceleration of the robot
* **delta** steering angle of the robot
2020-04-07 23:45:11 +08:00
## Demo
2020-10-30 01:40:16 +08:00
The MPC implementation is tested using ** [bullet ](https://pybullet.org/wordpress/ )** physics simulator. Racing car model is from: *https://github.com/erwincoumans/pybullet_robots* .

Results:
2020-04-07 23:45:11 +08:00

2020-10-30 01:40:16 +08:00
To run the pybullet demo:
2020-04-07 23:45:11 +08:00
```bash
2020-10-30 01:40:16 +08:00
python3 mpc_demo/mpc_demo_pybullet.py
```
To run the simulation-less demo:
```bash
python3 mpc_demo/mpc_demo_pybullet.py
2020-04-07 23:45:11 +08:00
```
## Requirements
```bash
pip3 install --user --requirement requirements.txt
```
2020-10-30 01:40:16 +08:00
## References
* [mpc paper ](https://borrelli.me.berkeley.edu/pdfpub/IV_KinematicMPC_jason.pdf )
* [pythonrobotics ](https://github.com/AtsushiSakai/PythonRobotics/ )
* [pybullet ](https://pybullet.org/wordpress/ )