The ABC’s of PID’s

If you have ever worked with any open-source multirotor controller boards in the past, then you have probably needed to tune the PID settings in the feedback loops a few times. For some, these settings make perfect sense, but for most, they are a complete mystery that seemingly requires the combined skills of a computer programmer and an electrical engineer to figure out. In this installment of Multirotor Flight, we are going to take a look at control feedback loops, the PID settings that adjust them, and how they all work together to make a multirotor aircraft controllable.

All closed loop control systems are designed to keep a specific device, or system of devices, operating at a pre-determined setpoint. Whether it is the thermostat on the wall that controls the temperature of your home, the cruise control in your car that keeps it traveling down the highway at a specific speed, or the gyro sensors in your multirotor that keep it level when you let go of the controls, all closed loop systems rely on PID settings to keep the system in equilibrium. Understanding PID settings, and how they affect the response of closed loop systems, will help out greatly in understanding how to properly tune a multirotor aircraft flight controller.

So what are PID’s? Some people refer to them as “pid” settings, said like the word “kid”, while others will refer to them as 3 individual letters, calling them “P-I-D” settings. While either one works, the letters do mean something. The letter “P” stands for “Proportional Control”, the letter “I” refers to “Integral Control” and the letter “D” stands for “Derivative Control”. Any of the readers that remember taking a calculus class in high school or college are familiar with the terms Proportional, Integral and Derivative, and when talking about PID settings, these terms do have similar meanings. To understand PID settings, we need to discuss each of these terms in brief detail in order to see how each one affects the control of our multirotor aircraft.

In every closed loop control system there are three terms that must be considered: A particular variable, a desired setpoint and an error value. When talking about multirotors, the variable could be the roll angle, with a desired setpoint of 0 degrees, or altitude with a desired setpoint of 50 feet above the ground. The error value only exists if the variable is NOT at the desired setpoint. To clarify this a little more, here is an example. If you are looking at the roll axis in a multirotor, and at one instant in time, the frame is rolled 4 degrees to the right, then the variable is Roll, the desired setpoint is 0 degrees, and the error value is 4 degrees. The purpose of the control loop is to sense the 4 degrees of error in roll, and then tell the system what to do in order to put the multirotor frame back to the desired set-point, which would be 0 degrees, or level, for a hover.

Figure 1
Figure 1

Figure 1 shows a block diagram for the operation of a typical PID closed loop control system. Starting at the left side of the diagram, we have two values that are compared against one another, namely, the desired Setpoint value and the current Output value of the system. Any difference or “error” between these two values is then fed into the 3 sections of the PID control loop for processing. In any PID control loop, the “P” term is related to the present error, the “I” term is related to the accumulation of past errors, and the “D” term is related to prediction of future errors. The corrections that are generated by each of these three sections are then added together to form a total correction value, which is finally fed into the Process section of the control loop.

For a multirotor controller, the process section creates each of the control pulse signals that get sent out to the speed controllers, which then drive the motors in the aircraft. Finally, the props that are attached to the motors provide the corrective forces needed to move the multirotor back to the desired setpoint. The output from the gyro sensors in the multirotor controller are looped back and compared once again to the setpoint value, and the entire process starts all over. This control loop runs continuously, hundreds of times per second, trying to keep the multirotor level when no control input is made, or moving the multirotor to a new attitude whenever a control input is given.

When all three sections of the PID control loop work together, and are properly tuned with respect to one another, you get a system that responds incredibly well to the constantly changing environment that is inherent in multirotor flight. The truly amazing thing is that there are multiple PID loops in a multirotor controller, all running together at the same time! Each axis of flight requires a separate control loop that is driven by the various sensors. Pitch, Roll, Yaw, Altitude, Heading, and Position are all continuously monitored and maintained at some specific set point as directed by the internal sensors, or to new set points as commanded by the pilot. When you stop and think about it, multirotor controllers do a lot of stuff that we simply take for granted!

Now that we have a basic understanding of what each of the P-I-D terms means, and how they relate to one another, let’s look at each one in a little more detail to get a better understanding of how they actually work. As we said earlier, the P part of the PID loop stands for Proportional Control. Like the name suggests, proportional control responds proportionally to the amount of error in the system. If there is a small error in the system, then the feedback loop will generate a small force to correct it back to the desired setpoint. If there is a large error detected, then a large force will be applied to correct the system back to the desired setpoint.

This process can be pictured easily from the standpoint of driving a car down a highway. If you are driving down a straight section of highway and your car is drifting slightly to the right, you will turn the steering wheel just a few degrees to the left to get the car headed back to its desired path. If the car is drifting very quickly to the right, then you would turn the steering wheel a lot more to the left to make the correction. When setting the Proportional part of a PID loop, the idea is to apply the proper amount of correction force to get the system back to the desired setpoint. With the P value set too high, the system will overcorrect and can oscillate back and forth around the desired setpoint, and with too low of a P value, the system will slowly wander towards the setpoint, without ever getting there.

