Before I get too far into this section I should state clearly and up front that I am not a ``controls'' expert and have no formal training in this field. What I say here is said to the best of my knowledge. If anything here is mistaken or misleading, I'd appreciate being corrected. I'd like to credit my boss, Bob Hain, and my coworker, Rich Kaszeta, for explaining this basic theory to me, and answering my many questions.
The altitude hold module I developed is an example of a PID controller. PID stands for proportional, integral, and derivative. These are three components to the control module that will take our input variable (error), and calculate the value of the output variable required to drive our error to zero.
A PID needs an input variable, and an output variable. The input variable will be the error, or the difference between where we are, and where we want to be. The output variable is the position of our control surface.
The proportional component of the PID drives the output variable in direct proportion to the input variable. If your system is such that the output variable is zero when the error is zero and things are mostly linear, you usually can get by with proportional control only. However, if you do not know in advance what the output variable will be when error is zero, you will need to add in a measure of integral control.
The integral component drives the output based on the area under the curve if we graph our actual position vs. target position over time.
The derivative component is something I haven't dealt with, but is used to drive you towards your target value more quickly. I'm told this must be used with caution since it can easily yield an unstable system if not tuned correctly.
Typically you will take the output of each component of your PID and combine them in some proportion to produce your final output.
The proportional component quickly stabilizes the system when used by itself, but the system will typically stabilize to an incorrect value. The integral component drives you towards the correct value over time, but you typically oscillate over and under your target and does not stabilize quickly. However, each of these provides something we want. When we combine them, they offset each others negatives while maintaining their desirable qualities yielding a system that does exactly what we want.
It's actually pretty interesting and amazing when you think about it. the proportional control gives us stability, but it introduces an error into the system so we stabilize to the wrong value. The integral components will continue to increase as the sum of the errors over time increases. This pushes us back the direction we want to go. When the system stabilizes out, we find that the integral component precisely offsets the error introduced by the proportional control.
The concepts are simple and the code to implement this is simple. I am still amazed at how such a simple arrangement can so effectively control a system.