## 5 Control System Module
### The NMPC Algorithm  
The model predictive control (MPC) is considered to be an optimal control algorithm that uses a model of the plant to find a series of optimal control signals by minimizing a cost function. A plant model is used at each sampling iteration in order to predict the future behavior of the system during the prediction horizon. Taking into account the  
Figure 3: Rear axle bicycle model. predictions, an objective function can be minimized with respect to the future sequence of inputs. A quadratic function of states and control inputs can be used to define the cost function in (44) as:  
\[ \begin{split}\mathrm{J}(k)=\sum_{i=1}^{H_{p}}(\mathrm{X}^{\mathrm{ T}}(k+\mathrm{i}\mid k)\mathrm{Q}\mathrm{X}(k+\mathrm{i}\mid k))+\\ (\mathrm{U}^{\mathrm{T}}(k+\mathrm{i}\mid k)\mathrm{RU}(k+\mathrm{i} \mid k))\end{split}  \tag{44}\]  
where the value of \(\mathrm{x}\) at the time instant (m) is predicted at the time instant (n), the relation is shown with \(\mathrm{x}(\mathrm{m}\mid\mathrm{n})\). Vectors of system states and control inputs are defined as \(\mathrm{X}\) and \(\mathrm{U}\) respectively. Weighting matrices (Q and R) are used to penalize the state's error and control effort, respectively. The prediction horizon is represented as \(H_{p}\), which is an important factor in defining prediction horizon duration time (T). The prediction horizon duration can be formulated as follows:  
\[\mathrm{T}=H_{p}*\mathrm{dt} \tag{45}\]  
The second part in the aforementioned cost function (44) minimizes the control effort. The optimization problem is defined such that it can find a proper series of control inputs and states as follows:  
\[\mathrm{X},\mathrm{U}=\mathrm{argmin}\{\left|J(k)\right|\} \tag{46}\]  
\[\mathrm{X}(k\mid k)=\mathrm{X}_{0} \tag{47}\]  
\[\mathrm{X}(k+\mathrm{i}+1\mid k)=fd(\mathrm{X}(k+\mathrm{i}\mid k),\mathrm{U} (k+\mathrm{i}\mid k) \tag{48}\]  
where the receptive value of states is represented by \(\mathrm{X}_{0}\). It corresponds to the numeric value of states that is measured at current time instant. Prediction model in optimization is defined in (48). In addition, there is a possibility to impose some bounds, defined in (49) and (50), on the magnitude of control variables and states as follows:  
\[\mathrm{X}_{\mathrm{min}}\leq\mathrm{X}(k+\mathrm{i}\mid k)\leq\mathrm{X}_{ \mathrm{max}}\qquad\qquad\mathrm{i}\in\left[0,H_{p}\right] \tag{49}\]  
\[\mathrm{U}_{\mathrm{min}}\leq\mathrm{U}(k+\mathrm{i}\mid k)\leq\mathrm{U}_{ \mathrm{max}}\qquad\qquad\mathrm{i}\in\left[0,H_{p}-1\right] \tag{50}\]  
It has been proven that only some initial control predictions are effective in stabilizing the system. Hence, in the majority of cases, another parameter called the control horizon (\(H_{\mathrm{c}}\)) is defined, which is the optimized number of control moves at each control interval. It falls between one and the prediction horizon. The final goal of optimizing the objective function is to reduce the error while states are approaching the desired point. Therefore, (51) substitutes for (44) as follows:  
\[ \begin{split}\mathrm{J}(k)=\sum_{i=1}^{H_{p}}\parallel\mathrm{X} (k+\mathrm{i}\mid k)-\mathrm{r}(k+\mathrm{i})\parallel_{Q_{r}}^{2}+\sum_{i=1}^ {H_{r}-1}\parallel\mathrm{U}(k+\mathrm{i})\mid k\parallel_{Q_{u}}^{2}+\\ \sum_{i=2}^{H_{r}-1}\parallel\mathrm{U}(k+\mathrm{i}-1)-\mathrm{U} (k+\mathrm{i}-2)\parallel_{Q_{nt}}^{2}\end{split}  \tag{51}\]  
where the predicted states vector is represented with \(\mathrm{X}(k+\mathrm{i}\mid k)\). Here, \(\mathrm{r}(k+\mathrm{i})\) is the desired set-point vector. In addition, some weighting matrices (\(Q_{e}\), \(Q_{u}\), and \(Q_{nt}\)) are employed to reduce state tracking error, control effort, and rate of change in control signal, respectively. In the modified cost function, the third summation reduces stress on actuators by limiting the rate of change.