PID autotune

It's all about the code!
Post Reply
User avatar
AndreyB
Site Admin
Posts: 14327
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

PID autotune

Post by AndreyB »

Too many PIDs already and tuning each manually is just too painful. We need some sort of PID autotune. Anyone has ideas?

What would be a good real or virtual process to play with on a bench? What PID autotune do we want to use?

This would be used for idle PID, alternator PID and else.

See http://pages.mtu.edu/~tbco/cm416/cctune.html
See https://ir.nctu.edu.tw/bitstream/11536/27727/1/000183931000005.pdf
See https://en.wikipedia.org/wiki/Ziegler%E2%80%93Nichols_method
See https://github.com/MarlinFirmware/Marlin/blob/1.1.x/Marlin/temperature.cpp#L222
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
stefanst
contributor
contributor
Posts: 703
Joined: Wed Feb 17, 2016 12:24 am
Location: USA 08530

Re: PID autotune

Post by stefanst »

In order to auto-tune a PID controller, it may be necessary to determine the expected behavior of the controlled element. There are several different distinct types of behavior, even in an engine.

Let's start with idle:
We want to achieve a target-rpm and what we can control is idle-air (also timing and fuel, but let's keep it simple for now). Idle air determines torque. The torque counteracts losses like friction and pumping losses. If our engine had no losses, even a minimal amount of torque would spin it to infinity rpms. We also have an energy storage device: our rotating masses, clutch, flywheel and crankshaft. The energy storage will resist change. If we open the idle valve, the rpms will go up slowly. If we close it they will drop slowly. The greater the rotating masses, the more slowly the changes. This is the reason why luxury engines get big flywheels- to smooth things out.
Now, if our losses decrease suddenly (i.E. turning off the AC), the torque applied is greater than the torque needed to overcome friction and our engine rpms will increase. This will not happen instantaneously, because energy is stored in the rotation. Since losses increase with rpm, at some rpm the losses will be sufficient to consume all out torque and we have a stable situation again. Similar effects would happen with a sudden increase in losses (turning on the AC, adding electric consumers and thus load to the alternator etc. The rpms will drop until the new losses are small enough to be counter-balancing the torque.

And since everybody loves a visual aid, here is one:
pt1 behavior idle control..png
pt1 behavior idle control..png (10.47 KiB) Viewed 14525 times
This type behavior is called pt1 if memory serves right. It's reasonably benign to control, since the energy-storage device helps us slow things down. This seems to be the type of DUC (Device Under Control) that the Cohen Coon method, as mentioned in the paper linked above, would be good for.

An electronic equivalent to this type of DUC would be an RC network:
rc_pt1_simulator.png
rc_pt1_simulator.png (9.03 KiB) Viewed 14525 times
Equivalents:
U in -> Idle-Air setting
R1 together with C1 simulates the energy storage in the rotation masses
R2 simulates the friction losses (lower R-> higher losses)
U out -> engine rpm

Implementing Cohen Coon should work well for this type behavior.

More on other types of DUCs soon.....
User avatar
kb1gtt
contributor
contributor
Posts: 3758
Joined: Tue Sep 10, 2013 1:42 am
Location: ME of USA

Re: PID autotune

Post by kb1gtt »

See page 61 of this process controller
https://cdn.automationdirect.com/static/manuals/solocontrolm/solocontrolm.pdf

Basically the auto-tune turns the output 100% on until it passes the target. Then it changes to 0% output, and waits until it crosses the set value. Then it turns on 100% again. When it does this, it finds the cycle time and amplitude then calculates the magic numbers. This auto-tune approach has been fairly good for many process controlled items like a temperature controlled room. I'm not sure how they generate the magic numbers, but I figured this would be a good reference.
Welcome to the friendlier side of internet crazy :)
User avatar
AndreyB
Site Admin
Posts: 14327
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: PID autotune

Post by AndreyB »

Some data andreika has asked for: turning radiator fan on and off and looking at how RPMs would react.

At some point half through the video I've turned off alternator PID control, so on the second half of the video we can also see how fast VBatt reacts to radiator fan turned on or off.

https://svn.code.sf.net/p/rusefi/code/misc/logs/2003_mazda_miata/2017-07-25_22.03.13_radiator_fan_on_off.msl.7z
Attachments
screenshot.png
screenshot.png (56.96 KiB) Viewed 14493 times
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
kb1gtt
contributor
contributor
Posts: 3758
Joined: Tue Sep 10, 2013 1:42 am
Location: ME of USA

Re: PID autotune

Post by kb1gtt »

These links might be of interest.
http://scilab.ninja/study-modules/scilab-control-engineering-basics/module-4-pid-control/

Using Scilab-XCOS to simulate PID controller.ogv - YouTube
https://www.youtube.com/watch?v=CC6yONnLSlY

x2c appears to be a graphical math tool perhaps scilab at the core, which can run on a STM32 nucleo board.
http://www.mechatronic-simulation.org/x2c/x2c-home/
http://www.mechatronic-simulation.org/stm32-nucleo-board/
Welcome to the friendlier side of internet crazy :)
User avatar
AndreyB
Site Admin
Posts: 14327
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: PID autotune

