# Design and Implementation of Model Predictive Control for a Gyroscopic Inverted Pendulum
## 2 Dynamic System Modeling
### Model Verification  
The parameters for the inverted pendulum in Table 1 are derived from the 3D design of the system with selected hardware. By substituting these parameters into Equation (17), a numerical model that simulates the system's behaviours is obtained in Matlab/Simulink (MathWorks, Inc., Natick, MA, USA) by using an S-function block. To verify the model of the system's dynamics, the responses of the mathematical model are compared with the responses of a computer-aided engineering (CAE) model that is constructed with the commercial software package ADAMS (MSC Software Corporation, Newport Beach, CA, USA). An inverted pendulum is an unstable system that falls if no control actions are taken. Therefore, it is difficult to make a comparison based on the uncontrolled responses of this system. Preventing the inverted pendulum from falling can thus generate enough data for comparisons. Two dynamic models are controlled to balance around the upright position by using the same controllers. A simple control algorithm such as PID can be used to generate the closed loop responses. The PID control scheme for the system is shown in Figure 4. Three PID controllers are used to control the inverted pendulum. The PID controller in the outer loop minimizes the roll angle \(\theta\) and generates reference gimbal angles for the inner loop. Since the two flywheels spin at a constant speed in opposite directions, the gimbal angles \(\alpha_{1}\) and \(\alpha_{2}\) are controlled in different directions to allow a summation of the effective torque on the horizontal axis:  
\[T_{H}=-(\dot{\alpha}_{1}\dot{\mathbf{f}}_{1}\cos\alpha_{1}+\dot{\alpha}_{2} \mathbf{I}\dot{\phi}_{2}\cos\alpha_{2}). \tag{18}\]  
The main task of the two PID controllers in the inner loops is controlling the gimbal frames to follow the reference signals. When the gimbal angles change at rate of \(\dot{\alpha}_{1}\) and \(\dot{\alpha}_{2}\), the gyroscopic effects generate torques at the pivot of the inverted pendulum that can stabilize the system, as shown in \begin{table}
\begin{tabular}{c c c c} \hline \hline
**Parameter** & **Symbol** & **Value** & **Unit** \\ \hline Mass of the inverted pendulum & \(m_{b}\) & 21.4508 & kg \\ \hline Mass of the gimbal frame & \(m_{g1}\), \(m_{g2}\) & 1.9143 & kg \\ \hline Mass of the flywheel & \(m_{f1}\), \(m_{f2}\) & 1.9063 & kg \\ \hline Mass moment of inertia for the inverted pendulum mass & \(\mathbf{I}_{g1}\), \(\mathbf{I}_{g2}=\left[\begin{array}{cc}I_{hxx}\\ I_{hyx}\\ I_{hyx}\\ I_{yz}\\ I_{yzx}\\ I_{yzy}\\ \end{array}\right]\) & \(\left[\begin{array}{cc}0.9182\\ -0.0003&0.9971\\ 0.0016&0&0.3081\end{array}\right]\) & kgm\({}^{2}\) \\ \hline Mass moment of inertia for the inverted pendulum mass & \(\mathbf{I}_{g1}\), \(\mathbf{I}_{g2}=\left[\begin{array}{cc}I_{gxx}\\ I_{gxx}\\ I_{gxy}\\ \end{array}\right]\) & \(\left[\begin{array}{cc}0.0089\\ 0&0.0067\\ 0&0.0027\end{array}\right]\) & kgm\({}^{2}\) \\ \hline Mass moment of inertia for the inverted pendulum mass & \(\mathbf{I}_{f1}\), \(\mathbf{I}_{f2}=\left[\begin{array}{cc}I_{fxx}\\ I_{fxx}\\ I_{yzx}\\ I_{yzx}\\ \end{array}\right]\) & \(\left[\begin{array}{cc}0.0049\\ 0&0.0049\\ 0&0.0093\end{array}\right]\) & kgm\({}^{2}\) \\ \hline Gravitational acceleration & \(g\) & 9.806 & m/s\({}^{2}\) \\ \hline Lower and upper limits of the gimbal torques & \(\boldsymbol{\tau}_{\min},\boldsymbol{\tau}_{\max}\) & \(-4.0\), \(4.0\) & Nm \\ \hline \hline \end{tabular}
\end{table} 
Table 1: Parameters of the system. Equation (18). After the inverted pendulum is stabilized in the upright position, PID2 and PID3 bring the gimbal angle back to its zero position and waiting for the next action. In this simulation, the flywheels spin at a constant speed of 4000 rpm which is controlled independently using speed controllers, and the inverted pendulum is given an initial angle of \(5^{\circ}\). The parameters for the outer loop PID are \(K_{P1}=3\), \(K_{I1}=5.65\), and \(K_{D1}=0.02\).