From cd7b3cd26bf24a93c8236e7893afa544af02531e Mon Sep 17 00:00:00 2001 From: Andrew de Quincey Date: Sun, 16 May 2021 00:43:34 +0100 Subject: [PATCH 1/6] tweak windup so it isn't temperature dependant --- config.py | 18 ++++++++---------- lib/oven.py | 33 ++++++++++++++++----------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/config.py b/config.py index 10d3a24..132e849 100644 --- a/config.py +++ b/config.py @@ -57,7 +57,7 @@ sensor_time_wait = 2 # These parameters work well with the simulated oven. You must tune them # to work well with your specific kiln. Note that the integral pid_ki is # inverted so that a smaller number means more integral action. -pid_kp = 25 # Proportional +pid_kp = 25 # Proportional pid_ki = 200 # Integral pid_kd = 200 # Derivative @@ -66,14 +66,12 @@ pid_kd = 200 # Derivative # # Initial heating and Integral Windup # -# During initial heating, if the temperature is constantly under the +# During initial heating, if the temperature is constantly under the # setpoint,large amounts of Integral can accumulate. This accumulation # causes the kiln to run above the setpoint for potentially a long # period of time. These settings allow integral accumulation only when -# the temperature is within stop_integral_windup_margin percent below -# or above the setpoint. This applies only to the integral. +# the temperature is close to the setpoint. This applies only to the integral. stop_integral_windup = True -stop_integral_windup_margin = 10 ######################################################################## # @@ -96,20 +94,20 @@ sim_R_ho_air = 0.05 # K/W " with internal air circulation # If you change the temp_scale, all settings in this file are assumed to # be in that scale. -temp_scale = "f" # c = Celsius | f = Fahrenheit - Unit to display +temp_scale = "f" # c = Celsius | f = Fahrenheit - Unit to display time_scale_slope = "h" # s = Seconds | m = Minutes | h = Hours - Slope displayed in temp_scale per time_scale_slope time_scale_profile = "m" # s = Seconds | m = Minutes | h = Hours - Enter and view target time in time_scale_profile # emergency shutoff the profile if this temp is reached or exceeded. # This just shuts off the profile. If your SSR is working, your kiln will -# naturally cool off. If your SSR has failed/shorted/closed circuit, this +# naturally cool off. If your SSR has failed/shorted/closed circuit, this # means your kiln receives full power until your house burns down. # this should not replace you watching your kiln or use of a kiln-sitter -emergency_shutoff_temp = 2264 #cone 7 +emergency_shutoff_temp = 2264 #cone 7 -# If the kiln cannot heat or cool fast enough and is off by more than +# If the kiln cannot heat or cool fast enough and is off by more than # kiln_must_catch_up_max_error the entire schedule is shifted until -# the desired temperature is reached. If your kiln cannot attain the +# the desired temperature is reached. If your kiln cannot attain the # wanted temperature, the schedule will run forever. kiln_must_catch_up = True kiln_must_catch_up_max_error = 10 #degrees diff --git a/lib/oven.py b/lib/oven.py index da6f2d4..1183e34 100644 --- a/lib/oven.py +++ b/lib/oven.py @@ -54,7 +54,7 @@ class Board(object): self.active = True log.info("import %s " % (self.name)) except ImportError: - msg = "max31855 config set, but import failed" + msg = "max31855 config set, but import failed" log.warning(msg) if config.max31856: @@ -64,7 +64,7 @@ class Board(object): self.active = True log.info("import %s " % (self.name)) except ImportError: - msg = "max31856 config set, but import failed" + msg = "max31856 config set, but import failed" log.warning(msg) def create_temp_sensor(self): @@ -76,7 +76,7 @@ class Board(object): class BoardSimulated(object): def __init__(self): self.temp_sensor = TempSensorSimulated() - + class TempSensor(threading.Thread): def __init__(self): threading.Thread.__init__(self) @@ -118,7 +118,7 @@ class TempSensorReal(TempSensor): '''take 5 measurements over each time period and return the average''' while True: - maxtries = 5 + maxtries = 5 sleeptime = self.time_step / float(maxtries) temps = [] for x in range(0,maxtries): @@ -246,10 +246,10 @@ class SimulatedOven(Oven): # set temps to the temp of the surrounding environment self.t = self.t_env # deg C temp of oven self.t_h = self.t_env #deg C temp of heating element - + # call parent init Oven.__init__(self) - + # start thread self.start() log.info("SimulatedOven started") @@ -307,9 +307,9 @@ class SimulatedOven(Oven): self.runtime, self.totaltime, time_left)) - + # we don't actually spend time heating & cooling during - # a simulation, so sleep. + # a simulation, so sleep. time.sleep(self.time_step) @@ -397,7 +397,7 @@ class PID(): self.lastErr = 0 # FIX - this was using a really small window where the PID control - # takes effect from -1 to 1. I changed this to various numbers and + # takes effect from -1 to 1. I changed this to various numbers and # settled on -50 to 50 and then divide by 50 at the end. This results # in a larger PID control window and much more accurate control... # instead of what used to be binary on/off control. @@ -411,12 +411,11 @@ class PID(): if self.ki > 0: if config.stop_integral_windup == True: - margin = setpoint * config.stop_integral_windup_margin/100 - if (abs(error) <= abs(margin)): + if self.kp * error < window_size: self.iterm += (error * timeDelta * (1/self.ki)) else: self.iterm += (error * timeDelta * (1/self.ki)) - + dErr = (error - self.lastErr) / timeDelta output = self.kp * error + self.iterm + self.kd * dErr out4logs = output @@ -430,14 +429,14 @@ class PID(): output = float(output / window_size) - if out4logs > 0: + if out4logs > 0: # log.info("pid percents pid=%0.2f p=%0.2f i=%0.2f d=%0.2f" % (out4logs, -# ((self.kp * error)/out4logs)*100, +# ((self.kp * error)/out4logs)*100, # (self.iterm/out4logs)*100, -# ((self.kd * dErr)/out4logs)*100)) +# ((self.kd * dErr)/out4logs)*100)) log.info("pid actuals pid=%0.2f p=%0.2f i=%0.2f d=%0.2f" % (out4logs, - self.kp * error, + self.kp * error, self.iterm, - self.kd * dErr)) + self.kd * dErr)) return output From 941a158295a7ae944a28237f4b810dcfdab53b50 Mon Sep 17 00:00:00 2001 From: Andrew de Quincey Date: Wed, 26 May 2021 17:24:19 +0100 Subject: [PATCH 2/6] should be abs() --- lib/oven.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oven.py b/lib/oven.py index 1183e34..2268388 100644 --- a/lib/oven.py +++ b/lib/oven.py @@ -411,7 +411,7 @@ class PID(): if self.ki > 0: if config.stop_integral_windup == True: - if self.kp * error < window_size: + if abs(self.kp * error) < window_size: self.iterm += (error * timeDelta * (1/self.ki)) else: self.iterm += (error * timeDelta * (1/self.ki)) From 7bd5c969e4eb787c0b5b78b64c5503855c1ccf83 Mon Sep 17 00:00:00 2001 From: Jason Bruce Date: Tue, 1 Jun 2021 15:23:45 -0400 Subject: [PATCH 3/6] - updated the installation docs --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 0f5b306..a023e07 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,14 @@ My controller plugs into the wall, and the kiln plugs into the controller. ## Software -### Raspbian +### Raspberry PI OS -Download [NOOBs](https://www.raspberrypi.org/downloads/noobs/). Copy files to an SD card. Install raspbian on RPi using NOOBs. +Download [Raspberry PI OS](https://www.raspberrypi.org/software/). Use Rasberry PI Imaging tool to install the OS on an SD card. Boot the OS, open a terminal and... - $ sudo apt-get install python3-pip python3-virtualenv libevent-dev git virtualenv - $ git clone https://github.com/jbruce12000/kiln-controller.git + $ sudo apt-get update + $ sudo apt-get dist-upgrade + $ sudo apt-get install python3-virtualenv libevent-dev virtualenv + $ git clone https://github.com/jbruce12000/kiln-controller $ cd kiln-controller $ virtualenv -p python3 venv $ source venv/bin/activate From 937228cc5fa0efaf3143c014ad30e3884aef6117 Mon Sep 17 00:00:00 2001 From: Jason Bruce Date: Fri, 4 Jun 2021 09:08:57 -0400 Subject: [PATCH 4/6] - changed the pid tuning page to a manual, pracitical approach --- docs/pid_tuning.md | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/docs/pid_tuning.md b/docs/pid_tuning.md index c90ad9f..995ee36 100644 --- a/docs/pid_tuning.md +++ b/docs/pid_tuning.md @@ -10,45 +10,41 @@ A controller with properly tuned PID values reacts quickly to changes in the set ## Try the Existing Values -My kiln is Skutt KS-1018 with a kiln vent. Try the current settings for pid_kp, pid_ki, and pid_kd and if they work for you, you're done. Otherwise, you have some experimentation ahead of you. The following exercise took me about 2 hours of testing. +My kiln is Skutt KS-1018 with a kiln vent. Try the current settings for pid_kp, pid_ki, and pid_kd and if they work for you, you're done. Otherwise, you have some experimentation ahead of you. The following exercise took me about an hour of testing. ## The Tuning Process -in config.py set the PID values like so... +I'm a big fan of manual tuning. Let's start with some reasonable values for PID settings in config.py... - pid_kp = 1.0 - pid_ki = 0.0 - pid_kd = 0.0 + pid_kp = 20 + pid_ki = 50 + pid_kd = 100 -run a test schedule. I used a schedule that switches between 200 and 250 F every 30 minutes. +When you change values, change only one at a time and watch the impact. Change values by either doubling or halving. -What you are looking for is overshoot (in my case 25F) past 200F to 225F. The next thing the controller should do is undershoot by just a little below the set point of 200F. If these two things happen, great. If not, you will need to change pid_kp to a higher or lower value. +Run a test schedule. I used a schedule that switches between 200 and 250 F every 30 minutes. The kiln will likely shoot past 200. This is normal. We'll eventually get rid of most of the overshoot, but probably not all. -Once you get the overshoot and minimal undershoot, you need to record some values. First grab the overshoot... in my case 25F. +Let's balance pid_ki first (the integral). The lower the pid_ki, the greater the impact it will have on the system. If a system is consistently low or high, the integral is used to help bring the system closer to the set point. The integral accumulates over time and has [potentially] a bigger and bigger impact. - pid_kp = 25 +* If you have a steady state (no oscillations), but the temperature is always above the set point, increase pid_ki. +* If you have a steady state (no oscillations), but the temperature is always above the set point, decrease pid_ki. +* If you have an oscillation but the temperature is mostly above the setpoint, increase pid_ki. +* If you have an oscillation but the temperature is mostly below the setpoint, decrease pid_ki. -Measure the time in seconds from high peak to low peak. In my case this was 725 seconds. Multiply that number by 1.5 to get the Integral. So 725 * 1.5 = 1088. +Let's set pid_kp next (proportional). Think of pid_kp as a dimmable light switch that turns on the heat when below the set point and turns it off when above. The brightness of the dimmable light is defined by pid_kp. Be careful reducing pid_kp too much. It can result in strange behavior. - pid_ki = 1088 +* If you have oscillations that don't stop or increase in size, reduce pid_kp +* If you have too much overshoot (after adjusting pid_kd), reduce pid_kp +* If you approach the set point wayyy tooo sloooowly, increase pid_kp + +Now set pid_kd (derivative). pid_kd makes an impact when there is a change in temperature. It's used to reduce oscillations. -Now set the derivative at 1/5 of the Integral. So 1088/5 = 217 +* If you have oscillations that take too long to settle, increase pid_kp +* If you have crazy, unpredictable changes in temperature, reduce pid_kp - pid_kd = 217 - -in essence these values mean... - -| setting | Value | Action | -| ------- | ----- | ------ | -| pid_kp | 25 | react pretty slowly | -| pid_ki | 1088 | predict really far forward in time and make changes early | -| pid_kd | 217 | heavily dampen oscillations | - -Now, run the test schedule again and see how well it works. Expect some overshoot as the kiln reaches the set temperature the first time, but no oscillation. Any holds or ramps after that should have a smooth transition and should remain really close to the set point [1 or 2 degrees F]. +Expect some overshoot as the kiln reaches the set temperature the first time, but no oscillation. Any holds or ramps after that should have a smooth transition and should remain really close to the set point [1 or 2 degrees F]. ## Troubleshooting * only change one value at a time, then test it. -* If there is too much overshoot, decrease pid_kp. -* If the temp is always below the set point, decrease pid_ki (which increases the integral action). * if the above does not work, try the Ziegler / Nichols method https://blog.opticontrols.com/archives/477 From 5574c467b2339b005d65818ea1fa8e94def70ffe Mon Sep 17 00:00:00 2001 From: Jason Bruce Date: Fri, 4 Jun 2021 09:12:49 -0400 Subject: [PATCH 5/6] fixed bug in instructions --- docs/pid_tuning.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/pid_tuning.md b/docs/pid_tuning.md index 995ee36..ac4ed18 100644 --- a/docs/pid_tuning.md +++ b/docs/pid_tuning.md @@ -27,7 +27,7 @@ Run a test schedule. I used a schedule that switches between 200 and 250 F every Let's balance pid_ki first (the integral). The lower the pid_ki, the greater the impact it will have on the system. If a system is consistently low or high, the integral is used to help bring the system closer to the set point. The integral accumulates over time and has [potentially] a bigger and bigger impact. * If you have a steady state (no oscillations), but the temperature is always above the set point, increase pid_ki. -* If you have a steady state (no oscillations), but the temperature is always above the set point, decrease pid_ki. +* If you have a steady state (no oscillations), but the temperature is always below the set point, decrease pid_ki. * If you have an oscillation but the temperature is mostly above the setpoint, increase pid_ki. * If you have an oscillation but the temperature is mostly below the setpoint, decrease pid_ki. @@ -40,7 +40,7 @@ Let's set pid_kp next (proportional). Think of pid_kp as a dimmable light switch Now set pid_kd (derivative). pid_kd makes an impact when there is a change in temperature. It's used to reduce oscillations. * If you have oscillations that take too long to settle, increase pid_kp -* If you have crazy, unpredictable changes in temperature, reduce pid_kp +* If you have crazy, unpredictable behavior from the controller, reduce pid_kp Expect some overshoot as the kiln reaches the set temperature the first time, but no oscillation. Any holds or ramps after that should have a smooth transition and should remain really close to the set point [1 or 2 degrees F]. From 09ee64403fc6c97d6625cb00042304ab89408a37 Mon Sep 17 00:00:00 2001 From: Jason Bruce Date: Mon, 7 Jun 2021 08:22:21 -0400 Subject: [PATCH 6/6] adding a list of thermocouple types and feature about integral wind-up --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a023e07..9202c35 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,12 @@ Turns a Raspberry Pi into an inexpensive, web-enabled kiln controller. * supports PID parameters you tune to your kiln * monitors temperature in kiln after schedule has ended * api for starting and stopping at any point in a schedule - * support of MAX31856 + * supports MAX31856 and MAX31855 thermocouple boards + * support for K, J, N, R, S, T, E, or B type thermocouples * accurate simulation - * support for shifting schedule when kiln cannot heat quickly enough + * support for shifting schedule when kiln cannot heat quickly enough + * support for preventing initial integral wind-up + **Run Kiln Schedule**