The Analytical Design and Implementation of Inverted Pendulum Control System using Linear Quadratic Regulator

This paper presents the design process of an inverted pendulum control system using the LQR method. The system model is obtained by analyzing the free body diagram of the inverted pendulum system, then after obtaining the system model, proceed with creating a state space equation which is then used for the LQR control system where the energy used can be adjusted according to needs. 10 scenarios were used to obtain the desired design criteria. Determining the right scenario can be done using MATLAB simulation so that the system response can be seen by determining the correct Q matrix and R matrix coefficients. Through several tests it can be shown that to keep the pendulum stable it requires proper energy settings.


INTRODUCTION
The utilization of the inverted pendulum on a cart serves as a valuable platform for researchers to assess various control strategies within the realm of control systems, as highlighted by Banerjee and Pal (Banerjee & Pal, 2018) and Owais et al. (Owais et al., 2019).Contemporary technologies frequently leverage the inverted pendulum concept, exemplified by its use in controlling the orientation of a space booster during take-off to maintain a vertical position, as discussed by Ogata (Ogata, 2010), Vijayalakshmi et al. (Vijayalakshmi et al., 2013), and Strakoš & Tůma (Strakoš & Tůma, 2017).Due to its characteristics as a fast, multi-variable, and inherently unstable open-loop, high-order nonlinear system, controlling the inverted pendulum system requires robust measures for stability, as emphasized by Shuang and Jian (Shuang & Jian, 2014).Additionally, the IPBD robot developed by Fahmizal et al. in 2019 adopts the system model of an inverted pendulum, showcasing another application of this approach (Fahmizal et al., 2019).
Balancing an inverted pendulum primarily involves three approaches: a) applying torque at the pivot point, b) horizontally moving the cart, and c) rapidly oscillating the support up and down, as outlined by Parvathy and Daniel (Parvathy & Daniel, 2015).In this scenario, the system features a single input-the force applied to the cart-and two outputs-the position of the cart and the angle of the pendulum (Parvathy & Daniel, 2015).
Implementing classical control theory on complex systems poses challenges, whereas modern control theory is characterized by its ease of application, simplicity, and time efficiency compared to artificial intelligent control theory, as noted by Singh and Singla (Singh & Singla, 2017).An example of modern control theory is the Linear Quadratic Regulator (LQR) controller.Hence, in addressing the cart pendulum problem discussed in this article, the preference is given to the LQR Controller as a representative of modern control theory.
LQR stands out as an optimal control approach with successful applications across various systems and the primary challenge in designing an LQR controller lies in the selection of controller parameters, where the chosen values must minimize a performance index.And also a fundamental application of control theory involves the DC motor, and the LQR controller is recognized as a method for effectively regulating its speed and position (Erkol, 2018).
In this article we study in depth the influence of variations in the coefficients of the R matrix regarding the energy required to handle friction problems.We study the process of designing and implementing an LQR control system that begins with an inverted pendulum system analysis, preparation of vector matrix equations, LQR control design by determining the gain K which is done with MATLAB based on selecting a Q matrix and R matrix with a state matrix.Furthermore, the value of the gain K is simulated in MATLAB and implemented into a real-time system with the main controller Arduino Mega 2560.Two different scenarios but almost same , namely scenario 9 and scenario 10 have been selected to be tested on the real time system.The testing results show that the energy factor gives big effect in controlling the inverted pendulum system.This means that big energy can make the system to be responsive.The other important thing is viscous friction of the mechanical system build that contributes to bigger energy consume.So neglecting that parameter can make the system to have a problem.This article is organized into four sections.Next is the methodology section which consists of system modeling and analysis, discussion of the design of the Linear Quadratic Regulator (LQR) and it explains about simulation taken that using MATLAB which uses two different scenarios so that the response of the system to the initial condition can be seen.The second section includes the implementation of physical devices and control algorithm.In the third The Analytical Design and Implementation of Inverted Pendulum Control System using Linear Quadratic Regulator ELKOMIKA -627 section the results and discussion of test is carried out.Finally, the fourth section is the conclusion of this research.

