Let’s talk about PID controllers: What are they? And why do we need them?

Hamza
6 min readFeb 7, 2016

--

Imagine you’re in your car on a long deserted road and you suddenly need to some note-taking or do something that requires both your hands. For this to happen successfully, your car needs to move at a constant speed and you need to take your hands off the steering wheel. The normal way to do this is to turn on the cruise controller in your car but imagine your car doesn’t have one, how do you achieve this?

You know what, let’s build our own basic cruise controller.
Intuitively, the first algorithm that comes to mind is this

v = current speed of car
x = required speed

start loop:
if v greater than x, reduce v (i.e. decelerate)
else if v less than x, increase v (i.e. accelerate)
and probably
if v is equal to x, do nothing

This works perfectly. When we code this up and create a visual representation of our movement, this is what we get.

Cruise Controller: First attempt, no PID

When it gets to 20 speed units, it oscillates a little but the oscillations are so small you won’t even notice.

Good right? :)

Nah. Not quite. I’ll explain

This is what we’re doing to our car.

Here’s a clearer picture

At 20 speed units, we had to step on the pedal sharply within very short intervals to maintain it, and this can only lead to one thing, damage of our actuators. More actually, spilling your drink or being unable to write whatever you wanted to or losing a tooth. We obviously do not want that. Another reason this is a terrible idea is, who drives this way anyway?.

How do we solve this?

The simple answer in our case is PID controllers.

PID controllers are widely used, low level controllers used in systems that require some form of control. PID stands for Proportional, Integral and Differential controllers. It consists of these three parts which contribute to achieving optimal control of our systems.

A more bookish definition (from Wikipedia) goes thus

A proportional–integral–derivative controller (PID controller) is a control loop feedback mechanism (controller) commonly used in industrial control systems. A PID controller continuously calculates an error value as the difference between a measured process variable and a desired setpoint.

The P controller

What a P controller does is that it applies measured input proportional to the error (i.e. deviation of the actual output from required output) to the system. That’s sounds really decent. If we’re really behind our required speed we apply more force to our pedal and as we draw closer to the required speed we gradually reduce the force exerted. A P controller is defined mathematically as:

P controller

P out = desired output, Kp = proportionality gain constant, e = error term

In pseudo code, our system looks like this:

//x = required speed
//cs = current speed
//e = error (i.e. deviation from the required speed)
//f= Force applied to pedal
//K = proportionality gain constant

start loop
e = x - cs
f = -Ke

I coded this up and created a graphical representation of our car’s movement in space and we got this…

P only controller

This looks really lovely. Our actuators aren’t unnecessarily stressed and the car is pretty stable. No oscillations. However, there’s a little problem. We haven’t been able to attain the exact desired speed. Why? There’s this little thing called drag, or air resistance. Air resistance is this external opposing force which increases the faster we go and vice versa. It more or less hinders us from reaching our desired speed. So what do we do about this? We introduce the I term.

The PI controller

The PI controller consists of a Proportional part and an Integral part. The Integral part continuously gathers the errors in our system over time and feeds a force that makes up for it back into the system.

The Integral term

Since we’re dealing with discrete time, the integral term is relatively simple to compute in code. It’s simply a sum of the (current speed — desired speed)s over time. In pseudo code, this controller will look somewhat like this

//x = required speed
//cs = current speed
//e = error (i.e. deviation from the required speed)
//ae = accumulated error
//f= Force applied to pedal
//Kp = proportionality gain constant
//KI = integral gain constant

start loop
e = x - cs
ae = ae + e

f = Kp * e + KI * ae

This improves our cruise controller to something like this

PI Controller in Action

Voila! The PI Controller does the job. We begin by accelerating nice and smooth till we get to our goal velocity. No oscillations, no hands, quite stable. PI controllers alone are sufficient in most control problems.

The PID Controller

In certain systems, using a P — only controller or a PI doesn’t help achieve the needed tracking or stability. In some cases, they produce an undesirable overshoot which needs to eliminated. As explained earlier, the I part of a controller has a contributory effect on the magnitude of the P part but the D part has a counteractive effect on it making it really good for controlling overshoots. It however increases the settling time.

The D part stands for the “derivative term” and is calculated by multiplying the rate of change of the error term by Kd, the derivative gain.

The derivative term

The expression below represents a PID controller

A PID controller

Parameter Optimization

The last thing I’ll be talking about is parameter optimization. While we know how to make PID controllers, selecting good values for our gains isn’t so trivial. It could take series of trials with different values to achieve the kind of output one requires from a controller. Someone once termed the act of picking these parameters “an art”. There are however a few systematic ways. The only one I’ve tried is Twiddle also known as coordinate ascent. There are other methods like Ziegler–Nichols and Tyreus Luyben and they all have their pros and cons.

In conclusion, I think it’s important to note that there are still many more modifications that can be done to PID controllers to attain design objectives. There are also other types of controllers.

If this interests you, go ahead and do some more research :) and yes, hit Recommend

--

--