# What's the best way to train PID controllers?

This is borderline non-gamedev but PIDs are commonly used in games so I thought I'd give it a shot.

I've got a few PID controllers in my game, used for things like steering and throttle control. I'm having trouble tuning them accurately. It never feels like I'm getting closer to the optimial solution, and it's very tempting to just leave them "good enough" with the odd oscillation or spike I can't get rid of.

The one controller that's driven me to this post with frustration produces minor oscillations for very very small error inputs. At large inputs it's ok but not very stable.

Does anyone have any tips or resources on how I can train these things more reliably?

• I don't think controllers are stable. Are you married to the idea of using a PID controller? If not could you explain a specific case in more detail, I might be able to give a more stable solution. Jul 29, 2010 at 0:15
• I'm looking for generic approaches to tuning PID controllers. Do you tune the P, I and D coefficients in a certain order? Do you have to form a closed system to tune or have you done it inside the game environment? Jul 29, 2010 at 12:01

## 4 Answers

There's actually a decent article in AI Game Programming Wisdom 2 by Euan Forrester regarding PID Controllers.

I'll just reiterate what he said about tuning the different coefficients:

1. Start with the proportional coefficient (P) - This has the largest effect on the behaviour of the controller. Set the other 2 coeffs to 0 and tune P until you get a rough desired effect.
2. Only vary one coefficient at a time - Get a decent feel for one coefficient to make sure it does approximately what it does while keeping the other 2 constant.
3. Tune the coefficients in real time - The benefits are a bit obvious really. You don't want to recompile everything every time you make a change to a value, that's just common coding sense.
4. (Optional) Have an instant replay feature in place - This will just help in case you observe something really weird happening with your PID values so you can replay the steps up until that point.

He also offers a number of online resources (some of which are dead links so I'll only post the ones that are still available):

Tuning a PID Temperature Controller

Hope that helps

~Ray

Recent #AltDevBlogADay article describes PID controllers quite nicely, and the WebGL example on author's site can be useful for fine-tuning the variables.

For starters, Wikipedia actually has a good article on how to begin tuning. Without more details it's hard to go into more detail in a response.

Don't tune pids using the simplistic "trial and error" method! You will never get good results because parameters depend on each other. Use FOPDT (first order plus dead time) model together with IMC formulas to calculate Kp, Ki and Kd from test data.

Do a step test on your control signal (directly towards the "plant" without controller) and record your process variable that you are trying to control as it relates to the control signal. Then look at the data and note down process gain, deadtime and process time. (more info here and here).

Generally just read through the whole ControlGuru site. It is a very good resource. I have used the FOPDT model quite successfully to just get the right pid values instantly without any trial and error. It works great and produces an optimal controller. When you then compute your Kp, Ki and Kd from Kp, Ti and Td you also have an option to specify "aggressiveness" value which is a good way to just tell your already properly tuned controller how fast it should converge.

Also you should probably use derivative on PV instead of derivative on error. There is really no point in most cases to do derivative on error. To do this you use negative sign before Kd and use this formula:

This avoids an effect called "derivative kick" when you change your setpoint. More info here.

I never tune manually anymore at all. I think FOPDT model is the simplest method there is to tune PID loops very quickly. You also have to distinguish though between self regulating (self-stabilizing) process and an integrating process (which does not stabilize - such as a position of an object after a force has been applied to it). The tuning methods are a bit different but also are described at the controlguru site here.