0% found this document useful (0 votes)
12 views27 pages

Inverted Pendulum Control: LQR & LQG Simulation

Uploaded by

tienninh05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views27 pages

Inverted Pendulum Control: LQR & LQG Simulation

Uploaded by

tienninh05
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

MINISTRY OF EDUCATION AND TRAINING

Ho Chi Minh City University of Technology and Education


High-Quality Faculty

FINAL REPORT

SIMULATION REPORT ON CONTROL OF INVERTED


PENDULUM VEHICLE USING LQR AND LQG

SEMESTER II – YEARS 2024-2025

LECTURER: Vũ Văn Phong

Nguyễn Tuấn An_20151252 (lớp thứ 6)


Nguyễn Tiến Ninh_22151034 (lớp thứ 5)

Ho Chi Minh City, June 2025

1
2
CHAPTER 1. Introduce

The inverted pendulum cart is a nonlinear, multivariable,


and naturally unstable mechanical system, which is often used
as a classic benchmark problem in automatic control theory. The
main objective is to keep the pendulum upright (inverted
equilibrium) while controlling the position of the cart.
Successful control of this system requires an efficient controller
that can stabilize the system and respond well to the
requirements.

This paper presents the process of mathematical modeling,


design and simulation of two advanced controllers: Linear
Quadratic Optimization Controller (LQR) and Linear Gaussian
Quadratic Optimization Controller (LQG) for the inverted
pendulum cart system. The controllers are calculated using
MATLAB and simulated in Simulink environment to evaluate
the performance.

3
CHAPTER 2. Mathematical model
of inverted pendulum car

Figure 2.1: Mathematical model of inverted pendulum system on vehicle

Figure 2.2: Variables in the nonlinear equation of the inverted pendulum


4
The inverted pendulum vehicle system is described by the
following parameters:
 M: Vehicle mass (kg). In this simulation: M = 1 kg.
 m: Pendulum mass (kg). In this simulation: m = 0.1 (kg).
 l: Half the pendulum length (m). In this simulation: l = 1 (m).
 g: Gravitational acceleration (m/s²). In this simulation:
g = 9.81 (m/s²).

The system is linearized around the vertical equilibrium


point (the pendulum is at 0 radians from the vertical).
The "state" of a system is a set of variables that fully
describes its current state. Knowing these state variables at any
given time, we can predict what the system will be like in the
future (given the effects on it).

The state variables of the system are selected as:


 x_1 = θ: The angle of deviation of the pendulum from the
vertical (rad).
 x_2 = θ_dot: Angular velocity of the pendulum (rad/s).
 x_3 = x: Location of the vehicle (m).
 x_4 = x_dot: Vehicle speed (m/s).
We can group these state variables into a State Vector:

5
X = [θ; θ_dot; x; x_dot]
 Control signal: u (force acting on the vehicle)

The linearized state equation of the system has form:


 The state equation describes how the state variables change
over time, under the influence of their own and external
forces (control signals).
 The inverted pendulum vehicle system is inherently complex
(nonlinear). To simplify the controller design, it is often
linearized around the desired operating point (e.g., the
pendulum is upright, the vehicle is stationary).
"Linearization" is like approximating a complex curve with a
small straight line near a certain point.
After linearization, the equation of state has a simpler form:
X_dot = AX + Bu
Where:
 X_dot: Tells how fast the states are changing (vector of
derivatives: [θ_dot; θ_ddot; x_dot; x_ddot]).
 A: The state matrix. It tells how the current states influence
each other to produce a change. For example, part of A
would say that angular velocity θ_dot will change angle θ.

A = [ 0 1 0 0; % θ_dot = 1*θ_dot (dòng này định nghĩa trạng


thái thứ 2 là đạo hàm trạng thái 1)
10.78 0 0 0; % θ_ddot = 10.78*θ + ... (gia tốc góc phụ thuộc
góc)
6
0 0 0 1; % x_dot = 1*x_dot (dòng này định nghĩa trạng thái thứ
4 là đạo hàm trạng thái 3)
-0.98 0 0 0 ] % x_ddot = -0.98*θ + ... (gia tốc xe phụ thuộc góc)

 B: Control matrix. It shows how the control force u (the


force pushing the car) affects the change of states.

