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.
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.