PID control thermostat.

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

PID control thermostat.

#1 Post by igorka » 24 Jan 2023, 12:09

For a long time I had a desire to make a regulator working according to the Proportional Integral Differential regulation law, abbreviated as PID. I will not describe here how it is better than the classic relay with hysteresis, whoever is interested will find the answers himself. The only thing worth noting is that this regulator is used where high accuracy in maintaining the temperature (or other controlled parameter) is needed.
The classic PID controller uses floating point values ​​in its calculations and this option is better suited for ESP_Easy (due to the peculiarities of the mathematics implemented in the firmware), but initially I wrote code for an 8-bit microcontroller, where memory is limited as well as core speed, so the implementation is such.
Another interesting solution I think is how the load management is implemented, this is the "Bresenham's" algorithm. of course, you can apply PWM control or phase-pulse control, it all depends on the task and your imagination.
Important point! The "PID" function must be called with the same time period!
All coefficients P, I, D are selected individually, on a real object where the regulator will work!

Code: Select all

On System#Boot do
  let,1,27//scaling I
  let,2,0//working variable "Bresenham's"
  let,3,100//discreteness "Bresenham's"
  let,4,5//P_gain 
  let,5,1//I_gain
  let,6,0//D_gain
  let,7,1//Gain_scaling
  let,8,0//Feedback
  let,9,280//Setpoint
  let,10,0//PID_p
  let,11,0//PID_i
  let,12,0//PID_d
  let,13,0//PID_px
  let,14,0//PID_y
