Tuning controller gains can be difficult, what general strategies work well to get a stable system that converges to the right solution?
For small, low torque motors with little or no gearing, one procedure you can use to get a good baseline tune is to probe it's response to a disturbance.
To tune a PID use the following steps:
- Set all gains to zero.
- Increase the P gain until the response to a disturbance is steady oscillation.
- Increase the D gain until the the oscillations go away (i.e. it's critically damped).
- Repeat steps 2 and 3 until increasing the D gain does not stop the oscillations.
- Set P and D to the last stable values.
- Increase the I gain until it brings you to the setpoint with the number of oscillations desired (normally zero but a quicker response can be had if you don't mind a couple oscillations of overshoot)
What disturbance you use depends on the mechanism the controller is attached to. Normally moving the mechanism by hand away from the setpoint and letting go is enough. If the oscillations grow bigger and bigger then you need to reduce the P gain.
If you set the D gain too high the system will begin to chatter (vibrate at a higher frequency than the P gain oscillations). If this happens, reduce the D gain until it stops.
I believe this technique has a name. I'll put it here when I find it.
A similar experimental method to hauptmech's answer that I was taught in college:
- Set all gains to 0.
- Increase Kd until the system oscillates.
- Reduce Kd by a factor of 2-4.
- Set Kp to about 1% of Kd.
- Increase Kp until oscillations start.
- Decrease Kp by a factor of 2-4.
- Set Ki to about 1% of Kp.
- Increase Ki until oscillations start.
- Decrease Ki by a factor of 2-4.
The Ziegler-Nichols method is more precise if you can get an accurate number for the oscillation period. It does generally cause oscillations using the "classic PID" numbers given, so it's not always optimal.
For general rules on the effect of each term on rise time, overshoot, settling time, steady-state error, and stability, see Table 1 of "PID Control System Analysis and Design", by Li, Ang, and Chong in IEEE Control Systems Magazine.
Embedded.com has moved my article yet again, but here is where it is now. This shows you both how to write a PID loop (figuring out how to do it in something other than floating point is left as an exercise to the reader) and how to tune it.
The best way depends a lot on your abilities. The way to get the best tuning, assuming you're an experienced control system hand, is usally to measure the response of the plant ("plant" == "the thing you're controlling"), then depending on how you did the measurements extract a model of the plant and design to that, or just design directly to the measurements.
For certain difficult plants you'll find that you can't ever make satisfactory measurements, in which case you have to go by models alone. Those are rare, but satisfying when you get them working.
Sebastian Thrun presented a simple algorithm for tuning PID in his "How to Program a Robotic Car" class. It's called "twiddle", he describes it here.
Twiddle is very prone to finding local minima--this means that you could come up with a set of three constants that are okay, but not optimal for the situation. The problem of tuning PID constants is a subset of a more general search problem to find certain parameters to maximize utility (in this case, minimizing error of the PID algorithm). You can look into other general solutions to this problem, like hill-climbing, simulated annealing, genetic algorithms, etc. that might end up finding more optimal solutions.
In contrast to the other two answers I'd say a good manual way of tuning a PID is ignoring the Kd. So start at 0 and increase Kp until you are getting to the target, then increase Ki to get rid of steady state error.
Kd can confuse the issue as it responds poorly to noise, then you start adding filters on your analog input and then question what that does besides slow down your control and make the whole thing harder to work out...
Another thing bound to confuse me is if the PID equation is in the standard form or in an independent (parallel in wikipedia) form. The effect of Ki seems to inverse when the form is the wrong way to what you think it is. Both types are used in automation, sometimes with the option to switch between them.
Of course experimental tuning can be done as mentioned in the other answers, but if you are able to define a reasonable dynamic model for whatever you want to control and are able to identify its parameters, you should be able to design your controller based on well-defined criteria such as overshoot, rise time, settling time, steady-state error and so on.
There are even tools in MATLAB that are able to tune your controller to optimize for a combination of these criteria, which makes it even better to use.
Know your controller
Learning what each parameter in the PID controller does is quite helpful as well. All the experimental algorithms are based on this kind of knowledge somehow. If you not only follow instructions, but are able to get that feeling yourself, you may find it easier to tune your controller manually.
Real world problems
In the end, it all depends on what you can actually do with your system to get some knowledge about how it works and what kind of experimentation can be done. The best thing is to actually learn more about PID controllers and control theory in general, IMO, but I'm biased :)
I'll try to expand a little from my experience for those who may be interested. I think the problem is we have a lot of control theory that is somewhat inaccessible (and sometimes not useful) and then we have rules of thumb that make assumptions about systems that are often inaccurate.
Let's talk first about why control loops become unstable. For this discussion I'll assume a linear system. Informally this means that if your control signal is a sine wave at a given frequency then your observed output is at the same frequency and if you change the amplitude of your control system your output responds at the same ratio. This assumption is a good approximation for many real world systems and lets us look at different frequencies in isolation.
If you look at the control path you have a set-point, your PID controller, your system (aka "Plant"), and then your sensor. Imagine a fixed set-point and a sine wave from your sensor (this is equal to a real world disturbance at the sensor, fed back). In an unstable system your feedback causes the control loop to amplify the error rather than reducing it such that as time increases your amplitude increases. The reason this happens is due do a delay, or for this particular frequency a phase shift between the input and output. For a given frequency we can look at that open loop (i.e. no feedback) shift and amplitude of the output and when we draw all those on a graph we get something like a Bode Plot. If we have a situation in this open loop graph where the error keeps getting amplified then we have an unstable system. If the delay is less than 1/2 the wavelength or the gain is less than x1 the system will be stable. In practice we want some margin from that point (gain margin and phase margin) which is why you'll see this "backing off" in many of the manual/heuristic methods.
The main problem with those manual methods is that you're flying blind and you're pretty much guaranteed to get a poor control system.
Also keep in mind that the meaning P, I and D is related to what your sensor is measuring and what control you're applying. A common mistake in home built controllers is for people to think they're applying P when they're actually not. Motor controllers often have a position loop, running over a velocity loop running over a torque loop. (A cascade)
OK but how does this help us?
The first point I'd like to make is that if you're building your own PID controller you should also build a way of measuring the open loop response. Do a frequency sweep at the input to your controller and measure the sensor's output with the feedback disconnected. Then you can draw the open loop Bode plot and see why your system is stable and be able to trade off the various controls. It's also useful to measure the closed loop response and you can do that with any system by doing a frequency sweep of your set-point while the loop is closed. Both these aren't that hard and don't require a lot of theoretical knowledge.
If you're simply tweaking controls without any understanding of what's going on under the hood you won't be able to optimize your system. Building some intuition about these systems isn't that hard. E.g. the proportional gain has no effect on phase but simply increases the open loop gain across all frequencies. So what you're doing when you're increasing the proportional gain in all those manual tuning methods is finding the frequency where the phase goes to -180. See this to get some more idea about the impact of the various controls on your frequency response.
Quite often getting best closed loop performance involves tweaking the system and not just the controller gains. What you want is to make the system as "stiff" as possible. That will let you ramp up the control parameters and get the best open and closed loop bandwidth. In my experience in motor control applications the proportional gain is the one that should be doing most of the "work" and the integrator the "rest". I don't think you need a D term at all. Having a low pass filter and a notch filter helps a lot in situations where you may have some mechanical resonance but setting them without a Bode Plot is very difficult (the oscillation frequency you observe under closed loop may be different than the open loop one).
If safety is a concern (very powerful motors or a system that could be destroyed by the motor going out of control) you need to put in some limits before you start tuning (e.g. current limit, maximum position error) to protect the system. Then you need to get some sort of feel for the range of the parameters. If your feedback has 40 counts per rotation or 4000 counts per rotation your parameters will be a factor of 100 for a given system. My approach would be to first find a range where you have some poor controllability and then ramp up from there starting with P and then I (though again you're flying blind). Backing off creates this stability margin.
Beyond closed loop
Closed loop attempts to take out the error from the system. It's always going to have a somewhat limited performance. What you want to do is minimize the error your closed loop controller sees and one way to do that is through a technique called feed forward. In feed-forward you go around the controller and drive a command directly to the system. An example of that would be acceleration feed-forward. If you know you're motor's torque constant and you know the load you can pretty much tell how much current you need to drive to get a certain acceleration of the load. You simply take the command input acceleration, multiply it by a constant and add that to the controller's drive command. You're basically doing what it would take to drive the system if there was no controller and the closer you can get the less error your loop has to take out and the better your system will perform. It makes a huge difference in practice.
Ziegler-Nichols is an easy manual method. More robust methods also exist - these usually rely on mathematical solutions (analytic, iterative optimization, etc.)
Beyond that, google "self-tuning PID" for some automated techniques. My favorite is the application of neural networks to PID tuning.
If you want to have PID accurate coefficient:
- get statistic actuator data (i.e. collecting input voltage + encoder pulse in a const frequently (0.01s) for DC-motor)
- get transfer function (TF) of your actuator (using ident toolbox in Matlab).
- using pid_tool toolbox in Matlab and upload your transform function.
There is a quicker approach called Ziegler–Nichols:
I just came across this article† showing the results of using genetic algorithms to tune a PID controller.
†The link rotted, but archive.org has a copy at Evolve Better Control Solutions With Genetic Algorithms.