yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
520 Motor📖
1.About motor
  • About motor
2.ROS robot control board drive
  • 2.1 Control motor
  • 2.2 Timer captures encoder data
  • 2.3 PID control robot car
  • 2.4 Kinematic Analysis-Mecanum wheel structure
  • 2.5 Kinematic Analysis-Ackerman structure
  • 2.6 Kinematic Analysis-4WD structure
3.STM32 Self-balancing car tutorials
Get started quickly
  • 03.P, PI, PD controller theory
  • 01.Balancing car assembly
  • 02.Get started quicklyy
  • 03.Balance car interface description
  • 04.Common Problem Analysis
  • 05.Balance Car V2 Library Function Development Manual
Environment construction and development
  • 01.MDK-ARM installation
  • 02.STM32CubeIDE installation
  • 03.Common driver installation
  • 04.Download the program
  • 05.MDK-ARM project usage
  • 06.Program simulation
Robot car PID control course
  • 01.PID basic concept
  • 02.PID example analysis
  • 04.Position PID
  • 05.Incremental PID
  • 06.Cascade PID
  • 07.Balance principle of car
  • 08.Car upright control (PD)
  • 09.Car speed ​​control (PI)
  • 10.Car steering control (PD)
  • 11.Got angle and angular velocity (DMP algorithm)
  • 12.Got angle and angular velocity (Kalman filter algorithm)
  • 13.Got angle and angular velocity(Complementary filter algorithm)
Robot car basic course
  • 01.Car parameter adjustment
  • 02.Ultrasonic obstacle avoidance
  • 03.Ultrasonic follow
  • 04.Bluetooth remote control
  • 05.Load balance
Robot car LQR control
  • Robot car LQR control
Circuit design
  • 01.Overall circuit design
  • 02.STM32 minimum system
  • 03.Serial port circuit design
  • 04.Power circuit design
  • 05.MPU6050 Circuit Design
  • 06.Motor drive circuit design
Mathematical model
  • Balance car mathematical model
4.4-channel motor drive module
Introduction and Debug
  • 1.Introduction to 4-channel motor drive board
  • 2.Control command
  • 3.Motor introduction and usage
  • 4.PC host
IIC communication
  • For Arduino
  • For ESP32
  • For Jetson NANO
  • For MSPM0G3507
  • For MSPM0G3519new
  • For PICO2
  • For RDK X5
  • For RPi5
  • For STM32
USART communication
  • For Arduino
  • For ESP32
  • For Jetson NANO
  • For MSPM0G3507
  • For MSPM0G3519new
  • For PICO2
  • For RDK X5
  • For RPi5
  • For STM32
5.2-channel motor drive module
For MSPM0G3507
  • 1.AT8236 drive tutorials
  • 2.TB6612 drive tutorials
For MSPM0G3519
  • 1.AT8236 drive tutorialsnew
  • 2.TB6612 drive tutorialsnew
Download
  • Model File
  • Annex_ROS_robot_board
  • Annex_STM32_balancing_car
  • Annex_4channel_motor_drive
  • Annex_2channel_motor_drive
Download ZIP

Welcome to 520 Motor repository

Robot car LQR control

Balance car LQR high-level control

Balance car LQR high-level controlLQRLQR-Linear (Linear Model)LQR-Quadratic (Quadratic Cost Function)LQR-RegulatorLQR advantages and disadvantagesLQR and PIDLQR implementation ideas1. Establish a mathematical model of the balance car2. Design state space expressions3. Design objective function4. Design/implement LQR controllerDesign LQR controllerImplement LQR controller5. Simulation verification6. Practical applicationProgram running resultsCode Implementation

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.

The LQR-Linear algorithm can be used to calculate the optimal state feedback gain matrix K, and K can be adjusted to achieve stable control of the tilt angle and speed.

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.

xxxxxxxxxx
The balance car can be adjusted to minimize the tilt angle deviation. We can add these factors to the quadratic cost function and use the LQR-Quadratic algorithm to design the corresponding optimal controller.

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.

xxxxxxxxxx
The response speed or stability of the balance car under different working conditions can be adjusted. The weight relationship between the controller's system state error and input power can be changed by adjusting the Q and R matrices, thereby achieving different regulation effects.

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.

​x
State space equations can describe the dynamic characteristics of the system, including state variables, input variables, system matrices, and output matrices, 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

xxxxxxxxxx
x is the system state vector, u is the control input vector

A, B, C, D are system parameter matrices

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

xxxxxxxxxx
Q and R are the state feedback matrix and input feedback matrix respectively

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

  • Solve the optimal gain matrix K: Use the LQR algorithm to solve the optimal state feedback matrix Q and input feedback matrix R when the cost function is minimized.

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

⑤: Solve the gain matrix K of the LQR controller

  • Controller output: Control input 𝑢=−𝐾𝑥

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.

xxxxxxxxxx
The controller will adjust the control input in real time according to the current system state to achieve the best performance of the system

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.

xxxxxxxxxx
By continuously optimizing parameters and adjusting strategies, the system can achieve better stability, robustness and performance

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

xxxxxxxxxx
Product supporting materials source code path: Attachment → Source code summary → 6.LQR → Matlab

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.

xxxxxxxxxx
Product supporting materials source code path: Attachment → Source code summary → 6.LQR → STM32_code

Implementation code

xxxxxxxxxx
//LQR状态反馈系数
float K1= -62.1608, K2= -72.9295, K3=-356.0969, K4=-35.7579 , K5=15.8114, K6=15.8114;
float K5OLD=15.8114, K6OLD=15.8114;

L_accel=-(K1*x_pose+K2*(x_speed-Target_x_speed)+K3*(angle_x-Target_angle_x)+K4*gyro_x+K5*angle_z+K6*(gyro_z-Target_gyro_z));
R_accel=-(K1*x_pose+K2*(x_speed-Target_x_speed)+K3*(angle_x-Target_angle_x)+K4*gyro_x-K5*angle_z-K6*(gyro_z-Target_gyro_z));

 

Load balance
Overall circuit design

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文