PID controller

A few weeks ago I sat in on an online Q&A seminar run by one of my colleagues (and course co-co-cordinater!), Daniel Markl. One of the questions that came up was on ‘PID controllers’. PID controllers work using a 3 part equation that connects ‘error’ to an ‘input’ value, and they allow automated systems to reach a required ‘setpoint’. For example, a drone needs to get to a certain set height (say 200 metres), how far away the drone is from that height is the error (so if the drone is at 50 metres the error is 150). The input value is what speed the drone’s rotors spin at and so need some sort of maths equation that allows the error to control rotor speed so the drone get to, and stays, at the right height. What starts off as simple idea, gets more complicated when we realise the drone needs the rotors spinning just to stay stationary in the air. The same is true of industrial heaters, or flow regulators.

A PID controller can compensate for these complications using this mathematical equation:


If you did (and remember!) high school maths, the equation should be ‘interpretable’, but I’ll explain the terms below.

When Daniel talked the class through PID controllers, I was curious because I couldn’t immediate see how they would work, even although I understood the equations. Anyway, life moves on and I quickly forgot about it (we’d just moved into the Covid UK lockdown so I had a lot on my mind!). However, youtube/google must have been listening (!) because I started getting youtube videos about PID controllers (like this one) in my feed! Aaannndddd…I just got so curious I had to build one!

Over the last few months I’ve been using Scratch to build small gaming simulations and run simple process modeling. Scratch is simple to learn, but can also be run through a browser so unlike more sophisticated languages (like python) it doesn’t need a special install. It seemed the ideal place to build a virtual drone (and cheaper too!).

So, my drone stays at a steady height if the rotation of the rotors is 100 rpm (revolutions per minute). If the rotors go faster the drone climbs, if they go slower the drone falls. In the equation above ‘rpm (t)’ is the speed of the rotors at a certain point in time, E(t) is the error (that’s the difference between the drones height at that time and the height I want it at). The first term on the right hand side of the equation is proportional to the error (the P term), the second term is related to the integral of the error over time and the third term is related to the differential of the error. There’s a more detailed explanation in the video link above.

The Scratch simulation is below. Click the green flag to get going, and the red button to stop. A ‘open in new window’ link is here.

I built my ‘virtual drone’ with a ‘dashboard’ so I could track the drones progress as a graph, I could change the height I wanted the drone to fly to and I could change the Kp, Ki and Kd settings to see what would happen. I wanted to be able to pause the simulation, send the drone back to its starting point, and delete all the previous tracks. I also wanted to be able to see a readout of height and rotor speed. If the drone reaches the right hand side of the window, it chances colour (slightly) and comes back out from the left hand side. The ‘back to base’ button also changes the drone’s colour slightly to help distinguish between different traces.


So, if your interested in PID controllers and don’t want to lose a valuable drone (!), ‘njoy.

A wee bit of history

Any power source needs controlling. If an attentive human is the controller than things usually work out OK. (When we drive along the road we don’t usually see cars going extremely slow or fast; the human drivers regulate the speeds.) However, the industrial revolution of the 18th and 19th centuries gave controllers three problems. Firstly, presence: now that engines needed to run 24/7 a constant controlling presence was needed that had to be always attentive, knowledgeable and oblivious to external influence to push the engine too hard. Secondly, speed: making alternations to the power being applied to a system may need to be done faster, or at smaller time intervals, than a person can cope with. Thirdly, mechanisation and the steam engine increased the raw power of the systems, thus making impact of accidents catastrophic. How many times did you send the PID drone crashing to the ground?

Early steam pioneers, like local boy James Watt, refined centrifugal governors as controllers. These were essentially proportional controllers, and so designing and building them for practical use was considered a highly skilled occupation. However, they could be prone to ‘self oscillation’, where the ‘high/low’ speeds of the engine synchronized with the ‘high/low’ stream input to give self-reinforcing feedback signal with would lead to increasingly strong vibrations in the engine. Think of the high pitch whine you get on some sound systems, and image the same idea with steam and a boiler: it’s not going to end well!).

The Scottish mathematician and physicist, James Clerk Maxwell, found time to study this problem while studying thermodynamics and kinetics, developing colour photography, unifying electric and magnetic theories to identify light as electromagnetic, predict the rest of the EM spectrum and lay the groundwork for Einstein! Maxwell’s paper ‘On Governors’, lead to the development of controllers which included mechanical equivalents of the integral term (the ‘I’ in PID), and is considered to that started the field of ‘control theory‘. There’s very little information about Maxwell’s work on control theory out there, but the Royal Society of Edinburgh ran a symposium celebrating the 150th anniversary of Maxwell’s equations, and a summary of his work on governors is presented here.

Categories: Tags: , , ,

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s