Crash course on PID tuning

General Tuning Questions And Discussions
User avatar
Posts: 423
Joined: Fri Feb 02, 2018 3:13 pm

Crash course on PID tuning

Postby NSFW » Tue Mar 31, 2020 3:31 am

Feedback loops show up in tuning from time to time. Turbocharged cars often use it to control boost. Cruise-control uses feedback to control speed. Drive-by-wire cars use feedback to control the throttle blade angle when idling, and cable-throttle cars use it to control the IAC motor. I recently had some major headaches with my car stalling when it was supposed to return to idle (e.g. when I push the clutch pedal and lift off the gas pedal) and that turned out to be due to the PID feedback loop that controls idle airflow.

The total feedback is just three terms (values) added together: Proportional, Integral, and Derivative terms (hence "PID" feedback). Those get added to the default position of the throttle blade or IAC, which is determined by the "desired airflow" or "base airflow" table (two names for the same thing). The default throttle position should be close to the right position to get the desired idle speed - the closer the better. But it doesn't need to be exactly right because the feedback will take care of the rest.

"Gain" is just a number that scales the amount of a feedback term. The PCM contains a bunch of tables that contain gain values.

"Error" is the difference between the target value (sometimes called "setpoint") and the actual value. So for idle the error is just actual_RPM - desired_RPM.

All of this gets evaluated around 50 to 100 times per second:

Proportional term = proportional_gain * error

This term is "proportional" to the error. Large errors cause large values. This can't get the idle to the target speed by itself though, because if the error was zero this term would be zero and the throttle/IAC would go back to the default position. So this term will pull the actuator in the right direction, but not quite far enough.

Integrated_error = integrated_error + error
Integral term = integral_gain * integrated_error


Note that the first formula above is adding the error value to the integrated_error value. So if the engine is 100 RPM too high for a short period of time, the integrated error will be small, but if it stays 100 RPM too high for a while, the integrated error value will grow larger. So this term will gradually open the throttle further and further, until RPM reaches the target value. This term could be sufficient by itself, but you get better/faster results with all three terms.

One common problem with the integral term is that it can get quite large over time (called "integral windup") and then it takes a while to shrink back down. That was a big part of my stalling problem. I'd let off the gas and push in the clutch, the integral term would aggressively close the throttle (because it was tuned for a 50 pound clutch/flywheel) and then the engine would stall because the throttle was closed. Turning this way down helped a lot.

Derivative_error = error - error_from_previous_iteration
Derivative term = derivative_gain * derivative_error


The value of this term depends on whether the error is growing or shrinking. This helps offset the integral windup problem. For example, when the integral term is growing large and RPM is rapidly approaching the target, this term opens the throttle to avoid overshooting idle and staling the engine. I was turning this up 10% at a time today.

Throttle blade angle (or IAC position) = default + proportional + integral + derivative + idle_trim

I'm not 100% sure about the idle trim but I think it is just based on the integral term, but updated very slowly. That way the integral term doesn't need to rediscover the right value every time you return to idle.

You can kinda think of the P, I, and D gains as just a single number. Feedback loops work OK that way. But the PCM looks up the gain based on tables for a little more flexibility. There are separate columns for "RPM too high" and "RPM too low" gains, which is nice because it let me use high gain for underspeed and low gain for overspeed.

The proportional gain stays at zero for small errors, which means that the throttle blade doesn't move, and instead the PCM relies on ignition timing to make small corrections to idle speed.
Please don't PM me with questions about tuning or flashing - start a thread instead. Thanks!

Posts: 106
Joined: Mon Feb 11, 2019 12:48 pm
Location: DFW, Texas

Re: Crash course on PID tuning

Postby jlvaldez » Tue Jul 14, 2020 5:00 pm

good thread. I like everything you have here.

I have played with PID tuning on gen 3 LS's, and it does help stabilize idle for sure :)

User avatar
Posts: 331
Joined: Sun Jan 25, 2015 4:21 pm
Location: Sydney

Re: Crash course on PID tuning

Postby j_ds_au » Tue Jul 14, 2020 7:28 pm

Why was your integral term ever increasing? Why did your throttle position sensor fail to ever reach zero error at idle? Surely there's a problem there, otherwise the integral should only increase up to the point that the target throttle position is reached. So either this wasn't able to reach target, or its sensor incorrectly reported that it hadn't, or somehow the target was wrong.

The "D", which I think I learnt as "differential", is only meant to speed up the system response in the case of fast changing conditions. It is not meant to correct failures of the "integral" to settle.

Just random thoughts on reading this ... possibly affected by rust.

Joe.