System Modeling and Analysis
The cart inverted pendulum comprises a pendulum connected to a cart through a shaft, trailing along a horizontal track by means of a DC motor.The motor, controlled by the back-and-forth movement, governs the position and velocity of the cart within a limited rail length, as described by Borkar (Borkar, 2017).This inverted pendulum falls into the category of underactuated mechanical systems, meaning it cannot be directed to follow arbitrary trajectories.With fewer inputs than the total degree of freedom (DOF), it possesses two DOFs: one for the horizontal motion of the cart and another for the pendulum's angular motion in the vertical plane.However, only the cart position is actively manipulated, indirectly influencing or controlling the pendulum, as articulated by Valluru (Valluru, 2018).
To examine the inverted pendulum system on the cart, the initial stage involves inspecting the free-body diagram of the inverted pendulum system, as depicted in Figure 1 below.Establish the angle of the rod from the vertical line as θ.Additionally, designate the coordinates of the center of gravity of the pendulum rod as( ,  ).Consequently, The rotational movement of the pendulum rod around its center of gravity can be expressed as : where I represents the moment of inertia of the rod about its center of gravity, as indicated by (Ogata, 2010).The movement of the center of gravity of the pendulum rod in the horizontal direction is expressed as : The upward and downward movement of the center of gravity of the pendulum rod is described as : Putra, et al.

ELKOMIKA -628
The movement of the cart in the horizontal direction is delineated by (Ogata, 2010) : To maintain the vertical position of the inverted pendulum, it is reasonable to consider () and  ̇() as small values, leading to approximations such as sin  ≑ , cos  = 1,  ̇ = 0.
As the mass in this system is concentrated at the upper end of the rod, the center of gravity coincides with the center of the pendulum ball.In this scenario, the moment of inertia of the pendulum around its center of gravity is negligible, allowing us to assume I=0 in Equation (10).Consequently, the mathematical model for this system is simplified as follows : Equations ( 11) and ( 12) can be modified to From Equation ( 13) we obtain and from Equation ( 14) we obtain Define state variables  ,  ,  , and  by The Analytical Design and Implementation of Inverted Pendulum Control System using Linear Quadratic Regulator ELKOMIKA -629 Please observe that the angle  represents the rotation of the pendulum rod around point P, while  denotes the position of the cart.If we regard u and  as the system outputs, then Therefore, by applying the definition of the state variables along with Equations ( 13) and ( 14), we derive : Expressed in vector-matrix equations, we can formulate : Equations ( 17) and ( 18) provide a state-space representation of the inverted-pendulum system (Ogata, 2010).Using these numeric values, Equations ( 17) and ( 18) can be rewritten as Putra, et al.

ELKOMIKA -630
The controllability matrix's rank, denoted as  = [     ] is 4, indicating that the linear mathematical model of the cart pendulum system is fully controllable.

Linear Qudratic Regulator
The optimal control problem of the following system equations : is to ascertain the matrix K for the optimal control vector : To minimize the performance index J, it is expressed as : In the optimization process, the performance index  is minimized, as indicated by the equation, where  and  represent weight matrices. should be a positive definite or positive semi-definite symmetric matrix, and  must be a positive definite symmetric matrix.The weighting matrices are preferred to be diagonal matrices.The values of the elements in the weighting matrices are crucial as they influence the impact on the performance index J, as discussed by Banerjee and Pal (Banerjee & Pal, 2018).The second term on the right-hand side of Equation ( 22) assesses the energy of the control signal.The matrices  and  play a role in determining both the error and the energy expenditure, as emphasized by Ogata (Ogata, 2010) and Kuo (Kuo, 1975).As depicted in Equation ( 21), representing the optimal control law, it follows that determining the unknown elements of the K matrix to minimize the performance index results in the optimality of () = −() for the initial state (0) (Ogata, 2010). is expressed as :  There are certain limitations that are maintained in preparing this system model.These constraints entail that the cart should not travel a distance exceeding 0.17 meters from its initial position but this provision does not apply to the assembled hardware system because the maximum linear speed of a DC motor is around 13.6 cm per second assuming a pulley radius of 6.5 mm .So the maximum distance for cart movement is limited to below 13.6 cm.

