Skip to content

Control Master

Advanced control algorithms are the core of drone performance optimization. In this project, you will learn how to implement LQR and MPC control algorithms to improve drone flight stability and response speed.

  • LQR (Linear Quadratic Regulator) principles
  • MPC (Model Predictive Control) principles
  • System modeling and identification
  • Controller design and implementation
ItemQuantityNotes
Fully Assembled Drone1-
Computer1With VS Code + ESP-IDF environment
USB Cable1For programming
MATLAB or Python1For algorithm design and simulation

Step 1: Understand Advanced Control Algorithms

Section titled “Step 1: Understand Advanced Control Algorithms”

Design optimal controllers by minimizing quadratic cost functions.

Predict future states based on system models and optimize control sequences.

  1. Establish linearized model of the drone
  2. Include attitude dynamics and position dynamics
  3. Use MATLAB or Python for system identification and model validation

Use MATLAB’s lqr() function or Python’s control library to design LQR controller:

from control import lqr
# System matrices
A = [[...]] # State matrix
B = [[...]] # Input matrix
Q = [[...]] # State weight matrix
R = [[...]] # Control weight matrix
# Calculate LQR gain
K, S, E = lqr(A, B, Q, R)

Create a new controller file in components/core/crazyflie/modules/src/controller:

controller_lqr.c
void controllerLQR(control_t *control, setpoint_t *setpoint, const state_t *state) {
// Calculate state error
float error[12];
error[0] = setpoint->position.x - state->position.x;
error[1] = setpoint->position.y - state->position.y;
error[2] = setpoint->position.z - state->position.z;
// ... other state errors
// LQR control law: u = -K * error
float control_output[4];
for (int i = 0; i < 4; i++) {
control_output[i] = 0;
for (int j = 0; j < 12; j++) {
control_output[i] -= K[i][j] * error[j];
}
}
// Apply control output
control->thrust = control_output[0];
control->roll = control_output[1];
control->pitch = control_output[2];
control->yaw = control_output[3];
}
  1. Compile and flash the code
  2. Flight test, compare performance differences between advanced controller and PID controller
  • Check if system model is accurate
  • Adjust weight matrices Q and R
  • Increase state weight Q
  • Decrease control weight R

Congratulations! You have implemented advanced control algorithms, which is the core technology of drone control!

In the next project, you will learn how to develop multi-sensor data fusion systems.