Multi-rotor Control System
In order to maintain stability, the quadcopter must continuously take measurements from onboard sensors and make adjustments to the speed of each rotor to keep the aircraft level. The quadcopter must be able to maintain this equilibrium autonomously and this requires sensors and a sophisticated control system.
In order to determine orientation, we read data from the inertial measurement unit or IMU. The IMU is typically comprised of at least a three-axis accelerometer and a 3-axis gyroscope. We can use the output from these sensors to determine the angular position or attitude of the quadcopter. The accelerometer measures the acceleration forces (such as gravity) being applied to each axis. The gyroscope measures the rate of angular rotation for each axis. See Implementing a Quadcopter IMU for more detail (including source code) on my IMU implementation.
Challenges with Detecting Orientation
Unfortunately, using either of these sensors alone cannot provide an accurate representation of the attitude of the quadcopter. Both the accelerometer and gyroscope readings are susceptible to major problems which means we must be very careful with how we utilize this data.
The accelerometer can detect all of the forces working on the object. This works very well for detecting gravity on stationary objects but when the object is moved in space, these forces will be included in the readings as well and the accelerometer data will be unusable. Also, accelerometers are prone to vibration and noise and can be unstable over short term measurements.
The gyroscope provides a very accurate estimate of angular position by using numeric integration. However, over longer time durations, the measurements have a tendency to drift resulting in inaccurate readings.
Enter the Complimentary Filter
So we have the accelerometer which is bad for short-term estimates but provides a stable long-term average and we have the gyroscope which responds quickly but drifts over time. We can use digital processing to correct for these problems when we make use of these measurements. The two most common approaches to doing this are the Kalman Filter and the Complimentary Filter. The internet is abundant with information on both of these techniques but, for our purposes, the complimentary filter is easier to understand and is much more computationally efficient.
The idea is that we use the gyroscope for short-term estimates but favour the accelerometer for longer-term estimates. In order to do this, we remove the unstable, high frequency component of the accelerometer measurements with a low-pass filter reducing the influence of short-duration signals but maintaining a stable long-term average. We then apply a high-pass filter to the gyroscope measurements to remove any long-term drift. The two filtered signals are summed together to create a single angle estimate that combines the best parts of both sensor signals. In this way, the complementary filter removes noise from the accelerometer and eliminates gyroscope drift.
For those interested in the specifics, here is a great site that explains the math in more detail.
Using The Sensor Data To Control Flight
Now that we have an accurate estimate of our aircraft orientation, we can use this information to make adjustments to the speed of each rotor to keep the aircraft level. To find out how this is done, see Multi-rotor Control System II.