B = [ 0; % Lực u không trực tiếp làm thay đổi θ_dot


-1; % Lực u gây ra gia tốc góc θ_ddot (theo chiều ngược lại với
quy ước)
0; % Lực u không trực tiếp làm thay đổi x_dot
1 ] % Lực u gây ra gia tốc xe x_ddot

 u: Control signals (forces applied to the car). This is what our


"brain" will calculate.

Output equation: Y = CX (depending on the variables to be


measured/observed)

CHAPTER 3. LQR Controller

3.1. What is LQR? (Linear Quadratic Regulator)


3.1.1. What does "Linear" mean?

7
“Linear” here implies that this controller is designed for systems
with linear mathematical models (like the X_dot = AX + Bu
equation we just discussed above).

3.1.2. What does "Quadratic Regulator" mean?


"Quadratic" (i.e., quadratic, e.g., x²) refers to the way LQR
makes decisions. It tries to minimize a quadratic "cost function."
"Regulator" means that its goal is usually to keep the states of
the system at a desired value (usually 0, i.e., the pendulum is
upright, the car is stationary).

3.1.3. The goal of LQR: Optimal balance


LQR tìm cách tính toán tín hiệu điều khiển u sao cho đạt được
sự cân bằng tốt nhất giữa hai yếu tố:
 Keep the states close to the desired value (usually 0): That is,
keep the pendulum upright (θ close to 0) and the vehicle in
the desired position (x close to 0). If the states deviate from
0, the “cost” will increase.
 Use as little control energy as possible: That is, do not apply
the force u too strongly or too often. Using more control
energy also increases the “cost”.
The “cost” function J that LQR tries to minimize is of the form:
J = ∫(X'QX + u'Ru)dt
Simple explanation:

8
 ∫ ... dt: Means calculating the total "cost" over time, from
start to finish.
 X'QX: Cost due to states deviating from 0.
 X: State vector ([θ; θ_dot; x; x_dot]).
 Q: State weight matrix. This is a matrix of our choice. It tells
LQR which states we care about keeping closer to 0. For
example, if the element of Q corresponding to θ is large,
LQR will try very hard to keep θ close to 0.

 u'Ru: Cost due to control energy use.


 u: Control signal.
 R: Control weight matrix. This is also a matrix of our choice.
If R is large, LQR will try to use less force u (energy saving).
LQR will find a state feedback control law:
u = -KX
 K: LQR gain matrix. This is the real “brain” that LQR
calculates. It tells how much force u should be applied and in
what direction based on the current X state values. A minus
sign (-) usually means negative feedback, which helps
stabilize the system (for example, if the pendulum tilts to the
right, the force will push the cart to the right to bring the
pendulum back to the vertical position).

3.2. Design LQR using MATLAB


MATLAB is a powerful software for engineering calculations.

9
3.2.1. The weight matrices Q and R

q11 = 1; % Trọng số cho trạng thái θ (hoặc trạng thái đầu


tiên, giả sử là θ)
q33 = 100; % Trọng số cho trạng thái x (hoặc trạng thái
thứ ba, giả sử là x)
r1 = 1; % Trọng số cho tín hiệu điều khiển u

Q = [q11 0 0 0;
0 1 0 0;
0 0 q33 0;
0 0 0 1];
R = r1;

 Setting q33 = 100 (much larger than the other weights for the
state) means that we are very interested in keeping the
vehicle position x close to 0.
 q11 = 1 means that we are also interested in keeping the
pendulum angle θ close to 0, but not as much as keeping the
vehicle position x.
 R = 1 is a relative value, allowing LQR to use a moderate
amount of control energy. If R were very large, the vehicle
would move more slowly and less aggressively.

3.2.2. Calculation of gain matrix K


MATLAB has the lqr command to do this easily.
K = lqr(A, B, Q, R)
We have:
 K = [-70.1356 -22.1091 -10.0000 -11.0514]

10
This means that the control law will be:

u = -(-70.1356*θ - 22.1091*θ_dot - 10.0000*x - 11.0514*x_dot)


u = 70.1356*θ + 22.1091*θ_dot + 10.0000*x + 11.0514*x_dot

(Note: the signs of the K elements and the minus sign in u=-
KX are important. Based on these K values, it looks like the
coefficients in the final equation are positive.)
The command P = care(A, B, Q, R) calculates the matrix P,
which is the solution to a complex mathematical equation called
the Riccati equation, which is the basis of LQR. We don't need
to go into P here, just know that it is part of the process of
calculating K.

