# PID control

I made a Labview PID controller!

I know, exciting stuff, right! It must be, look at all the exclamation marks I keep using!

For anyone not convinced of how amazingly exciting this is, I’ll take a quick step back and explain why or what PID is.

If you have, for example, two people trying to hang a painting. One person (we’ll call him Colin) stands back from the painting and judges the position and tells the second person (we’ll call her Laura) which way to move the painting on the wall. To start with the painting is waaay off to one side, it’s practically half out of the living room window. So Colin tells Laura to move it a long way to the left. But given how far the painting was off to start with, Laura overshoots to the right. Seeing this overshoot Colin instructs Laura to move it back to the left but less than before. Again Laura overshoots but is now even closer to the central position.

This overshooting continues for a while but with each movement Colin and Laura get closer to the central position.

This process of slowly narrowing down the correct position is the human equivalent of PID, it seems obviously and fairly basic but it’s actually quite difficult to tell a computer/electronic system how to do. For starters, the example is more nuanced that you might think. Colin is modifying the instructions given to Laura not just based on the paintings current position to the centre point but also based on how much Laura overcorrected from last time. These two values are called present and past errors and Colin uses those to estimate Laura’s future error so he knows whether to say “move it a 10cm to the right” or “move it 30cm right” in order to move the 15cm required.

That’s what PID does – it’s a mathematical way of assessing a control systems present, past and future error to estimate what value would be best to feedback to the motor.

Thankfully some very smart people have already worked out how to translate this seemingly basic concept into the following equation.

$u(t) = MV(t) = K_{p}e(t) + K_{i}\int_{0}^{t} e(\tau)d\tau + K_{d}\frac{d}{dt}e(t)$

You don’t need to understand the whole equation, or indeed any of it as I’ve transcribed it to Labview – which, while looking a lot prettier, probably isn’t any easier to understand!

The important bits are the values denoted by $K_{p}$, $K_{i}$ and $K_{d}$. These are the tuning parts of the equation where you give it arbitrary values in order to make the equation work for your system.

These values have the effect of tuning Colin and Laura to make sure they don’t overcorrect by too much. For example, if Colin was only able to give commands in the scale of km, then hanging the painting might get a whole lot harder.

PID controllers are everywhere, from lasers to ship navigation controls and there is some suggestion that our brains use a form of PID for our own fine motor control. Having now watched two children learn basic motor control, I have to say that anecdotally this kind of correction mechanism seems very plausible.

Labview code – PIDidly.vi