Journal of Fuzzy Systems and Control, Vol. 3, No 2, 2025 |
An LQR-Based ANFIS Control for Double-Linked Inverted Pendulum on Cart
Truong-Phuong-Nam Pham 1, Trong-Bang Tran 2, Van-Dong-Hai Nguyen 3, Tai-Tue Nguyen 4, Gia-Thinh Nguyen 5,
Duy-Phat Nguyen 6, Dong-Khang Nguyen 7, Van-An Ha 8, The-Nam-Chau Trinh 9, Trung-Thang Nguyen 10,*
1,3,4,5,6,7,8,9,10 Ho Chi Minh City University of Technology and Education (HCMUTE), Ho Chi Minh City (HCMC), Vietnam
2 Department of Mechanical Engineering, Konkuk University, Seoul 05029, Republic of Korea
10 Cao Thang Technical College, Vietnam
Email: 1 22151119@student.hcmute.edu.vn, 2 bang11111@konkuk.ac.kr, 3 hainvd@hcmute.edu.vn,
4 21151490@student.hcmute.edu.vn, 5 20151004@student.hcmute.edu.vn, 6 21144346@student.hcmute.edu.vn,
7 21145161@student.hcmute.edu.vn, 8 21145624@student.hcmute.edu.vn, 9 21145082@student.hcmute.edu.vn,
10 nguyentrungthang@caothang.edu.vn
*Corresponding Author
Abstract—This paper presents a double-linked inverted pendulum on a cart system, which is highly nonlinear and inherently unstable. In the simulation, the state variable outputs are processed through three ENCODER blocks with a resolution of 1000 pulses, as we aim to develop a mathematical model that closely approximates real-world experiments. The objective of this study is to use an ANFIS controller to learn from data that closely resembles the actual system behavior under an LQR controller and apply it in a simulation environment to evaluate the stability and response of the system under both ANFIS and LQR controllers. The results show that the ANFIS controller provides better responses than the LQR controller.
Keywords—ANFIS Control; LQR Control; Intelligent Control; SIMO System; Double-Linked Inverted Pendulum
The inverted pendulum (IP), characterized by its under-actuated, non-linear, and unstable nature, has been extensively studied by numerous researchers. These studies aim to analyze the behavior and assess the performance of various novel and traditional control algorithms [1].
Many approaches have been effectively implemented to control the double-linked inverted pendulum. For example, PID controllers have been used to stabilize the system [2]-[4]. Other methods include the application of fuzzy controllers [5], as well as optimal control techniques utilizing LQR controller for controlling the double-linked inverted pendulum [6]-[9].
This research focuses on developing a standardized and systematized process for deriving dynamic equations by integrating advanced mathematics, classical physics, and robot motion analysis. Specifically, using data from John Gray's forward kinematics, we apply mathematical techniques to generate equations related to positions, angular velocities, and linear velocities. Based on these, the potential energy and kinetic energy equations are constructed efficiently. Finally, by employing the Euler-Lagrange equation, the dynamic equations are derived in a straightforward manner [10].
ANFIS (Adaptive Neuro-Fuzzy Inference System) is a hybrid intelligent system that combines fuzzy logic and artificial neural networks (ANNs) to enhance learning and inference capabilities. It was introduced to take advantage of both techniques, providing a powerful tool for modeling complex nonlinear systeam. By using a hybrid learning procedure, the proposed ANFIS can construct an input-output mapping based on both human knowledge (in the form of fuzzy if-then rules) and stipulated input-output data pairs [11]. ANFIS has been effectively applied in learning various control methods including PID, LQR, and nonlinear control [12]-[16]. It has been implemented on systems that are considered iconic in control algorithm research within the field of automation in particular and electrical-electronics engineering in general.
In this paper, the authors first develop a mathematical model of the system and then perform simulations using both a fuzzy controller and an LQR controller. These algorithms are later tested through simulations.
To begin with, we analyze system to build up the motion of the whole system. Basically, the overall analyzation is shown in the Fig. 1.
Experience in setting and assigning system parameters has been inherited from research documents on pendubot. Our previous experimental studies on this model, as presented in various publications, demonstrate that analyzing the system's dynamic equations using the Euler–Lagrange method provides high accuracy and closely approximates the real model. The parameters in Table 1 are measured and identified from our actual model.
Parameters | Description | Values |
| 0.491 |
|
| 0.172 |
|
| 0.055 |
|
| 0 |
|
| 0.0033 |
|
| 0.0017 |
|
| 0.1265 |
|
| 0.154 |
|
| 9.81 |
|
However, this method presents a significant challenge as it has not been systematically structured to establish relationships between state variables when calculating kinetic and potential energy. These relationships are often determined through geometric methods, which can be difficult to apply to higher-order systems. Our proposed solution is to utilize the forward kinematic computation method from robotics, based on the algebraic approach introduced by John J. Craig [10].
The coordinate system is defined as shown in Fig. 2, where we introduce additional coordinate frames c2 and c3 to determine the center of mass of the pendulums.
From the axis system in Fig. 2, the DH table is filled in Table 2. In there, the condition parameters are
,
and
; the rests are constant parameters.
i |
|
|
|
|
1 | 0 |
| 0 | 0 |
2 |
| 0 | 0 |
|
3 |
| 0 | 0 |
|
4 |
| 0 | 0 | 0 |
This table is constructed following the Denavit–Hartenberg (DH) convention as introduced in [10]. In Table 2, the DH parameters are chosen based on the configuration of the double-linked pendulum system.
Next stage, T identity matrix is being derived in the equation (1) base on the formula in [10].
| (1) |
With parameters from DH table, T matrixes are written as:
| (2) |
| (3) |
| (4) |
| (5) |
Coordinates of the centers of gravity of the joints with reference to the previous joint.
| (6) |
| (7) |
| (8) |
To determine the coordinates of each joint, we multiply in turn as follows:
| (9) |
| (10) |
| (11) |
To find the system kinetic energy, we need the linear velocity and angular velocity at the center of gravity of each pendulum. Regarding the linear velocity, we need to determine the center of gravity of each pendulum and the cart through the equations (12), (13) and (14).
| (12) |
| (13) |
| (14) |
After that, linear velocity is derivative of coordinates with respect to time.
| (15) |
| (16) |
| (17) |
In terms of the angular velocity, base on the analyzation from document [10] the vector x of each pendulum can be determined through the matrix below.
| (18) |
| (19) |
| (20) |
Angular velocity is determined by the derivative of vector x matrix with respect to time.
| (21) |
| (22) |
| (23) |
This equation consists of two components
(kinetic energy) and
(potential energy). Theoretically, it is presented in the format.
| (24) |
Regard to the kinetic energy, it is total kinetic energy of cart and 2 poles.
| (25) |
| (26) |
| (27) |
| (28) |
Similarly with potential energy, the total potential energy is shown below.
| (29) |
| (30) |
| (31) |
| (32) |
From Euler-Lagrange equation (24):
| (33) |
| (34) |
| (35) |
The general form of the state equation, when systematized, is as follows:
| (36) |
The acceleration equation is rewritten from the state equation as follows:
| (37) |
Where:
| (38) |
With:
| (39) |
| (40) |
| (41) |
Thence, we obtain:
| (42) |
Linearizing the system, we obtain:
| (43) |
| (44) |
| (45) |
From the model, we can calculate matrix A, B
| (46) |
| (47) |
The system is discretized using a sampling period of 5 milliseconds.
| (48) |
The inverted pendulum on a cart system is described by the linear state-space equation as follows:
| (49) |
With:
: 