3.3. LQR Simulation using Simulink


3.3.1. LQR block diagram explanation

Figure 3.1: Overall diagram of LQR

11
 The "Inverted pendulum" block: This is the "heart" of the
simulation, representing our real inverted pendulum car. It
takes the control force u as input and outputs the current
states (theta_d i.e. θ_dot, theta i.e. θ, x_d i.e. x_dot, x).
 The lines connecting the "Inverted pendulum" go out: These
signals are the X states of the system.
 The "-K" Gain block: This is where the K*X multiplication
is performed. Since the control law is u = -KX, this block can
be named "K" and then have an adder block with a reference
input of 0 and an input of -KX from the "K" block. Or simply
a gain block with a value of -K if the reference is 0. In your
picture, it is a Gain block named "K" and a summation
junction with a minus sign, meaning it is calculating 0 - K*X
= -K*X.
 The u signal line goes into the "Inverted pendulum": This is
the calculated control force, acting back on the system.
 Scope block (icon looks like a TV screen): Used to plot the
signal graph over time, helping us see if the pendulum is
upright, if the car is at position 0.

12
Figure 3.2: Inside the "Inverted pendulum" block

Here is how the mathematical model X_dot = AX + Bu (or its


nonlinear version) is realized using blocks:

 Input u: Control force.


 "fcn" block: This block contains the system's kinematic
equations. It calculates the accelerations (theta_dd - angular
acceleration, and x_dd - vehicle acceleration) based on the
current states and the force u.
 "1/s" (Integrator) blocks: In mathematics, integrating the
acceleration gives the velocity, integrating the velocity gives
the position.theta_dd qua khối 1/s thứ nhất -> theta_d (vận
tốc góc).
 theta_d through the second 1/s block -> theta (angle).
 Similarly for x_dd -> x_d -> x.
 Outputs theta_d, theta, x_d, x: These are the state variables
of the system.

3.3.2. LQR Simulation

13
Figure 3.3: LQR state-time graph

 Top graph (vehicle position x): Blue line. The vehicle may
initially be at a non-zero position (e.g., due to the initial
conditions set), then it quickly moves to zero and stops there
after about 2 seconds.
 Middle graph (pendulum angle θ): The blue line is the actual
θ angle, the red line is the desired angle (0 radians - upright).
The pendulum may initially be tilted slightly. The LQR
controller quickly applies force to the vehicle to return the
pendulum to the upright position (θ angle to 0) after about
1.5 seconds. The response is very smooth, with almost no
oscillations.
 Bottom graph (angular velocity θ_dot and vehicle velocity
x_dot): Both of these velocities (blue and red lines) also
quickly decrease to zero, meaning that the pendulum no
longer rotates and the vehicle no longer moves after it has

14
stabilized.=> Nhận xét về LQR: Trong điều kiện lý tưởng
(không có sai số đo lường, không có tác động bất ngờ từ bên
ngoài, biết chính xác các trạng thái), bộ điều khiển LQR hoạt
động cực kỳ tốt. Nó ổn định hệ thống xe con lắc ngược một
cách nhanh chóng và chính xác.

15
CHAPTER 4. LQG Controller

4.1. Why we need LQG?


4.1.1. What is "noise"?
 Process Noise: Random, unforeseen effects on the system
itself. Examples: a light breeze blowing on a pendulum,
uneven friction on the rails of a vehicle.
 Measurement Noise: Errors in the sensors used to measure
the state. Examples: an angle sensor may have a slightly
incorrect reading, a position sensor may vibrate.
LQR assumes that there is no noise at all. If there is noise, the
performance of the LQR will be degraded.

4.1.2. Not everything can be measured all the time.


 The LQR controller (u = -KX) requires exact knowledge of
all states X = [θ; θ_dot; x; x_dot]. But in practice:
 We may only install sensors to measure the angle θ and the
position x.
Measuring the velocity θ_dot and x_dot directly can be difficult
or expensive. One often has to calculate (estimate) them from
position measurements.

4.2. What does LQG mean ?

16
LQG is an upgrade of LQR to solve the above problems. It
consists of two main parts:
 LQR controller: Still the same K controller design as before.
 Kalman filter: An intelligent "detective", whose task is to
