View Full Version : Tune a PID for heavens sake.

02-08-2012, 08:33 PM
So I read this (http://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method) and it mentions

The "P" (proportional) gain, Kp is then increased (from zero) until it reaches the ultimate gain Ku, at which the output of the control loop oscillates with a constant amplitude. Ku and the oscillation period Tu are used to set the P, I, and D gains depending on the type of controller used:

My question is at what "set point" do I begin to make it oscillate.

Iwant to control temperature and can span a wide range of temperatures so what temp do I use to get KU?

02-08-2012, 11:23 PM
Will this help? The tuning process is usually a combo of trial-and-error and some rules of thumb.


02-08-2012, 11:25 PM
Check out the manual tuning subsection of the wikipedia article:


Executive summary: Start with all three coefficients at zero, run P up until it oscillates, drop P to about half that value, run I up to improve response time, if it gets unstable run D up to compensate but beware too much D causes overshoot. That's basically how it was done between discovery of the math and all these cheap newfangled computers capable of doing simulations and optimization.

02-09-2012, 12:40 AM
If you're not sure what set point to use, typically when tuning them you start with a set point that is very close to your measured value, then you change the set point to something new (but reasonably realistic) and watch it respond. If it oscillates then you reduce the Kp param and do it again, then tune Kd, then Ki.

Temp controls are interesting in that they generally can't cool, or actively go down like they can heat, or actively go up, meaning that the response parameters may need to be different if your new set point is below the current measurement or above it. PIDs in general are a kind of fiddly black magic.

02-09-2012, 10:20 AM
Another source of tuning tips is lakeshore:


02-09-2012, 02:28 PM
Thanks everyone looks like I have some reading to do. :smile:

02-09-2012, 05:07 PM
Running a simulation should be instructive. MATLAB (expensive) could be used, or Scilab (free).

02-09-2012, 06:34 PM
Octave is an open-source, free alternative to MATLAB that is very MATLAB compatible.


SONIC the Hedgehog
02-09-2012, 06:37 PM
Needed my 200th post, and I wanted to find something interesting and worthwhile to post, so what is trying to be oscillated? But also how would that help with temperature control/measurement?

02-09-2012, 06:42 PM
@Sonic: A classic form of feedback loop:


Read and learn.....very useful.

SONIC the Hedgehog
02-09-2012, 06:44 PM
Wow! I'm an honors student with straight As.....someone needs to slap a little sense into me

SONIC the Hedgehog
02-09-2012, 07:10 PM
So what's happening then is Bits is trying to use the propeller to control a PID so measure a variety of temperatures, and she would be trying to correctly tune the oscillations to accurately track the temperatures?

02-09-2012, 11:22 PM
Sonic, bits is trying to make the Prop control (most likely) a heater to maintain a constant temperature. In a PID loop, each sample cycle you start with an error, which is the difference between the actual temperature and your target. You use your error history to develop three terms, which you multiply by constants to generate this cycle's control output -- a term Proportional to the immediate error, one one based on the Integral which you can think of as the total area under the error curve (with area under the target being negative), and one based on the Derivative or the rate at which the error is changing. Once you get your terms you multiply each by a constant and bam, you get the output at which to set your heater for that sample cycle.

Implementing a PID loop is a lot easier than understanding all the highfalutin math that explains why they work so well; with the right constants they can track a moving target closely and recover quickly without overshoot when there is a sudden change. What Bits is asking is where do you start figuring out what those three multipliers are for the P, I, and D terms. It can be done theoretically if you know stuff like how fast your heater adds energy, how fast heat is lost through insulation, and if there are any delays or other factors. But for many years this "PID tuning" has also been done by seat of the pants estimation, which works even if you don't have a computer and don't understand Calculus.

SONIC the Hedgehog
02-09-2012, 11:49 PM
Yeah I read about the errors. Wikipedia said that after each cycle the PID will try to correct th error as much as it can. Ah I get it, thanks a bunch, and I might have to start my own project like that.

02-10-2012, 03:10 AM
I have a coworker who recently used a commercial PID control board and a few hundred bucks worth of OTS parts to build his father a power steering rig for his speedboat; his father's disabled and has little upper body strength, so L built him a rig where he can steer the boat by twiddling a potentiometer. His math not extending much beyond Algebra I he read the instructions and hand-tuned the PID controller. He sent me a video of his Dad tooling around Mobile Bay looking happy as a clam that he can still go fishing.

SONIC the Hedgehog
02-10-2012, 03:27 AM
That's very innovative! So with simple math routines can be calculated and then the proper results would be achieved? That sounds very simple.

02-10-2012, 03:31 AM
It takes a whole lot of understanding to figure out PID.
It will come to you.
Be patient.

SONIC the Hedgehog
02-10-2012, 03:34 AM
Thank you for your teachings Jedi master, I'm sure Bits will learn a lot.

02-10-2012, 07:59 AM
How to tune P, I, and D in a PID loop? First start with things they effect directly, like disturbance rejection and system noise. But even that is getting ahead of myself, the zeroith thing to do is to put real meaningful units on every quantity in the servo loop. For a thermal system Kp will have units of [watts/degree] Kd will have units of [watts/ (degree/second) ] Ki will have units of [watts / (degree-second)] and so on. It just so happens that Kp has the units of thermal conductance and acts EXACTLY like you had a physical thermal bridge to a heat source at the set-point temperature. Similarly simplifying the units of Kd get you [Joule/degree] which has the units of heat capacity. Ki doesn't really have a physical equivalent, but engineering units still make it's effects easier to quantify.

Cranking Kp till the system oscillates is a good starting point, but you can likely further double Kp if you add enough Kd. (with mechanical positioning systems often Kp can be quadrupled or more this way) What's the cost of high Kp and Kd? System noise and idle power consumption. I.e. the derivative term acts as a high-pass filter so it has it's largest effect at high frequencies where the signal to noise ratio is often low. Note: to use a large Kp you NEED a similarly large Kd term otherwise the system will have a high frequency resonance where it's easy to disturb the system. (in the case of a thermal system, the physics provides a lot of Kd on it's own, so this term can often be dropped) What's the gain with high values of Kp? Rejection of external disturbance at middle frequencies and the ability to use larger values of Ki for enhanced DC and low frequency disturbance rejection. Indirectly, a large Kp also accelerates command response. (with just Kp and the system model it's also pretty easy to calculate how much an external heat source will effect the temperature of the system, aka disturbance rejection) Also, Ki gain does NOT like infeasible commands. If you give the thermal system a step temperature command, Ki WILL cause overshoot. If you instead use a smooth ramp command, Ki won't cause overshoot but will reduce tracking error during and after the ramp. After all this, you're pretty free to choose Kp, Ki, and Kd. It's common for useful stable values of these gains to cover several orders of magnitude and because of this, changes to gains smaller than 2x are rarely worth the trouble.

If command response is your goal it's better to use a system model to construct a FeedForward controller. (aka an open-loop controller run in parallel with the PID loop) Something as simple as multiplying the rate of command temperature change by the heat capacity of the thermal system can really speed up command response. A FeedForward controller does requires feasible commands, aka commands that stay within the physically limited capabilities of your system. I.e. no temperature steps, as these require many Joules of energy to be added instantly, something not physically practical.


P.S. The accumulation and difference terms used by computers to approximate integration and differentiation are approximations. When operating near the sample frequency of the the servo loop, the error in these approximations becomes significant, and will show up in the dynamics of the system.