Balance car LQR high-level control

LQR (Linear Quadratic Regulator) is a commonly used high-order control method that can help the balancing car achieve stable movement.

LQR

LQR (Linear Quadratic Regulator) is a classic linear quadratic regulator used to design the optimal state feedback controller for continuous-time linear systems.

The performance of the system is optimized by minimizing a cost function, while considering the state and control input of the system.

LQR-Linear (Linear Model)

LQR-Linear refers to the use of the LQR algorithm for control design when the state space model of the system is linear.

LQR-Quadratic (Quadratic Cost Function)

LQR-Quadratic refers to the use of a quadratic cost function to define the objective function, and the controller design is optimized by the LQR-Quadratic algorithm.

LQR-Regulator

LQR-Regulator can be used to adjust the state feedback matrix Q and the input feedback matrix R to achieve the regulation and stability of the system state.

LQR advantages and disadvantages

AdvantagesDescription
StabilityThe LQR algorithm can design a stable closed-loop system so that the system can quickly restore balance when subjected to external disturbances
Performance optimizationBy adjusting the state feedback matrix Q and the input feedback matrix R, the system performance can be optimized, such as fast response and error reduction.
Easy to implementThe LQR algorithm is a linear control method with simple calculation and easy implementation
DisadvantagesDescription
High model requirementsThe LQR algorithm requires an accurate system mathematical model for design. If the system model is inaccurate or has errors, it may lead to poor control effect
Low efficiencyIn complex systems, the LQR algorithm may require a lot of computing resources and time to solve the optimal controller gain matrix K, resulting in low computational efficiency
Poor robustnessThe LQR algorithm is sensitive to parameter changes and external interference, and may show poor robustness when facing nonlinear, time-varying or uncertain systems

LQR and PID

LQR (Linear Quadratic Regulator) and PID (Proportional-Integral-Derivative) are two commonly used control algorithms, which have some differences in design principles, adjustment methods and applicable scenarios.

 LQRPID
Design PrincipleLQR is a linear quadratic regulator based on the state space model. It designs the optimal controller by minimizing the weighted square sum of the system state variables.PID is a classic feedback control algorithm that calculates the control output based on the proportion, integration and differentiation of the error signal.
Regulation MethodLQR adjusts and stabilizes the system state by adjusting the state feedback matrix Q and the input feedback matrix R. The trade-off between system performance indicators can be balanced by the weight matrices Q and R.PID affects the controller's response speed, stability and anti-interference ability to the error signal by adjusting the proportional gain Kp, integral time Ti and differentiation time Td.
Applicable ScenariosQR is suitable for linear dynamic systems and is usually used for control problems that require optimization of specific performance indicators (such as minimizing energy consumption, trajectory tracking, etc.).PID is widely used in various industrial fields for control problems that do not require high control accuracy or do not require complex optimization targets.
PerformanceLQR has good performancePID is relatively simple and easy to implement

LQR implementation ideas

LQR control block diagram: full-state feedback control system

image-20240826195052563

A, B, C: state space equations

K: gain matrix

1. Establish a mathematical model of the balance car

Establish a dynamic model of the balance car, including the motion equations of the balance car, state space expressions, etc.

2. Design state space expressions

Convert the dynamic model of the balance car into a state space form, that is, a set of linear equations that describe the relationship between the system state and input.

image-20240921114955210

①: State equation

②: Output equation

3. Design objective function

Based on the system performance indicators and requirements, design an objective function to evaluate the system performance.

image-20240921115356943

③: Quadratic cost function

4. Design/implement LQR controller

Design LQR controller

Based on the state space expression and objective function of the system, use the LQR algorithm to solve the optimal controller gain matrix K.

Determine the optimal state feedback matrix Q and input feedback matrix R by minimizing the objective function.

Implement LQR controller

image-20240921120439052

④: Riccati equation → Solve the optimal control problem of the linear system

⑤: Solve the gain matrix K of the LQR controller

Apply the optimal controller gain matrix K to the balancing car system, and adjust the tilt angle and speed in real time to maintain balance.

5. Simulation verification

Matlab can directly calculate K using the lqr function

Use Matlab to verify the designed LQR high-order controller to check whether the system can work stably and meet the performance index requirements.

6. Practical application

You need to use MATLAB to run the program parameter_LQR.m and modify the corresponding parameters in the program to adapt to your own car

image-20240921103211399

Use SIMULINK to simulate it:

image-20240921103232724

Simulation results:

image-20240921103251581

Program running results

You can modify the parameters obtained by running the program to the corresponding code, and then fine-tune the parameters to achieve the best results!

image-20240921104238498

Code Implementation

When running this case code, the balancing car can only retain the standard package accessories, and other advanced accessories need to be removed; keep the balancing car vertical to the ground at 90° before starting.

Implementation code