estimate the real state of the system (X_hat - read as "X hat"
or "X estimate") based on noisy measurements and the
system model.
The control law of LQG will be: u = -KX_hat (using the
estimated state instead of the real state).

4.2.1. Kalman filter


The Kalman filter works in a cycle:
 Prediction: Based on the estimated state in the previous step
and the system model, it predicts what the current state will
be.
 Update: It takes in information from the sensors (noisy
measurements) and compares it to its prediction. It then
adjusts the state estimate to be “as close” as possible to
reality, taking into account the reliability of the model and
the noise of the measurements.
It is very good at “filtering out” noise and making the best
possible estimate of states, even those that are not directly
measured.

17
4.2.2. What does "Gaussian" mean?
“Gaussian” (referring to the Gaussian distribution, also known
as the normal, bell-shaped distribution) here implies that the
LQG controller assumes that the disturbances (both process and
measurement disturbances) have a statistical characteristic that
follows a Gaussian distribution. This is a common and
reasonable assumption in many cases.

4.3. LQG design using MATLAB


The K calculation part of LQR remains the same. We need to
add parameters for the Kalman Filter.

4.3.1. Determine what is measured (Matrix C)


We need to tell the Kalman Filter what states are directly
measured by the sensors. This is done through the output matrix
C.

C = [1 0 0 0; % Dòng 1: Đo trạng thái thứ nhất (giả sử là


θ)
0 0 1 0]; % Dòng 2: Đo trạng thái thứ ba (giả sử là x)

This means we have a sensor that measures angle θ and a sensor


that measures position x. We do not measure θ_dot and x_dot
directly.

18
4.3.2. Noise description (Matrix Qn, Rn)
We need to set the Kalman Filter to know the expected level of
noise in the system:
 G = [1 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1]; (usually a unit matrix,
indicating that the process noise can affect all states).
 Qn = 0.000001*G;: Covariance matrix of the process noise
(w). The smaller Qn, the more confident we are that the
system model (X_dot = AX + Bu) is accurate and less
affected by external noise. The value of 0.000001 here is
very small, meaning that we assume low process noise.
 Rn = [0.001 0; 0 0.01];: Covariance matrix of the
measurement noise (v).
 0.001 is the variance of the noise in measuring θ.
 0.01 is the variance of the noise in measuring x.
The smaller Rn is, the more confident we are that the sensors are
measuring accurately. These values indicate that the noise in
measuring x is expected to be larger than the noise in measuring
θ.

4.3.3. Calculation of the Kalman Filter gain matrix L


MATLAB has the LQE command (Linear Quadratic Estimator -
another name for Steady-State Kalman Filter) to calculate the
gain matrix L for the filter:
L = lqe(A, G, C, Qn, Rn)

19
After running this command, MATLAB will calculate and
display the matrix L
L = [ 6.5617 -0.0571;
21.5437 -0.1876;
-0.5713 0.1470;
-1.9568 0.0271 ]

This L matrix is used by the Kalman Filter to adjust its state


estimates based on the difference between the actual
measurement and the prediction.

4.4. Simulating LQG with Simulink: Challenges with noise


4.4.1. Giải thích sơ đồ khối LQG

Figure 4.1: LQR block diagram

20
The LQG diagram will look a bit more complicated than the
LQR because it must include:
 The "Inverted pendulum" object model: Still the same.
 The Kalman Filter implementation block: This block will
take as input u (the applied control signal) and y_noisy (the
noisy measurement signal from the sensor). Its output is
X_hat (the estimated states).
 The control calculation block u = -KX_hat: Uses the X_hat
from the filter.
 The noise generation blocks: To simulate the process noise
and the measurement noise.
In Figure 4.1, the blocks located below the "Inverted pendulum"
block and connected to its input/output are the implementation
of the LQG controller, including the estimation part (Kalman
Filter) and the feedback control part. There are "Random
Number" blocks to add noise to the measurement signals.

4.4.2. Setting parameters for noise

21
Figure 4.2: Noise parameter settings for LQR

Figure 4.2 shows the settings window for the two “Random
Number” blocks. This is how we introduce the “imperfections”
of the real world into the simulation.
 Mean = 0: Noise usually fluctuates around 0, not skewing the
measurement in a fixed direction.
 Variance: Measures how “spread” or “strength” the noise is.
The larger the variance, the stronger the noise.
 One block has Variance = 0.001. 
 The other block has Variance = 0.01 (stronger noise).
