Chapter 11. PIDs

Hidden between mode and mix we find the proportional–integral–derivative (PID) controller. On the one hand, it’s just a little smoothing filter applied to the axis commands before sending them to the motors. On the other hand, as a drone maker and pilot, you may well spend more time dealing with PIDs than any other component of the control system—not changing the algorithm, since that comes in but a few standard flavors, but tuning the PID’s parameters. And few things will make as much difference in how your drones fly!

The Algorithm

As with gyroscopic stabilization, at least some of the credit for the PID algorithm goes to the Sperry family. In this case it was Elmer Sperry, father of Lawrence, who built the first autopilot for airplanes; he came up with the idea while working on an autopilot for Navy ships. He noticed that the helmsmen would compensate for not just the current course error, but the immediate history of that error and any sudden changes in the error. He was implementing this with pneumatic systems, but the principle remains the same to this day.

P—Proportional

When the person at the helm moves the rudder further as the ship gets further off course—that is, if the rudder position is proportional to the course error—then that person at the helm is acting as the “P,” or proportional, part of the algorithm. Similarly, if you press the accelerator pedal of your car harder in proportion to how much your speed is below the target, you are implementing the “P” part of a PID controller.

The “P” term controls the overall force the control system will use to achieve the desired output. Not enough “P,” and your drone won’t have the power to respond to your control inputs. The controls will feel mushy. Too much “P,” and your drone will overreact and possibly oscillate wildly.

I—Integral

So now you go up a hill in your car and slow down a bit, making you press the pedal harder. Perhaps this is enough to hold your new, slower speed, but there is a persistent error of a couple of miles per hour. When you give it a bit more gas to fix this ongoing error, you are now adding the “I,” or integral, part of the algorithm.

A mathematician might describe an integral as the area under a curve. In Figure 11-1, the blue line represents the error and the red line the area under this curve, or the sum of past errors. As long as the error stays positive (the green areas), then the larger the area gets, the higher the red line goes. When the blue line goes below the axis we say the resulting area is negative and subtracts from the total. When we add up all the areas we get the sum of all past error.

Figure 11-1. The red line is the sum of all past values of the blue line

The “I” term corrects for long-term error to make sure that the output finally reaches the desired value. This also causes the output to reach the desired value faster. Increasing “I” makes your drone feel snappier, responding quicker to control inputs, but too much can cause overshoot and oscillation, much like too much “P.”

D—Derivative

If the integral term is about the long-term error, the derivative or “D” term is about the instantaneous response. In the moment when you first hit that hill and your car begins to slow down, you might just give a bit of extra gas to keep it from slowing down too much while you find the right pedal position to maintain your speed. The “D” term responds to the change in the error, not the current size of the error.

A mathematician would probably describe the derivative of a curve as the slope at a point. If we plot the speed of your car versus time, then as you start up the hill, the curve will slope downward. Similarly, if you don’t take your foot off the gas quickly enough when the road levels out, your speed might suddenly increase and the plot of your speed will slope upward. In Figure 11-2, the red line is above zero when the blue line is rising and below zero when it falls.

Figure 11-2. The “D” term depends on the slope of the error

The “D” term makes the immediate response faster and can help prevent overshoot and damp out oscillations.

Implementation

For an algorithm that has so many uses and that gets so much academic attention, implementing it is pretty easy. There are a couple of fine points between theory and practice even here, though.

In theory, the integral or sum of all past errors will just keep getting larger and larger if it is not corrected completely. In practice, it rarely works out well to let a variable increase without limit, so actual PID implementations usually include a limit on possible runaway integrals, called integral windup. We see this in the following code, where constrain() is used in the calculation of pid_isum:

// compute PID for each axis  
for (int i=0; i<MYPIDITEMS; i++)
{
  e = axisCmd[i];
  pid_isum[i] = constrain(pid_isum[i] + (e * deltat), -1, 1); // constrain to stop windup
  pid_ddt[i] = (e - prevE[i]) / deltat;
  axisCmdPID[i] = (pid_p[i] * e) + (pid_i[i] * pid_isum[i]) + (pid_d[i] * pid_ddt[i]);
  prevE[i] = e;
}

Tuning

Tuning a PID can be as simple as turning a knob on your transmitter until your drone flies better, or as complex as a full functional analysis of the system followed by an algorithmic calculation of the tuning parameters. We will use an iterative process of changing a value (perhaps with your smartphone) and doing short flight tests until a satisfactory result is obtained.

Theory

The following process assumes a starting point with “P” set to some low value, 0.5 or less, and “I” and “D” set to 0. At each step, change the parameter in question using the Bluetooth connection to your computer or smartphone, then take a short flight to test the results. A few seconds is usually enough.

  1. Increase “P” until oscillations begin. Reduce to half this amount.
  2. Increase “I” if needed to make control response quicker. If oscillations occur, back off some and leave it.
  3. Increase “D” if needed to make response to disturbances quicker.
  4. As “I” and “D” are increased, “P” may need to be decreased some.

