# Design and Implementation of Model Predictive Control for a Gyroscopic Inverted Pendulum
## 3 Model Predictive Control  
MPC is an optimal control algorithm that takes account of system constraints such as its physical limitations. As shown in Figure 6, MPC uses a discrete time linear model to predict the system's future outputs  
\[\begin{array}{c}\mathbf{x}(k+1)=\mathbf{A}_{d}\mathbf{x}(k)+\mathbf{B}_{d} \mathbf{\tau}(k),\\ \mathbf{y}(k)=\mathbf{C}_{d}\mathbf{x}(k),\end{array} \tag{24}\]where \(\mathbf{x}(k)\) is the state vector, \(\mathbf{y}(k)=\left[\begin{array}{cc}\theta(k)&\alpha_{1}(k)&\alpha_{2}(k)\\ \end{array}\right]^{T}\) is the output vector consisting of the system's roll angle and the flywheels' gimbal angle, and \(\boldsymbol{\tau}(k)=\left[\begin{array}{cc}\tau_{a1}(k)&\tau_{a2}(k)\\ \end{array}\right]^{T}\) is the vector of gimbal control torques at the \(k\)th step. The discrete state-space matrices, \(\mathbf{A}_{d}\), \(\mathbf{B}_{d}\), and \(\mathbf{C}_{d}\), are easily computed from the continuous linear model (18) using a discrete sampling time \(T_{s}\). The prediction for system states in m steps ahead are calculated by:  
\[\left[\begin{array}{c}\mathbf{y}(k  
In addition to controlling the roll angle \(\theta\) of a gyroscopic inverted pendulum, MPC also controls the gimbal angles \(\alpha_{1}\) and \(\alpha_{2}\) back to zero positions. This ensure that the flywheels generate larger effective torque, as shown in Equation (18). The control problem for this system involves two control inputs and three control outputs. The control performance and the robustness of MPC are affected by the parameters in Equation (27). Putting larger weights on the weighting matrix \(\mathbf{Q}\) causes the controller to adhere tightly to tracking errors (so the tracking errors are decreased), but it requires more gimbal torques from the electric motors. Increasing the value of the weighting matrix \(\mathbf{R}\) gives a higher priority to reducing the control efforts and results in looser tracking performance. The other parameters of MPC are the prediction horizon m and the control horizon n. By choosing a smaller value of m, the controller is more aggressive. However, as point out by [PERSON] et al. [PERSON] _et al._ (2013), a large prediction horizon means that further increases have only a minor effect on control performance.  
In the case whereby an external force acts on the system and its magnitude is measureable, the effect of this force on the system's dynamics is predicted by adding a term of measured disturbance into the prediction model. The linear prediction model (24) then becomes  
\[\begin{split}\mathbf{x}(k+1)=\mathbf{A}_{d}\mathbf{x}(k)+\mathbf{ B}_{d}\mathbf{\tau}(k)+\mathbf{B}_{v}\mathbf{v}(k),\\ \mathbf{y}(k)=\mathbf{C}_{d}\mathbf{x}(k),\end{split} \tag{30}\]  
where \(\mathbf{B}_{v}\) is the disturbance parameter matrix and \(\mathbf{v}(k)\) describes the measured disturbance.  
Figure 2 demonstrates the acting forces on the pendulum-like bicycle when it goes into a constant-radius turn. The centrifugal force affects the system's dynamics, so the predicted values for future outputs take this into account by using the prediction model in Equation (30). The control scheme for this case is shown in Figure 7, where the centrifugal force acting on the system's centre of mass is given in the form of a measured disturbance, \(\mathbf{v}(\mathbf{k})\).  
MPC contributes its advantages of multiple control inputs and multiple control outputs together with online optimization to control the system. However, the main drawback of MPC is its larger computation load for prediction and the online optimization process. Therefore, evaluating the performance of MPC in real-time is very important to guarantee the applicability of the control algorithm in a real-time system.