The second part of the PID control loop, the “I” component, is the Integral control. Back from the calculus days you may remember that if you integrate a mathematical formula, you get the area under the curve, which is the accumulation of a value over time. The purpose of the Integral component of the PID control loop is to respond to how the error occurs over time. If something happens to push the system out of equilibrium, and the Proportional part of the control loop is taking too long to respond, the Integral part of the loop will add in more corrective force to bring the system back into equilibrium quicker. If the gain is set too high, too much correction will be applied and the system will oscillate back and forth around the desired set point. If the gain in the Integral portion of the control loop is set too low, it will take a long time for the system to get exactly back to the desired set point, and will always drift slightly one direction or the other.

The third and final part of the PID loop is the Derivative control. If you remember from calculus, if you take the derivative a formula, you get the slope of the curve, which is an indication of how rapidly the value is changing. Likewise the “D” portion of the PID settings, or Derivative Control, responds to how quickly the error value is changing. For our multirotors, if a machine is in a hover, and gets hit by a huge gust of wind that rapidly flips it up on its side, you want the corrective forces to be applied quickly, and in a large enough amount to bring the frame back to a level state. So while the Proportional control gives a lot of corrective force if the frame is displaced a large amount, the Derivative Control will add in additional corrective force if the movement happens quickly. When adjusting the Derivative portion of the PID loop, if you have too much gain, the response will happen too quickly, and correct too much, causing the machine to overshoot past the desired “level” point which can lead to oscillation, and if you do not have enough gain, the response will be sluggish and not respond quickly enough.

With any PID control system, you can tune the performance to meet the type of flying that you are doing and the mass of your specific airframe. When the parameters are set up perfectly, you will always have a small amount over correction in the system that dampens our after 1 or 2 small oscillations. This is great for an aerobatic machine where you want quick, crisp controls. However, for a big camera rig multirotor, you would want a much smoother response and set the gains a little lower.

Figure 2
Figure 2

Figure 2 shows the response characteristics of 4 different amounts of gain in a PID control loop with respect to time. The origin of the graph indicates a current steady state of control, the thin gray line represents the new desired setpoint value, and each of the colored lines represents how the system gets from the current value to a new desired setpoint value with respect to time. Curve A, the red line, shows the response of a system with not enough gain. On this curve it takes a long time for the system to respond to the change, and it never quite gets all the way there. In a multirotor this would create a very sluggish feel to the controls, a machine that wanders around a lot and has very slow response to external forces that disrupt the position of the craft.

Curve B, the green line, represents what is known as Critical Gain. This is the condition that gets you to the new set point as quickly as possible, but without overshooting past the desired setpoint. This would be an ideal condition for a large camera platform multirotor where you want smooth steady response, without any jerkiness in the controls.

Curve C, the blue line, represents a system that has a little too much gain, but is still stable. This system gets you to the new set point as quickly as possible while remaining stable, but with this much gain in your control system, you will actually go a little past the desired set point, which is called overshoot, and then wobble a tiny bit back and forth before settling on the new desired setpoint. This amount of gain would be good for an aerobatic type multirotor, where you want the response to be as quick as possible, and you do not mind if it overshoots and wobbles a tiny bit from time to time.

Finally, Curve D, the orange line, shows a system that has way too much gain and cannot stabilize itself. In this condition, the corrections happen too fast and are too great to ever allow the multirotor to level out. A multirotor that is set up like this will never stabilize, and will do the “Hula Dance” or “Toilet Bowl” maneuvers all the time. In extreme cases the oscillations will actually get greater and greater with each cycle until the machine becomes completely uncontrollable and smashes into the ground. If you ever increase the gain in one of the PID loops and the machines starts oscillating right after takeoff, set it back down immediately! If you continue flying, you will probably regret it.

Most flight controller boards come with some PID values preset in them, which are usually pretty safe and slightly conservative. If you want to fine tune the settings a bit to suit your flying style, then by all means do it. There are a few things you should always remember when setting PID settings though. First, only vary one setting at a time. If you do change more than one setting at a time, you never know which one is actually causing the changes you see. Second, it is best to move in small steps, no more than about 10% of whatever the current value is. For example, if you have an I setting of 0.050 in a control loop, and want to raise it, try 0.055 first, then 0.060, then 0.065 and so on. If you try changing a setting too much at once, you can end up with a completely unstable controller that destroys your multirotor. Third, when you find a set of variables that works well for you, WRITE THEM DOWN! Files get easily overwritten, so if you have a combination that works well, keep a little logbook with your PID settings written down in it so you can refer back to them later on if you need to.

Each and every flight controller board has its own unique set of PID settings, so you will have to do a little research, check the on-line forums, and generally ask around to find good starting points for your PID settings. From there, you can fine tune the values to get the board to meet the requirements of your particular frame and flying style. Hopefully this has helped you understand the mystery of PID settings, and enable you to try fooling around with them yourself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s