Simulation
To be able to see the state of the system, we have to display it in graphical form.By using the MATLAB application and program, responses to initial conditions can be observed.Here we set The Analytical Design and Implementation of Inverted Pendulum Control System using Linear Quadratic Regulator ELKOMIKA -631  = 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 1 ,  = [1] and we set the initial condition of theta to 0.17.
With MATLAB lqr command the gain K=[ -17.8518 -3.7585 -1.0000 -1.8787] is obtained and plot it.The graphic display can be seen as in Figure 3 where in general the movement of this system is in accordance with the system model created but the movement of the cart (position) is still far from the specified maximum limit, namely 0.136 m (13.6 cm), therefore the correct Q matrix and R matrix coefficients must be selected.
The control signal display can be seen in Figure 5 where the signal is displayed with the help of Simulink with a configuration as in Figure 4 below.It can be seen from the picture that the u signal given by this system has a maximum value of 3 so the response is not fast and setting the R matrix is needed for additional energy or control signals.The following is a table of several scenarios that are carried out to meet system requirements.This data can be obtained by using the MATLAB lqr calculation program.A comparison graph of several scenarios was obtained as in Figure 6 below.From the picture it can be seen that in scenario 1, the cart movement is at a maximum of 30 cm, which is still far from system requirements.Next, scenario 2 changes the matrix coefficient  to 10, which results in a change in cart movement to a maximum of 21.9 cm, but this situation does not meet the system boundary requirements, so scenario 3 is carried out, namely changing  to 10, which results in cart movement to a maximum of 19.5 cm.This result still does not meet the requirements, so scenario 4 is carried out, namely changing  to 100 which results in a maximum cart movement of 16 cm.This situation still does not meet the system design criteria.
Next, changes to the coefficients were made in scenario 5 and scenario 6 but this had no effect on the position of the cart as shown in Figure 6.
Next treatment is scenario 7 to adjust the R matrix coefficient from 1 to 0.1 which results in the cart movement reaching the limit of 14.2 cm but the system criteria have not been achieved so scenario 8 is carried out where the R matrix coefficient from 0.1 is changed to 0.01 which causes the cart position to move to a maximum of 13.7 cm but still does not meet the system criteria.Next, scenario 9 is carried out which changes the matrix coefficient R from 0.01 to 0.001 which causes the movement of the cart to reach a maximum of 13.5 cm.This condition meets the requirements of the system criteria, but to provide more information, an additional scenario is added, namely to scenario10, where at this stage the R matrix coefficient is changed from 0.001 to 0.0001 which causes cart movement to reach a maximum of 13.4 cm which is safe enough situation to exceed the system criteria.
The Analytical Design and Implementation of Inverted Pendulum Control System using Linear Quadratic Regulator ELKOMIKA -633 The following is a comparison of simulation of the use of the control signal in scenario 9 and scenario 10 which is a critical condition of the system as can be seen in Figure 7.The control signal is obtained from Simulink by sending it to the MATLAB workspace as shown in the Simulink diagram above, then the data is compiled for analysis.Here the author displays the control signal as an indicator of energy use in the system.

