2021-04-30 21:53:43 +00:00
# PID Tuning Using Ziegler-Nicols
This uses the Ziegler Nicols method to estimate values for the Kp/Ki/Kd PID control values.
The method implemented here is taken from ["Ziegler–Nichols Tuning Method" ](https://www.ias.ac.in/article/fulltext/reso/025/10/1385-1397 ) by Vishakha Vijay Patel
2021-04-30 21:59:55 +00:00
One issue with Ziegler Nicols is that is a **heuristic** : it generally works quite well, but it might not be the optimal values. Further manual adjustment may be necessary.
2021-04-30 21:53:43 +00:00
## Process Overview
1. First of all, you will record a temperature profile for your kiln.
2021-04-30 21:59:55 +00:00
2. Next, we use those figures to estimate Kp/Ki/Kd.
2021-04-30 21:53:43 +00:00
## Step 1: Record Temperature Profie
2021-04-30 21:59:55 +00:00
Ensure `kiln-controller` is **stopped** during profile recording: The profile must be recorded without any interference from the actual PID control loop (you also don't want two things changing the same GPIOs at the same time!)
2021-04-30 21:53:43 +00:00
2021-05-01 08:58:06 +00:00
Make sure your kiln is completely cool - we need to record the data starting from room temperature to correctly measure the effect of kiln/heating.
2021-05-01 09:27:58 +00:00
There needs to be no abnormal source of temperature change to the kiln: eg if you normally run with a kiln plug in place - make sure its in place for the test!
2021-05-01 08:58:06 +00:00
2021-04-30 21:59:55 +00:00
To record the profile, run:
2021-04-30 21:53:43 +00:00
```
2021-05-01 13:18:27 +00:00
python kiln-tuner.py recordprofile zn.csv
2021-04-30 21:53:43 +00:00
```
The above will drive your kiln to 400 and record the temperature profile to the file `zn.csv` . The file will look something like this:
```
time,temperature
4.025461912,45.5407078
6.035358906,45.5407078
8.045399904,45.5407078
10.05544925,45.59087846
...
```
## Step 2: Compute the PID parameters
Once you have your zn.csv profile, run the following:
```
2021-05-01 13:18:27 +00:00
python kiln-tuner.py zn zn.csv
2021-04-30 21:53:43 +00:00
```
The values will be output to stdout, for example:
```
Kp: 3.853985144980333 1/Ki: 87.78173053095107 Kd: 325.9599328488931
```
2021-04-30 21:59:55 +00:00
(Note that the Ki value is already inverted ready for use in config)
2021-04-30 21:53:43 +00:00
------
## Sanity checking the results
If you run
```
2021-05-01 13:18:27 +00:00
python kiln-tuner.py zn zn.csv --showplot
2021-04-30 21:53:43 +00:00
```
2021-04-30 21:59:55 +00:00
It will display a plot of the parameters. It should look simular to this ![kiln-tuner-example.png ](kiln-tuner-example.png ).
Note: you will need python's `pyplot` installed for this to work.
2021-04-30 21:53:43 +00:00
The smooth linear part of the chart is very important. If it is too short, try increasing the target temperature (see later).
2021-04-30 21:59:55 +00:00
The red diagonal line: this **must** follow the smooth part of your chart closely.
2021-04-30 21:53:43 +00:00
## My diagonal line isn't right
You might need to adjust the line parameters to make it fit your data properly. You can do this as follows:
```
2021-05-01 13:34:57 +00:00
python kiln-tuner.py zn zn.csv --tangentdivisor 4
2021-04-30 21:53:43 +00:00
```
`tangentdivisor` modifies which parts of the profile is used to calculate the line.
It is a floating point number >= 2; If necessary, try varying it till you get a better fit.
## Changing the target temperature
By default it is 400. You can change this as follows:
```
2021-05-01 13:18:27 +00:00
python kiln-tuner.py recordprofile zn.csv --targettemp 500
2021-04-30 21:53:43 +00:00
```
2021-05-01 13:18:27 +00:00
(where the target temperature has been changed to 500 in the example above)