If you have ever seen a flight controller, you know they are tiny. You can hold one in the palm of your hand, and yet they have some of the most complicated algorithms of any embedded system. This mini series will cover just one of these parts, and that is how drones stay stable using an algorithm called the complementary filter. If you prefer videos, I highly recommend this one.
The Inertial Measurement Unit
The inertial measurement unit, or IMU for short, is one of the main sensors used on a drone. They come in many variants, but the one which is most commonly used is the 6 axis version, with a 3 axis gyroscope, and a 3 axis accelerometer. Both of these sensors are used to measure how much a drone is turning in any direction, so that it can correct accordingly to stay level, but both sensors produce wildly different results.
Gyroscope
Gyroscopes are sensors that use earth’s gravity to measure angular rate, which we can multiply by the time that has passed in order to get an angle reading. This allows them to be very accurate in most cases. The only problem with gyroscopes is that they are prone to drift. Here is an example:
As you can see, even though the gyroscope is not actually moving, the values it is outputting are slowly increasing. This can be caused by a number of factors, but it mainly has to do with how gyroscopes work internally. This is a big issue for drones, because for example, if we feed this data, and we tell the drone to try to maintain a 0 degree angle, it will keep trying to turn in the opposite direction to compensate for the drift. This is why we also use an accelerometer.
Accelerometer
Now on the other side of the spectrum, we have the accelerometer. The accelerometer uses acceleration in a certain direction to measure how much something has moved in a specific direction. A combination of these values can then be used to estimate rotation. Accelerometers work reasonably well, considering they aren’t using some sort of constant force to measure the angle. The only problem with them is that they are very sensitive to external forces. Here is an example:
As soon as you look at this data, it looks way worse than the gyroscope, but it’s actually not. To be fair, I did shake the sensor a little bit, but the drone will also be vibrating slightly, and doing this doesn’t affect the gyroscope. The only reason this data is good, is because it doesn’t drift. Now we need to see how we can combine the data from the accelerometer with the gyroscope to get accurate readings.
Sensor Fusion
Sensor fusion is the process of combining data from 2 or more different sensors to produce one accurate reading. This is exactly what we need to do with the IMU sensors. Lets see how we can use the complementary filter to do this.
Complementary Filter
The complementary filter is a very simple, and highly effective algorithm that can be used to fuse that data of an IMU to get accurate angular readings. Here is a diagram which explains the steps:
So first, in this diagram, we find the angular rate using the gyro sensor, and multiply with the time since last called to get the change in degrees since last called. Then we add with the current angle (which is 0 the first time around), and set that as our current orientation. Next we take 1% of the accelerometer estimate and add that to 99% of the current orientation. Finally, we add this to the new gyro reading and loop forever. This produces a very accurate result considering the simplicity of it. Here is what we get as an output:
Eureka! Finally, we get an accurate reading which correctly represents the orientation of the sensor. Here is an image what happens if I turn the object 90 degrees on all 3 sensors:
As you can see, the filtered data is much more accurate than the other 2. The accelerometer spikes when it is moving too fast in any direction, and the gyroscope data drifts too much. The filter provides us with the best of both worlds.
Conclusion
Now that you are able to sense the orientation of the drone, you may be wondering how you can use this to stabilize it. That is what we will be covering in the next part of this article, so stay tuned. In part 2 of this article you are going to need to understand PWM motor control, so be sure to check that out. If you have any questions, suggestions, or comments, please feel free to comment down below, if not I’ll see you next time.
Leave a Reply