Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Example PID code for controlling heating

Sat Oct 17, 2015 12:16 pm

With the use of two RPI's I did make a thermostat for the house heating controlled bij a webpage and python programming
The basic is up and running at the moment and working fine.
The programm is very basic and controlling the heating off and on depending on the surrounding temperature.
This means a big temperature sweep above the setpoint because the heatingsystem is still hott after reaching the setpoint.
I do like to do a better control by a PID controlled setpoint.

I'm now looking for some example PID programm's (in python) to help me to start.
Does somebody know where to find these programm?

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Example PID code for controlling heating

Sat Oct 17, 2015 5:14 pm

Seen a post on here previously but can't find it. You need to do something like this but with appropriate factors (very probably orders of magnitude and sign different from these)

Code: Select all

p_fact = 1.0
i_fact = -0.01
i_total = 0.0
d_fact = -5.0
last_e = None
while True:
  e = t_set - read_temp()
  if last_e is None:
    last_e = e
  i_total += e
  if (e * p_fact + (e - last_e) * d_fact + i_total * i_fact) > 0.0:
    heating_on(True)
  else:
    heating_on(False)
  last_e = e
  time.sleep(300.0)
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: Example PID code for controlling heating

Sat Oct 17, 2015 7:31 pm

Thanks
I will examine the code to understand 8-)

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: Example PID code for controlling heating

Mon Oct 19, 2015 11:40 am

I do have a question about the code above:
Because the temperature most of the time is above the setpoint, the i_total value is always decreasing.
This causes an uncontrolled system.
Does the i_total not be resetted somewhere/ sometimes?

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: Example PID code for controlling heating

Mon Oct 19, 2015 12:27 pm

Integrator reset is a massive debate point for control systems engineers. Typically you should tune your system such that you don't need to reset the integrator. Many PID algorithms limit the integral term such as the example linked to here.

Having said that, given the on/off nature of most home heating systems, you are unlikely to need the integral term. There are a few schemes that slowly pulse width modulate the heating system (5 minutes on, 1 minute off) that might work well for you.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Example PID code for controlling heating

Mon Oct 19, 2015 1:07 pm

As @scotty101 says, the integral factor is not so relevant in an on/off domestic system. As well as the pulse width scheme he suggests you could put in a mixing valve to control the temperature of water in the radiators, but that's a fair bit more complicated. As I said, I could have got the signs wrong but the idea is that i_total should be gradually increasing while the rooms are too cold then decreasing while the rooms are too hot, on average it should be zero and shouldn't need to be reset. However there will be times where the temperature difference isn't a result of the heating control system doing its job and in those circumstances i_total might have accumulated a "wrong" error that takes too long to correct, hence a good idea to limit its value.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: Example PID code for controlling heating

Mon Oct 19, 2015 7:35 pm

Thanks for the answers.
I did find out that PID is not working for my heating system. Because it is a simple on/ off system (central city heating system).
So modulate is not working.
Beside that my e value is always 0.1 or 0 degree. So PID is not realy helping to cut down the sweep above setpoint.

My problem is that the heating is sweeping above the setpoint for about 0,8 to 1 degree.
Now I like to figure out that learnig from the sweep above the setpoint to stop the heating early with as result it is just reaching the setpoint.

Is somebody have some suggestions how to programm this by code which is learnig from the sweep in the past?

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Example PID code for controlling heating

Mon Oct 19, 2015 8:45 pm

Hello,
unfortunately, the faster a control loop responds,the more it tends to overshoot. Think this results from the heat energy stored in the radiators. You could attach a second temp sensor to the radiator, limiting the temperature. This loop is responding faster, as the sensor is on the radiator. Or you add a second control loop for the radiator temp and the setpoint for this loop comes from the PID for the room. Cascaded control.
Good luck
Gerhard

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Example PID code for controlling heating

Mon Oct 19, 2015 10:07 pm

It's not necessarily easy getting the PID factors right. If the derivative factor is a larger negative number then a rapidly rising temperature will depress the output value. i.e. the heating will switch off before the temperature has reached the set point. You should be able to get it tuned perfectly by next May.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”