This will all make more sense with an example. Let’s say the setpoint represents the desired roll angle of our drone, shown in Figures 11-3 through 11-5 by a blue line. It starts out at the center, moves quickly to one extreme, then moves more slowly to the other extreme and suddenly back. The green lines show the output of the PID controller and the red lines show the resulting roll angle of our hypothetical drone.

In Figure 11-3, we start with “P” set to 0.5, which is a little low. The resulting angle does not move as far as the setpoint. As we increase “P” to 1, the result gets closer to the setpoint, and the little oscillations are starting to get very noticeable. By the time we have “P” set to 2, the oscillations are too much, so according to our procedure, we back off and set it to 1.

Figure 11-3. Even with “P” turned up until the oscillations get bad, the output still does not reach the setpoint

A drone with too little “P” just won’t have the maneuvering power to respond to your control inputs or disturbances from the wind. The word “wobble” comes to mind. With too much “P,” we get oscillations. This might seem a lot like the low-“P” wobble, but it will usually be faster. The drone drives itself back and forth instead of just flopping around.

In Figure 11-4, we add a new, cyan-colored line to show how the integral term adds up. In the top graph, we leave “I” at 0. You can see that the integral just keeps getting bigger or smaller as long as we hold position, since we are doing nothing to correct the error. In the middle graph, we set “I” to 0.5, which just corrects out the error, but also adds to the oscillations. With “I” set to 1, the oscillations are increasing, not decreasing, so we must back off a bit, according to our process. We will go forward with “I” set 0.8.

Figure 11-4. Increasing I brings the output up to the setpoint but adds more oscillation

Setting “I” to 0 works fine sometimes, so it’s hard to say what too little “I” looks like, except that a little more makes everything a little snappier. Too much “I” creates obvious hard, fast oscillations, much the same as with “P.”

At this point, the oscillations are too high to tolerate, but just watch what the “D” term can do! After trying several values, a “D” of around 1.4 seems to most perfectly damp out the oscillations without slowing down the response or creating oscillations of its own. In the third graph we can see that pushing “D” up to 3 creates unacceptable ringing in the output.

Figure 11-5. Increasing “D” can damp out the oscillations from “P” and “I,” or add more of its own

Too little “D” does not make a signature of its own; it simply allows the “P” and “I,” oscillations to continue.

Set right, “D” damps these oscillations out. Set “D” too high and we get oscillations again, but often faster those caused by “P” or “I.” They may, in fact, be too fast to see directly as many drone control systems run PID loops 500 times a second, but the drone will become unflyable nonetheless.

The final result looks quite flyable. At each fast setpoint change, the PID controller gives an immediate strong response, followed by a strong response in the other direction to stop the motion as the setpoint is reached. There is a single wobble after that as the system locks on to the new position, and then it stays perfectly flat.

Practice

Getting your PIDs tuned right can be frustrating, especially when more than one parameter needs changing and the resulting oscillations keep crashing your drone. But most people need never find themselves in this situation. In fact, in the most common case—trying to make a working drone fly a little better—turning PIDs can be both easy and satisfying.

If your drone flies well now but you would like it to be either smoother or quicker in its control response, then the PID adjustments should be easy. Actually, I would always start by adjusting the RC parameters, but assuming you have done that, you can almost always just move “P,” “I,” and “D” up and down in equal proportion. If the current settings are 1, 0.1, and 0.01, then adjusting up by 10% gives 1.1, 0.11, and 0.011. Move up or down in small increments and you should be able to tell that it’s getting worse before things get so bad as to cause crashes.

If you’ve just built a brand new drone, however, or made a major modification like changing motors or props, then you might have to make more drastic changes. If possible, find someone else who has built a drone using similar components and start with their values. I give working values for all the projects in this book, for example. If you can’t manage that, the default values in the popular flight control systems are often better than starting with “D” and “I” at 0. Some flight control systems, like ArduCopter, allow you to tune parameters in flight using your RC radio, which can make the process much faster; some can even perform an autotune and get to workable settings by themselves.

If you have a completely new codebase or otherwise are starting from scratch, then some kind of test rig will allow you to debug the code without crashing and breaking something. Figure 11-6 shows the test rig I used to debug the PID algorithm in the Visible Drone’s code.

The PID algorithm is one of the most useful and important algorithms in control systems engineering. For me, understanding PIDs really starts to unlock the mystery of how things work. There really is more going on than meets the eye, but it is a process that we can understand and use to make our machines work better. Your drone will respond better. Your robot arm will move more smoothly. Your coffee pot will hold a more precise temperature. Not bad for less than 10 lines of code!

Figure 11-6. This rig allows a new PID implementation to be tested without crashes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.184.214