STM32 Drone Flight Controller from scratch? You can do it

STM32 Drone Flight Controller from scratch? You can do it


7 minute read

Table of Contents

If you are looking for an interesting and advanced project in the STM32 Programming, not just "LED Blink", developing the code for the STM32 Drone Flight Controller could definitely excite you. It involves not only Embedded Programming, but also motor control, IMU sensor Integration, and Control Systems (PID, or more advanced Control techniques). Undoubtedly, working on these topics will pave the way for improving your embedded programming skills and general understanding of working on complex projects. Whether you’re a student or a hobbyist, this guide will help you take your embedded systems skills to the next level.

In this article, I’ll walk you through how I built a quadcopter from scratch using an STM32 microcontroller. We’ll explore the main principles of integrating the IMU sensor and Orientation Estimation. Then, we cover the PID Controller for balancing the drone and wireless RC Joystick communication. 

The objectives of this article are not to explain every detail of designing the flight controller. Instead, I aim to provide you with general guidance and ideas to help you start working on the project. But, if you are looking for the complete and step-by-step approach, do not forget to check my course on designing the flight Controller based on the STM32 MCU:

STM32 Drone Flight Controller Coding

Hardware

Although building the hardware of the drone is not the primary topic of this article, let me briefly introduce the hardware I am using. In this tutorial, I used the STEVAL-FCU001V2 Flight Controller and the STEVAL-DRONE02 Quadcopter Kit to develop my quadcopter. This kit is relatively tiny for safe indoor flight tests and has all the essential components to build the drone: motors, propellers, frame, battery, and flight controller. The last element in turn includes the STM32 MCU, IMU sensor, barometer, and MOSFETs to run the motors.

STM32 Drone: Main Principles

The figure below illustrates the standard configuration of the quadcopter, which consists of four motors: two rotate counter-clockwise and the other two in a clockwise direction. The first obvious question: why do we need this configuration? What if we use fewer or more motors? The answer is that we can control all degrees of freedom in this setting. If we want to control the height, we adjust the speed of all motors. To maintain the pitch angle, we adjust the speed difference between M1/M2 and M3/M4. Roll angle is controlled by tuning the difference between M1/M4 and M2/M3. Finally, we can stabilize the Yaw angle by tuning the speed between M1/M3 and M2/M4.

STM32 Drone Illustration

So, for a quadcopter to fly, it has to:

1. Control the Motors

2. Estimate the Tilt angles (Yaw, Pitch, and Roll)

3. Apply the Control System techniques to control the angles by adjusting the motors' speed

STM32 Drone: Running Motors

One of the main parts of the drone is the motors. Along with propellers mounted on the motor, they create a thrust that lifts the drone's body. The thrust generated depends on the motors' speed, so by dynamically changing the speed, we can lift and maintain the drone's balance at the same time. 

We typically use Brushless DC Motors (BLDC) in quadcopters because they are efficient, durable, and cost-effective. However, they require special hardware to drive them: Electronic Speed Controllers (ESCs). Meanwhile, you can also find DC motors in small drones that do not require complex hardware. In my case, I am using the DC motor.

In both cases, we have to deliver a Pulse Width Modulation (PWM) signal to drive the motors.  The figure below demonstrates the waveform of the PWM signal. Typically, we use a 50 Hz PWM signal with a duty cycle ranging from 1 (0% speed) to 2 (100 % speed) ms. 

For driving the DC Motors, we can use a simple MOSFET or a DC Motor Driver. In that case, we vary the Duty cycle from 0 to 100 % and can set a custom PWM frequency. Check the article below for detailed guidance on generating a PWM signal in STM32 MCUs:

STM32 PWM Generation

When using the ESCs, the PWM duty cycle varies from 1 to 2 ms. Setting 1 ms results in zero speed, whereas setting 2 ms yields the maximum speed. Consequently, intermediary values generate speed between two extremes. 

STM32 Drone: Attitude Estimation

The following primary topic in building the drone is attitude estimation. The orientation estimation measures tilt angles, enabling the drone to use these angles as feedback to maintain a balanced position. Otherwise, it will not be able to stabilize itself, let alone fly. 

Nowadays, Inertial Measurement Unit (IMU) sensors are widely used in attitude estimation. They contain an accelerometer, a gyroscope, and sometimes a magnetometer. Of course, the raw values of the IMU sensor are no help in measuring the angles. We use them as an input to data fusion algorithms (Kalman filter, Complementary filter) to estimate the Euler angles. Of course, explaining every detail of the attitude estimation is beyond the objectives of this article, but let me present general ideas. 

An accelerometer measures the acceleration in 3 axes: x,y, and z. Since we have a gravity that always points towards the ground, we can use this invariance to find the drone's orientation. Depending on the quadcopter's attitude, the acceleration in each axis will vary accordingly. If the gravity is aligned with the z-axis, we obtain the maximum on that axis and zero in the other axes. 


STM32 Drone Accelerometer IllustrationAccelerometer Measurement Illustration


A gyroscope, on the other hand, measures the angular speed along each axis. It means we can increment them to obtain the Euler angles. The gyroscope suffers from the drifting issue: a tiny bias will lead to a huge error because of the increment procedure. On the other hand, the accelerometer is noisy: the vibrations generated by the drone's motor distort the accelerometer readings. 

STM32 Drone accelerometer vs. gyroscope

Hence, we have to use the data fusion algorithms to combine the accelerometer and gyroscope readings to achieve better precision. Using just one of them does not help solve the problem. The pseudo-code below demonstrates a simple complementary filter to fuse the data:

alpha = 0.99;
accely_accelz_sqrt = sqrt(accel_z^2 + accel_y^2);
accel_angle_pitch =  atan2(-accel_x, accely_accelz_sqrt);
accel_angle_roll = atan2(accel_y, accel_z);
pitch = alpha * (pitch + gyro_x * dt) + (1 - alpha) * accel_angle_pitch; 
roll = alpha * (roll + gyro_y * dt) + (1 - alpha) * accel_angle_roll;


Attitude estimation is a vast topic that involves not only the embedded programming but also linear and vector algebra. Do not be afraid of these words because I have created a complete guide on the attitude estimation, which covers everything rigorously:

STM32 Attitude Estimation

STM32 Drone: PID Controller

The PID Controller is one of the widely used Closed-loop Control techniques. It is computationally efficient and relatively easy to implement and tune. It is used to track the reference by feeding the error into the Controller. The Image below demonstrates a diagram of the PID Controller. 

Since I have an article about the PID Controller, I will skip explaining the details. You can refer to the article for a comprehensive guide on the PID Controller:

PID Controller Implementation in C

In our case, let's discuss how we can use the PID Controller for balancing the drone. 

STM32 Drone: Balancing the Drone

Coming soon...

STM32 Drone: RC Joystick Integration

Coming soon...

« Back to Blog