Tuning controller gains can be difficult, what general strategies work well to get a stable system that converges to the right solution?
1$\begingroup$ A question targeted at a more specific question would be more useful. Otherwise, you should just look at the PID article, with a section on tuning $\endgroup$– ronalchnOct 26, 2012 at 21:07
2$\begingroup$ I've always found tuning PIDs to be very dependent on the characteristics of the system, which is why I've never found auto-tune systems to be terribly useful. They are fine for a first pass, low performance set of parameters, but they are far from optimal, and you will have the same problem with any general strategy which is not optimised for the mechanical, electrical and control aspects of a given system. $\endgroup$– Mark Booth ♦Oct 26, 2012 at 22:47
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.
2$\begingroup$ Sounds like Ziegler-Nichols: en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method $\endgroup$ Nov 30, 2012 at 16:40
2$\begingroup$ @Throwback1986, This is definitely not Ziegler-Nichols which only uses the P gain and where I and D gain are then calculated accordingly from the oscillating P gain and the oscilation period $\endgroup$– dm76Jan 15, 2014 at 15:03
$\begingroup$ Do you have additional advice for tuning specifically a robot arm that has multiple joints that need tuning? Do you start from the base and work outwards to the end-effector, or the other way around, or does it even matter? $\endgroup$ Oct 21, 2017 at 21:20
1$\begingroup$ In a academic or industrial setting you will want a more sophisticated control method. For a hobbiest; gravity has a damping effect, so tune each joint at the angle where gravity effects the motors least. For instance, with the arm pointing straight up so that gravity is acting on the bearings and not the motor. Order does not matter. Adding a feedforward gravity compensation term to your control is probably the easiest improvement (use recursive Newton-Euler for a serial arm) if you can't get the performance you want. $\endgroup$ Oct 21, 2017 at 22:12
$\begingroup$ The pidtuner.com tool has given me better results than the Ziegler-Nichols method, which has several known drawbacks; folk.ntnu.no/skoge/publications/2003/tuningPID/more/… $\endgroup$ Oct 9, 2019 at 8:42
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.
2$\begingroup$ Kd first? I always thought that it was best to start with Kp. $\endgroup$ Nov 9, 2012 at 23:48
$\begingroup$ I'm transcribing from old lecture slides, if there's a good reason to not do it that way I'd be curious to hear it. $\endgroup$ Nov 10, 2012 at 0:13
1$\begingroup$ That's the algorithm that I advocate for "seat of the pants" tuning. It's going to get you about as close as you can without modeling the system or measuring its response. (Modeling and/or measuring is the only way to go if you really need the right tuning). $\endgroup$ Jul 3, 2013 at 21:46
$\begingroup$ Tuning Kd first doesn't make any sense to me, as in every PID I've implemented the sign of Kd is negative. It's used to reduce the control signal when very large changes in errors are occuring, in essence damping the system down. If you only have Kd, the system will move in the opposite direction, increasing the error and compounding the problem. If Kd is positive, then whenever a large change in the error occurs in the right direction, you're going to make it change even faster and overshoot your target. $\endgroup$ Feb 19, 2014 at 16:06
$\begingroup$ Actually, tuning D gain first makes some sense, but I'm assuming that "increasing" D gain means increasing absolute value and it's only increasing damping. The biggest problem with D gain is that it amplifies high-frequency noise and it's nice to see that effect without the P gain. So, you're essentially starting by looking for the gains at which it stops behaving like damping and starts to amplify noise and lagged error signals. I'm assuming for step 2, you need to poke your plant to see if it oscillates. $\endgroup$ Mar 2, 2014 at 7:38
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
There is a chance one of these things, among others, will get on your way when tuning your controller: windup, inadequate sampling rate, saturation.
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 :)
$\begingroup$ I don't understand why people keep suggesting heuristics for tuning PID while nowadays we are provided with plenty of automatic tools for plant identification and controller design that don't require deep theoretical background. Thumb up for this answer then. $\endgroup$ Jan 23, 2015 at 21:50
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.
$\begingroup$ "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." I would agree with that for typical motor control application. The P does the main job and the I compensate for long-term drift/errors. But here, the quadcopter will subject to lots of external turbulence, so the D term is important to get a fast response. As for the I term, unless the quad is in autonomous navigation mode, it's not that crucial. $\endgroup$– dm76Jan 15, 2014 at 15:57
$\begingroup$ @dm76: Most of my experience is with motor control, and I make that disclaimer ;-) My intuition though says that there's still room for I in quadcopter application and I think if P is very high it could deal with external disturbances (just like it does in motor control). I would really need to take a closer look at those systems to say something definite about quadcopters... I think a lot of what I say here does apply (especially do your tuning in the frequency domain and have the open loop response of the system). $\endgroup$ Jan 15, 2014 at 20:34
2$\begingroup$ @dm76: And really those "rule of thumb" PID tuning result in very mediocre performance in motors and presumably the same in quadcopters. Sometimes a well placed notch filter can make an order of magnitude difference... $\endgroup$ Jan 15, 2014 at 20:36
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 a accurate PID coefficient:
- Get your statistic actuator data (i.e. collecting input voltages + encoder pulses in a const frequently (
0.01sec) time for
- Get transfer function (TF) of your actuator (using Ident toolbox in MATLAB).
- Finally, use PIDTool toolbox in MATLAB and upload your transform function.
Or in a quick and simple manner:
There is a quicker approach called Ziegler–Nichols: