# Design and Implementation of Model Predictive Control for a Gyroscopic Inverted Pendulum
## 4 Control Results and Discussion
### Simulation Results
#### 4.2.1 Straight Running and Disturbance Rejection  
The first case study uses the inverted pendulum system to evaluate the control performance for returning the vehicle back to upright position from an initial angle and an external disturbance. Initially, the inverted pendulum is tilted at an angle of \(10^{\circ}\) and the flywheels spin at a speed of 4000 rpm. The maximum gimbal angle torque that can be generated is 4 Nm, as shown in Table 1.  
The control object in this case is to stabilize the inverted pendulum in upright position while maintaining minimum the precession angles \(\alpha_{1}\), \(\alpha_{2}\) to be as small as possible by using a sequence of optimized control toques. To meet this control goal, the ratio between the weighting matrices for tracking errors and the control inputs should be sufficiently large. In order to ensure small tracking errors, a larger ratio is chosen for the weighting matrix \(\mathbf{Q}\) to \(\mathbf{R}\) as  
\[\mathbf{Q}=\left[\begin{array}{ccc}700&0&0\\ 0&400&0\\ 0&0&400\end{array}\right],\,\mathbf{R}=\left[\begin{array}{ccc}1&0\\ 0&1\end{array}\right]. \tag{31}\]  
Further increases in this ratio can cause very aggressive control actions that reduce the control performance. The value of 700 is the weighting on error in \(\theta\), and the value of 400 is the weight on the error of the precession angles, \(\alpha_{1}\), \(\alpha_{2}\). By choosing the values 700 and 400, greater priority is given to minimizing the error in \(\theta\) than to reducing the gimbal angles. In other words, the MPC prioritizes minimizing the roll angle error first. When this error is relatively small, MPC reduces the gimbal angle errors, \(\alpha_{1}\) and \(\alpha_{2}\), back to zero. In this case, the prediction horizon, m, is chosen as 50. A smaller value of m results in larger steady-state errors and a larger value of m requires higher changing rate in the gimbal angles. Since the control horizon n is related to solving the optimization problem, increasing the control horizon might give better performance. However, the optimization problem then becomes more complicated and causes unfeasible control inputs. After tuning, the value n = 18 gives the best control performance.  
Figure 10 shows the results for the first case study where the initial condition of the system is:  
\[\left[\begin{array}{cccccc}\dot{\theta}(0)&\theta(0)&\dot{\alpha}_{1}(0)& \alpha_{1}(0)&\dot{\alpha}_{2}(0)&\alpha_{2}(0)\end{array}\right]^{T}=\left[ \begin{array}{cccccc}0&10^{\circ}&0&0&0&0\end{array}\right]^{T}, \tag{32}\]  
Here, the MPC initially generates maximum gimbal torques of 4 Nm to rotate the gimbal angles \(\alpha_{1}\) and \(\alpha_{2}\) in opposite directions at the largest changing rates of gimbal angles. By the gyroscopic effects, the control torque in Equation (18) is generated along the inverted pendulum pivot to bring it back to the upright position. When the inverted pendulum approaches to the upright position, the MPC slowly brings the gimbal angles back to zero positions to avoid any unwanted actions of the gyroscopic effect on the inverted pendulum. As shown in Figure 10, the inverted pendulum is stabilized after 2 s. To determine its ability to reject disturbances, an impulse disturbance is applied at 3 s. The applied disturbance is a lateral force of 300 N that acts at the centreof mass \(b\) of the inverted pendulum. When the impulse disturbance is applied, the gimbal angles change from 0 to \(38^{\circ}\) in order to generate the counteracting momentum and reject the disturbance. At 5.5 s, the pendulum returns to the balanced state and at 6 s, the gimbal angles are at the zero positions. As shown in Figure 10c, the control torques reach the limited values during the stabilization process. However, byincorporating these limitations as hard constraints (28) in the online optimization process, MPC allows good control actions with only small variations in the control torques.