These values should correspond to what we defined in Qn and
Rn for the Kalman filter to work optimally (although Qn is very
small, it is possible that process noise is added inside the
"Inverted pendulum" block or is not explicitly modeled by a
separate "Random Number"). These "Random Number" blocks
are most likely to model measurement noise on the θ and x
measurement channels.
 Sample time = 0.01: Noise is generated every 0.01 seconds.

4.4.3. LQG simulation results

22
Figure 4.3: LQG simulation result graph

The simulation time is 6 seconds.


 Top graph (vehicle position x): The blue line (x position) is
no longer as smooth as in LQR. It oscillates constantly
around 0. This is due to the interference and the controller
has to constantly adjust. However, the important thing is that
it still keeps the vehicle close to 0, without drifting too far.
 Middle graph (pendulum angle θ): The blue line (θ) also
oscillates more around 0 (red line). The amplitude of the
oscillation is larger than in LQR, showing the difficulty of
controlling in a noisy environment. But the pendulum still
does not fall over!
 Bottom graph (velocity θ_dot and x_dot): The velocity
signals are also very "noisy" and oscillate, reflecting the
constant effort of the controller.

23
=> Comments on LQG: In the presence of disturbances, the
LQG controller still keeps the inverted pendulum system stable.
The pendulum does not tip over and the car stays close to the
desired position. However, due to the effects of disturbances and
errors in the state estimation process of the Kalman Filter, the
states (angles, positions) will fluctuate more and will not be as
"smooth" as in the ideal LQR case. This is a practical trade-off:
we accept a slight loss in accuracy in exchange for the ability to
operate stably under uncertainty.

CHAPTER 5. Comparison of LQR


and LQG

24
Đặc điểm Bộ điều khiển LQR Bộ điều khiển LQG
Giả định về hệ thống Tuyến tính, không có Tuyến tính, có nhiễu
nhiễu, do được tất cả (quá trình và đo lường,
các trạng thái một cách tuân theo phân phối
chính xác. Gaussian), không nhất
thiết đo được tất cả các
trạng thái.
Cấu trúc Đơn giản: Chỉ cần tính Phức tạp hơn: Cần tính
k và phản hồi u = -kx. K cho LQR và L cho Bộ
lọc Kalman. Luật điều
khiển u = -k*x_hat.

Yêu cầu về cảm biến Cần cảm biến cho tất cả Chỉ cần cảm biến cho
các trạng thái. một số trạng thái có thể
đo được (được định
nghĩa trong ma trận C).
Hiệu năng (lý tưởng) Rất tốt. Đáp ứng nhanh, Tốt, nhưng có thể hơi
chính xác, mượt mà. kém hơn LQR một chút
do có thêm bước ước
lượng.
Hiệu năng (có nhiễu) Hiệu năng sẽ suy giảm Hoạt động tốt hơn nhiều
nghiêm trọng, có thể so với LQR. Duy trì sự
gây mất ổn định. ổn định, mặc dù đáp
ứng sẽ 'rung lắc' hơn.

Ứng dụng thực tế Ít phổ biến cho các hệ Phổ biến hơn nhiều cho
thống phức tạp có các ứng dụng thực tế vì
nhiễu. nó tính đến nhiễu và hạn
chế cảm biến.

Put simply:
 LQR is like a great racer on a perfect, flawless track in good
weather. He will reach record speeds.

25
 LQG is like a good racer on a bumpy, potholed, slightly
foggy road. He may not be as fast as the other racer on a
perfect track, but he knows how to handle the difficult
conditions to reach the finish line safely.

CHAPTER 6. Conclusion

26
Through the simulation of the inverted pendulum car, we have
seen the important things:
 Mathematical model is the foundation: To control anything,
we need to understand how it works through mathematical
equations.
 LQR is a powerful controller under ideal conditions: It shows
the maximum potential of the system when we have perfect
information.
 There is "noise": Real-world systems are always affected by
uncertainties and measurement errors.
 LQG is designed for the real world: By combining LQR with
Kalman Filter, LQG can estimate the state and control the
system stably even when there is noise.
 There is always a trade-off: LQG may not be as "smooth" as
LQR under ideal conditions, but it is much more reliable in
practice.

MATLAB and Simulink are useful tools: They help us design,


simulate, and test control ideas visually before building actual
hardware.

27

You might also like