Implementation
In a physical device, the motor's position and the rod's angle can be measured using an incremental rotary encoder, with velocities computed as derivatives, as mentioned by zjor (zjor, 2019).To stabilize the pendulum, a calculated force needs to be applied, as discussed in a previous section.However, the control signal generated by the Arduino corresponds to the width of the PWM signal, essentially representing the voltage applied to the DC motor, according to zjor (zjor, 2019).The block diagram of the system is shown in Figure 8 and the wiring diagram of the entire real-time system is illustrated in Figure 9(a) below.The actual inverted pendulum system employed in this study comprises a linear motion guide equipped with a bearing block at the top, as depicted in Figure 9(b).The hardware setup involves a cart moving along the linear motion guide axis.The pendulum is connected to an incremental rotary encoder via a coupling shaft.To stabilize the pendulum at the unstable equilibrium point, a 12-volt DC motor is utilized alongside a dual H bridge L298N (Owais et al., 2019).The following is a table of physical devices To implement the control algorithm we have developed an Arduino program from zjor (zjor, 2019) with some modifications due to physical specifications.To display the numerical values from the real-time system, we use ExcelDataStreamer so the parameter values can be analyzed.The following is the flowchart of Arduino program which consists of initialization, verify the state, measure and calculate all parameters, activation of LQR control, providing signals to the motor drive and looping.
The Analytical Design and Implementation of Inverted Pendulum Control System using Linear Quadratic Regulator ELKOMIKA -635 In the two scenarios above, graphical analysis has met the system requirements criteria but there are two tests carried out because the author wanted to know the similarities and differences between the two.The differences in parameters are: 1. Scenario 9 uses energy worth 250 and the movement of the cart is below 13.6 cm. 2. Scenario 10 uses energy worth 800 and the same movement is below 13.6 cm.
The testing consists of two tests where test 1 uses parameters in scenario 9 and test 2 uses parameters in scenario 10.In test 1 the pendulum is moved by hand through the right side (positive) because the pendulum angle designation (calibration) is positive, this is done to avoid calculation errors in the initial condition.The result is that physically, the control system can stabilize the pendulum but can not last long.More energy is needed to keep the pendulum stable.It is clear because we do not include viscous friction in calculation.The graph of the test result can be seen in Figure 11 below, the pendulum to fall in the 5th second after the pendulum was successfully erected as shown at blue color.
From the Table 4 it can be seen that theta show around zeros of radian from time 17:34.4 to 17.37.5, this means that pendulum is stable and then theta goes to -0.8071(rad) at 17.38.5 and goes to -1.0137(rad) at 17.39.1 but in this moment the cart is not responsive or less energy to balancing it so the pendulum is falling down.In contrast to the results of test 1, in test 2 the condition of the pendulum was still standing upright as seen in Figure 12(a) and theta was around zero radians as seen in Figure 12(b) which shows that the control system can work well.It is clear that more energy can keep the pendulum stable.

Disturbances Testing
Disturbance testing is carried out by applying an external torque to the pendulum which aims to disturb the balance of the pendulum.In the first test we found the pendulum remained stable even though external torque was added.It can be seen in Table 5 that when external torque is added to the right side at 40:03.0 the theta-dot changes to 0.1438 from a stable condition but the cart can balance the pendulum well.The graphical state can be shown in Figure 13(a) otherwise the cart cannot balance the pendulum as illustrated in Figure 13(b).In the second disturbance test the external torque was added to the pendulum in the left side as shown in Figure 13

ELKOMIKA -638
From the disturbance test, there is no difference in the direction of the right and left sides, but the speed of the cart is the main obstacle so that the pendulum can not balance if external torque is added.

CONCLUSION
The simulation indicates that the role of the Q and R matrices is to govern the stabilization process of the inverted pendulum.By increasing the coefficients of the diagonal Q matrix, the response conditions (theta, theta-dot, position and cart velocity) are reduced in this case if the coefficients of the diagonal matrix  has a value of 10 and  has a value of 100, so the movement of the cart is reduced to 16 cm.Meanwhile, by reducing the R matrix coefficient, in this case the value [0.0001], the energy usage becomes greater, up to a value of 800, which causes the cart velocity to increase.By combining the settings of the Q matrix and R matrix, a system that has a fast response will be obtained.the testing results it can be concluded that the friction parameter (viscous friction) which is not included in the calculation will have an effect on the physical system so that to overcome it requires greater energy, however the LQR control method can control the inverted pendulum well.To get the best response, the right DC motor speed and torque are needed.

Figure 1 .
Figure 1.Free-Body Diagram of the Inverted Pendulum System matrix  can be acquired by solving the algebraic Riccati equation, as outlined by Ogata (Ogata, 2010), Valluru et al. (Valluru et al., 2018) and Prakash et al. (Prakash et al., 2016) :  *  +  −   *  +  =  (24) The diagram illustrating the optimal configuration is presented in the Figure below.

Figure 3 .
Figure 3. Response to Initial Condition

F
Figure 5.Control Signal

Figure 6 .
Figure 6.Variation of Cart Position

Figure 9 .
Figure 8. Block Diagram of Inverted Pendulum Control System

Figure 12
Figure 12. (a)Physical Result of Test 2 (b) The State of Test 2 (b).

Figure 13
Figure 13.(a) Stable System (b) System to Fail

Table 1 .
System Parameters By replacing the specified parameter values for , ,  and, then

Table 2 .
Variation of Several Scenarios

Table 3 .
Physical Components

Table 4 .
Numerical Values of State of Test 1

Table 5
State of First Disturbance Test