# Visualizing PID loops

This post is an attempt to add a visual explanation of PID loops. I had read Michael King's excellent post , but I wasn't sure how to design loops to do specific things.

To me PID loops seemed a bit of a black box, so I decided to graph them out in order to better understand the inputs and output of the equations. They are actually quite simple, but I've never seen a good visual explanation. I may have some details wrong, so if you have additions, please let me know and I will update the graphs.

For this example I will show how the steering works on my plane using the rudder. The goal is to connect the bearingError (the difference between the plane bearing and the target bearing) to the rudder. The trick is that you can't just create a direct relationship, or your plane will death spiral. You need to connect the bearingError --> IR sensor --> rudder. That way the plane is always stabilized while it's turning.

Here is a diagram of the bearing error. I'm limiting the error to ±30° because I want to limit how fast my plan can turn. More on that later.

IR sensors:

The diagram below explains the input you can expect from the sensors. On a hot day you could expect the full range of 0-1023 from them. The value 511 means that the opposing cameras can see exactly the same temperature. This usually means they are looking at the horizon and hopefully you've placed them onto your plane in a perfectly flat orientation. On average days we need to scale the output to the full range, then we offset it by 511 so that we get a positive and negative value for pitch and roll.

This graph shows the relationship of the IR sensors output to the rudder. You can see the trough created by the sensor values. The plane can actually bounce around in this trough causing the drunk driver effect.

In this diagram, the sensor output is scaled to .65. This works for my plane, and keeps oscillations at a minimum.

Connecting the bearing error to the IR sensors.
In order to stabilize while turning we need to connect the planes bearing to an IR value which we will use as an offset of the actual value. In a sense we are moving the trough to the left or right based on how much we need to turn. I guessed my plane can only safely turn at an angle defined by the IR sensors as ±180. I also guessed I could ignore bearing errors larger than ±30° as my plane can only turn so fast. If you graph the values you'll see 30° bearing = 180IR and -30 = 180IR.

Notice that the proportional term is a lever with a direct output. There is no delay. If you want to have the response be delayed, use the Integrator. To calculate the value for the integrator, take the proportional value and divide it by the number of seconds you want the change to take. For example, if I want change to take 5 seconds, the I term is -6/5 or -1.2

If you want some rapid response, and some delayed response, pick a ratio for each, do the math separately and then sum the answer. For example:
Output 60IR for proportional and 120IR for the integral. This will cause the rudder to react to 1/3 of the input right away and 2/3 will be applied smoothly over a 5 second period.

A throttle may want all the input as an integral since the smooth transition can keep your plane from torqing sideways.

When you take the IR offset value generated by the above equation and apply it to the stabilization you'll see the equilibrium piont has shifted to the right.

The plane will fight to hold that angle by moving the rudder back and forth. If the plane is holding the turn, note that rudder is straight.

I know this is pretty basic stuff, but I could not wrap my head around it until I made these graphs.
Jason

Views: 834

Developer
Comment by Ryan Beall on November 9, 2009 at 7:49pm
I frequent wikipedia their visualizion is a bit more engineering based but grabs all three concepts really well.
http://en.wikipedia.org/wiki/PID_controller

I hate when people just shove you off onto wikipedia but the plots really help. If you get muddled in the explinations just look at the grapsh. The verbage is techy I guess but the plots really get you the feel for gain levels.

IE: I would never have integral gain set as high as you have described above. It's just not needed for aircraft. A good example is battery volts vs throttle setting. To maintain altitude and airspeed it requires a given throttle setting. Well as the battery dies this % obviously needs to rise. Well a integrator that is twice as strong as the proportional on something moveing as slowly as batt-volts would oscilate this system like crazy! You would need an equal amount of derivative to cancel it out and then you kinda just took away everything you tried to create. Like you said batt-volts changes in the timespan of minutes so keep all the integral stuff really slow. Integrals will kill pid's if you arn't careful.....never more than half the P is my recomendation but like I posted in http://diydrones.com/forum/topics/705844:Topic:46763 for 90% of the controllers required to navigate an aircraft you need no D or I. Good luck!

Developer
Comment by Ryan Beall on November 9, 2009 at 7:58pm
I mis read your gain stuff looks like you are ok with the numbers. However the integrator by definition leads the error....the derivative lags. Does that make sense? It is kinda hard to picture because its counter intuitive on the back side of the over shoot it makes you think its lagging beings the integral error is winding down. But it always leads and derivative always lags. It is all about "phase"

http://www.expertune.com/tutor.html

Developer
Comment by Jason Short on November 9, 2009 at 8:02pm
yeah, I just made that example up. My turning algorithm is all proportional right now, but the throttle I helps me a lot. If I use proportional on the throttle the plane can jerk hard right as the motor spins up. I'm playing now with an integrator that goes away from zero slowly, and goes towards zero quickly in an attempt to get the best of both worlds. I've also thought of pairing two integrators with different rates to act like PI, but smoother.

What the plots on Wikipedia left out for me is how to design the loops; what goes in, what goes out, and where the P comes from.
Jason

Developer
Comment by Ryan Beall on November 9, 2009 at 8:21pm
Ahh yeah, they are all what's called "step response" so its an instantaneous max error state response output. I havn't done any rudder plane control laws but you will find it intersting (depending up your implemenation) how most of the gains for P are 1.0;

IE:
kp_roll = 1.0
kp_pitch = 1.0
(assuming your error terms are in deg and you have average rc servo travels)
Comment by Ty on November 11, 2009 at 8:58am
This is a fantastic write-up. I don't have any engineering background, so it's nice to follow someone else who is really exploring the basic stuff, and writing their discoveries and epiphanies.

Comment

Join DIY Drones

1

2

3

4

5

6

## Groups

155 members

162 members

43 members

6 members

• ### PIXHAWK

1108 members

Season Two of the Trust Time Trial (T3) Contest
A list of all T3 contests is here. The current round, the Vertical Horizontal one, is here