This repository contains an implementation of a Nonlinear Model Predictive Control (NMPC) for a unicycle model using the OpEn (Optimization Engine) package.
The code is structured to allow easy configuration and simulation of the unicycle's trajectory tracking capabilities.
The project implements:
- Unicycle mobile robot model
where
- NMPC controller using OpEn optimization engine
- Reference trajectory generation (figure-8 pattern)
- Real-time simulation with configurable parameters
- Visualization of trajectory tracking performance
OpEn_unicycle/
├── build/ # Generated OpEn optimizers (auto-created)
├── config/
│ └── NMPC_config.yaml # MPC configuration parameters
├── images/ # Images for README
├── model/
│ ├── __init__.py
│ └── dynamics.py # Unicycle dynamics model
├── optimizer/
│ ├── __init__.py
│ ├── optimizer.py # OpEn optimizer setup
│ └── cost.py # Cost function definitions
├── trajectory/
│ ├── __init__.py
│ └── ref_traj_utils.py # Reference trajectory generation
├── main_NMPC.py # Main simulation script
├── requirements.txt # Python dependencies
└── README.md
This code requires the following dependencies:
- Rust (for OpEn package compilation)
- OpEn Python package (optimization engine)
- Python 3.8+ with packages listed in
requirements.txt
Please follow the instructions on the OpEn website to install OpEn properly.
- Clone the repository:
git clone https://github.com/Trigger-FK/OpEn_unicycle.git- Navigate to the project directory:
cd OpEn_unicycle- Install the required Python packages:
pip install -r requirements.txt- Build and run the NMPC optimizer:
python main_NMPC.py-
Interactive prompts:
- When prompted with
Do you want to build the optimizer? (y/n):- Enter
yto build the optimizer (required for first run or after config changes) - Enter
nto skip the build process and proceed directly to simulation
- Enter
- When prompted with
-
Simulation output:
- The simulation will run for 30 seconds (configurable)
- Real-time trajectory tracking performance is displayed
- Results are visualized in plots showing:
- State trajectories
$(x, y, \theta)$ vs reference - Control inputs
$(v, \omega)$ over time - 2D trajectory plot (x-y plane)
- State trajectories
The MPC controller can be configured by editing config/NMPC_config.yaml:
# MPC Params
state_dim: 3
input_dim: 2
sampling_time: 0.1
horizon_len: 10
umin: [0.0, -1.57]
umax: [0.6, 1.57]
Q: [20, 30, 2]
R: [0.1, 0.01]
QN: [40, 60, 4]- Sampling Time: Controls the MPC update frequency
- Horizon Length: Number of prediction steps (longer = better performance, slower computation)
- Weight Matrices:
Q: Penalizes state tracking errorsR: Penalizes control effortQN: Terminal cost weights (typically higher than Q)
After simulation, the results will be shown in plots:

- Rust not found: Ensure Rust is installed and in your PATH
- OpEn build fails: Check that all dependencies in
requirements.txtare installed - TCP connection errors: The optimizer automatically tries multiple ports (8333-8336)
- Slow performance: Reduce horizon length or increase sampling time in config
This project is licensed under the BSD 2-Clause License - see the LICENSE file for details.
Fumiya Matsuzaki - Trigger-FK
- OpEn (Optimization Engine) - Fast and accurate embedded optimization
- CasADi - Symbolic framework for automatic differentiation