User avatar
Posts: 423
Joined: Fri Feb 02, 2018 3:13 pm

Re: Crash course on PID tuning

Postby NSFW » Sun Aug 16, 2020 1:50 pm

j_ds_au wrote:Why was your integral term ever increasing? Why did your throttle position sensor fail to ever reach zero error at idle? Surely there's a problem there, otherwise the integral should only increase up to the point that the target throttle position is reached. So either this wasn't able to reach target, or its sensor incorrectly reported that it hadn't, or somehow the target was wrong.

The "D", which I think I learnt as "differential", is only meant to speed up the system response in the case of fast changing conditions. It is not meant to correct failures of the "integral" to settle.

Just random thoughts on reading this ... possibly affected by rust.

Joe.


The target in this case was RPM, not throttle position sensor.

The integral did not run away forever, it just closed the throttle so aggressively that the engine would die (because RPM can drop to zero much more quickly than with the stock flywheel). After the engine has stalled, the RPM is several hundred RPM lower than the target value, so the integral would move in the other direction right away - but by then it was too late.
Please don't PM me with questions about tuning or flashing - start a thread instead. Thanks!

Posts: 135
Joined: Mon Jun 29, 2009 9:43 am
Location: Western Queensland

Re: Crash course on PID tuning

Postby brindo » Sun Aug 16, 2020 5:23 pm

A general rule of thumb I have in process conditions is that if the gain is high, keep the derivative low. Derivative can often cause a loop to get out of control. Cat in particular have a pretty vague explaination of PID loop values, and usually state something like if you want better response or stability just increase the values. They express the values as a percentage though. But setting everything to 100% ends up breaking things.

User avatar
Posts: 331
Joined: Sun Jan 25, 2015 4:21 pm
Location: Sydney

Re: Crash course on PID tuning

Postby j_ds_au » Thu Aug 20, 2020 1:09 am

NSFW wrote:
j_ds_au wrote:Why was your integral term ever increasing? Why did your throttle position sensor fail to ever reach zero error at idle? Surely there's a problem there, otherwise the integral should only increase up to the point that the target throttle position is reached. So either this wasn't able to reach target, or its sensor incorrectly reported that it hadn't, or somehow the target was wrong.

The "D", which I think I learnt as "differential", is only meant to speed up the system response in the case of fast changing conditions. It is not meant to correct failures of the "integral" to settle.

Just random thoughts on reading this ... possibly affected by rust.

Joe.


The target in this case was RPM, not throttle position sensor.

The integral did not run away forever, it just closed the throttle so aggressively that the engine would die (because RPM can drop to zero much more quickly than with the stock flywheel). After the engine has stalled, the RPM is several hundred RPM lower than the target value, so the integral would move in the other direction right away - but by then it was too late.

OK, that explains it a bit better. (I was guessing at the target;-)

So the problem isn't really with the integral, that's just an after-effect then, although for this case, the PID should stop since RPM isn't going to be under its control once the engine has stalled. That should be the case anyway, right? Isn't the engine run open-loop immediately after a (re)start?

Sound like either the proportional or differential part is too big. Theoretically, a negative differential could (maybe) even be used as a dampener in some situations, although I don't know if that's appropriate here.

Joe.

PS. Actually, the integral could still be the problem. If it builds up/down too quickly for the response time (momentum/inertia) of the system, it will cause an over/undershoot.

User avatar
Posts: 423
Joined: Fri Feb 02, 2018 3:13 pm

Re: Crash course on PID tuning

Postby NSFW » Fri Aug 21, 2020 2:57 pm

I'm pretty sure both proportional and integral were much too high. Together they were trying to bring RPM down, succeeding wildly at doing so, and bringing RPM down so far below the target that the engine would stall. There's a pretty brief window between RPM dropping below the target, and dropping so low that the engine stalled. To be honest I'm just assuming that the P and I terms were trying to pull RPM back up during that window. If not for the nonlinear behavior in the 0-250 RPM range, it might have oscillated a few times before settling down... but 100 RPM is not sustainable. :)

The momentum of the old clutch/flywheel assembly caused it to respond more slowly to large P and I corrections. But the stock assembly was around 50 pounds and the new stuff is only about 32.

For the D gain, I basically just increased it a bit at at time until the RPM paused at a higher-than-target value before resuming its decrease toward the the target. Then I backed it down a notch so that RPM would decrease smoothy toward the target. I think damping is a fairly good analogy.
Please don't PM me with questions about tuning or flashing - start a thread instead. Thanks!

Return to General Tuning Discussion

Who is online

Users browsing this forum: No registered users and 2 guests