This project implements a Model Predictive Control (MPC) for robot using Acados in C++.
- Acados
- CMake
- Eigen
- Matplotlib C++ (for plotting)
To build the project, follow these steps:
-
Clone the repository:
git clone https://github.com/SokhengDin/NMPC-ACADOS-CPP.git cd NMPC-ACADOS-CPP
-
Create a build directory and navigate to it:
mkdir build cd build
-
Run CMake to configure the project:
cmake ..
-
Build the project:
make
After building the project, you can run the solver using the following command:
./build/differential_drive_solver
The MPC cost function in this implementation is formulated as a nonlinear least-squares problem. The cost function penalizes the deviation of the predicted states and controls from their reference values over a prediction horizon.
Let
The cost function is defined as:
where:
-
$N$ is the prediction horizon length -
$W$ is the weight matrix for the stage cost, defined as a block-diagonal matrix:
-
$Q \in \mathbb{R}^{n_x \times n_x}$ is the weight matrix for the state deviation -
$R \in \mathbb{R}^{n_u \times n_u}$ is the weight matrix for the control input deviation -
$R_{rate} \in \mathbb{R}^{n_u \times n_u}$ is the weight matrix for the control input rate deviation -
$W_e$ is the weight matrix for the terminal cost, which penalizes the deviation of the final state from the reference state
The cost function minimizes the weighted sum of the squared deviations of the predicted states and controls from their reference values, as well as the control input rate deviations. The terminal cost ensures that the final state reaches the desired reference state.
The MPC problem is solved using the Sequential Quadratic Programming (SQP) method, which iteratively solves a sequence of quadratic programming (QP) subproblems to find the optimal control inputs.
At each iteration
where:
-
$d_x$ and$d_u$ are the search directions for the states and controls, respectively -
$H^{(i)}$ is the Hessian matrix of the Lagrangian function at iteration$i$ -
$\nabla J(x^{(i)}, u^{(i)})$ is the gradient of the cost function at iteration$i$ -
$f(x_k, u_k)$ represents the nonlinear system dynamics -
$\nabla f(x_k^{(i)}, u_k^{(i)})$ is the Jacobian of the system dynamics at iteration$i$ -
$\bar{x}_0$ is the initial state -
$\mathcal{X}$ and$\mathcal{U}$ are the feasible sets for the states and controls, respectively
The QP subproblem is solved to obtain the search directions $(d_x^, d_u^)$, which are used to update the current estimate of the optimal solution:
where
The SQP iterations continue until a convergence criterion is met, such as the norm of the search directions falling below a specified tolerance or reaching a maximum number of iterations.
The ACADOS solver used in this implementation employs a real-time iteration (RTI) variant of the SQP method, which allows for efficient and fast solution of the MPC problem in real-time applications.
The Runge-Kutta method is used to approximate the state evolution of the system given the current state and control input. In this implementation, the fourth-order Runge-Kutta method (RK4) is employed.
Given the current state
-
$f(x_k, u_k)$ represents the system dynamics function -
$h$ is the time step size -
$k_1$ ,$k_2$ ,$k_3$ , and$k_4$ are the intermediate stages of the RK4 method
The RK4 method provides a higher-order approximation of the state evolution compared to simpler methods like Euler's method. It takes into account the slope of the system dynamics at multiple points within the time step, resulting in a more accurate approximation.
By using the Runge-Kutta method, the MPC algorithm can predict the future states of the system more accurately, leading to improved control performance.