# What I learned from building my second quadcopter — an in depth breakdown of PID controllers

Wilbur, my personal UAV project’s been one of the biggest challenges I’ve had this year. Last month I released an update of what the process was like building the manual mode and what I learned from it, but this time I wanted to so something a little bit different. Going in I had no idea what controllers I would use, why some are better than others or any general direction to start the project in, and learning that was one of the most time consuming parts of the project overall. In this update, I’m going to give a breakdown of the PID algorithms I used, why they’re beneficial to my application and why they’re so important to learn for anyone doing anything with drones.

# What’s a PID loop again?

In another article, I briefly went over the principles of a PID controller and why it’s so commonly used, but a basic overview is that it’s a controller that uses the error between the bot’s current and desired state to determine the output to the motors.

A PID controller’s made of 3 parts: the proportional term, the integral term and the derivative term, which all get added together to produce a single output. Every second the PID gets input from the sensors and using the desired position set by whatever control method the drone uses(transmitter, CV, etc), finds the difference(error) between it and that desired position. That error value gets fed into all 3 equations and eventually multiplied by each’s respective constants to get the output.

# We don’t really need the derivative

Once the error value is gained, it’s passed to a proportional, integral and derivative equation. The easiest way to think of a derivative is just as a prediction based on past values of what the error’s potentially going to be in the future, usually gotten by finding the slope(rate of change) of the pas error values which would be the distribution of error over the distribution of the time elapsed.

Put simply, the derivative finds out how quickly the error is being corrected. The problem is that since the derivative dramatically changes with any change in y(as does the slope), once the output from the PI equations make any changes to reduce error the derivative output skyrockets and makes the entire quad unstable. Usually this is referred to as “derivative kick”, and while there are a lot of ways to prevent it from happening, the easiest would be just to remove the derivative term from the controller entirely. This article’s mainly going to be focused on how PI(the most common one) controllers work and how it applies to UAV tech.

# The easy way to do it

Let’s say we have a drone on the ground and we want to get it up to 5 metres, hence an error value of 5. The easiest way to get to that and what some of the earliest PIDs used was a proportional term, which only uses the error for that specific period in time. Once it has that error or e(t), it still needs to send it as a measurable output for the controller so it multiplies the error by the proportional gain, which is just how much the control variable(in this case, the esc pulse output in ms) would need to change to bring it from 0–100% output, giving us an output value the escs can read and use.

But there is a major and probably obvious issue with this. Let’s say the drone did reach its 5m destination without any issues, and the sensors kept measuring the error over time. Since it’s gotten to its desired state, the error value would be 0 so the entire proportional gain would be 0, hence the escs would get no output value and the drone would drop to the ground. A P-only controller is bound to have major stability and oscillation issues(usually called offset), which is why we need the integral term.

# The slightly less easy way to do it

Imagine now that instead of just using the error at that time, the controller used all previous values and noticed the slowly declining error and adjusted output accordingly, reducing oscillations up and down. This is essentially the goal of an integral term. If you remember your high school calculus, integrals are used to find the continuous sum of the outputs of a function(in this case the error) over x(time) using infinitesimals.

Now the drone’s reached 0 error, it’s begun falling but instead on just relying on P gain and falling into a loop, the P gain’s added to the integral gain, which is just the integral sum(output of integral function) multiplied by the integral constant(range of values for output variable). Suddenly, every single oscillation gets smaller and smaller as the integral sum gets smaller over time because of how often the error’s being lowered. It does add another level of complexity to the system, yes, but it also fixes the core problem with P controllers and makes it much more immediate in executing a command.

# What this means for me

The first major usage of a PID was in 1922 when a Russian engineer named Nicolas Minorsky used it to control a steam engine, a theory he had developed from looking at the natural behaviours of the ship’s helmsman. Ever since then it’s been used in everything from planes to industrial equipment to self-driving cars, and is still being perfected today. In any kind of robotics, PID loops are so useful because they represent 100 years of progress and effort from some of the greatest minds in history. Looking forward, I’m planning on using PID loops to navigate my drone fully autonomously with computer vision, and if this project’s taught me anything, as I said in previous articles it’s that just like a PID loop, the process of analyzing and pivoting based on your mistakes is so crucial to anything you do.