-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathHeaterSim.cpp
More file actions
90 lines (74 loc) · 3.2 KB
/
HeaterSim.cpp
File metadata and controls
90 lines (74 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
HeaterSim.cpp - Simple heater simulation library
Copyright (c) 2014 Thijs Elenbaas. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/******************************************************************************
* Includes
******************************************************************************/
#include "HeaterSim.h"
#include <math.h>
/******************************************************************************
* Definitions
******************************************************************************/
#define _HEATERSIM_VERSION 0.1.0 // software version of this library
#define LN2 0.693147f
/******************************************************************************
* Constructors
******************************************************************************/
HeaterSim::HeaterSim(float ambientTemp)
{
_ambientTemp = ambientTemp;
_boilerTemp = _ambientTemp;
_delayedBoilerTemp = _ambientTemp;
_halfTimeCooling = 30.0f; // time in sec needed to cool to half of temp difference with surroundings
_heaterSpeed = 2.0f; // time in sec to heat the boiler by 1 degree
_responseTime = 5.0f; // Delay in response of temperature detector
_lastTime = millis();
}
void HeaterSim::SetAmbientTemp(float ambientTemp) {
CalcTemperature();
_ambientTemp = ambientTemp;
}
void HeaterSim::SetBoilerTemp(float boilerTemp) {
_boilerTemp = boilerTemp;
_delayedBoilerTemp = boilerTemp;
}
void HeaterSim::SetHeaterState(bool boilerOn) {
CalcTemperature();
_boilerOn = boilerOn;
}
float HeaterSim::GetTemp() {
CalcTemperature();
return _delayedBoilerTemp;
}
void HeaterSim::CalcTemperature()
{
float rate = LN2/_halfTimeCooling;
unsigned long currentTime = millis();
if (currentTime >= _lastTime) _deltams = max(currentTime - _lastTime,1ul);
_deltaTime = _deltams/ 1000.0f;
float deltaTemp = _boilerTemp - _ambientTemp;
float deltaTempAfterCooling = deltaTemp*exp(-rate*_deltaTime);
float inflowTemp = _deltaTime * (_boilerOn?_heaterSpeed:0.0f);
float currentTemp = _ambientTemp + deltaTempAfterCooling + inflowTemp;
currentTemp = min(max(currentTemp,0.0f),100.0f);
_lastTime = currentTime;
_boilerTemp = currentTemp;
CalcDelayedTemperature();
}
void HeaterSim::CalcDelayedTemperature()
{
float fract = min(max(_deltaTime/_responseTime,0.0f),1.0f);
_delayedBoilerTemp = fract* _boilerTemp + (1.0f - fract)* _delayedBoilerTemp;
}