loopTimerSet,1,1//turn on the timer with a period of 1 second
endon
on Rules#Timer=1 do
  let,8,[termo_1#temperature]//it's not 280, it's 28.0. But you need to write without a comma!
  let,8,[var#8]*10//this is necessary in order not to lose the capacity of the temperature sensor.
  let,9,[var#9#F]//removes everything after the comma
  let,10,[int#9]-[int#8]//PID_p = Setpoint - Feedback
  let,10,[var#10#F]
  let,12,[int#10]-[int#13]//PID_d = PID_p - PID_px
  let,12,[var#12#F]
  let,13,[int#10]//PID_px = PID_p
  let,13,[var#13#F]
  let,14,[int#10]*[int#4]//PID_y = (PID_p * P_gain)
  let,14,[var#14#F]
  let,14,[int#14]+([int#11]*[int#5])/[int#1]//PID_y = PID_y + (PID_i * I_gain)/scaling I
  let,14,[var#14#F]
  let,14,[int#14]+([int#12]*[int#6])//PID_y = PID_y + (PID_d * D_gain)
  let,14,[var#14#F]
  //let,14,[int#14]+[int#12]//if you do not use the D component in the calculations, then you can write it like this
  //let,14,[var#14#F]
  let,14,[int#14]/[int#7]//PID_y = PID_y / Gain_scaling
  let,14,[var#14#F]
    if [int#14] > 100//control variable constraint. 100 because it is convenient to take for 100%
      let,14,100
        else 
    if [int#14] < 0//remove negative numbers
  let,14,0
    endif
    let,11,[int#11]+[int#10]
    let,11,[var#11#F]
  endif
    let,2,[int#2]+[int#14]//conversion of PID value value to power factor
  if [int#2]>=[int#3]
    let,2,[int#2]-[int#3]
   gpio,0,0//active level
   else
   gpio,0,1//low level
  endif
endon
Here are some heat curves. Unfortunately, I have nothing to do with high-quality graphics.
The state of rest.
The state of rest.
3.png (53.99 KiB) Viewed 956 times
change the temperature setpoint to 28. Heating 100%
change the temperature setpoint to 28. Heating 100%
4.png (65.26 KiB) Viewed 956 times
The PID controller starts to work and reduce the heating power.
The PID controller starts to work and reduce the heating power.
5.png (74.94 KiB) Viewed 956 times
Typical overshoot for a PID controller.
Typical overshoot for a PID controller.
6.png (101.29 KiB) Viewed 956 times
Stabilization mode with minimal temperature fluctuations, this is normal.
Stabilization mode with minimal temperature fluctuations, this is normal.
7.png (99.07 KiB) Viewed 956 times
I changed the extension of the temperature graph (upper graph) so that the accuracy of the maintained temperature can be more clearly seen.
I changed the extension of the temperature graph (upper graph) so that the accuracy of the maintained temperature can be more clearly seen.
9.png (119.45 KiB) Viewed 956 times
Last edited by igorka on 26 Jan 2023, 08:46, edited 2 times in total.

igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: PID control thermostat.

#2 Post by igorka » 24 Jan 2023, 12:27

Photo to understand the work of the "Bresenham's" algorithm:
25% power.
25% power.
output_25.png (28.02 KiB) Viewed 951 times
50% power.
50% power.
output_50.png (20.73 KiB) Viewed 951 times
75% power.
75% power.
output_75.png (26.38 KiB) Viewed 951 times
99% power.
99% power.
output_99.png (23.91 KiB) Viewed 951 times
At 50% (function call 1 time per second), 1 second on - one second off. And so on.

TD-er
Core team member
Posts: 7508
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: PID control thermostat.

#3 Post by TD-er » 24 Jan 2023, 12:27

There is still an mismatch in endif's in your code:

Code: Select all

  if [int#14] > 100//control variable constraint. 100 because it is convenient to take for 100%
    let,14,100
  else 
    if [int#14] < 0//remove negative numbers
      let,14,0
    endif
  else // <<<==== 2nd 'else', mismatch
    let,11,[int#11]+[int#10]
    let,11,[var#11#F]
  endif
I changed the indents, so you can see where the mismatch is.

igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: PID control thermostat.

#4 Post by igorka » 24 Jan 2023, 13:45

TD-er wrote: 24 Jan 2023, 12:27 There is still an mismatch in endif's in your code
This happened due to the fact that there was an error in the notepad, and there was code with comments :oops: :roll: . On ESP the code is flooded without error. Thank you for bringing this to our attention! In the first message removed unnecessary else.

TD-er
Core team member
Posts: 7508
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: PID control thermostat.

#5 Post by TD-er » 24 Jan 2023, 15:07

You have a lot of these:

Code: Select all

  let,14,[var#14#F]
However, you can also use the variable like this when referring it:

Code: Select all

  [var#14#F]
That may save quite a few lines of rules code.

User avatar
chromo23
Normal user
Posts: 647
Joined: 10 Sep 2020, 16:02
Location: germany

Re: PID control thermostat.

#6 Post by chromo23 » 25 Jan 2023, 17:52

Nice Idea,
i will try this soon...
I took the liberty to remove the unnecessary declaring of variables as TD-er suggested and did some formatting.
(You wouldn’t even need to declare variables with 0 at boot, but i guess you know that...)

Code: Select all

 On System#Boot Do
  Let,1,27    		//scaling I
  Let,2,0       	//working variable "Bresenham's"
  Let,3,100   		//discreteness "Bresenham's"
  Let,4,5       	//P_gain 
  Let,5,1       	//I_gain
  Let,6,0       	//D_gain
  Let,7,1       	//Gain_scaling
  Let,8,0       	//Feedback
  Let,9,280    		//Setpoint
  Let,10,0      	//PID_p
  Let,11,0      	//PID_i
  Let,12,0      	//PID_d
  Let,13,0      	//PID_px
  Let,14,0      	//PID_y
  LoopTimerSet,1,1    	//turn on the timer with a period of 1 second
Endon

On Rules#Timer=1 Do
  Let,8,[termo_1#temperature]    		//it's not 280, it's 28.0. But you need to write without a comma!
  Let,8,[var#8]*10                         	//this is necessary in order not to lose the capacity of the temperature sensor.
  Let,10,[var#9#F]-[int#8]            		//PID_p = Setpoint - Feedback
  Let,12,[var#10#F]-[int#13]        		//PID_d = PID_p - PID_px
  Let,13,[int#10]                          	//PID_px = PID_p
  Let,14,[int#10]*[int#4]              		//PID_y = (PID_p * P_gain)
  Let,14,[var#14#F]+([int#11]*[int#5])/[int#1] 	//PID_y = PID_y + (PID_i * I_gain)/scaling I
  Let,14,[var#14#F]+([var#12#F]*[int#6])	//PID_y = PID_y + (PID_d * D_gain)
  //Let,14,[int#14]+[int#12]         		//if you do not use the D component in the calculations, then you can write it like this
  Let,14,[var#14#F]/[int#7]         		//PID_y = PID_y / Gain_scaling
  If [var#14#F] > 100                  		//control variable constraint. 100 because it is convenient to take for 100%
    Let,14,100
  Else 
    If [var#14#F] < 0           		//remove negative numbers
      Let,14,0
    Endif
    Let,11,[int#11]+[int#10]
    Let,11,[var#11#F]
  Endif
  Let,2,[int#2]+[int#14]      			//conversion of PID value value to power factor
  If [int#2]>=[int#3]
    Let,2,[int#2]-[int#3]
    GPIO,0,0
  Else
    GPIO,0,1
  Endif
Endon

igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: PID control thermostat.

#7 Post by igorka » 26 Jan 2023, 08:37

chromo23 wrote: 25 Jan 2023, 17:52 Nice Idea,
i will try this soon...
I took the liberty to remove the unnecessary declaring of variables as TD-er suggested and did some formatting.
(You wouldn’t even need to declare variables with 0 at boot, but i guess you know that...)
Greetings! If my code is useful to someone, I will only be happy. And of course I don't mind if you or someone else changes it to suit your needs or optimizes it. This is source code, so there are a lot of extra lines, so it's easier for me to visually track the execution sequence. I wrote about it here: viewtopic.php?p=62648#p62648
Before setting the P, I, D coefficients, you need to set I and D to 0, and start selection from P. A correctly adjusted P coefficient (I, D is still 0, this is the first stage of adjustment) will never allow the regulator to reach the set temperature, it will be slightly -a bit less. The second step will be the setting of the Integral component, the speed of reaching the operating mode depends on its value. I left the coefficient D equal to zero, an accuracy of 0.2 ° C was enough for me. Here you can clearly see how this or that coefficient affects:
Image
Please note that I have the reverse control logic! Where 0 is on and 1 is off.

Code: Select all

  Let,2,[int#2]+[int#14]      			
  If [int#2]>=[int#3]
    Let,2,[int#2]-[int#3]
    GPIO,0,0//active level
  Else
    GPIO,0,1//low level
  Endif
Endon

TD-er
Core team member
Posts: 7508
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: PID control thermostat.

#8 Post by TD-er » 26 Jan 2023, 08:59

Do you have a good source of information on how to tune the PID controller?
In a previous job, I have been working a lot with PID controllers to fine-tune motor controllers on CNC 3D measurement machines (so called geometric metrology).
However, I always found this tuning to be quite a lot of work and then started working on some auto-tuning algorithm.
But always did this on intuition, not hindered by any knowledge or being educated in this matter.
So it would be nice if I finally can really 'understand' PID tuning and maybe once make a module for it for ESPEasy. :)
Like I said, I do almost anything here on intuition.

igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: PID control thermostat.

#9 Post by igorka » 26 Jan 2023, 09:32

TD-er wrote: 26 Jan 2023, 08:59 Do you have a good source of information on how to tune the PID controller?
In a previous job, I have been working a lot with PID controllers to fine-tune motor controllers on CNC 3D measurement machines (so called geometric metrology).
However, I always found this tuning to be quite a lot of work and then started working on some auto-tuning algorithm.
But always did this on intuition, not hindered by any knowledge or being educated in this matter.
So it would be nice if I finally can really 'understand' PID tuning and maybe once make a module for it for ESPEasy. :)
Like I said, I do almost anything here on intuition.
I confess that I, like you, do it intuitively, and not using formulas from textbooks. Although there are quite a few good PID tuning guides on the internet. Also, the difficulty lies in the fact that I am in the Russian-speaking part of the Internet, it will probably be difficult for you to understand materials that are not in English. On the other hand, I also communicate with you through Google translator ...
Here the guy has implemented his PID library with autotuning function for Arduino.
https://alexgyver.ru/lessons/pid/
It would be cool if the PID was in ESP_Easy by default!!! :D

TD-er
Core team member
Posts: 7508
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: PID control thermostat.

#10 Post by TD-er » 26 Jan 2023, 09:56

Just added an GitHub Issue for it, to keep track of the links and ideas
https://github.com/letscontrolit/ESPEasy/issues/4482

igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: PID control thermostat.

#11 Post by igorka » 26 Jan 2023, 14:01

TD-er wrote: 26 Jan 2023, 09:56 Just added an GitHub Issue for it, to keep track of the links and ideas
https://github.com/letscontrolit/ESPEasy/issues/4482
It's good, but I didn't use his examples. I just gave a link to this guy's site, as he does smart things and is passionate about it.

TD-er
Core team member
Posts: 7508
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: PID control thermostat.

#12 Post by TD-er » 26 Jan 2023, 14:03

Yep, I browsed through those pages and they looked practical enough for me to read again at a later time, when I will look into this PID feature.

igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: PID control thermostat.

#13 Post by igorka » 26 Jan 2023, 14:45

:idea: Made some changes to the code:

Code: Select all

 On System#Boot Do
  Let,1,27    		//scaling I
  Let,2,0       	//working variable "Bresenham's"
  Let,3,100   		//discreteness "Bresenham's"
  Let,4,5       	//P_gain 
  Let,5,1       	//I_gain
  Let,6,0       	//D_gain
  Let,7,1       	//Gain_scaling
  Let,8,0       	//Feedback
  Let,9,280    		//Setpoint
  Let,10,0      	//PID_p
  Let,11,0      	//PID_i
  Let,12,0      	//PID_d
  Let,13,0      	//PID_px
  Let,14,0      	//PID_y
  LoopTimerSet,1,1    	//turn on the timer with a period of 1 second
Endon

On Rules#Timer=1 Do
  Let,8,[termo_1#temperature]    		            //it's not 280, it's 28.0. But you need to write without a comma!
  Let,8,[var#8]*10                         	                    //this is necessary in order not to lose the capacity of the temperature sensor.
  Let,10,[var#9#F]-[int#8]            		           //PID_p = Setpoint - Feedback
  Let,12,[var#10#F]-[int#13]        		           //PID_d = PID_p - PID_px
  Let,13,[int#10]                          	                   //PID_px = PID_p
  Let,14,[int#10]*[int#4]              		           //PID_y = (PID_p * P_gain)
  if [int#11] < -100
    let,11,-100//i_min
      else
    if [int#11] > 2000
      let,11,2000//i_max
        endif
       else
      Let,14,[var#14#F]+([int#11]*[int#5])/[int#1]   //PID_y = PID_y + (PID_i * I_gain)/scaling I
  endif 
  Let,14,[var#14#F]+([var#12#F]*[int#6])	           //PID_y = PID_y + (PID_d * D_gain)
  //Let,14,[int#14]+[int#12]         		          //if you do not use the D component in the calculations, then you can write it like this
  Let,14,[var#14#F]/[int#7]         		         //PID_y = PID_y / Gain_scaling
  If [var#14#F] > 100                  		         //control variable constraint. 100 because it is convenient to take for 100%
    Let,14,100
  Else 
    If [var#14#F] < 0           		                         //remove negative numbers
      Let,14,0
    Endif
    Let,11,[int#11]+[int#10]
    Let,11,[var#11#F]
  Endif
  Let,2,[int#2]+[int#14]      			         //conversion of PID value value to power factor
  If [int#2]>=[int#3]
    Let,2,[int#2]-[int#3]
    GPIO,0,0
  Else
    GPIO,0,1
  Endif
Endon
An unpleasant feature was found out, if, for example, the regulator maintained the temperature (any value) around 28, then changed the setpoint [int#9] to 23 for hot, then for some time in the variable [int#11] there will be a large negative number. Because of this, if you change, for example, again to the same 28, then the output to the operating mode will be very long and "you can grow old or freeze. :lol: " In order to avoid this, I entered the minimum and maximum values of the variable [int#11] (these values are checked experimentally on the working object), they are like this for me ... By the way, they write about the limitations of the i - component in textbooks.

igorka
Normal user
Posts: 72
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: PID control thermostat.

#14 Post by igorka » 31 Jan 2023, 08:55

The principles of regulation systems, their differences and features are simply described. With an emphasis on the PID controller. There is a link to the original source (Italian).
https://arduino.ru/forum/pesochnitsa-ra ... ent-512052

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest