PID Tuning in Betaflight 4.2

So, you've got a drone.  You're flying it all over the place, having a great time.  You hook it up to your computer and start poking around in Betaflight Configurator.  You started with small configuration adjustments - mapping channels, setting up the buzzer, configuring serial ports.  You stumble onto the PID tuning tab.  There's a bunch of numbers, sliders, and ratios.  Things aren't particularly clear, but you know you want to get the best performance out of your quad.  How does all this stuff work?

Maybe you've read the tuning notes.  These are a goldmine for in-depth info on how to get great performance.  The thing is, these notes are pretty technical and it's hard to get a grasp on what exactly is going on.  You've probably stumbled across the PID tuning guide.  Again, fairly technical.  You've probably even watched a handful of videos with folks walking through the blackbox logging process, analyzing gyro traces and demonstrating what the quad flies like when things are all wrong.

I've been through all this stuff as well, and things were only making a little bit of sense.  So, I figured I'd try to talk through what I've learned.  Here goes.

What the heck are PIDs?

Lots of electrical systems have feedback loops.  Think about your thermostat.  You program your thermostat to keep the room at a certain temperature.  Your thermostat has a temperature sensor in it to know when to turn on the heater.  When the temperature sensor reads a temperature below where you set it, the heater turns on.  Once the appropriate temperature is reached, the heater turns off.  This process is called setpoint tracking.  You set the temperature you want the system to hold, and it turns the heater on and off depending on whether it's at or below the temperature you've set.

PIDs adjust the strength of the response based on how far from setpoint you are.  In thermostat terms, this would be like the heater blasting hotter air through the vents if it's measuring much colder than setpoint, or blowing only slightly warm air if you're only off by a few degrees.  Say it's winter, there's snow outside, you've left for some errands.  Unfortunately, someone left a window open and let out all the warm air.  When you come home, it's damn near freezing inside.  The thermostat is set to 78 degrees, so it kicks on.  In PID terms, your sensor is reporting that it's far and away from setpoint.  This means that your PID error is very high.  If the furnace was using a PID controller to adjust the temperature of the heated air, it would blast the fires of hell through your vents in order to reduce that error and approach setpoint as quickly as possible.

Gyro and Setpoint

Relating this analogy to drones, setpoint is where you move the sticks on your radio and the gyro is the sensor that measures how far from setpoint you are.  This is the flight controller's feedback loop.  If I quickly roll left on the stick, the drone is going to roll left as quickly as it can.  It's likely going to lag behind setpoint for any number of reasons.  This will introduce PID error.  The PID loop will compensate for that error by adjusting the motor output until setpoint is reached.  Once the setpoint and the gyro match the PIDs have nothing left to do.

Gyro setpoint tracking - blackbox log traces

In the above image, we can see the setpoint (green) is showing a sharp roll left.  The gyro (purple) is tracking it fairly closely, but it lags behind setpoint at the beginning and the end of the stick movements.  This causes the P (red) to spike up until setpoint is reached.  The magnitude of the response is directly proportional to the amount of PID error - the further the gyro is from setpoint, the harder P will push to correct it.  As the gyro gets closer to setpoint, the P response drops.  Once gyro matches setpoint, P returns to zero.

In an ideal world, with a perfectly tuned quad, the gyro will exactly match setpoint at all times.  If I tell it to roll left, it will roll left at precisely the time that I tell it to.  If I stop rolling left, it will stop rolling at precisely the time that I tell it to.  In this ideal circumstance, a blackbox log would show the gyro and setpoint traces directly on top of each other.  Note that this is an ideal and not realistic - there's always latency and outside forces requiring correction.

Proportional, Integral, and Derivative

These three terms work together to correct the difference between setpoint and gyro.  P term responds proportionally based on error.  The further you are from setpoint, the stronger the correction.  If we're talking in close proximity and you say you can't hear me, I'll talk a little louder.  If you're across the room and you say you can't hear me, I'll yell.

[work in progress]