Post by AndreyB »

Will just mention it here - DTN0003_Introduction_to_PID_Control.pdf

See https://rusefi.com/forum/viewtopic.php?p=29364#p29366
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
AndreyB
Site Admin
Posts: 14327
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: PID autotune

Post by AndreyB »

Not auto tune but very cool

Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
User avatar
kb1gtt
contributor
contributor
Posts: 3758
Joined: Tue Sep 10, 2013 1:42 am
Location: ME of USA

Re: PID autotune

Post by kb1gtt »

This was posted by @mck1117 in slack, it's being posted here to keep an archived copy of it longer term. Slack goes away after a while. The attached is a printed PDF copy of the below link.
https://warwick.ac.uk/fac/cross_fac/iatl/reinvention/archive/volume5issue2/hornsey
Attachments
A_Review_of_Relay_Auto-tuning_Methods_for_the_Tuning_of_PID-type_Controllers.pdf
(3.27 MiB) Downloaded 365 times
Welcome to the friendlier side of internet crazy :)
User avatar
kb1gtt
contributor
contributor
Posts: 3758
Joined: Tue Sep 10, 2013 1:42 am
Location: ME of USA

Re: PID autotune

Post by kb1gtt »

This page seems to be a quicker way of explaining the relay auto-tune method. PDF copy attached.
https://pages.mtu.edu/~tbco/cm416/Atune.html
Attachments
Autotune_(Relay)_Tuning_Method_.pdf
(85.25 KiB) Downloaded 353 times
Welcome to the friendlier side of internet crazy :)
mck1117
running engine in first post
running engine in first post
Posts: 1494
Joined: Mon Jan 30, 2017 2:05 am
Location: Seattle-ish

Re: PID autotune

Post by mck1117 »

kb1gtt wrote:
Sat Apr 11, 2020 10:22 am
This page seems to be a quicker way of explaining the relay auto-tune method. PDF copy attached.
https://pages.mtu.edu/~tbco/cm416/Atune.html
Good find - that's a nice concise description of what I've implemented.
User avatar
kb1gtt
contributor
contributor
Posts: 3758
Joined: Tue Sep 10, 2013 1:42 am
Location: ME of USA

Re: PID autotune

Post by kb1gtt »

Did you implement the Ziegler and Nichols method for the PID's that come out of the relay auto-tune method? I'll have to check a process controller I use at work in a temperature chamber we have. I recall it's PID's were very low P, with high I and even higher D. The PID's were crazy, but the end result works very well. It's common that temperature process controllers invert the gain. So perhaps that really is Ziegler, but from memory it didn't seem like it was close to what I expected. I suspect it's some other guess at the PID's.
Welcome to the friendlier side of internet crazy :)
mck1117
running engine in first post
running engine in first post
Posts: 1494
Joined: Mon Jan 30, 2017 2:05 am
Location: Seattle-ish

Re: PID autotune

Post by mck1117 »

kb1gtt wrote:
Sun Apr 12, 2020 12:56 am
Did you implement the Ziegler and Nichols method for the PID's that come out of the relay auto-tune method? I'll have to check a process controller I use at work in a temperature chamber we have. I recall it's PID's were very low P, with high I and even higher D. The PID's were crazy, but the end result works very well. It's common that temperature process controllers invert the gain. So perhaps that really is Ziegler, but from memory it didn't seem like it was close to what I expected. I suspect it's some other guess at the PID's.
Yes - that's what you do with them. The testing (oscillation) yields the ultimate gain (K_u) and oscillation period (T_u) that you then plug in to Zeigler-Nichols. Here's the code that computes kP/kI/kD from Ku/Tu - it should be pretty legible even for non-programmers: https://github.com/rusefi/rusefi/blob/6481b9df0a5def72335904d7753d40729c90ceb2/firmware/controllers/actuators/electronic_throttle.cpp#L260
User avatar
AndreyB
Site Admin
Posts: 14327
Joined: Wed Aug 28, 2013 1:28 am
Location: Jersey City
Github Username: rusefillc
Slack: Andrey B

Re: PID autotune

Post by AndreyB »

We really need a step by step user documentation and some video tutorial :)
Very limited telepathic abilities - please post logs & tunes where appropriate - http://rusefi.com/s/questions

Always looking for C/C++/Java/PHP developers! Please help us see https://rusefi.com/s/howtocontribute
Abricos
contributor
contributor
Posts: 849
Joined: Mon Aug 18, 2014 12:32 am
Location: Carteret, NJ 07008

Re: PID autotune

Post by Abricos »

I hope this video will help you in setting up your ETB ...
using ETBpid Autotune options ...

Post Reply