: 
In which:
Applied force on cart
Torque acting on the first pendulum
Torque acting on the second pendulum
Objective is to determine the input vector ‘u’, so as to
minimize the performance index:
| (50) |
In which:
Q is a positive-semi definite and R is a positive definite matrix. Here the elements of the matrix u are determined, so as to minimize the performance index. Then
is optimal for any initial state x(0). Where P is the solution of Riccati equation, K is the linear optimal feedback matrix. Now we only need to solve the Riccati equation [12].
| (51) |
Where Q and R are determined by the genetic algorithm Q=diag([0.422465 53.3543 2.077422 1.3516 4.709233 43.886800]), R= 0.842308.
Therefore,
| (52) |
The gain matrix 𝐾 is computed using the discrete-time LQR method via MATLAB’s function:
| (53) |
The computed state feedback gain matrix K is K1= 0.6162; K2= 7.4389; K3= -51.6704; K4= -23.9153; K5= -211.4428; K6= -38.9656.
Fig. 3 illustrates the block diagram of the LQR controller used in this study. In the diagram, x is the state vector, u is the control input, and y is the output vector. The feedback gain matrix K is designed to minimize a quadratic performance index as defined in Equation (49). This structure follows the standard LQR formulation, as described in [12].
First, we collect the input and output data of the system
when using the LQR controller. We gather 2001 samples of input data and state variables with a sampling time of 0.05s over a duration of 10 seconds. The initial conditions are set to
. After collecting the data, we load it into anfisedit toolbox available in MATLAB, as shown in Fig. 4.
The FIS file using three Trimf membership functions for each input and constant output is generated in anfisedit. Finally, we train the FIS file with hybrid learning, setting the error tolerance to zero and the number of epochs to 29. The training error converges and no longer changes at the threshold of 0.0038119 as shown as Fig. 5.
The final ANFIS network structure is shown in Fig. 6. The control rule surface view of ANFIS controller is shown in Fig. 7.
The ANFIS control structure is designed and shown in Fig. 8. This figure illustrates the ANFIS control structure, which combines fuzzy logic and neural networks to control the double-linked inverted pendulum system. This structure takes input data, processes it through fuzzy rules, and generates a control signal to stabilize the system.
System overview:
An overview of the blocks representing the system in the simulation environment is presented in Fig. 9.
Cart’s Encoder:
The encoder block simulates a real encoder with a resolution of 1000 pulses. The cart’s position output is fed into the encoder block and converted into meters for rounding calculations to minimize errors. It is then converted back into pulses and output, shown in Fig. 10.
Encoder of the first and second pendulum:
The encoder block simulates a real encoder with a resolution of 1000 pulses. The output of the angular deviation of the two pendulums is fed into the encoder block and converted into meters for rounding calculations to minimize errors. It is then converted back into pulses and output, shown in Fig. 11.
The LQR control system structure is shown in Fig. 12.
The ANFIS control system structure is shown in Fig. 13.
The response results of the double-linked inverted pendulum on cart system under the ANFIS and LQR controllers are shown in the four figures, corresponding to the cart position, the deviation angle of the first pendulum, the deviation angle of the second pendulum, and the applied force on the cart.
As mentioned above, we aim for a simulation environment that closely resembles real-world conditions. Therefore, the system's output has been connected to simulation blocks representing an ENCODER with a resolution of 1000 pulses. As a result, the system's response will exhibit oscillations around the equilibrium position.
Fig. 14 illustrates the cart's position response. The red line, representing the cart position using the LQR controller, oscillates within the range of -0.08 meters to 0.16 meters. This oscillation amplitude is not excessively large and remains within an acceptable range. In contrast, the black line, which represents the response of the ANFIS controller, shows a smaller oscillation amplitude compared to LQR and appears more stable. This indicates that ANFIS has learned from LQR's operation and even provides a better response than LQR.
Fig. 15 illustrates the deviation angle of the first pendulum. The red line represents the angular displacement when utilizing the LQR controller, maintaining stability within an oscillation range of -0.06 radians to 0.06 radians (equivalent to ±3.43 degrees). This oscillation amplitude is well within acceptable limits for practical implementation. The black line depicts the response of the ANFIS controller, exhibiting a comparable amplitude to the LQR-based system. This indicates that ANFIS has effectively learned and replicated the dynamic behavior of the LQR controller.
Fig. 16 illustrates the angular deviation of the second pendulum. The red line represents angular displacement when using the LQR controller, maintaining stability within an oscillation range of -0.03 radians to 0.03 radians (equivalent to ±1.72 degrees). This oscillation amplitude is relatively small compared to the first pendulum, indicating improved damping characteristics. The black line represents the response of the ANFIS controller, which exhibits a similar oscillation range to LQR but with slightly higher oscillatory energy. This suggests that ANFIS has effectively learned the control strategy of LQR, though minor residual oscillations remain.
Fig. 17 illustrates the control force applied to the cart. The force generated by the LQR controller oscillates within a range of ±10N, which leads to significant cart oscillations due to strong actuation. However, the ANFIS controller has effectively learned the control behavior of LQR and has even outperformed it by reducing the applied force, resulting in less oscillation of the cart. This is a positive outcome for the ANFIS controller, as it contributes to reducing control energy consumption while maintaining system stability.
After observing the system's response, we evaluated the controller's computation time over approximately 1200 cycles for the system described in Fig. 18. The evaluation results were highly positive, indicating that the controller can operate effectively under real-time conditions, with its computation time being consistently faster than the sampling period (0.01 s). This serves as a foundation for further implementation on the physical model.
In this study, we propose using MATLAB's ANFIS toolbox to learn from a controller that has been successfully simulated. A research paper on the mathematical modeling of this system and the implementation of LQR is being written simultaneously. Building on these successes, we have successfully applied an intelligent ANFIS controller in a simulation environment for a highly nonlinear system such as the double inverted pendulum on a cart. Through this research, we found that ANFIS has effectively learned the operation of LQR, making it a highly attractive option in fields such as mobile robotics and other control system applications. Our next objective is to apply this ANFIS controller to a real-time system, and the results of this study will serve as a valuable reference for future research and development.
This paper belongs to project SV2025-95 funded by HCMUTE. We, authors, want to give thanks to that support.
Nguyen Xuan Chiem, Design of Embedded Control System with Fuzzy Controller and Nonlinear Controller for the Line Follower Robot